# Matlab: Detect Peaks and Signal Jump

I want to detect 6 peaks in a signal. I am open to any suggestions. There is one problem where my signal sometimes jumps high and stays higher than the rest of the signal. I don't know why this happens but would like a solution to lower the signal back to the points where the rest of the signal was lower, which might help detect all 6 peaks. I attached a picture.

• If you know that the jump is greater, or much greater than the peaks, themselves, and if you know an estimate of the value of the jump, then you can simply detect first when it occurs (as per the pic, say val=80), and when that happens subtract its value (the pic shows ~160), then, when the jump drops below 80, you know it has past and the trick is no longer needed, until the next jump. In the meantime, perform detection of the peaks as you wish. Two ifs, though. Mar 22, 2019 at 6:59

I don't recommand that you use a simple derivative. It's not a robust method, especially when there's noise or when the changes that you want to detect are spread on many samples.

There are 2 on-line algorithms that I used in the past that are pretty robust

1 - Filtered derivative algorithm

This algorithm uses a filtered derivative as the name implies. It's insensitive to noise and by selecting the proper number of samples you can easily detect a change that occurs on many samples.

2 - CUSUM (cumulative sum)

https://en.wikipedia.org/wiki/CUSUM

This algorithm detects a change in the mean. If the change exceeds a certain threshold, a change is detected. From there, you can estimate the amplitude of the change.

I think the easiest option is to detect the massive sudden increase/decrease. Try calculating the difference between samples. If this difference is too big (positive or negative), we substract this difference on all future measured samples.

Create a variable for this offset. start with an offset of 0, when de absolute value of the difference between samples is too big, add this (non absolute) difference to the offset, then all next samples are $$x_{new}=x-offset$$.

• How do you define "to big" ... what determines if it is too big ... 2 standard deviations, greater than the mean, what metric could I use ... Mar 22, 2019 at 15:19
• Try to calculate the difference in the samples. for sample $i$ calculate the difference between sample $i$ with sample $i-1$. The threshold to detect if the difference is too big should obviously be less than the smallest signal jump you've encountered, but not small that you detect a false signal jump. In the example you've provided, I would go for a threshold value like 75. Mar 22, 2019 at 16:02

Try calculating a moving-window fit of a well-chosen test function to your data. This would also remove the necessity of removing your signal jumps. Your peaks don't really look like a peak to me, but more like a smeared-out box-car function. You could choose the analysis window in such a way that it is approximately equal to the width of the peak and use something like $$a(x-w_\textrm{center})^4 + c$$ as a fit function ($$w_\textrm{center}$$ is a shift of the window center so that your parameter $$a$$ yields a maximum in the middle of your peaks). You will then have to set an appropriate threshold on the resulting series of $$a$$-parameters, which should have a much clearer peak-like structure. You can also concentrate only on the negative values of $$a$$ to identify only the peaks and not the dips between the peaks.

I wrote this to detect shapes that I know before hand (like sawtooth and steps). So if your "peak" shapes are consistent, it could find those two without changing the original signal. You need to play with parameters, but I think it will work:

https://www.mathworks.com/matlabcentral/fileexchange/61291-shape_projection_filter?s_tid=prof_contriblnk

And then I've always had success with peakfinder: https://www.mathworks.com/matlabcentral/fileexchange/25500-peakfinder-x0-sel-thresh-extrema-includeendpoints-interpolate