Skip to content

Commit

Permalink
Make sure peak detection works with negative signals
Browse files Browse the repository at this point in the history
  • Loading branch information
smoia committed Oct 9, 2024
1 parent 75fa443 commit a2b7e8c
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions peakdet/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,23 @@ def peakfind_physio(data, *, thresh=0.2, dist=None):
data = utils.check_physio(data, ensure_fs=ensure_fs, copy=True)
# first pass peak detection to get approximate distance between peaks
cdist = data.fs // 4 if dist is None else dist
thresh = np.squeeze(np.diff(np.percentile(data, [5, 95]))) * thresh
locs, heights = signal.find_peaks(data[:], distance=cdist, height=thresh)

# check if data is negative, if so make it all positive and continue with signal
phys_signal = data.data - data.data.min() if data.data.min() < 0 else data.data
logger.debug(
f"Negative signal detected (min = {data.data.min()}), workgin with positive signal for peak detection."
)

thresh = np.squeeze(np.diff(np.percentile(phys_signal, [5, 95]))) * thresh
locs, heights = signal.find_peaks(phys_signal, distance=cdist, height=thresh)
logger.debug(
f"First peak detection iteration. Acquiring approximate distance between peaks (Number of peaks: {len(locs)})"
)

# second, more thorough peak detection
cdist = np.diff(locs).mean() // 2
heights = np.percentile(heights["peak_heights"], 1)
locs, heights = signal.find_peaks(data[:], distance=cdist, height=heights)
locs, heights = signal.find_peaks(phys_signal, distance=cdist, height=heights)
data._metadata["peaks"] = locs
logger.debug(
f"Second peak detection iteration. Acquiring more precise peak locations (Number of peaks: {len(locs)})"
Expand Down

0 comments on commit a2b7e8c

Please sign in to comment.