Aug-28-2020, 09:28 AM
Hello there. I'm new to Python and my goal is to replicate Matlab's findpeaks prebuilt function.
The goal is to find positive and negative peaks. In Matlab you just give as parameters the data and the minimum peak height.
The data is represented by the variable fh and the minimum peak height is represented by the variable pk_ht = 0.44554
fh is here https://paste.ofcode.org/u25D7BdXz3cUkVbJ9iRZz
This is the result I have in matlab when I call the function.
a1, peak_loc1 = findpeaks(fh, pk_ht)
You have a1 here https://paste.ofcode.org/3ayAhPYpiQHshJLiuzR8Lkw
and you have peak_loc1 here https://paste.ofcode.org/mxjrjECvdJLTXAvU2mVtyS
In Python I used the scipy signal library which seems to be very similar.
With this code
Here is a1 https://paste.ofcode.org/RfSjgvJbWNBXTtwZXpQCwK
and here is peak_loc1 https://paste.ofcode.org/32evhaBHPceX8qba9qS4Kyd
To make the difference easier to see please take a look at the screenshot where I put the values side by side
![[Image: EYUbi4W.png]](https://i.imgur.com/EYUbi4W.png)
Values in white are identical. However at some point there is weirdness going on where Matlab's corresponding value are shifted one position later. I highlighet 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.
I've been losing my mind for days now and really can't come up with any solution. It's even weirder that if I change the data with some other data (fh variable), then I get 1:1 results. This fh data, though has this annoying issue.
Can you guys help me figure out what could be the issue? I also tried numpy based libraries for findpeaks, but I end up with similar results or even worst. So this solution so far has been the closest to the Matlab's
The goal is to find positive and negative peaks. In Matlab you just give as parameters the data and the minimum peak height.
The data is represented by the variable fh and the minimum peak height is represented by the variable pk_ht = 0.44554
fh is here https://paste.ofcode.org/u25D7BdXz3cUkVbJ9iRZz
This is the result I have in matlab when I call the function.
a1, peak_loc1 = findpeaks(fh, pk_ht)
You have a1 here https://paste.ofcode.org/3ayAhPYpiQHshJLiuzR8Lkw
and you have peak_loc1 here https://paste.ofcode.org/mxjrjECvdJLTXAvU2mVtyS
In Python I used the scipy signal library which seems to be very similar.
With this code
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 when I call the function
a1, peak_loc1 = findpeaks(fh, pk_ht)the results do not match precisely.
Here is a1 https://paste.ofcode.org/RfSjgvJbWNBXTtwZXpQCwK
and here is peak_loc1 https://paste.ofcode.org/32evhaBHPceX8qba9qS4Kyd
To make the difference easier to see please take a look at the screenshot where I put the values side by side
![[Image: EYUbi4W.png]](https://i.imgur.com/EYUbi4W.png)
Values in white are identical. However at some point there is weirdness going on where Matlab's corresponding value are shifted one position later. I highlighet 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.
I've been losing my mind for days now and really can't come up with any solution. It's even weirder that if I change the data with some other data (fh variable), then I get 1:1 results. This fh data, though has this annoying issue.
Can you guys help me figure out what could be the issue? I also tried numpy based libraries for findpeaks, but I end up with similar results or even worst. So this solution so far has been the closest to the Matlab's