Islocalmin/max on an Animated Plot and Indices Problem

Hi again!! I try to use islocalmin/max instead of findpeaks for my valleys and peaks, then use the previous answers for the same problem. But when I put the code in my for-loop code, it says "Array indices must be positive integers or logical values". Am I doing something wrong? because before I used findpeaks and call it even before the for-loop so it's not actually a real-time signal processing, so i put it in the for-loop and use local min/max instead. Also i multiplied my i to 100 because I want to plot it fastly, but also get problems on it.
%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 = 0.5; %cut-off frequency
order = 2;
[b1, a1] = butter (order, fc/(fs/2));
%--------------------------------------------------------------------------
% magnitude computation
mag = sqrt(x.^2+y.^2+z.^2);
figure (1)
plot (mag); axis tight;
mag = filtfilt (b1, a1, mag);
%--------------------------------------------------------------------------
% Initialize the plot
figure(2);
h = plot(time(1), mag(1)); hold on; % Start with the first data point
xlabel('Time'); axis tight;
title('Real-time Signal Analysis');
h.XDataSource = 'x1'; % Set the data source properties for dynamic updating
h.YDataSource = 'y1';
for i = 2:length (time)
% Update the data source
x1 = time(1:i*100);
y1 = mag(1:i*100);
refreshdata(h, 'caller');
drawnow;
[TF1, P1] = islocalmin (mag);
[TF2, P2] = islocalmax (mag);
TF1 = -TF1;
valid_vks_indices = P1 (time(P1) <= x1 (end));
valid_pks_indices = P2 (time(P2) <= x1 (end));
if ~isempty (valid_vks_indices)
plot (time(valid_vks_indices), TF1(ismember(P1, valid_vks_indices)), 'or');
end
if ~isempty (valid_pks_indices)
plot (time(valid_pks_indices), TF2 (ismember(P2, valid_pks_indices)), 'or');
end
pause(0.001);
end
hold off;
Thank you in advance for the help and I am still working on being good in Matlab, too.

Respuestas (1)

Dyuman Joshi
Dyuman Joshi el 28 de En. de 2024
The syntax of islocalmax (and islocalmin) is different than that of findpeaks.
Check the documentation for the appropriate syntax and incorporate the corrections accordingly.

5 comentarios

Thank you!! I checked again and fixed it. I used find(islocalmin) and find (islocalmax) instead.
@Stella, If you don't need the indices explicitly, you can use the functions directly as well -
A = [2 4 6 4 3 7 5 6 5 10 4 -1 -3 -2 0];
idx = islocalmax(A)
idx = 1×15 logical array
0 0 1 0 0 1 0 1 0 1 0 0 0 0 0
B = A(idx)
B = 1×4
6 7 6 10
Logical indexing is generally faster than using find().
If my answer solved your problem, please consider accepting the answer.
Thank you for your idea! But I actually want to plot in my animated graph all the peaks and valleys that were found and show them one by one as the data gets every 100 iterations until the end of the data. Previously I used the isempty and ismember function on my findpeaks and works perfectly fine, but now it only shows the plot with the peaks/valleys but blinking with different colors.
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);
% newdata = mag(mag(:,1)<=x1,:);
% newdata2 = mag(mag(:,1)<=x1,:);
if ~isempty (vks)
plot (time, mag, x_vks, y_vks, 'or')
end
if ~isempty (pks)
plot (time, mag, x_pks, y_pks, 'or')
end
pause (0.01);
Could you please share the data you are working with, so I can run your code and see what the issue is?
Ohh !!Thank you!! I was actually having trouble with it for days and I want to have like the same (or closer) one with a labview code i made before where i also waited sometime before showing the peaks/valleys to make sure that they are really the peaks and valleys. I know I already have my labview code, but I am still new to matlab so I am not yet familiar with a lot of the functions. I also attached the example video of the labview code i made.

Iniciar sesión para comentar.

Preguntada:

el 28 de En. de 2024

Comentada:

el 30 de En. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by