Python Forum
scipy.signal.find_peak VS Matlab's findpeaks function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
scipy.signal.find_peak VS Matlab's findpeaks function
I'm interested in finding positive and negative peaks in a data sample with Python.

To give you a better idea, I'm considering Matlab's findpeaks function (

For example, when in Matlab I do:

[a1,peak_loc1] = findpeaks(data,"DoubleSided",'MinPeakHeight',peak_height)

I get the peaks and the indices at which the peaks occur.

Now, I found a good candidate in the scipy signal find_peaks function (

This seems very similar. But as is, it does not provide the results I want. In fact it returns the peaks and a dictionary containing properties of the returned peaks.

So to return the a1 and peak_loc1 values I did a little extra operation:

import numpy as np
from scipy.signal import find_peaks

def findpeaks(arr, h, w=1, d=1):
pos = find_peaks(arr, height=h, width=w, distance=d)
pos_list = dict(zip(pos[0], pos[1]['peak_heights']))

neg = find_peaks(arr * -1, height=h, width=w, distance=d)
neg_list = dict(zip(neg[0], neg[1]['peak_heights'] * -1))

full_list = {**pos_list, **neg_list}
full_list = dict(sorted(full_list.items()))

heights = list(full_list.values())
indexes = list(full_list.keys())

return heights, indexes
However the results I get do not match 1:1 the results in Matlab.

To make the difference easier to see please take a look at this screenshot where I put the values side by side. On the left you have matlab's values and on the right Python's values.

[Image: xNsuv.png]

Values in white are identical. However at some point there is wierdness going on where Matlab's corresponding value are shifted one position later. I highlighted the matching values with colors. And then there is two values (in red) which do not have any correspondence in the ones calculated by Python.

This is my Python code:

Please note data is a simple np.array but it has around 12K values in it, so you can copy it here:

data = *
peak_height = 0.4455
a1, peak_loc1 = findpeaks(data, peak_height )
What can be the reason? The only thing that came to my mind is that there is some parameter missing... shouldn't Matlab and Python functions do the same thing? Why they give different results with same input data?

These are the correct a1 and peak_loc1 variables I should be getting:

This are the a1 and peak_loc1 values I'm currently getting with Python which are not correct:

Any help would be greatly appreciated
Comparing the output - Matlab identify some peaks that python did not and vice verse
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

(Sep-15-2020, 11:36 AM)buran Wrote: Comparing the output - Matlab identify some peaks that python did not and vice verse

Yes, that's pretty apparent.

The question is why this happens and how can I get the same behavior of Matlab's peak finder function.

If I change the input variable "data" with another one I have, I get the same results of Matlab. This "data" array though gives different results. I can't figure out why. If it behaves different it should do so everytime, not based on the input "data" variable I feed into it.

Possibly Related Threads…
Thread Author Replies Views Last Post
  scipy.signal findpeaks claw91 10 864 Sep-15-2020, 10:48 AM
Last Post: buran
  Is there similar function to lsqnonlin (in matlab) in python? Jay_Nerella 1 1,572 Nov-11-2019, 08:40 AM
Last Post: feli_x

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020