Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
scipy.signal findpeaks
#1
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
#2
Documentation says the function may return unexpected results for data containing NaNs. Does your fh data contain NaNs?
#3
(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
#4
So uh, guys? Anybody willing to help?
#5
If you need additional info I can provide... there's almost 200 views but no actual contribution here :(
#6
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/referen...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
#7
how to edit the original post?

The code paste link is not working anymore
#8
you can post the code here, or if too long - make a repo (e.g. on github)
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

#9
(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?
#10
you can create new one, but please post a link in this thread too.
Or just post in this one.
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



Possibly Related Threads…
Thread Author Replies Views Last Post
  scipy.signal.find_peak VS Matlab's findpeaks function claw91 2 1,204 Sep-15-2020, 12:27 PM
Last Post: claw91

Forum Jump:

User Panel Messages

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