Sep-15-2020, 10:47 AM
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/referen...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:
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]](https://i.stack.imgur.com/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
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
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/referen...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, indexesHowever 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]](https://i.stack.imgur.com/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