If someone could point me in the right direction it would be greatly
appriciated.
Todd Cumming
The National Research Council of Canada
>> MaxX = max(x)
>> Location = find(x == MaxX)
>> YValue = y(Location)
a hint:
help imregionalmin
help imregionalmax
us
Thanks for the response!
Todd
"Jeremy Smith" <smit...@umn.NOSPAM.edu> wrote in message
news:ef11...@webx.raydaftYaTP...
I don't know if there are particular functions to do this, but I would
look at the first and second differences in the x and y directions,
assuming the data are sampled on a regular 2D grid. If they are not,
implementing the simple idea below could get messy...
To sketch the idea, we know that a 1D function f(x) has a maximum or
minimum at point x_0 if the 1st derivative f'(x_0) == 0. To check
which it was, consider the 2nd derivative f"(x) to find
f(x_0) is a minimum if f"(x_0) > 0
f(x_0) is a maximum if f"(x_0) < 0
So your porblem basically amounts to search the first difference
of your data to find extremal points, and then check the 2nd
differences at those points to see if they are minima or maxima.
Remember to include the posibility of saddle points, where the point
is a maximum along the x direction and minimum along the y direction,
or vice versa.
Rune
A fairly simple minded expression to return the indexes of all
the local maxima of a vector "a" follows:
find(a>=[a(2:end) inf] & a>[inf a(1:end-1)])
If the maxima is a sequence of several equal values, this will
return the index of the first of those. If you wanted it to
return the index of the last one, just swap the >= and the >.
If you wanted to include endpoints of the sequence as possible
local maxima, just change the two instances of "inf" to "-inf".
If you think about how this works, I think you will find it
easy to extend it to look for local minima as well.
This may be good enough for many applications, although the reason
I called it "simple minded" is that it can be fooled by a plateau.
(As Todd mentioned, plateaus can be tricky).
For instance a sequence such as [1 2 3 3 3 4 4] has no local
maximum, but one will be identified by my simple minded approach.
Suppose we take this 15 element example:
a = [1 2 2 3 2 1 1 5 1 17 17 17 15 20 1 ];
% 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 - index of a
A better algorithm that handles plateaus should return the following
indices as local maxima: 4, 8, 10, 14
And for local minima is should return indicies: 6, 9, 13
This is tricky, but I think you will find that the following code
produces this result exactly:
--------------------------------------------------------------
p = 1:length(a);
b = diff([inf a])~=0;
aa = a(b); pp = p(b);
maxima = pp(find(aa>[aa(2:end) inf] & aa>[inf aa(1:end-1)]))
minima = pp(find(aa<[aa(2:end) -inf] & aa<[-inf aa(1:end-1)]))
---------------------------------------------------------------
~Paul
Expanding on this with an example:
---------------------- findextrema.m -----------------------
% create array with maxima and minima
p = peaks(101);
x = 0:100; % x values
y = 0:100; % y values
% compute derivatives in x- and y-directions
[dp_dx,dp_dy] = gradient(p,x,y);
% compute second derivatives
[d2p_dx2,d2p_dy_dx] = gradient(dp_dx,x,y);
[d2p_dx_dy,d2p_dy2] = gradient(dp_dy,x,y);
% plot contour lines where the first derivatives are zero
cx = contour(x,y,dp_dx,[0 0],'r');
hold on
cy = contour(x,y,dp_dy,[0 0],'g');
% Wherever the red and green lines cross is a point where the derivatives
% are both zero and hence either a local extremum or a saddle point. To
% determine which, check the second derivatives at those intersections-- if
% d2p_dx2 and d2p_dy2 are the same sign and positive then you have a
% minimum, if they are the same sign and negative then you are at a minimum
% and if they have different signs then you are at a saddle point.
flag = zeros(101,101);
flag(d2p_dx2 > 0 & d2p_dy2 > 0) = 1; % minima
flag(sign(d2p_dx2) ~= sign(d2p_dy2)) = 2; % saddle
flag(d2p_dx2 < 0 & d2p_dy2 < 0) = 3; % maxima
% Let's plot those regions using yellow for maxima, gray for saddle points
% and cyan for minima and then overlay the contour lines from the first
% figure
figure
map = [0 1 1;0.5 0.5 0.5;1 1 0];
surf(x,y,flag)
colormap(map)
shading flat
view(2)
axes
contour(x,y,dp_dx,[0 0],'r')
hold on
contour(x,y,dp_dy,[0 0],'g')
-------------------- end findextrema.m ---------------------
I'll leave it for you to figure out how to find the intersections of the red
and green lines, cx and cy.
--
Doug Schwarz
dmschwarz&urgrad,rochester,edu
Make obvious changes to get real email address.
I was going through this thread as even I need to find the first peak in my data (ignoring the plateaus) and then shift that first peak to origin. I did try the following lines of code but it is giving error on line 2 i.e b= diff.. and then when I remove '~=0' theni it gives error at line 3 :aa= a(b)
Please help me with my problem as I am new to MATLAB
Thank you
p = 1:length(a);
b = diff([inf a])~=0;
aa = a(b); pp = p(b);
maxima = pp(find(aa>[aa(2:end) inf] & aa>[inf aa(1:end-1)]))
minima = pp(find(aa<[aa(2:end) -inf] & aa<[-inf aa(1:end-1)]))
Paul Mennen <pa...@mennen.org> wrote in message <4307FE4D...@mennen.org>...
function peaks=findpeaks222(pressuredata)
[m,n]=size(pressuredata);
startindex=2;
finalindex=m-1;
index=1;
peaks=zeros(m,2);
for i=startindex:finalindex
if (pressuredata(i,2)>=pressuredata((i-1),2)...
&& pressuredata(i,2)>=pressuredata((i+1),2))...
|| (pressuredata(i,2)<=pressuredata((i-1),2)...
&& pressuredata(i,2)<=pressuredata((i+1),2))
peaks(index,1)=pressuredata(i,1);
peaks(index,2)=pressuredata(i,2);
index=index+1;
end
end
peaks=[peaks(1:(index-1),1),peaks(1:(index-1),2)];
figure
hold on
plot(pressuredata(:,1),pressuredata(:,2),'k')
plot(peaks(:,1),peaks(:,2),'ro','linewidth',3)
hold off
end
So run this like:
t=1:.1:20;
x=sin(t)+.5*sin(5*t);
pressuredata=[t',x']
z=findpeaks222(pressuredata)
Hope this helps,
--John
Okay so I have a text filw with 10 columns (floating point integers) and I read this file doing a textscan and separate all 10 columns in a cell array each so each is like variable is like huge column matrix. Then from each column I plot that data Vs time and from that figure I need to find first peak value (I have to ignore plateaus). And it is giving me error when I used Paul's code
Thanks
Annie
ImageAnalyst <imagea...@mailinator.com> wrote in message <44d4951a-0971-459d...@p4g2000vba.googlegroups.com>...
> On May 21, 12:32?pm, "Annie " <annieman...@gmail.com> wrote:
> > Hello Paul
> >
> > I was going through this thread as even I need to find the first peak in my data (ignoring the plateaus) and then shift that first peak to origin. I did try the following lines of code but it is giving error on line 2 i.e b= diff.. and then when I remove '~=0' theni it gives error at line 3 :aa= a(b)
> >
> > Please help me with my problem as I am new to MATLAB
> >
> > Thank you
> >
> > p = 1:length(a);
> > b = diff([inf a])~=0;
> > aa = a(b); pp = p(b);
> > maxima = pp(find(aa>[aa(2:end) inf] & aa>[inf aa(1:end-1)]))
> > minima = pp(find(aa<[aa(2:end) -inf] & aa<[-inf aa(1:end-1)]))
> >
> >
> >
> --------------------------------------------------------------------------------------------------
> Annie:
> I ran the code with Paul's a of this:
> > > a = [1 2 2 3 2 1 1 5 1 17 17 17 15 20 1 ];
> > > % ? ?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 - index of a
Didn't know matlab had nice neat functions for local maxes and mins.
Shows how much I know.
--John
It is hard to make any effective help if you don't provide the error message. From what I read Paul assume row vectors, but your data is columns. Did you take any action to take care of that? (use ";" for concatenation or transpose your vector; not doing both of course)
Bruno
The error message I get is as follows:
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
Error in ==> data_analysis at 63
b = diff([inf a])~=0;
Regarding the column vector point, I tried swapping diff([inf a]) to [a inf] as well but same error. Also what will be limits in aa>[aa(2:end) inf] & aa>[inf aa(1:end-1)])); for column vector? It wont change right?
Thanks a lot
Annie
"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <gv5iul$jhv$1...@fred.mathworks.com>...
You must change [ x y ] to [ x ; y ]
The first is row-concatenation the second is column.
Bruno
I'll just throw out one more solution based on convolutions
%%Sample data array
D=zeros(5); D(9)=-1; D(13)=1;
%%%%Find local maxes as follows. To find local mins, do the same on -D
A1=[-1 0 0; 0 1 0; 0 0 0];
A2=rot90(A1);
A3=rot90(A2);
A4=rot90(A3);
B1=[0 -1 0; 0 1 0; 0 0 0];
B2=rot90(B1);
B3=rot90(B2);
B4=rot90(B3);
T=conv2(D,A1,'valid')>0;
T=T&( conv2(D,A2,'valid')>0 );
T=T&( conv2(D,A3,'valid')>0 );
T=T&( conv2(D,A4,'valid')>0 );
T=T&( conv2(D,B1,'valid')>0 );
T=T&( conv2(D,B2,'valid')>0 );
T=T&( conv2(D,B3,'valid')>0 );
T=T&( conv2(D,B4,'valid')>0 );
[I,J]=find(T);
I=I+1;
J=J+1;
Hello Bruno
Dou you mean change [inf a] to [inf;a] or [inf : a]?? If we put a semicolon, it gives a syntax error
Semi colon is correct, like below:
>> a=[1; 2; 3]
a =
1
2
3
>> [inf; a]
ans =
Inf
1
2
3
Further reading: http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/f2-12841.html#f2-21117
Bruno
Thanks a lot but I am still not able to get this right and it is critical for me, please help.
Now my code looks like
p = 1:length(y);
b = diff([inf ; y])~=0;
aa = y(b); pp = p(b);
Imx = pp(find(aa>[aa(2:end) inf] & aa>[inf aa(1:end-1)]));
and I get following error still
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
Error in ==> data_analysis_peak at 65
Imx = pp(find(aa>[aa(2:end) inf] & aa>[inf aa(1:end-1)]));
Please help!
_______________________________________________________________
"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <gv6pcn$bo5$1...@fred.mathworks.com>...
You haven't read carefully Annie. I wrote above "You must change [ x y ] to [ x ; y ]", and look at the line where the error occurs. Did you change the syntax accordingly? And did you try to understand the difference between
[x y] % and
[x; y] %?
Again, please read the doc.
> help horzcat
> help vertcat
It is fundamental to understand the basic matrix manipulation. You cannot rely permanently on me or people on this board to fish for you. It's time for you to catch the fish.
Bruno
Is there a way to capture the first relevant peak in a generic and consistent manner?
__________________________________________________________________
"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <gvhk16$efr$1...@fred.mathworks.com>...