Find min and max values in a constant interval.

18 views (last 30 days)
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

Accepted Answer

Star Strider
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)
T1 = 1536×5 table
Sec mm N N-mm segments ______ ________ ______ _____ ________ 30.421 -0.21716 -19508 72081 147 30.423 -0.21744 -19509 72444 147 30.425 -0.2175 -19510 72486 147 30.427 -0.21666 -19509 72702 147 30.429 -0.21753 -19510 72646 147 30.431 -0.21773 -19509 72706 147 30.433 -0.21825 -19510 72574 147 30.435 -0.21739 -19508 72533 147 30.437 -0.21806 -19506 72399 147 30.438 -0.21821 -19506 72374 147 30.44 -0.21829 -19505 72291 147 30.442 -0.2181 -19505 72205 147 30.444 -0.21642 -19505 72104 147 30.446 -0.21646 -19507 71872 147 30.448 -0.21606 -19507 71568 147 30.45 -0.21704 -19509 71007 147
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
segs = 3×2
1 512 513 1024 1025 1536
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
Dzhem Kurtulan
Dzhem Kurtulan on 14 Jun 2021
Hello Star Strider, thank you for your answer.
I uploaded a part of my data, but actually data is much bigger, around 700MB.
What i am trying to do is, to plot the drawn lines on the original data. So plot of max and mins. Your cell array pks and vys has 3 values for each column, so i am a bit confused about it, could you explain me that?
Star Strider
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.

Sign in to comment.

More Answers (1)

KSSV
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.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!