Skip to content

Interpolate waveforms to 30kHz for classifier compatibility#648

Open
abuzarmahmood wants to merge 2 commits into
katzlabbrandeis:masterfrom
abuzarmahmood:fix-issue-193-waveform-interpolation
Open

Interpolate waveforms to 30kHz for classifier compatibility#648
abuzarmahmood wants to merge 2 commits into
katzlabbrandeis:masterfrom
abuzarmahmood:fix-issue-193-waveform-interpolation

Conversation

@abuzarmahmood
Copy link
Copy Markdown
Member

Fixes #193

This PR adds waveform interpolation to enable the classifier to work with data recorded at different sampling rates, as long as the pre and post spike windows are the same.

Changes

  • Added interpolate_waveforms_to_30khz() function in utils/clustering.py: Interpolates waveforms from any sampling rate to match the expected 30kHz sampling rate using cubic interpolation
  • Added interpolate_waveforms() method to spike_handler class in utils/blech_process_utils.py: Wrapper method that uses the spike snapshot parameters and actual sampling rate from the data
  • Updated blech_process.py: Interpolates waveforms before passing them to the classifier

How it works

When data is recorded at a different sampling rate (e.g., 20kHz), the same time window (e.g., 0.5ms pre + 1.0ms post) will contain a different number of samples than at 30kHz. The interpolation function:

  1. Calculates the expected number of samples at both the actual and target sampling rates
  2. Creates time vectors for both sampling rates
  3. Uses cubic interpolation to resample each waveform to the target number of samples
  4. Returns waveforms with the same shape as if they were recorded at 30kHz

This allows the classifier, which was trained on 30kHz data, to work correctly with data from any sampling rate.

abuzarmahmood and others added 2 commits December 10, 2025 15:37
Addresses issue katzlabbrandeis#193 by adding waveform interpolation to match the
expected sampling rate of the classifier. This allows the classifier
to work with data recorded at different sampling rates as long as
the pre and post spike windows are the same.

Changes:
- Add interpolate_waveforms_to_30khz() function to clustering.py
- Add interpolate_waveforms() method to spike_handler class
- Update blech_process.py to interpolate waveforms before classification

Co-authored-by: Ona <no-reply@ona.com>
@abuzarmahmood
Copy link
Copy Markdown
Member Author

Solution Overview

This implementation solves the sampling rate compatibility issue by interpolating waveforms to a standard 30kHz sampling rate before classification.

Technical Details

The solution adds a new interpolation step in the processing pipeline:

  1. Waveform Extraction → Extract waveforms at native sampling rate (e.g., 20kHz, 25kHz, 30kHz)
  2. Dejittering → Align waveforms by their minima
  3. Interpolation (NEW) → Resample to 30kHz using cubic interpolation
  4. Classification → Pass standardized waveforms to classifier

Key Implementation Points

  • Cubic interpolation preserves waveform shape while resampling
  • Early return optimization when actual sampling rate equals target (no unnecessary computation)
  • Time-based interpolation ensures temporal accuracy regardless of sampling rate
  • Minimal code changes to existing pipeline - only adds one function call before classification

Example

For a 1.5ms window (0.5ms pre + 1.0ms post):

  • At 20kHz: 30 samples
  • At 25kHz: 37.5 → 37 samples
  • At 30kHz: 45 samples

After interpolation, all waveforms have 45 samples, matching the classifier's expectations.

Testing Recommendations

To verify this works correctly:

  1. Test with data at different sampling rates (20kHz, 25kHz, 30kHz)
  2. Verify classifier predictions are consistent across sampling rates for the same neural data
  3. Check that waveform shapes are preserved after interpolation

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.

Given pre and post spike-window, interpolate all extracted waveforms to have same number of datapoints / time as expected by 30Khz sampling rate

1 participant