Two Subplots in an Animated For-loop Problem

Hi! I am new to Matlab and trying to learn more each day, and have some issues regarding making both of my subplots play at the same time. For the first plot, I searched for the peaks and valleys and plot them as the data recognized them as peaks/valleys, and have no problem with it. However, the second plot that I want to show is the displacement gathered from the peaks and valleys and show them when the first plot passes through the peaks/valleys. I also don't know how to incorporate the code for it, and I only know to use "comet()" on it but still not what i am looking for. My problem is in the for loop I made. Can you help me? Thanks in advance!
figure(3);
subplot (2,1,1)
h = plot(time(1), mag(1)); hold on; % Start with the first data point
ylabel('Acceleration');
title('Real-time Signal Analysis');
h.XDataSource = 'x1'; % Set the data source properties for dynamic updating
h.YDataSource = 'y1';
for i = 1:200:numel (time)
x1 = time(1:i);
y1 = mag(1:i);
refreshdata(h, 'caller');
drawnow;
[pks, locs] = findpeaks (mag,'MinPeakDistance',1500);
[vks, vlocs] = findpeaks (-mag, 'MinPeakDistance',1300);
vks = -vks;
valid_vks_indices = vlocs (time(vlocs) <= x1 (end));
valid_pks_indices = locs (time(locs) <= x1 (end));
if ~isempty (valid_vks_indices)
plot (time(valid_vks_indices), vks(ismember(vlocs, valid_vks_indices)), 'or');
end
if ~isempty (valid_pks_indices)
plot (time(valid_pks_indices), pks (ismember(locs, valid_pks_indices)), 'or');
end
pause(0.001);
subplot (2,1,2);
xlabel ('Time'); ylabel('Displacement');
for j = i
vel = cumtrapz (time, mag);
vel = filtfilt (b2, a2, vel);
disp = cumtrapz (time, vel);
disp = filtfilt (b2, a2, disp);
plot (time, disp)
end
end

4 Comments

