Get absolute peak location in a vector in MATLAB - matlab

Suppose I have the A matrix like this:
A = [0,0,0,0,0,0,106,10,14,20,20,23,27,26,28,28,28,23,28,28,21,18,106,14,12,
17,16,15,22,19,20,18,21,23,23,18,17,15,106,28];
>> plot(A)
I want the index of the peaks in this dataset. The answer that I'm looking for in this example, is a matrix which contains those x value in your curve when you put the "data cursor" on absolute peaks.
The answer I want is: K = [7 23 39] which is the locations of the peaks.
When I use the plot command in MATLAB, the peaks are clearly visible. How can I find the x value of absolute peaks? Is it possible?

This does not give you all the peaks, but it gives you the maximum value in the set:
max_locations = find(A==max(A))
If you want to find the peaks, use the findpeaks function:
[peakVal,peakLoc]= findpeaks(A);

If you want maximum 5 or 10 peaks, use the following
[peakVal,peakLoc]= findpeaks(A,'sort','descend');

Related

Matlab findpeaks unable to detect peak

I have a 1d signal with a very clear peak, which I wish to characterise. For this, Matlab recommends the findpeak() function. I used it, but it could not detect any peaks.
Can anyone tell me why the peak detection fails, and how I could correct this? Is it because of the slight asymmetry, or because of its location close to the origin?
Here is the signal:
-0.749429547824983,-1.48893649456578,-3.69142404184161,-5.90518397007934,-8.94050442864937,-13.9397113416771,-18.6485000060921,-26.171050819591,-33.8321611238473,-42.6887187100784,-52.7848046259627,-64.0885716722416,-76.4819754427361,-89.7534943093904,-103.596636684011,-117.616848525362,-137.728485864984,-149.968644032202,-165.760526765682,-173.438446162355,-177.794367330021,-180.665504657482,-181.587837865151,-182.05669036563,-181.953081445756,-181.224964655595,-179.466956009378,-174.935326783301,-166.108729320377,-154.8188727578,-138.989450390713,-124.745660884327,-110.75406648731,-97.5101283485409,-85.3020579114654,-75.9579975036876,-67.4601854156705,-61.2940646311251,-55.6332658221936,-50.4524017554082,-45.7226429946525,-41.4134661940384,-37.4939178270878,-33.9335009233627,-30.7027744357005,-27.7737375478043,-25.120055867709,-22.7171735026633,-20.5423444708846,-18.1299037240041,-16.0034032566806,-13.4349709052746,-11.2814814601965,-9.22383728236694,-7.54259823223305,-6.16891326304915,-5.04643005083767,-4.12908680572436,-3.3792629187093,-2.76624567764036,-2.26496715909135,-1.85496794618472,-1.51954915870186,-1.24507971658957,-1.02043099016049,-0.836515683557349,-0.685911850401105,-0.562556362193813,-0.461494999843215,-0.378678691801432,-0.31079735465677,-0.255144373245673,-0.209506048358245,-0.172071392676599,-0.141358513015601,-0.116154515064569,-0.0954664351129079,-0.0784811657953865,-0.0645327193959676,-0.053075478741892,-0.0436623352584338,-0.035926816959731,-0.0295684746597058,-0.0243409295220307,-0.0200420949390602,-0.0165061752797916,-0.0135971170520871,-0.0112032475556222,-0.00923288465794644,-0.00761074093974044,-0.00627497778144994,-0.00517479134910001,-0.00426843397916662,-0.00352159205421904,-0.00290605583052415,-0.00239862841900826,-0.00198023071526613,-0.00163516691682862,-0.00135052167802519,-0.00111566519662662,-0.000881935744953948,-0.000697915251838727,-0.000524637598946218,-0.000394567839006708,-0.000296885161988861,-0.00020976002143167,-0.000148545955794115,-9.72008185405489e-05,-6.36690385791491e-05,-4.17477119966825e-05,-2.74018483759325e-05,-1.8003902287259e-05,-1.18410413188178e-05,-7.7955452552938e-06,-5.13728164003599e-06,-3.38881214609516e-06,-2.2376175188872e-06,-1.47892273125427e-06,-9.78415790827604e-07,-6.47913246914071e-07,-4.29460109665109e-07,-2.84930223745026e-07,-1.89217408323062e-07,-1.25773079886823e-07,-8.36787965589242e-08,-5.57238587861215e-08,-3.71416697587417e-08,-2.47783039314225e-08,-1.6544922828963e-08,-1.10567854116822e-08,-7.39503946153151e-09,-4.94938072790637e-09,-3.31402364108024e-09,-2.2188483621877e-09,-1.48377653147047e-09,-9.88495439963603e-10,-6.52325826840569e-10,-4.20789022022039e-10,-2.56587161450693e-10,-1.50808306057169e-10,-6.34502662515917e-11,-3.11362883724889e-11
and here is the x-data:
0.7,0.700231481481481,0.700462962962963,0.700925925925926,0.701388888888889,0.701851851851852,0.702777777777778,0.703472222222222,0.704166666666667,0.704861111111111,0.705555555555555,0.70625,0.706944444444444,0.707638888888889,0.708333333333333,0.709027777777778,0.709722222222222,0.711111111111111,0.711805555555555,0.7125,0.713194444444444,0.713541666666667,0.713888888888889,0.71412037037037,0.714351851851852,0.714814814814815,0.715277777777778,0.715740740740741,0.716666666666667,0.718055555555556,0.719097222222222,0.720138888888889,0.721180555555555,0.722222222222222,0.723263888888889,0.724305555555556,0.725,0.725694444444444,0.726388888888889,0.727083333333333,0.727777777777778,0.728472222222222,0.729166666666667,0.729861111111111,0.730555555555556,0.73125,0.731944444444444,0.732638888888889,0.733333333333333,0.734027777777778,0.735069444444444,0.736111111111111,0.7375,0.738888888888889,0.740277777777778,0.741666666666667,0.743055555555555,0.744444444444444,0.745833333333333,0.747222222222222,0.748611111111111,0.75,0.751388888888889,0.752777777777778,0.754166666666667,0.755555555555556,0.756944444444444,0.758333333333333,0.759722222222222,0.761111111111111,0.7625,0.763888888888889,0.765277777777778,0.766666666666667,0.768055555555555,0.769444444444444,0.770833333333333,0.772222222222222,0.773611111111111,0.775,0.776388888888889,0.777777777777778,0.779166666666667,0.780555555555555,0.781944444444444,0.783333333333333,0.784722222222222,0.786111111111111,0.7875,0.788888888888889,0.790277777777778,0.791666666666667,0.793055555555555,0.794444444444444,0.795833333333333,0.797222222222222,0.798611111111111,0.8,0.801388888888889,0.802777777777778,0.804166666666667,0.80625,0.808333333333333,0.810416666666667,0.8125,0.814583333333333,0.817708333333333,0.820833333333333,0.823958333333333,0.827083333333333,0.830208333333333,0.833333333333333,0.836458333333333,0.839583333333333,0.842708333333333,0.845833333333333,0.848958333333333,0.852083333333333,0.855208333333333,0.858333333333333,0.861458333333333,0.864583333333333,0.867708333333333,0.870833333333333,0.873958333333333,0.877083333333333,0.880208333333333,0.883333333333333,0.886458333333333,0.889583333333333,0.892708333333333,0.895833333333333,0.898958333333333,0.902083333333333,0.905208333333333,0.908333333333333,0.911458333333333,0.914583333333333,0.917708333333333,0.920833333333333,0.922916666666667,0.925
I copy-pasted them from a comma-delimited .txt file.
If you look at the help you can see:
PKS = findpeaks(Y) finds local peaks in the data vector Y. A local peak
is defined as a data sample which is either larger than the two
neighboring samples or is equal to Inf.
So by default it is looking for a positive peak, you can supply the -ve of the signal and it will find it.
I was able to find the peak by fitting the posted data to a three-parameter Extreme Value peak equation, "y = a * exp(-exp(-((x-b)/c))-((x-b)/c)+1.0)", with parameter b as the peak at approximately x = 0.714. Fitted parameters of a = -1.7877846561352214E+02, b = 7.1375869130522429E-01, and c = 6.4029302224962413E-03 yielded RMSE = 2.64 and R-squared = 0.997.

