Find min and max values in a constant interval.
19 views (last 30 days)
Show older comments
Dzhem Kurtulan
on 11 Jun 2021
Commented: Star Strider
on 14 Jun 2021
Hello all,
i have a dataset, which records Torque in a cyclic rotation, which creates a sinus signal. It writes every 30 seconds 512 Values and then stops for 30 seconds again.
This dataset records constantly the required torque, but i only need the max and min of the sinus curve for each cycle. I could not make findpeaks() work on this dataset. Could you help me to extract max and min values of each cycle? I added a small section of the data.
Regards
2 Comments
Accepted Answer
Star Strider
on 11 Jun 2021
The findpeaks function works, however it needs help to detect correctly the maxima and minima.
Try this —
opts = detectImportOptions('https://www.mathworks.com/matlabcentral/answers/uploaded_files/649685/Example.txt', 'DecimalSeparator',',', 'VariableNamingRule','preserve');
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/649685/Example.txt',opts)
t = T1.Sec;
trq = T1.('N-mm');
figure
plot(t, trq)
grid
xlabel('Time (s)')
ylabel('Torque (N-mm)')
title('Original Signal')
gaps = [find(diff(t)>0.01); size(T1,1)]; % Time Discontinuities
segs = [[0;gaps(1:end-1)]+1 gaps] % Segment Indices
segnrs = size(segs,1);
figure
for k = 1:size(segs,1)
idxrng = (segs(k,1):segs(k,2));
[pks{k},plocs{k}] = findpeaks(trq(idxrng), 'MinPeakProminence',250); % Maximum Values & Relative Indices For Each Segment
[vys{k},nlocs{k}] = findpeaks(-trq(idxrng), 'MinPeakProminence',250); % Minimum Values & Relative Indices For Each Segment
subplot(segnrs,1,k)
plot(t(idxrng)-t(segs(k,1)), trq(idxrng))
hold on
plot(t(idxrng(plocs{k}))-t(segs(k,1)), pks{k}, '^r')
plot(t(idxrng(nlocs{k}))-t(segs(k,1)), -vys{k}, 'vr')
hold off
grid
title(sprintf('Segment #%d',k))
ylabel('Torque (N-mm)')
text(t(idxrng(plocs{k}))-t(segs(k,1)), pks{k}, compose(' \\leftarrow %+6.0f',pks{k}), 'Horiz','left')
text(t(idxrng(nlocs{k}))-t(segs(k,1)), -vys{k}, compose(' \\leftarrow %+6.0f',-vys{k}), 'Horiz','left')
end
xlabel('Time (s)')
The code is reasonably straightforward. The only complications are the indexing for the various segments in the loop. The maxima and minima are in the ‘pks’ and ‘vys’ cell arrays respectively, and are displayed on the plots.
.
2 Comments
Star Strider
on 14 Jun 2021
I interpreted the information in the original post as as best as I could. My code provides the maxima and minima for each cycle, as requested, and demonstrates how to retrieve the results of that analysis.
To calculate and plot the maxima and minima of the entire data set (and not of the individual cycles), use the envelope function.
More Answers (1)
KSSV
on 11 Jun 2021
If A is your complete matrix.
t = A(:,1) ; % this is seconds column
iwant = A(t==30,:) ; % extract chunk which is for 30 secs
Now you have data of 30 seconds in hand, you can find max and min of the column you want.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!