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


Messages In This Thread
scipy.signal findpeaks - by claw91 - Aug-28-2020, 09:28 AM
RE: scipy.signal findpeaks - by deanhystad - Aug-28-2020, 09:59 AM
RE: scipy.signal findpeaks - by claw91 - Aug-28-2020, 10:06 AM
RE: scipy.signal findpeaks - by claw91 - Aug-31-2020, 10:35 AM
RE: scipy.signal findpeaks - by claw91 - Sep-03-2020, 12:13 PM
RE: scipy.signal findpeaks - by claw91 - Sep-10-2020, 07:41 AM
RE: scipy.signal findpeaks - by claw91 - Sep-11-2020, 10:19 AM
RE: scipy.signal findpeaks - by buran - Sep-11-2020, 10:21 AM
RE: scipy.signal findpeaks - by claw91 - Sep-15-2020, 09:53 AM
RE: scipy.signal findpeaks - by buran - Sep-15-2020, 10:28 AM
RE: scipy.signal findpeaks - by buran - Sep-15-2020, 10:48 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  scipy.signal.find_peak VS Matlab's findpeaks function claw91 2 5,472 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