Two Subplots in an Animated For-loop Problem

조회 수: 4 (최근 30일)
Stella
Stella 2024년 1월 31일
답변: Drishti 2024년 9월 3일
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
Angelo Yeo
Angelo Yeo 2024년 2월 1일
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')
Stella
Stella 2024년 2월 1일
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;

댓글을 달려면 로그인하십시오.

답변 (1개)

Drishti
Drishti 2024년 9월 3일
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.

카테고리

Help CenterFile Exchange에서 Measurements and Feature Extraction에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by