How to create a relative frequency histogram in matlab?

I have a set of 100 datapoints that I have split into 10 bins and displayed as a histogram, however I would like to display it as a relative frequency histogram so I need to scale the height of each bin by 100. How do I do this?
M = readmatrix('Data.csv');
y= M(:,1);
histogram(y,10)
would it be possible to divide the resulting bins in some simple way?
Use the probability normalization,
histogram(y,10,'Normalization','probability')
See the histogram documentation for the other normalizations available.

histogram of matrix with xlim

I have a m-by-n matrix of values between 10^-20 and 10^-8
I drew it by
figure();
hist(My_mat(:),15);
I obtain only one bins of the hist,also with '15' but I would to obtain several of bins between min and max values
Do you have any suggestions?
Indeed. To draw a histogram having arbitrary bins use:
nbins=10;
figure();
hist(data,nbins)
However, if no second argument is given the standard of 10 bins is used. I suspect your vector contains all identical numbers therefore yielding only a single column. Could you verify this?
Kind regards,
Ernst Jan
This works for me (I get 15 bins):
C=rand(300,600)*1e-20; % to have similar numbers as you have
nbins = 15;
hist(C(:),nbins);
If you still have problems, try
close all; clearvars;
before executing the rest.

use specgram in axes in GUI matlab

I create a matlab gui and have some element in it with some axes. I plot one of my desire plot in ploter1 ( first axes ) using
plot(handles.ploter1,xx); title(handles.ploter1,'Waveform');
and it is ok,but I want use specgram and plot specgram result in another axes by I dont know how can do it :(
I test
specgram(wav,N,fs,hamming(N/4),round(0.9*N/4));xlabel('time, s');
or
specgram(handles.ploter2,wav,N,fs,hamming(N/4),round(0.9*N/4));xlabel('time, s');
but return me error or nothing !!!
please help me. thank you very much
EDIT
as mentioned in the comments by bdecaf, what should work, is to set the current axes:
axes(handles.ploter2);
now, when using just
spectrogram(x,window,noverlap,F)]
the plot should be on the specified axes. If not, try:
hold on
before!
OLD
specgram or spectogram does not have a parameter for the plot. You have to define it later on.
I suggest to get the result first by:
[S,F,T]=spectrogram(x,window,noverlap,F)]
and then plot it on a specific axes:
plot(handles.ploter2, S,F)
But I am not sure about which parameter you want to plot. Please take a look at the docs.
From the docs:
[S,F,T] = spectrogram(...) returns a vector of frequencies, F, and a vector of times, T, at which the spectrogram is computed. F has length equal to the number of rows of S. T has length k (defined above) and the values in T correspond to the center of each segment.
[S,F,T] = spectrogram(x,window,noverlap,F) uses a vector F of frequencies in Hz. F must be a vector with at least two elements. This case computes the spectrogram at the frequencies in F using the Goertzel algorithm. The specified frequencies are rounded to the nearest DFT bin commensurate with the signal's resolution. In all other syntax cases where nfft or a default for nfft is used, the short-time Fourier transform is used. The F vector returned is a vector of the rounded frequencies. T is a vector of times at which the spectrogram is computed. The length of F is equal to the number of rows of S. The length of T is equal to k, as defined above and each value corresponds to the center of each segment.
[S,F,T] = spectrogram(x,window,noverlap,F,fs) uses a vector F of frequencies in Hz as above and uses the fs sampling frequency in Hz. If fs is specified as empty [], it defaults to 1 Hz.

Matlab, how to calculate AUC (Area Under Curve)?

I have the file data.txt with two columns and N rows, something like this:
0.009943796 0.4667975
0.009795735 0.46777886
0.009623984 0.46897832
0.009564759 0.46941447
0.009546991 0.4703958
0.009428543 0.47224948
0.009375241 0.47475737
0.009298249 0.4767201
[...]
Every couple of values in the file correspond to one point coordinates (x,y).
If plotted, this points generate a curve. I would like to calculate the area under curve (AUC) of this curve.
So I load the data:
data = load("data.txt");
X = data(:,1);
Y = data(:,2);
So, X contains all the x coordinates of the points, and Y all the y coordinates.
How could I calculate the area under curve (AUC) ?
Easiest way is the trapezoidal rule function trapz.
If your data is known to be smooth, you could try using Simpson's rule, but there's nothing built-in to MATLAB for integrating numerical data via Simpson's rule. (& I'm not sure how to use it for x/y data where x doesn't increase steadily)
just add
AUC = trapz(X,Y)
to your program
and you will get the area under the curve
You can do something like that:
AUC = sum((Y(1:end-1)+Y(2:end))/2.*...
(X(2:end)-X(1:end-1)));
Source: Link
An example in MATLAB to help you get your answer ...
x=[3 10 15 20 25 30];
y=[27 14.5 9.4 6.7 5.3 4.5];
trapz(x,y)
In case you have negative values in y, you can do like,
y=max(y,0)
[~,~,~,AUC] = perfcurve(labels,scores,posclass);
% posclass might be 1
http://www.mathworks.com/matlabcentral/newsreader/view_thread/252131
There are some options to trapz for the person ready to do some coding by themselves. This link shows the implementation of Simpson's rule, with python code included. There is also a File Exchange on simpsons rule.