Finding very top and bottom peaks of data using Matlab - matlab

I have a set data and I'd like to find the upper and lower peaks of it.
In Matlab, I am trying findpeaks command, but the result is strange.
Here is my simple code:
[pks,locs] = findpeaks(Data);
plot(locs,pks,'or')
and here is the result:
Can someone guide me on how I can find only the very top and very bottom peaks of the data?

You could use the additional input MinPeakProminence to tell Matlab to look only for, yeah well prominent peaks.
[pks,locs] = findpeaks(Data,'MinPeakProminence',4);
plot(locs,pks,'or')
You can play with the parameter and see what works best for you.

Related

how to calculate SNR in matlab from given two signals?

I am working on calculating the SNR of given signals.
length = linspace(-2*pi,2*pi);
signal_1 = sin(length);
plot(signal_1);
noise_1 = 0.5*rand(size(signal_1));
plot(noise_1);
snr_22 = snr(signal_1,noise_1);
But it shows only the noise figure (attached below) whenever I run this, nothing else. But it should also show signal figures and SNR in decibels. Can anyone help me out? I am using Matlab R2015b.
As the documentation on snr() shows:
snr(___) with no output arguments plots the spectrum of the signal in the current figure window and labels its main features. It uses different colors to draw the fundamental component, the DC value and the harmonics, and the noise. The SNR appears above the plot. This functionality works for all syntaxes listed above except snr(x,y).
emphasis mine.
In other words, call figure; snr(signal_1,noise_1, __);, i.e. with at least one other input argument and no output argument, at the end of your code to have the spectrum appear. Otherwise, take a look at the contents of snr_22 and plot it by hand.

matlab area under Gaussian curve 'trapz(y)' and 'trapz(x,y)' function which is more accurate?

I am very new to mat lab and I am trying to get to grips with integrating under a curve.
I wanted to see this difference between using the 'trapz(y)' and 'trapz(x,y)' to find the area under a curve of a Gaussian function what I can not seem to understand is why I am getting two different area values and I am trying to figure which one is more accurate.
dataset = xlsread('Lab 3 Results 11.10.18 (1).xlsx','Sheet3','C6:D515');
x=dataset(:,1);
a1=38.38;
b1=1179;
c1=36.85;
d1=6.3
y=a1*exp(-((x-b1)/c1).^2)-d1;
int1=trapz(x,y)
int2=trapz(y)
So when I run this code I get int1=1738.3 and int2=5.78.4 but when I integrated this function by hand using the trapezium rule my ans came out to be nearer int1 rather that int2 is there anyone that could shed some light on this if possible? I just cant imagen visulay how matlab is using the trapz rule two different ways,
Neither implementation is more accurate, but trapz(y) assumes unit spacing of each data point (e.g., spacing between data points is uniformly x = 1). See trapz documentation.
Since you know the x-coordinates, use trapz(x,y).

Assigning pixels to their local maximum matlab

I need to assign pixels to their local maximum in matlab. I can easily find the local maxima in matlab using imregional max or other derivatives such as extrema2. However, I also want to cluster the pixels based on these local maxima.
For example, if I have a peak then I also want to know if the pixels surrounding this peaks are connected to the maxima. I cant think of any efficient ways of doing this and any help or advice would be much appreciated.
Thank You
Basically you could limit the question to "does the pixel belong to the previous local maximum or to the next one?" and then look at the derivatives (gradients) of the pixels: if negative, it belongs to the previous, if positive it belongs to the next one :)
Following image
with maxima Max1=[17,59] and Max2=[83,59]
and the idea I mentioned for the dimension X:
Pixel=[randi(100),randi(100)];
[XG,YG]=imgradientxy(Image);
Maxima=[Max1;Max2];
ii=1;
while Pixel(1)>Maxima(ii,1);
ii=ii+1;
end
if XG(Pixel)>0
LocalMax=Maxima(ii,:);
else
LocalMax=Maxima(ii-1,:);
end
imshow(Image);hold on
plot(Pixel(1),Pixel(2),'r.');
plot(LocalMax(1),LocalMax(2),'gx');
will (sometimes) give you results like this:
It's just a sketch of my idea, not robust by anymeans, and probably with a couple of erros, so don't go copy-pasting it. You still have the homework to implement that for the 2 dimensional scenario ;)

Matlab gmdistribution.fit only finding one peak

I have an array named Area, which contains a set of values.
The histogram of the array looks like this
The bin width is 60 in this case. I'd like to fit two gaussians to the two peaks here (even if it won't be a great fit).
So I used:
options = statset('Display','final');
obj = gmdistribution.fit(area,2,'Options',options);
gausspdf = pdf(obj, xaxis);
A = sum(gausspdf);
gausspdf = gausspdf/A;
But when I try to plot the two fitted Gaussians, the resulting curve looks like this:
I'm quite confused, as there should be two peaks appearing in the plot?
The gmdistribution.fit method fits data according to maximum-likelihood criterion; that is, it tries to find parameters which maximize the likelihood given the data. It will not necessarily fit what you see or expect visually. Still, there is the possibility that the algorithm converged to a "bad" local minimum. You can try and set the initial conditions according to what you want to get, practically 'helping' the algorithm to converge to the desired result. You do this using the Start option to the fit method, which enables you to give it either an initial guess, in which case you should try and estimate the parameters from the histogram, or an initial component index for each data sample. See the documentation for more details.
I think that your peaks are too close and the function can't distinguish them. so maybe you should change the options for gmdistribution or apply a non-linear function to your data first to get more separate peaks in histogram.

MATLAB curve fit display equation on graph

Is there a way to display the curvefit equation on the graph produced without having to write it down myself manually every time? By GUI or command-line, anything is okay. Any hacks, some way to get around this?
Probably easiest to use the fit utility which is the non-graphical equivalent of using curvefit:
% sample data
x=[1:10]';
y = x+randn(10,1)*0.5;
plot(x,y,'o')
pars=fit(x,y,'poly1');
pars contains the result of the fit, which you can overlay on the plot above with
hold on
plot(pars)
If you want to see the values of individual parameters, you can type pars.p1 or pars.p2 (for this example, there may be other parameters "pn" for other models)
To display on the figure, you can do something simple like
xpos=3;
ypos=9;
text(xpos,ypos,{num2str([pars.p1;pars.p2])})
For more info look into the documentation for curvefit or try help curvefit or help fit.