finding prominent maxima and minima of a column vector

8 views (last 30 days)
Hello,
Suppose I Have a column vector wcich has multiple maxima and minima, I just want to find the index of N= 4 number of them ( prominent maxia and minima).
And if I use something like 'findpeak' then it gives a lots of local max and min as for a noisy signal. I want to get the prominant max & min indices of the 2nd colm of the attachment please.
Thank you

Accepted Answer

Star Strider
Star Strider on 21 Jun 2019
Although findpeaks needs some help here, it can find all four:
[D,S] = xlsread('ab1.xlsx');
[pks,plocs] = findpeaks([D(:,2); min(D(:,2))], 'MinPeakProminence',1); % Append ‘0’ To Far End To Identify Last Peak
[vls,vlocs] = findpeaks(-D(:,2), 'MinPeakProminence',1)
figure
plot(D(:,1), D(:,2))
hold on
plot(D(plocs,1), pks, '^r')
plot(D(vlocs,1), vls, 'vr')
hold off
grid
legend('Data','Peaks','Valleys')
The associated times are: D(plocs,1).
pks =
1.2000
1.2001
1.2001
1.2000
locs =
3165
4072
4675
5138
Similarly for the valleys:
vls =
1.0e-04 *
-0.0140
0.3678
0.5941
-0.1694
vlocs =
2708
3753
4446
4954
  2 Comments
az
az on 21 Jun 2019
Thank you so much Mr. strider.
I have little question,
why we need S here?
on the 2nd line why this is nessary at the last portion min(D(:,2))],
Thank you
Star Strider
Star Strider on 21 Jun 2019
As always, my pleasure.
We need ‘S’ so I know what the columns represent. (More knowledge is generally better.) There is no absolute guarantee that the first column is time. You mentioned a column vector, not the second column of an (Nx2) matrix, so I had to be sure I knew what I was working with.
The findpeaks function does not consider the end of a vector to be a peak, so it will never return that as a peak. Concatenating that vector with the minimum of the vector creates a peak (as far as findpeaks knows) where one did not previously exist. Since you stated that there were four peaks, and the last one looked like the first three (as much as we can see of it, anyway), I made it a peak by defining a lower value after it. (I do not generally recommend creating data where none previously existed, however here it seemes safe in order to define the last peak.)

Sign in to comment.

More Answers (1)

az
az on 21 Jun 2019
Thank you Mr. Strider for your explanation.
The reason I was trying to finf the peaks and vallys is that I want break the whole data to several segment from one peak to velly, vally to peak.
Here is mt whole data file where the column 8th was time and 12 was voltage I separately gave you before. and Column 25 has capacity that is sorted to start from origin in time axes everytime for a segment of data, from peak to vally , vally to peak.
I am using your code in capacity3.m file before segmenting data.
I think line 17 in not necessary as your code already gived 'plocs' 'vlocs' ascending order. I just want to be sure about the order.
But for chopping the data I am using the clumsy codes from line 19th to 26th, and this actually does not chopping the data as the data point remain connected. So is there any short cut way that I can chop the data after a peak or vally and start from next data point without loasing the data.
Thanks.
  9 Comments
az
az on 22 Jun 2019
Thank you Mr. Strider for helping me all the way.
This 'capture2' is the graph that actually I wanted.
I realize there are mostly 5 cionnecting data points after a vally to starting points of the curve leads toward peak.
And Almost 4 connecting data points after a peak to the starting curve leads towards vally.
So in capacity3.m I omit those data points manually from line 19th to 26.
Is there any way two for loop ( one for index_min or index_max row number , another for number to add (5/4) to even/odd number of index (index2/ index3 ) can do that?
Thank you
Star Strider
Star Strider on 23 Jun 2019
As always, my pleasure.
So in capacity3.m I omit those data points manually from line 19th to 26.’
There are several way to do this, one is to completely eliminate them by setting that range equal to the empty array [], another is to set them equal to NaN. What you do depends on the result you want.
I ran your code, and it seems to do what you want. There are several connecting lines that appear strange to me (they do not appear on the plots my code produces), however looking at your plot calls, you added them specifically, so they are not artefacts.
I do not understand what you want to do, especially with respect to adding more for loops. I encourage you to experiment to get your code to do what you want it to.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!