Islocalmin/max on an Animated Plot and Indices Problem

2 次查看(过去 30 天)
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.

回答(1 个)

Dyuman Joshi
Dyuman Joshi 2024-1-28
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 个评论
Dyuman Joshi
Dyuman Joshi 2024-1-30
Could you please share the data you are working with, so I can run your code and see what the issue is?
Stella
Stella 2024-1-30
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.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by