Python Forum
scipy.signal.find_peak VS Matlab's findpeaks function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: scipy.signal.find_peak VS Matlab's findpeaks function (/thread-29663.html)



scipy.signal.find_peak VS Matlab's findpeaks function - claw91 - Sep-15-2020

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 (https://it.mathworks.com/help/signal/ref/findpeaks.html).

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 (https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.find_peaks.html).

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: https://pastebin.pl/view/d3d5dba4

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: https://pastebin.com/ucPcx5YD

This are the a1 and peak_loc1 values I'm currently getting with Python which are not correct: https://pastebin.com/4UB8Rzj4

Any help would be greatly appreciated


RE: scipy.signal.find_peak VS Matlab's findpeaks function - buran - Sep-15-2020

Comparing the output - Matlab identify some peaks that python did not and vice verse


RE: scipy.signal.find_peak VS Matlab's findpeaks function - claw91 - Sep-15-2020

(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.