Plotting animated plots with peaks and valleys problem

4 visualizzazioni (ultimi 30 giorni)
Hi!! I have a problem that I can't do while using a for loop command for showing the delayed peaks and valleys. So far I plotted them in one graph, but at the beginning, the peaks and valleys are showed already in my plot. I wanted them to be shown one at a time (like the attached video) until it reached the end of the data. I tried using the pause command and while loop, but still not working. Am I doing something wrong or my code is incomplete? Also, thank you in advance!!!
P.S. I am still new to matlab, so I am sorry for my dumb question.
%raw data from XYZ direction
NormChest = readtable('TV_500mL.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);
save time.mat time
clear time
load time.mat
%design for low pass filter
fs = 1000; %sampling frequency
fc = 1; %cut-off frequency
order = 2;
[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 (2)
plot (mag); axis tight;
%mean value to 0
mag = mag - mean (mag);
mag = filtfilt (b1, a1, mag);
% peak and valley
[pks, locs] = findpeaks (mag, "MinPeakDistance",2000);
[vks, vlocs] = findpeaks(-mag, "MinPeakDistance",2000);
vks = -vks;
% Initialize the plot
n = time
figure(2);
h = plot(time(1), mag(1)); hold on; % Start with the first data point
xlabel('Time'); axis tight;
title('Subject 1 (500 mL) - Real-time Signal Analysis');
h.XDataSource = 'x1'; % Set the data source properties for dynamic updating
h.YDataSource = 'y1';
for i = 2:length(n)
% Update the data source
x1 = time(1:i*500);
y1 = mag(1:i*500);
refreshdata(h, 'caller');
drawnow;
plot (time (locs), pks, 'or');
plot (time (vlocs), vks, 'or');
pause(0.01); % Can be adjusted
end
hold off;

Risposta accettata

Rishi
Rishi il 23 Gen 2024
Hi Stella,
I understand from your query that you want to plot this graph such that the peaks and valleys show up one by one, as the graph reaches that point.
In the code you've provided, the following lines plot all of the possible peaks and valleys at once.
plot (time (locs), pks, 'or');
plot (time (vlocs), vks, 'or');
Instead, you can add a condition and select the valid indices, such that they only include the ones which are covered by the timestamps stored in 'x1'.
Here is the updated for loop:
for i = 2:length(n)
% Update the data source
x1 = time(1:i*500);
y1 = mag(1:i*500);
refreshdata(h, 'caller');
drawnow;
valid_pks_indices = locs(time(locs) <= x1(end));
valid_vks_indices = vlocs(time(vlocs) <= x1(end));
if ~isempty(valid_pks_indices)
plot(time(valid_pks_indices), pks(ismember(locs, valid_pks_indices)), 'or');
end
if ~isempty(valid_vks_indices)
plot(time(valid_vks_indices), vks(ismember(vlocs, valid_vks_indices)), 'or');
end
pause(0.01); % Can be adjusted
end
The loop now checks if the peak or the valley occured within the required timestamps, and plots them.
Hope this helps!
  1 Commento
Stella
Stella il 23 Gen 2024
Thank you, RIshi!! I also tried the ismember and isempty commands, but I don't know how to incorporate it in the process. Also, I have another question, is there a way that I can make the plotting faster? Because I want to show my "i" every 500 data, but I always have the error with it. I followed the "clear time" but it still shows my error (indexing error). But if I don't put the 500 in it, it take so long for me to finish one whole data in a short time. I used set(time) too, but still the same.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Measurements and Spatial Audio in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by