paulvangentcom / heartrate_analysis_python

Python Heart Rate Analysis Package, for both PPG and ECG signals
MIT License
947 stars 321 forks source link

peak detection edge window is off by one resulting in wrong peaks identified #88

Open christianschneebeli opened 2 years ago

christianschneebeli commented 2 years ago

In detect_peaks() from peakdetection.py the edges are calculated as

    peakedges = np.concatenate((np.array([0]),
                             (np.where(np.diff(peaksx) > 1)[0]),
                             np.array([len(peaksx)])))

As a result, each edge (except the first) start one index too early and finish one index to early. cf: peaksy[peakedges[i]:peakedges[i+1]].tolist() Thus if for example the last y_value from the first edge is larger than all the y values in the second edge, it is detected as the peak from the second edge. This would be the correct indices to slice the edges.

    peakedges = np.concatenate((np.array([0]),
                                (np.where(np.diff(peaksx) > 1)[0]+1),
                                np.array([len(peaksx)+1])))