Home > other >  Matlab : findpeaks with flat regions
Matlab : findpeaks with flat regions

Time:09-23

I have data which contains peaks that I want to detect with the function findpeaks from the Signal Processing Toolbox, in Matlab.

You will understand my problem with some images : My signal in red, peaks in blue

-> Here you have my data in red, and the finding peaks in blue ; with this code :

[peakHeight, peakLocation] = findpeaks(data,'minPeakHeight',4.3);

I wanted to only keep the first ones (yellow circles). Sometimes, the first peak in my "red block" is not at the yellow circles location, like this :

The peak is not at the start of the red block

I tried many parameters of findpeaks, like 'MinPeakProminence','Threshold' and 'MinPeakDistance', but the best I obtain is this, with findpeaks(data,'minPeakHeight',4.3,'MinPeakProminence',4); :

Not the good peaks

As my peaks in my data are not really flat, I don't always keep the first good ones. In my research I found and tried this : islocalmax(data,'FlatSelection','first');, but it doesn't work well too.

To sum up, I want to have one peak for every "red block". And these peaks need to be just after the rise. So please, have you some idea to solve my problem ? Thanks :)

CodePudding user response:

It looks like you just need to detect the points where the input data crosses some threshold, in your example this threshold might be 4.

For example:

% Create some dummy data
x = linspace(0,9,1000);
y = rand(size(x))/10   4.5*(mod(round(x),3)==1);
y = movmean(y, [3, 3] );

% Set a threshold
thresh = 4;
% Check when the signal exceeds this threshold
overThresh = y > thresh;
% Get the indicies where the threshold exceeding region starts
bRisingEdge = [false, diff(overThresh)>0];
% Get the x & y values at the rising edge
yRising = y(bRisingEdge);
xRising = x(bRisingEdge);

% Plot the results to confirm
figure(1); clf; plot( x, y, 'r' );
hold on; plot( xRising, yRising, 'ko', 'markersize', 10 );

plot

  • Related