Python Forum
scipy.signal findpeaks - 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 findpeaks (/thread-29331.html)

Pages: 1 2


scipy.signal findpeaks - claw91 - Aug-28-2020

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

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 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]

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


RE: scipy.signal findpeaks - deanhystad - Aug-28-2020

Documentation says the function may return unexpected results for data containing NaNs. Does your fh data contain NaNs?


RE: scipy.signal findpeaks - claw91 - Aug-28-2020

(Aug-28-2020, 09:59 AM)deanhystad Wrote: Documentation says the function may return unexpected results for data containing NaNs. Does your fh data contain NaNs?

it doesn't


RE: scipy.signal findpeaks - claw91 - Aug-31-2020

So uh, guys? Anybody willing to help?


RE: scipy.signal findpeaks - claw91 - Sep-03-2020

If you need additional info I can provide... there's almost 200 views but no actual contribution here :(


RE: scipy.signal findpeaks - claw91 - Sep-10-2020

Do you guys know if there's any difference betweeb Matlab's findpeaks and Scipy signal findpeaks?

Docs for Python here https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.find_peaks.html shows a lot of parameters. I'm feeding the same exact parameters that the Matlab function does, i.e. array and the peak height but there's lot more that can be added like: threshold, prominence, wlen, plateau size etc.

My guess is that Matlab's probably defaults some of these parameters to different value than the Python's... otherwise I cannot explain the differences in the result given the exact same input


RE: scipy.signal findpeaks - claw91 - Sep-11-2020

how to edit the original post?

The code paste link is not working anymore


RE: scipy.signal findpeaks - buran - Sep-11-2020

you can post the code here, or if too long - make a repo (e.g. on github)


RE: scipy.signal findpeaks - claw91 - Sep-15-2020

(Sep-11-2020, 10:21 AM)buran Wrote: you can post the code here, or if too long - make a repo (e.g. on github)

Is it ok if I make a new thread using some pastebin (which does not expire) just for the long variables and then you can delete this one?


RE: scipy.signal findpeaks - buran - Sep-15-2020

you can create new one, but please post a link in this thread too.
Or just post in this one.