Skip to content

Feature/amplitude#46

Closed
possibly-human wants to merge 6 commits intomasterfrom
feature/Amplitude
Closed

Feature/amplitude#46
possibly-human wants to merge 6 commits intomasterfrom
feature/Amplitude

Conversation

@possibly-human
Copy link
Copy Markdown
Contributor

Summary

Computes peak-to-peak amplitude in raw units using external peak events.
The module takes a raw signal plus two boolean gates (peak-min and peak-max, typically from a PeakDetector object) and measures amplitude as:

peak-to-peak = raw_peak_max - raw_peak_min

You can choose whether to emit the amplitude on each detected max or each detected min, and optionally enforce “strict” cycles that require a full min→max→min (or max→min→max) sequence before a new amplitude is produced.

Functionality

Inputs are:

  • Raw signal (float) — original, non-normalized signal whose peak-to-peak amplitude is measured.
  • Peak min gate (bool) — goes high when a local minimum is detected (e.g. PeakDetector MIN output).
  • Peak max gate (bool) — goes high when a local maximum is detected (e.g. PeakDetector MAX output).

Parameters are:

  • Mode (enum: OnMax / OnMin)
    • OnMax — emit amplitude when a max event occurs (using the most recent min). Conceptually: min → max → (emit).
    • OnMin — emit amplitude when a min event occurs (using the most recent max). Conceptually: max → min → (emit).
  • Strict cycles (bool) — when enabled, enforces full cycles:
    • OnMax: requires a new min before the next amplitude can be emitted on a max.
    • OnMin: requires a new max before the next amplitude can be emitted on a min.

Outputs are:

  • Peak-to-peak amplitude (float) — latest peak-to-peak amplitude in raw units, computed as
    last_peak_max_raw - last_peak_min_raw.
    The value is updated only at the chosen peak event (according to Mode) and otherwise held constant between events.

Implementation notes

  • Designed to work in tandem with the separate PeakDetector object, which supplies the peak_min_gate and peak_max_gate signals.

@jcelerier jcelerier self-requested a review December 9, 2025 02:41
{
halp::data_port<
"Peak-to-peak amplitude",
"Peak-to--peak amplitude in raw units, computed as (peak_max_raw - peak_min_raw).\n"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Peak-to-peak has an extra -

### Summary
Adds a peak-detection and amplitude-analysis utility for real-time signals. The object uses a normalized `Detection signal` to track four event types — rising trigger crossing (`PEAK_RISING`), falling trigger crossing (`PEAK_FALLING`), local maxima (`PEAK_MAX`), and local minima (`PEAK_MIN`) — based on configurable trigger, reload, and fallback parameters. Amplitude is measured from a separate `Raw signal`, with two available modes: `PeakLocked` or `CycleExtrema`.

Adapted from Sofian Audry’s [Plaquette implementation](https://plaquette.org/PeakDetector.html), with additional raw-amplitude tracking.

### Functionality
Inputs are:
- Detection signal (`float`, ideally normalized and scaled 0 to 1)
- Raw signal (`float`, original signal used for amplitude measurement)

Parameters are:
- Trigger threshold (`float` between 0 and 1. Level used for rising and falling trigger crossings)
- Reload threshold (`float` between 0 and 1. Level used to reset / re-arm the detector)
- Fallback tolerance (`float` between 0 and 1. Drop percentage between trigger and apex required to confirm maxima and minima)
- Amplitude mode (`enum`) — `PeakLocked` or `CycleExtrema`
- P2P update (`enum`) — updates peak-to-peak on max, min, or both

Outputs are:
- Peak max (`boolean`. True when a local maximum is confirmed on the detection signal)
- Peak min (`boolean`. True when a local minimum is confirmed on the detection signal)
- Peak rising (`boolean`. True when the detection signal crosses upward through the trigger threshold)
- Peak falling (`boolean`. True when the detection signal crosses downward through the trigger threshold)
- Peak-to-peak (`float`. Held raw peak-to-peak amplitude)
- Max above threshold (`float`. Held raw amplitude above the rising trigger baseline)
- Min below threshold (`float`. Held raw amplitude below the falling trigger baseline)

Modes explained

PeakLocked:
- Uses raw values paired with confirmed detect peaks
- Suitable when amplitude should follow the peaks confirmed by the detection signal

CycleExtrema:
- Uses raw extrema over two half-cycles: rising to falling for the upper excursion, and falling to next rising for the lower excursion
- Suitable when amplitude should follow the full raw waveform shape between trigger crossings

### Implementation notes
Uses a custom `PeakDetector` class stored in `3rdparty/extras` (to be reused in Respiration and EDA processes).

Raw baselines are interpolated at trigger crossings so amplitude is measured relative to the estimated raw value at the threshold crossing, not only at the nearest sample.
@possibly-human
Copy link
Copy Markdown
Contributor Author

Superseded by updated amplitude implementation with PeakLocked / CycleExtrema modes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants