Measuring Distance Between Peaks - matlab

I have used the (findpeaks) function in Matlab, in order to find the locations and the value of the peaks in the signal.
findpeaks(X_Segments{1});
X_Segments contains the data, here is sample of the figure that I got
As shown in the figure, some of the peaks are very close to each other, therefore, I'd to filter out these peaks (remove them) in order to have Peaks in the signal at regular intervals.

If you pass the parameter MinPeakDistance into the findpeaks function you can set the minimum distance between peaks. In your case:
findpeaks(X_Segments{1}, 'MinPeakDistance', 10);
Alternatively, you may find the MinPeakProminence or threshold parameters useful. See the examples here.

Related

How to find the relative minimums of a vector (not the minimum value)

Let's suposse I have a vector of values whose representation is the following one:
The vector of data has 2000 elements and as you see, there is another vector with degrees between -180º and 180º. I'd like to find the indices of each minimum peak, but I don't know how to implement the algorithm.
In other cases, I set a threshold value (for example -75dBm) and considered a minimum any value under -75dBm, but in this case, there are peaks above -70dBm and I can't increase the threshold value since my measurements will be wrong.
I hope someone can help me. Thank you for your responses.
For data like this, your best bet is to smooth it and then use findpeaks.
For an example
%% parameters to adjsut
%smoothing window length
smthwin=50;
%minimum peak prominence
mpp=0.01;
%% create some test data
x=(1:1000)/1000;
y=sin(3*x-0.5).*sin(5*x).*sin(9*x-0.1).*sin(15*x-0.3)+rand(size(x))/10;
%% smooth and findpeaks
%smooth it
%here I use a median filter, but smooth() is a great function too with lots of options
ysmth=medfilt1(y,smthwin);
%use findpeaks on -y to find local minima
[pks,pks_loc]=findpeaks(-ysmth,'MinPeakProminence',mpp);
%the location of the local minima is
mins=x(pks_loc);
%%plot to check
figure
plot(x,y)
hold on
plot(mins,-pks,'o','LineWidth',2)

Find the highest unknown number of peak points in Matlab

I want to find the highest peak points in the below histogram. For example as it is seen in the figure, I should select 4 peak points, but I get this 4 points information after seeing the histogram, so I need to find it by coding. Is there any method or algorithm to solve this problem?
If I select manually I could solve the problem. However, I do not know the number of the highest peak points. Actually the main problem is determining a threshold.
[pks,locs] = findpeaks(difference)
[sortedX,sortingIndices] = sort(difference,'descend');
locsize=size(locs,2);
counter=1;
peak_order=[];
while counter<5
for j=1:locsize
if sortingIndices(counter)==locs(j)
peak_order(counter)=sortingIndices(counter);
counter=counter+1;
end
end
end
sorted_peak_order=sort(peak_order)enter code here
findpeaks has a series of options to refine your results. In your case, the option 'MinPeakProminence' should work; it thresholds according to the prominence of a peak to its neighbors.
[pks,locs] = findpeaks(difference,'MinPeakProminence',0.25*max(difference))

MATLAB: count number of peaks

I have a graph like this and I want to determine the number of peaks. Since it's a wave function the whole graph has many peaks that is why I was unsuccefull in finding the number of peaks using functions such as findpeaks for the graph below it returns a number of peaks around 3000 whereas I want to have the number 7.
My idea was to do a for or while loop that counts the number of instances where the average is higher than 0.5. So ultimately I want a function that iterates in segments over the graph returns the number of peaks and the range of y index values for which this peak occurs (I think the best way to do this would to save them to a zeros matrix).
link of file data: https://www.dropbox.com/s/vv8vqv28mqzfr9l/Example_data.mat?dl=0
Do you mean you are trying to count the 'on' parts of your data?
You're on the right track using findpeaks. If you scroll to the bottom of the documentation you'll see that you can actually tweak the routine in various ways, for example specifying the minimum distance between peaks, or the minimum difference between a point and its neighbour before it is considered a peak.
By defining the minimum distance between peaks, I detected the following 7 peaks. Code is included below. Alternatively you can play around with the other parameters you can pass into findpeaks.
The only other thing to note is that I took the absolute value of your data.
load('Example_data.mat')
indx = 1:numel(number11);
[pks, locs] = findpeaks(abs(number11), indx, 'MinPeakDistance', 0.25e4);
hold on
plot(number11)
plot(locs,pks, 'rx')
disp(numel(pks))

How to measure the period between the peaks or lows of waves?

I want to measure the period between the peaks/lows of the adjacent waves shown in the figure.
This is an oscillatory behavior of calcium concentration in a cell. The peaks are not the same, hence I would need to calculate the peak/lows for each wave, obtain the corresponding time associated with the peaks/lows, and find the difference between adjacent peaks/lows. I have stored the calcium concentration values for every time "0.01".
Can anyone suggest me how I should code it? I would prefer to use smaller lines of code.
Look into the inbuilt findpeaks function that can return you the index of peaks in your signal.
You could use this to also find the lows in your signal by first squaring your signal. Something like this could work (I haven't tried this in MATLAB so there could possibly be some syntax issues):
% Square the signal so that the lows become peaks
signal = signal .^ 2;
% Get the location of the peaks in terms of t (your time vector)
[~, peaksAndLows] = findpeaks(signal,t)
% Find the difference between consecutive peaks/lows
periodsBetweenPeaksAndLows = diff(peaksAndLows);

determine index and value of first negative peak

I am solving a funcion which uses the moving average filter to remove noise. How can I determine index and value of first and second negative peak after I apply the filter to input data?
Use findpeaks on the negative of your data, then extract the first two elements to extract the first and second indices of where the negative peaks are located. Supposing your signal was stored in f, you would simply do:
[peaks, locs] = findpeaks(-f);
p = peaks(1:2);
loc = locs(1:2);
findpeaks works by finding local maxima. If you want to find local minima (i.e. negative peaks), you would apply findpeaks to the negative of your signal so that the local minima become local maxima, then apply the same algorithm. loc would contain the first two locations of where the negative peaks are, while p will determine those negative peak amplitudes.
However, you'll probably need to play around with the input parameters to findpeaks, instead of using the default ones to suit your data, but this should be enough to get you started.
Sidenote
If you don't have access to findpeaks, take a look at this post that I wrote to find peaks for FFT data. The data is different, but the overall logic is the same. However, this finds all peaks - both local maxima and minima. If you just want to find the minima, simply look at the negative of the signal rather than the absolute value.