I don't understand your pain points. Can you share the time, mag, a2, and b2 so that contributors including myself can understand your issue? You can save the variables into a mat file and attach it using the paperclip icon in the menu. Below is my trial to reproduce your issue. But, I still do not get what the issue is.
clear; close all; clc;
time = linspace(0, 1, 10000);
mag = rand(1,10000);
figure(3);
subplot (2,1,1)
h = plot(time(1), mag(1)); hold on; % Start with the first data point
ylabel('Acceleration');
title('Real-time Signal Analysis');
h.XDataSource = 'x1'; % Set the data source properties for dynamic updating
h.YDataSource = 'y1';
for i = 1:200:numel (time)
x1 = time(1:i);
y1 = mag(1:i);
refreshdata(h, 'caller');
drawnow;
[pks, locs] = findpeaks (mag,'MinPeakDistance',1500);
[vks, vlocs] = findpeaks (-mag, 'MinPeakDistance',1300);
vks = -vks;
valid_vks_indices = vlocs (time(vlocs) <= x1 (end));
valid_pks_indices = locs (time(locs) <= x1 (end));
if ~isempty (valid_vks_indices)
plot (time(valid_vks_indices), vks(ismember(vlocs, valid_vks_indices)), 'or');
end
if ~isempty (valid_pks_indices)
plot (time(valid_pks_indices), pks (ismember(locs, valid_pks_indices)), 'or');
end
pause(0.001);
subplot (2,1,2); hold on;
xlabel ('Time'); ylabel('Displacement');
for j = i
vel = cumtrapz (time, mag);
% vel = filtfilt (b2, a2, vel);
my_disp = cumtrapz (time, vel); % Note: "disp" is a name of built-in MATLAB function.
% my_disp = filtfilt (b2, a2, my_disp);
plot(time, my_disp)
end
end
Hi @Angelo Yeo! Thank you for answering, I actually want to animate my dispalcement together with the magnitude (first subplot). My idea was I will plot the first peak/valley then the distance from another peak/valley will be plotted on the displacement plot? here is my code:
%raw data from XYZ direction
NormChest = readtable('TV_1.5l_Hong.csv'); %%%get the raw data
C = table2array(NormChest(:,1));
x = table2array(NormChest(:,2));
y = table2array(NormChest(:,4));
z = table2array(NormChest(:,6));
total = table2array(NormChest(:,8));
time = C(:,1);
%--------------------------------------------------------------------------
% get relative acceleration
x = x - mean(x);
y = y - mean(y);
z = z - mean(z);
total = total - mean(total);
time = time - time (1);
%--------------------------------------------------------------------------
%design for low pass filter
fs = 1000; %sampling frequency
fc = 2; %cut-off frequency
order = 1;
[b1, a1] = butter (order, fc/(fs/2));
%design for high pass filter
fcv = 0.1;
orderv = 1;
[b2, a2] = butter (orderv, fcv*2/fs, "high");
%--------------------------------------------------------------------------
% magnitude computation
mag = sqrt(x.^2+y.^2+z.^2);
figure (1)
plot (mag); axis tight;
mag = filtfilt (b1, a1, mag);
mag = mag - mean (mag);
%--------------------------------------------------------------------------
% Initialize the plot
figure(3);
subplot (2,1,1)
h = plot(time(1), mag(1)); hold on; % Start with the first data point
ylabel('Acceleration'); axis tight;
title('Real-time Signal Analysis');
h.XDataSource = 'x1'; % Set the data source properties for dynamic updating
h.YDataSource = 'y1';
for i = 1:100:length (time)
x1 = time(1:i-1);
y1 = mag(1:i-1);
refreshdata(h, 'caller');
% drawnow;
%find local min and max
vks = find (islocalmin(mag, 'MinProminence',0.01));
% x_vks = time (vks);
% y_vks = mag (vks);
pks = find (islocalmax (mag, 'MinProminence',0.01));
% x_pks = time (pks);
% y_pks = mag (pks);
if ~isempty (vks)
plot (time (vks), mag (ismember 'or')
end
if ~isempty (time)
plot (time, mag, x_pks, y_pks, 'or')
end
pause (0.1);
subplot (2,1,2);
xlabel ('Time'); ylabel('Displacement');
for j = 1:100:length (time)
vel = cumtrapz (time, mag);
vel = filtfilt (b2, a2, vel);
disp = cumtrapz (time, vel);
disp = filtfilt (b2, a2, disp);
plot (disp)
end
pause (0.1);
end
hold off;
Your code doesn't work. Can you check the following lines?
plot (time (vks), mag (ismember 'or')
plot (time, mag, x_pks, y_pks, 'or')
Oh sorry!! This is the updated one.
figure(3);
subplot (2,1,1)
h = plot(time(1), mag(1)); hold on; % Start with the first data point
ylabel('Acceleration');
title('Real-time Signal Analysis');
h.XDataSource = 'x1'; % Set the data source properties for dynamic updating
h.YDataSource = 'y1';
for i = 1:200:numel (time)
x1 = time(1:i);
y1 = mag(1:i);
refreshdata(h, 'caller');
drawnow;
[pks, locs] = findpeaks (mag,'MinPeakDistance',1500);
[vks, vlocs] = findpeaks (-mag, 'MinPeakDistance',1300);
vks = -vks;
valid_vks_indices = vlocs (time(vlocs) <= x1 (end));
valid_pks_indices = locs (time(locs) <= x1 (end));
if ~isempty (valid_vks_indices)
plot (time(valid_vks_indices), vks(ismember(vlocs, valid_vks_indices)), 'or');
end
if ~isempty (valid_pks_indices)
plot (time(valid_pks_indices), pks (ismember(locs, valid_pks_indices)), 'or');
end
pause(0.001);
subplot (2,1,2);
xlabel ('Time'); ylabel('Displacement');
for j = i
vel = cumtrapz (time, mag);
vel = filtfilt (b2, a2, vel);
disp = cumtrapz (time, vel);
disp = filtfilt (b2, a2, disp);
plot (time, disp)
end
end
hold off;

Sign in to comment.

Answers (1)

Hi Stella,
I understand that you are encountering issue in updating the displacement together with the magnitude which is peaks or valleys in first subplot.
This can be resolved by modifying the calculation of the displacement after the calculation of the peaks/valleys. I have assumed the values of ‘time’ and ‘mag’ variables for generating the issue.
% Replace these with your actual data
time = linspace(0, 10, 10000); % Simulated time data
mag = sin(2 * pi * 0.5 * time) + 0.1 * randn(size(time)); % Simulated acceleration data with noise
For a clearer understanding, please refer to the code provided below:
% Iterate over the data in increments
for i = 1:200:numel(time)
x1 = time(1:i);
y1 = mag(1:i);
% Update the first plot
refreshdata(h, 'caller');
% Find peaks and valleys
[pks, locs] = findpeaks(mag, 'MinPeakDistance', 1500);
[vks, vlocs] = findpeaks(-mag, 'MinPeakDistance', 1300);
vks = -vks;
valid_vks_indices = vlocs(time(vlocs) <= x1(end));
valid_pks_indices = locs(time(locs) <= x1(end));
if ~isempty(valid_vks_indices)
plot(time(valid_vks_indices), vks(ismember(vlocs, valid_vks_indices)), 'or');
end
if ~isempty(valid_pks_indices)
plot(time(valid_pks_indices), pks(ismember(locs, valid_pks_indices)), 'or');
end
% Calculate velocity and displacement up to the current index
if i > 1 % Ensure there is more than one data point
vel = cumtrapz(time(1:i), mag(1:i));
vel = filtfilt(b2, a2, vel);
disp = cumtrapz(time(1:i), vel);
disp = filtfilt(b2, a2, disp);
else
disp = 0; % Initial displacement is zero
end
% Update the second plot
x2 = time(1:i);
y2 = disp;
refreshdata(h2, 'caller');
drawnow;
pause(0.001);
end
hold off;
In the above code, the velocity and displacement are generated using ‘cumtrapz’ function. Furthermore, I have attached the image of the output subplots received:
For further information, you can refer to the MATLAB Documentation of ‘cumtrapz’ function:
I hope this helps.

Categories

Find more on Graphics Objects in Help Center and File Exchange

Asked:

on 31 Jan 2024

Answered:

on 3 Sep 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!