2%% Videos
for the Defence
3% This script generates animations
for the defence presentation.
4% WARNING: This script contains a lot of repetition and is not very efficient.
8rng(11); % For reproducibility
17% animation specifications
20folder =
"C:\Users\moss\OneDrive - ZHAW\MT\Presentations\Defence";
23zhaw_blue = [0, 100, 166] / 255;
24zhaw_orange = [213, 78, 18] / 255;
25zhaw_green = [0, 105, 53] / 255;
28%% noise (acceleration)
29true_acc = zeros(size(t));
30std_noise = sqrt(5.3e-3 * sqrt(Fs)); % std of noise
31noise = std_noise * randn(size(t));
35F =
struct(
'cdata', cell(1, length(t)),
'colormap', cell(1, length(t)));
36fig = figure(
"Position", [0, 0, width, height],
"Visible",
"on",
"Color",
"black");
37plt = plot(t(1), noise(1),
"Color", zhaw_blue,
"LineWidth", 4);
39ylabel(
"Acceleration [m/s^2]");
43set(findall(fig,
'Type',
'axes'), ...
44 'FontName',
'CMU Bright', ...
46 'FontWeight',
'bold', ...
47 'GridColor',
'white', ...
48 'GridLineWidth', 1, ...
50 'XColor',
'white', ...
58for i = skip+1:skip:length(t)
61 set(plt,
"XData", t(1:i),
"YData", noise(1:i));
62 x_start = max(1, i - samples_visible);
63 x_end = min(i + margin * samples_visible, length(t));
64 xlim([t(x_start), t(x_end)]);
75v = VideoWriter(folder +
"\01_noise_animation.mp4",
"MPEG-4");
82%% mean noise animation
83evolving_mean = cumsum(noise) ./ (1:length(noise));
86F =
struct(
'cdata', cell(1, length(t)),
'colormap', cell(1, length(t)));
87fig = figure(
"Position", [0, 0, width, height],
"Visible",
"off",
"Color",
"black");
88plt(1) = semilogx(t(1), evolving_mean(1),
"Color", zhaw_blue,
"LineWidth", 4);
90plt(2) = semilogx(t(1), 0,
"Color", zhaw_orange,
"LineWidth", 4);
92ylabel(
"Acceleration [m/s^2]");
96set(findall(fig,
'Type',
'axes'), ...
97 'FontName',
'CMU Bright', ...
99 'FontWeight',
'bold', ...
100 'GridColor',
'white', ...
101 'GridLineWidth', 1, ...
102 'Color',
'black', ...
103 'XColor',
'white', ...
111for i = skip+1:skip:length(t)
114 set(plt(1),
"XData", t(1:i),
"YData", evolving_mean(1:i));
115 set(plt(2),
"XData", t(1:i),
"YData", zeros(i, 1));
116 x_start = 1; % max(1, i - samples_visible);
117 x_end = min(i + margin * samples_visible, length(t));
118 xlim([t(x_start), t(x_end)]);
121 F(i) = getframe(gcf);
129v = VideoWriter(folder +
"\02_mean_noise_animation.mp4",
"MPEG-4");
136%%
double integral noise
137% first integration (velocity)
138vel = cumsum(noise) / Fs;
139pos = cumsum(vel) / Fs;
142F =
struct(
'cdata', cell(1, length(t)),
'colormap', cell(1, length(t)));
144fig = figure(
"Position", [0, 0, width, height],
"Visible",
"off",
"Color",
"black");
145plt(1) = plot(t(1), pos(1),
"Color", zhaw_blue,
"LineWidth", 4);
147ylabel(
"Displacement [m]");
151set(findall(fig,
'Type',
'axes'), ...
152 'FontName',
'CMU Bright', ...
154 'FontWeight',
'bold', ...
155 'GridColor',
'white', ...
156 'GridLineWidth', 1, ...
157 'Color',
'black', ...
158 'XColor',
'white', ...
167for i = skip+1:skip:length(t)
170 set(plt(1),
"XData", t(1:i),
"YData", pos(1:i));
171 x_start = max(1, i - samples_visible);
172 x_end = min(i + margin * samples_visible, length(t));
173 xlim([t(x_start), t(x_end)]);
177 F(i) = getframe(gcf);
185v = VideoWriter(folder +
"\03_double_integral_noise_animation.mp4",
"MPEG-4");
192%%
double integral noise with additional bias
193vel = cumsum(noise + bias) / Fs;
194pos = cumsum(vel) / Fs;
197F =
struct(
'cdata', cell(1, length(t)),
'colormap', cell(1, length(t)));
199fig = figure(
"Position", [0, 0, width, height],
"Visible",
"off",
"Color",
"black");
200plt(1) = plot(t(1), pos(1),
"Color", zhaw_blue,
"LineWidth", 4);
202ylabel(
"Displacement [m]");
206set(findall(fig,
'Type',
'axes'), ...
207 'FontName',
'CMU Bright', ...
209 'FontWeight',
'bold', ...
210 'GridColor',
'white', ...
211 'GridLineWidth', 1, ...
212 'Color',
'black', ...
213 'XColor',
'white', ...
222for i = skip+1:skip:length(t)
225 set(plt(1),
"XData", t(1:i),
"YData", pos(1:i));
226 x_start = max(1, i - samples_visible);
227 x_end = min(i + margin * samples_visible, length(t));
228 xlim([t(x_start), t(x_end)]);
232 F(i) = getframe(gcf);
240v = VideoWriter(folder +
"\04_double_integral_noise_bias_animation.mp4",
"MPEG-4");
248% generate movement (accelerate (0.5s), constant speed (0.5s), decelerate (0.5s), constant speed (0.5s), ...)
249acc_mov = zeros(1, 2*Fs);
250acc_mov(1:0.5*Fs) = 1;
251acc_mov(Fs+1:1.5*Fs) = -1;
252acc_mov = repmat(acc_mov, 1, ceil(t_end / 2));
253acc_mov = acc_mov(1:size(t, 2));
254acc_mov_noise = acc_mov + noise;
255acc_mov_noise_bias = acc_mov + noise + bias;
258vel_mov = cumsum(acc_mov) / Fs;
259vel_mov_noise = cumsum(acc_mov_noise) / Fs;
260vel_mov_noise_bias = cumsum(acc_mov_noise_bias) / Fs;
261pos_mov = cumsum(vel_mov) / Fs;
262pos_mov_noise = cumsum(vel_mov_noise) / Fs;
263pos_mov_noise_bias = cumsum(vel_mov_noise_bias) / Fs;
265%% create a static plot with acc, vel, pos)
266fig = figure("Position", [0, 0, 1000, 1100], "Visible", "off", "Color", "black");
267splt(1) = subplot(3, 1, 1);
268plot(t, acc_mov, "Color", zhaw_blue, "LineWidth", 4);
269ylabel("Acceleration [m/s^2]");
272splt(2) = subplot(3, 1, 2);
273plot(t, vel_mov, "Color", zhaw_blue, "LineWidth", 4);
274ylabel("Velocity [m/s]");
277splt(3) = subplot(3, 1, 3);
278plot(t, pos_mov, "Color", zhaw_blue, "LineWidth", 4);
279ylabel("Displacement [m]");
286 'FontName', 'CMU Bright', ...
288 'FontWeight', 'bold', ...
289 'GridColor', 'white', ...
290 'GridLineWidth', 1, ...
291 'Color', 'black', ...
292 'XColor', 'white', ...
295exportgraphics(fig, folder + "\00_static_plot.pdf", "ContentType", "vector");
297%% animate acceleration
298F = struct('cdata', cell(1, length(t)), 'colormap', cell(1, length(t)));
299fig = figure("Position", [0, 0, width, height], "Visible", "off", "Color", "black");
300plt(1) = plot(t(1), acc_mov_noise_bias(1), "Color", zhaw_blue, "LineWidth", 4);
302plt(2) = plot(t(1), acc_mov(1), "Color", zhaw_orange, "LineWidth", 4);
304ylabel("Acceleration [m/s^2]");
308set(findall(fig, 'Type', 'axes'), ...
309 'FontName', 'CMU Bright', ...
311 'FontWeight', 'bold', ...
312 'GridColor', 'white', ...
313 'GridLineWidth', 1, ...
314 'Color', 'black', ...
315 'XColor', 'white', ...
324for i = skip+1:skip:length(t)
327 set(plt(1), "XData", t(1:i), "YData", acc_mov_noise_bias(1:i));
328 set(plt(2), "XData", t(1:i), "YData", acc_mov(1:i));
329 x_start = max(1, i - samples_visible);
330 x_end = min(i + margin * samples_visible, length(t));
331 xlim([t(x_start), t(x_end)]);
335 F(i) = getframe(gcf);
343v = VideoWriter(folder + "\05_acceleration_animation.mp4", "MPEG-4");
351F = struct('cdata', cell(1, length(t)), 'colormap', cell(1, length(t)));
352fig = figure("Position", [0, 0, width, height], "Visible", "off", "Color", "black");
353plt(1) = plot(t(1), pos_mov_noise_bias(1), "Color", zhaw_blue, "LineWidth", 4);
355plt(2) = plot(t(1), pos_mov(1), "Color", zhaw_orange, "LineWidth", 4);
357ylabel("Displacement [m]");
361set(findall(fig, 'Type', 'axes'), ...
362 'FontName', 'CMU Bright', ...
364 'FontWeight', 'bold', ...
365 'GridColor', 'white', ...
366 'GridLineWidth', 1, ...
367 'Color', 'black', ...
368 'XColor', 'white', ...
377for i = skip+1:skip:length(t)
380 set(plt(1), "XData", t(1:i), "YData", pos_mov_noise_bias(1:i));
381 set(plt(2), "XData", t(1:i), "YData", pos_mov(1:i));
382 x_start = max(1, i - samples_visible);
383 x_end = min(i + margin * samples_visible, length(t));
384 xlim([t(x_start), t(x_end)]);
388 F(i) = getframe(gcf);
396v = VideoWriter(folder + "\06_position_animation.mp4", "MPEG-4");
403%% animate position with zero velocity updates
406zero_vel = 351:400:length(t);
408% change velocity vector and position vector
409vel_uncorrected = vel_mov_noise_bias;
411for i = 1:length(zero_vel)
414 this_vel = vel_mov_noise_bias(zero_vel(i));
415 vel_mov_noise_bias(zero_vel(i):end) = vel_mov_noise_bias(zero_vel(i):end) - this_vel;
417 % velocity (w/o bias)
418 this_vel2 = vel_mov_noise(zero_vel(i));
419 vel_mov_noise(zero_vel(i):end) = vel_mov_noise(zero_vel(i):end) - this_vel2;
422 t_between = t(zero_vel(i)) - last_t;
423 last_t = t(zero_vel(i));
425 p_diff = (this_vel * t_between) / 2;
426 pos_mov_noise_bias(zero_vel(i):end) = pos_mov_noise_bias(zero_vel(i):end) - p_diff;
428 p_diff = repmat(this_vel / Fs, size(pos_mov_noise_bias(zero_vel(i):end)));
429 p_diff = cumsum(p_diff);
430 pos_mov_noise_bias(zero_vel(i):end) = pos_mov_noise_bias(zero_vel(i):end) - p_diff;
433%% Animation of Velocity with zero velocity updates
434F = struct('cdata', cell(1, length(t)), 'colormap', cell(1, length(t)));
435fig = figure("Position", [0, 0, width, height], "Visible", "off", "Color", "black");
436plt(1) = plot(t(1), vel_mov_noise_bias(1), "Color", zhaw_blue, "LineWidth", 4);
438plt(2) = plot(t(1), vel_mov(1), "Color", zhaw_orange, "LineWidth", 4);
439plt(3) = plot(t(1), vel_uncorrected(1), "Color", "red", "LineWidth", 4);
441ylabel("Velocity [m/s]");
445set(findall(fig, 'Type', 'axes'), ...
446 'FontName', 'CMU Bright', ...
448 'FontWeight', 'bold', ...
449 'GridColor', 'white', ...
450 'GridLineWidth', 1, ...
451 'Color', 'black', ...
452 'XColor', 'white', ...
456ylim(
get_axis_limits(margin, vel_mov_noise_bias(1), vel_mov(1), vel_uncorrected(1)));
460for i = skip+1:skip:length(t)
463 set(plt(1), "XData", t(1:i), "YData", vel_mov_noise_bias(1:i));
464 set(plt(2), "XData", t(1:i), "YData", vel_mov(1:i));
465 set(plt(3), "XData", t(1:i), "YData", vel_uncorrected(1:i));
466 x_start = max(1, i - samples_visible);
467 x_end = min(i + margin * samples_visible, length(t));
468 xlim([t(x_start), t(x_end)]);
469 ylim(
get_axis_limits(margin, vel_mov_noise_bias(1:i), vel_mov(1:i), vel_uncorrected(1:i)));
472 F(i) = getframe(gcf);
480v = VideoWriter(folder + "\07_velocity_zero_velocity_animation.mp4", "MPEG-4");
487%% Animation of Position with zero velocity updates
488F = struct('cdata', cell(1, length(t)), 'colormap', cell(1, length(t)));
489fig = figure("Position", [0, 0, width, height], "Visible", "off", "Color", "black");
490plt(1) = plot(t(1), pos_mov_noise_bias(1), "Color", zhaw_blue, "LineWidth", 4);
492plt(2) = plot(t(1), pos_mov(1), "Color", zhaw_orange, "LineWidth", 4);
494ylabel("Displacement [m]");
498set(findall(fig, 'Type', 'axes'), ...
499 'FontName', 'CMU Bright', ...
501 'FontWeight', 'bold', ...
502 'GridColor', 'white', ...
503 'GridLineWidth', 1, ...
504 'Color', 'black', ...
505 'XColor', 'white', ...
514for i = skip+1:skip:length(t)
517 set(plt(1), "XData", t(1:i), "YData", pos_mov_noise_bias(1:i));
518 set(plt(2), "XData", t(1:i), "YData", pos_mov(1:i));
519 x_start = 1; % max(1, i - samples_visible);
520 x_end = min(i + margin * samples_visible, length(t));
521 xlim([t(x_start), t(x_end)]);
525 F(i) = getframe(gcf);
533v = VideoWriter(folder + "\08_position_zero_velocity_animation.mp4", "MPEG-4");
540%% Animation of Position with zero velocity updates and smoothing
543pos_mov_noise_bias_smooth = pos_mov_noise;
546F = struct('cdata', cell(1, length(t)), 'colormap', cell(1, length(t)));
547fig = figure("Position", [0, 0, width, height], "Visible", "off", "Color", "black");
548plt(1) = plot(t(1), pos_mov_noise_bias_smooth(1), "Color", zhaw_blue, "LineWidth", 4);
550plt(2) = plot(t(1), pos_mov(1), "Color", zhaw_orange, "LineWidth", 4);
552ylabel("Displacement [m]");
556set(findall(fig, 'Type', 'axes'), ...
557 'FontName', 'CMU Bright', ...
559 'FontWeight', 'bold', ...
560 'GridColor', 'white', ...
561 'GridLineWidth', 1, ...
562 'Color', 'black', ...
563 'XColor', 'white', ...
567ylim(
get_axis_limits(margin, pos_mov_noise_bias_smooth(1), pos_mov(1)));
571for i = skip+1:skip:length(t)
574 set(plt(1), "XData", t(1:i), "YData", pos_mov_noise_bias_smooth(1:i));
575 set(plt(2), "XData", t(1:i), "YData", pos_mov(1:i));
577 x_end = min(i + margin * samples_visible, length(t));
578 xlim([t(x_start), t(x_end)]);
579 ylim(
get_axis_limits(margin, pos_mov_noise_bias_smooth(1:i), pos_mov(1:i)));
582 F(i) = getframe(gcf);
590v = VideoWriter(folder + "\09_position_zero_velocity_smooth_animation.mp4", "MPEG-4");
600noise2 = std_noise * randn(size(t));
603% generate movement (constant speed (0.5s), accelerate (0.5s), constant speed (0.5s), decelerate (0.5s), ...)
604acc_mov2 = zeros(1, 2*Fs);
605acc_mov2(1:0.5*Fs) = -1;
606acc_mov2(Fs+1:1.5*Fs) = 1;
607acc_mov2 = repmat(acc_mov2, 1, ceil(t_end / 2));
608acc_mov2 = acc_mov2(1:size(t, 2));
609acc_mov_noise2 = acc_mov2 + noise2;
610acc_mov_noise_bias2 = acc_mov2 + noise2 + bias2;
613vel_mov2 = cumsum(acc_mov2) / Fs + 0.5;
614vel_mov_noise2 = cumsum(acc_mov_noise2) / Fs + 0.5;
615vel_mov_noise_bias2 = cumsum(acc_mov_noise_bias2) / Fs + 0.5;
616pos_mov2 = cumsum(vel_mov2) / Fs + 0.125;
617pos_mov_noise2 = cumsum(vel_mov_noise2) / Fs + 0.125;
618pos_mov_noise_bias2 = cumsum(vel_mov_noise_bias2) / Fs + 0.125;
620%% Animation of both positions with zero velocity updates and smoothing
621F = struct('cdata', cell(1, length(t)), 'colormap', cell(1, length(t)));
622fig = figure("Position", [0, 0, width, height], "Visible", "off", "Color", "black");
623plt(1) = plot(t(1), pos_mov_noise_bias_smooth(1), "Color", zhaw_blue, "LineWidth", 4);
625plt(2) = plot(t(1), pos_mov_noise2(1), "Color", zhaw_green, "LineWidth", 4);
626plt(3) = plot(t(1), pos_mov(1), "Color", zhaw_orange, "LineWidth", 2);
627plt(4) = plot(t(1), pos_mov2(1), "Color", "yellow", "LineWidth", 2);
629ylabel("Displacement [m]");
633set(findall(fig, 'Type', 'axes'), ...
634 'FontName', 'CMU Bright', ...
636 'FontWeight', 'bold', ...
637 'GridColor', 'white', ...
638 'GridLineWidth', 1, ...
639 'Color', 'black', ...
640 'XColor', 'white', ...
644ylim(
get_axis_limits(margin, pos_mov_noise_bias_smooth(1), pos_mov_noise2(1), pos_mov(1), pos_mov2(1)));
648for i = skip+1:skip:length(t)
651 set(plt(1), "XData", t(1:i), "YData", pos_mov_noise_bias_smooth(1:i));
652 set(plt(2), "XData", t(1:i), "YData", pos_mov_noise2(1:i));
653 set(plt(3), "XData", t(1:i), "YData", pos_mov(1:i));
654 set(plt(4), "XData", t(1:i), "YData", pos_mov2(1:i));
656 x_end = min(i + margin * samples_visible, length(t));
657 xlim([t(x_start), t(x_end)]);
658 ylim(
get_axis_limits(margin, pos_mov_noise_bias_smooth(1:i), pos_mov_noise2(1:i), pos_mov(1:i), pos_mov2(1:i)));
661 F(i) = getframe(gcf);
669v = VideoWriter(folder + "\10_position_zero_velocity_smooth_animation_2.mp4", "MPEG-4");
676%% animation of both positions with same end position
678% correct end position
679pos_1_final = pos_mov;
680pos_2_final = pos_mov2;
682F = struct('cdata', cell(1, length(t)), 'colormap', cell(1, length(t)));
683fig = figure("Position", [0, 0, width, height], "Visible", "off", "Color", "black");
684plt(1) = plot(t(1), pos_1_final(1), "Color", zhaw_blue, "LineWidth", 4);
686plt(2) = plot(t(1), pos_2_final(1), "Color", zhaw_green, "LineWidth", 4);
688ylabel("Displacement [m]");
692set(findall(fig, 'Type', 'axes'), ...
693 'FontName', 'CMU Bright', ...
695 'FontWeight', 'bold', ...
696 'GridColor', 'white', ...
697 'GridLineWidth', 1, ...
698 'Color', 'black', ...
699 'XColor', 'white', ...
707for i = skip+1:skip:length(t)
710 set(plt(1), "XData", t(1:i), "YData", pos_1_final(1:i));
711 set(plt(2), "XData", t(1:i), "YData", pos_2_final(1:i));
713 x_end = min(i + margin * samples_visible, length(t));
714 xlim([t(x_start), t(x_end)]);
718 F(i) = getframe(gcf);
726v = VideoWriter(folder + "\11_position_final_animation.mp4", "MPEG-4");
736 margin (1, 1)
double = 0.05
746for i = 1:length(data)
747 low = min(low, min(data{i}));
748 high = max(high, max(data{i}));
751low = low - margin * abs(low);
752high = high + margin * abs(high);
756 high = high + margin;
function get_axis_limits(in margin, in data)