Master Thesis Code
by Simon Moser
Loading...
Searching...
No Matches
exp_240910_defence.m
Go to the documentation of this file.
1
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.
5
6clear; close all; clc;
7
8rng(11); % For reproducibility
9
10%% generate noise
11
12% Time specifications:
13Fs = 200;
14t_end = 30;
15t = 0:1/Fs:t_end-1/Fs;
16
17% animation specifications
18width = 1920;
19height = 800;
20folder = "C:\Users\moss\OneDrive - ZHAW\MT\Presentations\Defence";
21samples_visible = 400;
22margin = 0.05;
23zhaw_blue = [0, 100, 166] / 255;
24zhaw_orange = [213, 78, 18] / 255;
25zhaw_green = [0, 105, 53] / 255;
26skip = 4;
27
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));
32bias = 0.1;
33
34%% noise Animation
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);
38xlabel("Time [s]");
39ylabel("Acceleration [m/s^2]");
40grid on;
41
42% set font style
43set(findall(fig, 'Type', 'axes'), ...
44 'FontName', 'CMU Bright', ...
45 'FontSize', 20, ...
46 'FontWeight', 'bold', ...
47 'GridColor', 'white', ...
48 'GridLineWidth', 1, ...
49 'Color', 'black', ...
50 'XColor', 'white', ...
51 'YColor', 'white');
52
53% set axis limits
54ylim(get_axis_limits(margin, noise));
55
56F(1) = getframe(gcf);
57
58for i = skip+1:skip:length(t)
59
60 % update plot
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)]);
65
66 % save frame
67 F(i) = getframe(gcf);
68
69end
70
71% remove empty frames
72F = F(1:skip:end);
73
74% export animation
75v = VideoWriter(folder + "\01_noise_animation.mp4", "MPEG-4");
76v.Quality = 75;
77v.FrameRate = 25;
78open(v);
79writeVideo(v, F);
80close(v);
81
82%% mean noise animation
83evolving_mean = cumsum(noise) ./ (1:length(noise));
84
85% Animation
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);
89hold on
90plt(2) = semilogx(t(1), 0, "Color", zhaw_orange, "LineWidth", 4);
91xlabel("Time [s]");
92ylabel("Acceleration [m/s^2]");
93grid on;
94
95% set font style
96set(findall(fig, 'Type', 'axes'), ...
97 'FontName', 'CMU Bright', ...
98 'FontSize', 20, ...
99 'FontWeight', 'bold', ...
100 'GridColor', 'white', ...
101 'GridLineWidth', 1, ...
102 'Color', 'black', ...
103 'XColor', 'white', ...
104 'YColor', 'white');
105
106% set axis limits
107ylim(get_axis_limits(margin, evolving_mean));
108
109F(1) = getframe(gcf);
110
111for i = skip+1:skip:length(t)
112
113 % update plot
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)]);
119
120 % save frame
121 F(i) = getframe(gcf);
122
123end
124
125% remove empty frames
126F = F(1:skip:end);
127
128% export animation
129v = VideoWriter(folder + "\02_mean_noise_animation.mp4", "MPEG-4");
130v.Quality = 75;
131v.FrameRate = 25;
132open(v);
133writeVideo(v, F);
134close(v);
135
136%% double integral noise
137% first integration (velocity)
138vel = cumsum(noise) / Fs;
139pos = cumsum(vel) / Fs;
140
141% Animation
142F = struct('cdata', cell(1, length(t)), 'colormap', cell(1, length(t)));
143
144fig = figure("Position", [0, 0, width, height], "Visible", "off", "Color", "black");
145plt(1) = plot(t(1), pos(1), "Color", zhaw_blue, "LineWidth", 4);
146xlabel("Time [s]");
147ylabel("Displacement [m]");
148grid on;
149
150% set font style
151set(findall(fig, 'Type', 'axes'), ...
152 'FontName', 'CMU Bright', ...
153 'FontSize', 20, ...
154 'FontWeight', 'bold', ...
155 'GridColor', 'white', ...
156 'GridLineWidth', 1, ...
157 'Color', 'black', ...
158 'XColor', 'white', ...
159 'YColor', 'white');
160
161% set axis limits
162ylim(get_axis_limits(margin, pos(1)));
163
164% save first frame
165F(1) = getframe(gcf);
166
167for i = skip+1:skip:length(t)
168
169 % update plot
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)]);
174 ylim(get_axis_limits(margin, pos(1:i)));
175
176 % save frame
177 F(i) = getframe(gcf);
178
179end
180
181% remove empty frames
182F = F(1:skip:end);
183
184% export animation
185v = VideoWriter(folder + "\03_double_integral_noise_animation.mp4", "MPEG-4");
186v.Quality = 75;
187v.FrameRate = 25;
188open(v);
189writeVideo(v, F);
190close(v);
191
192%% double integral noise with additional bias
193vel = cumsum(noise + bias) / Fs;
194pos = cumsum(vel) / Fs;
195
196% Animation
197F = struct('cdata', cell(1, length(t)), 'colormap', cell(1, length(t)));
198
199fig = figure("Position", [0, 0, width, height], "Visible", "off", "Color", "black");
200plt(1) = plot(t(1), pos(1), "Color", zhaw_blue, "LineWidth", 4);
201xlabel("Time [s]");
202ylabel("Displacement [m]");
203grid on;
204
205% set font style
206set(findall(fig, 'Type', 'axes'), ...
207 'FontName', 'CMU Bright', ...
208 'FontSize', 20, ...
209 'FontWeight', 'bold', ...
210 'GridColor', 'white', ...
211 'GridLineWidth', 1, ...
212 'Color', 'black', ...
213 'XColor', 'white', ...
214 'YColor', 'white');
215
216% set axis limits
217ylim(get_axis_limits(margin, pos(1)));
218
219% save first frame
220F(1) = getframe(gcf);
221
222for i = skip+1:skip:length(t)
223
224 % update plot
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)]);
229 ylim(get_axis_limits(margin, pos(1:i)));
230
231 % save frame
232 F(i) = getframe(gcf);
233
234end
235
236% remove empty frames
237F = F(1:skip:end);
238
239% export animation
240v = VideoWriter(folder + "\04_double_integral_noise_bias_animation.mp4", "MPEG-4");
241v.Quality = 75;
242v.FrameRate = 25;
243open(v);
244writeVideo(v, F);
245close(v);
246
247%% noisy movement
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;
256
257% double integral
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;
264
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]");
270xlim([0, 5])
271grid on;
272splt(2) = subplot(3, 1, 2);
273plot(t, vel_mov, "Color", zhaw_blue, "LineWidth", 4);
274ylabel("Velocity [m/s]");
275xlim([0, 5])
276grid on;
277splt(3) = subplot(3, 1, 3);
278plot(t, pos_mov, "Color", zhaw_blue, "LineWidth", 4);
279ylabel("Displacement [m]");
280xlim([0, 5])
281xlabel("Time [s]");
282grid on;
283
284% set font style
285set(splt, ...
286 'FontName', 'CMU Bright', ...
287 'FontSize', 20, ...
288 'FontWeight', 'bold', ...
289 'GridColor', 'white', ...
290 'GridLineWidth', 1, ...
291 'Color', 'black', ...
292 'XColor', 'white', ...
293 'YColor', 'white');
294
295exportgraphics(fig, folder + "\00_static_plot.pdf", "ContentType", "vector");
296
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);
301hold on
302plt(2) = plot(t(1), acc_mov(1), "Color", zhaw_orange, "LineWidth", 4);
303xlabel("Time [s]");
304ylabel("Acceleration [m/s^2]");
305grid on;
306
307% set font style
308set(findall(fig, 'Type', 'axes'), ...
309 'FontName', 'CMU Bright', ...
310 'FontSize', 20, ...
311 'FontWeight', 'bold', ...
312 'GridColor', 'white', ...
313 'GridLineWidth', 1, ...
314 'Color', 'black', ...
315 'XColor', 'white', ...
316 'YColor', 'white');
317
318% set axis limits
319ylim(get_axis_limits(margin, acc_mov_noise_bias(1), acc_mov(1)));
320
321% save first frame
322F(1) = getframe(gcf);
323
324for i = skip+1:skip:length(t)
325
326 % update plot
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)]);
332 ylim(get_axis_limits(margin, acc_mov_noise_bias(1:i), acc_mov(1:i)));
333
334 % save frame
335 F(i) = getframe(gcf);
336
337end
338
339% remove empty frames
340F = F(1:skip:end);
341
342% export animation
343v = VideoWriter(folder + "\05_acceleration_animation.mp4", "MPEG-4");
344v.Quality = 75;
345v.FrameRate = 25;
346open(v);
347writeVideo(v, F);
348close(v);
349
350%% animate position
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);
354hold on
355plt(2) = plot(t(1), pos_mov(1), "Color", zhaw_orange, "LineWidth", 4);
356xlabel("Time [s]");
357ylabel("Displacement [m]");
358grid on;
359
360% set font style
361set(findall(fig, 'Type', 'axes'), ...
362 'FontName', 'CMU Bright', ...
363 'FontSize', 20, ...
364 'FontWeight', 'bold', ...
365 'GridColor', 'white', ...
366 'GridLineWidth', 1, ...
367 'Color', 'black', ...
368 'XColor', 'white', ...
369 'YColor', 'white');
370
371% set axis limits
372ylim(get_axis_limits(margin, pos_mov_noise_bias(1), pos_mov(1)));
373
374% save first frame
375F(1) = getframe(gcf);
376
377for i = skip+1:skip:length(t)
378
379 % update plot
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)]);
385 ylim(get_axis_limits(margin, pos_mov_noise_bias(1:i), pos_mov(1:i)));
386
387 % save frame
388 F(i) = getframe(gcf);
389
390end
391
392% remove empty frames
393F = F(1:skip:end);
394
395% export animation
396v = VideoWriter(folder + "\06_position_animation.mp4", "MPEG-4");
397v.Quality = 75;
398v.FrameRate = 25;
399open(v);
400writeVideo(v, F);
401close(v);
402
403%% animate position with zero velocity updates
404
405% find zero velocity
406zero_vel = 351:400:length(t);
407
408% change velocity vector and position vector
409vel_uncorrected = vel_mov_noise_bias;
410last_t = 0;
411for i = 1:length(zero_vel)
412
413 % velocity
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;
416
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;
420
421 % position
422 t_between = t(zero_vel(i)) - last_t;
423 last_t = t(zero_vel(i));
424
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;
427
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;
431end
432
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);
437hold on
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);
440xlabel("Time [s]");
441ylabel("Velocity [m/s]");
442grid on;
443
444% set font style
445set(findall(fig, 'Type', 'axes'), ...
446 'FontName', 'CMU Bright', ...
447 'FontSize', 20, ...
448 'FontWeight', 'bold', ...
449 'GridColor', 'white', ...
450 'GridLineWidth', 1, ...
451 'Color', 'black', ...
452 'XColor', 'white', ...
453 'YColor', 'white');
454
455% set axis limits
456ylim(get_axis_limits(margin, vel_mov_noise_bias(1), vel_mov(1), vel_uncorrected(1)));
457
458F(1) = getframe(gcf);
459
460for i = skip+1:skip:length(t)
461
462 % update plot
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)));
470
471 % save frame
472 F(i) = getframe(gcf);
473
474end
475
476% remove empty frames
477F = F(1:skip:end);
478
479% export animation
480v = VideoWriter(folder + "\07_velocity_zero_velocity_animation.mp4", "MPEG-4");
481v.Quality = 75;
482v.FrameRate = 25;
483open(v);
484writeVideo(v, F);
485close(v);
486
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);
491hold on
492plt(2) = plot(t(1), pos_mov(1), "Color", zhaw_orange, "LineWidth", 4);
493xlabel("Time [s]");
494ylabel("Displacement [m]");
495grid on;
496
497% set font style
498set(findall(fig, 'Type', 'axes'), ...
499 'FontName', 'CMU Bright', ...
500 'FontSize', 20, ...
501 'FontWeight', 'bold', ...
502 'GridColor', 'white', ...
503 'GridLineWidth', 1, ...
504 'Color', 'black', ...
505 'XColor', 'white', ...
506 'YColor', 'white');
507
508% set axis limits
509ylim(get_axis_limits(margin, pos_mov_noise_bias(1), pos_mov(1)));
510
511% save first frame
512F(1) = getframe(gcf);
513
514for i = skip+1:skip:length(t)
515
516 % update plot
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)]);
522 ylim(get_axis_limits(margin, pos_mov_noise_bias(1:i), pos_mov(1:i)));
523
524 % save frame
525 F(i) = getframe(gcf);
526
527end
528
529% remove empty frames
530F = F(1:skip:end);
531
532% export animation
533v = VideoWriter(folder + "\08_position_zero_velocity_animation.mp4", "MPEG-4");
534v.Quality = 75;
535v.FrameRate = 25;
536open(v);
537writeVideo(v, F);
538close(v);
539
540%% Animation of Position with zero velocity updates and smoothing
541
542% smooth position
543pos_mov_noise_bias_smooth = pos_mov_noise;
544
545% Animation
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);
549hold on
550plt(2) = plot(t(1), pos_mov(1), "Color", zhaw_orange, "LineWidth", 4);
551xlabel("Time [s]");
552ylabel("Displacement [m]");
553grid on;
554
555% set font style
556set(findall(fig, 'Type', 'axes'), ...
557 'FontName', 'CMU Bright', ...
558 'FontSize', 20, ...
559 'FontWeight', 'bold', ...
560 'GridColor', 'white', ...
561 'GridLineWidth', 1, ...
562 'Color', 'black', ...
563 'XColor', 'white', ...
564 'YColor', 'white');
565
566% set axis limits
567ylim(get_axis_limits(margin, pos_mov_noise_bias_smooth(1), pos_mov(1)));
568
569F(1) = getframe(gcf);
570
571for i = skip+1:skip:length(t)
572
573 % update plot
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));
576 x_start = 1;
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)));
580
581 % save frame
582 F(i) = getframe(gcf);
583
584end
585
586% remove empty frames
587F = F(1:skip:end);
588
589% export animation
590v = VideoWriter(folder + "\09_position_zero_velocity_smooth_animation.mp4", "MPEG-4");
591v.Quality = 75;
592v.FrameRate = 25;
593open(v);
594writeVideo(v, F);
595close(v);
596
597
598%% noisy movement 2
599
600noise2 = std_noise * randn(size(t));
601bias2 = -0.15;
602
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;
611
612% double integral
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;
619
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);
624hold on
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);
628xlabel("Time [s]");
629ylabel("Displacement [m]");
630grid on;
631
632% set font style
633set(findall(fig, 'Type', 'axes'), ...
634 'FontName', 'CMU Bright', ...
635 'FontSize', 20, ...
636 'FontWeight', 'bold', ...
637 'GridColor', 'white', ...
638 'GridLineWidth', 1, ...
639 'Color', 'black', ...
640 'XColor', 'white', ...
641 'YColor', 'white');
642
643% set axis limits
644ylim(get_axis_limits(margin, pos_mov_noise_bias_smooth(1), pos_mov_noise2(1), pos_mov(1), pos_mov2(1)));
645
646F(1) = getframe(gcf);
647
648for i = skip+1:skip:length(t)
649
650 % update plot
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));
655 x_start = 1;
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)));
659
660 % save frame
661 F(i) = getframe(gcf);
662
663end
664
665% remove empty frames
666F = F(1:skip:end);
667
668% export animation
669v = VideoWriter(folder + "\10_position_zero_velocity_smooth_animation_2.mp4", "MPEG-4");
670v.Quality = 75;
671v.FrameRate = 25;
672open(v);
673writeVideo(v, F);
674close(v);
675
676%% animation of both positions with same end position
677
678% correct end position
679pos_1_final = pos_mov;
680pos_2_final = pos_mov2;
681% Animation
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);
685hold on
686plt(2) = plot(t(1), pos_2_final(1), "Color", zhaw_green, "LineWidth", 4);
687xlabel("Time [s]");
688ylabel("Displacement [m]");
689grid on;
690
691% set font style
692set(findall(fig, 'Type', 'axes'), ...
693 'FontName', 'CMU Bright', ...
694 'FontSize', 20, ...
695 'FontWeight', 'bold', ...
696 'GridColor', 'white', ...
697 'GridLineWidth', 1, ...
698 'Color', 'black', ...
699 'XColor', 'white', ...
700 'YColor', 'white');
701
702% set axis limits
703ylim(get_axis_limits(margin, pos_1_final(1), pos_2_final(1)));
704
705F(1) = getframe(gcf);
706
707for i = skip+1:skip:length(t)
708
709 % update plot
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));
712 x_start = 1;
713 x_end = min(i + margin * samples_visible, length(t));
714 xlim([t(x_start), t(x_end)]);
715 ylim(get_axis_limits(margin, pos_1_final(1:i), pos_2_final(1:i)));
716
717 % save frame
718 F(i) = getframe(gcf);
719
720end
721
722% remove empty frames
723F = F(1:skip:end);
724
725% export animation
726v = VideoWriter(folder + "\11_position_final_animation.mp4", "MPEG-4");
727v.Quality = 75;
728v.FrameRate = 25;
729open(v);
730writeVideo(v, F);
731close(v);
732
733function limits = get_axis_limits(margin, data)
734
735arguments
736 margin (1, 1) double = 0.05
737end
738
739arguments (Repeating)
740 data (:, 1) double
741end
742
743low = inf;
744high = -inf;
745
746for i = 1:length(data)
747 low = min(low, min(data{i}));
748 high = max(high, max(data{i}));
749end
750
751low = low - margin * abs(low);
752high = high + margin * abs(high);
753
754if low >= high
755 low = low - margin;
756 high = high + margin;
757end
758
759limits = [low, high];
760
761end
function get_axis_limits(in margin, in data)