Skip to main content

Neuralyzer::Transforms::V2::MultichannelTemplateRealigner

Neuralyzer::Transforms::V2::MultichannelTemplateRealigner

Modular numerical engine for multichannel template calculation and alignment.

#include <MultichannelTemplateRealigner.hpp>

Public Functions

Name
MultichannelTemplate computeTemplate(arma::fmat const & voltage_matrix, std::span< int64_t const > event_sample_indices, std::span< int const > selected_channels, int w_pre, int w_post)
Compute multichannel cluster template by averaging aligned waveforms.
float computeCrossCorrelation(arma::fmat const & voltage_matrix, int64_t event_sample_index, int shift_offset, std::span< int const > selected_channels, MultichannelTemplate const & tmpl)
Compute multichannel template cross-correlation / goodness-of-fit.
int findOptimalDiscreteShift(arma::fmat const & voltage_matrix, int64_t event_sample_index, std::span< int const > selected_channels, MultichannelTemplate const & tmpl, int search_window)
Find optimal integer shift that maximizes multichannel goodness-of-fit against template.
double findOptimalSubSampleShift(arma::fmat const & voltage_matrix, int64_t event_sample_index, std::span< int const > selected_channels, MultichannelTemplate const & tmpl, int search_window, int sub_divisions =8, int kernel_half_width =8)
Find optimal continuous sub-sample shift using sinc interpolation.
std::vector< int64_t > realignEventsDiscrete(arma::fmat const & voltage_matrix, std::span< int64_t const > initial_event_sample_indices, std::span< int const > selected_channels, TemplateRealignerParams const & params)
Iteratively realign event timestamps to convergence using discrete shifts.
std::vector< double > computeSubSampleShifts(arma::fmat const & voltage_matrix, std::span< int64_t const > event_sample_indices, std::span< int const > selected_channels, TemplateRealignerParams const & params)
Compute continuous sub-sample alignment offsets for a set of events against their template.

Public Functions Documentation

function computeTemplate

static MultichannelTemplate computeTemplate(
    arma::fmat const & voltage_matrix,
    std::span< int64_t const > event_sample_indices,
    std::span< int const > selected_channels,
    int w_pre,
    int w_post
)

Compute multichannel cluster template by averaging aligned waveforms.

Parameters:

  • voltage_matrix Continuous multichannel voltage matrix (samples x channels)
  • event_sample_indices Discrete sample indices of events in this cluster
  • selected_channels Channel indices included in template
  • w_pre Pre-peak sample window
  • w_post Post-peak sample window

Return: MultichannelTemplate matrix

Precondition:

  • !voltage_matrix.is_empty()
  • !event_sample_indices.empty()
  • !selected_channels.empty()

function computeCrossCorrelation

static float computeCrossCorrelation(
    arma::fmat const & voltage_matrix,
    int64_t event_sample_index,
    int shift_offset,
    std::span< int const > selected_channels,
    MultichannelTemplate const & tmpl
)

Compute multichannel template cross-correlation / goodness-of-fit.

Parameters:

  • voltage_matrix Continuous voltage matrix
  • event_sample_index Center sample index
  • shift_offset Shift offset in samples
  • selected_channels Channel indices
  • tmpl Multichannel template

Return: Inner product sum V * T

function findOptimalDiscreteShift

static int findOptimalDiscreteShift(
    arma::fmat const & voltage_matrix,
    int64_t event_sample_index,
    std::span< int const > selected_channels,
    MultichannelTemplate const & tmpl,
    int search_window
)

Find optimal integer shift that maximizes multichannel goodness-of-fit against template.

Parameters:

  • voltage_matrix Continuous voltage matrix
  • event_sample_index Center sample index
  • selected_channels Channel indices
  • tmpl Multichannel template
  • search_window Max search shift (+/- samples)

Return: Optimal integer shift in [-search_window, +search_window]

function findOptimalSubSampleShift

static double findOptimalSubSampleShift(
    arma::fmat const & voltage_matrix,
    int64_t event_sample_index,
    std::span< int const > selected_channels,
    MultichannelTemplate const & tmpl,
    int search_window,
    int sub_divisions =8,
    int kernel_half_width =8
)

Find optimal continuous sub-sample shift using sinc interpolation.

Parameters:

  • voltage_matrix Continuous voltage matrix
  • event_sample_index Center sample index
  • selected_channels Channel indices
  • tmpl Multichannel template
  • search_window Max search shift (+/- samples)
  • sub_divisions Sub-sample steps per sample (e.g. 8 or 16)
  • kernel_half_width Sinc kernel half width

Return: Continuous optimal shift in samples

function realignEventsDiscrete

static std::vector< int64_t > realignEventsDiscrete(
    arma::fmat const & voltage_matrix,
    std::span< int64_t const > initial_event_sample_indices,
    std::span< int const > selected_channels,
    TemplateRealignerParams const & params
)

Iteratively realign event timestamps to convergence using discrete shifts.

Parameters:

  • voltage_matrix Continuous voltage matrix
  • initial_event_sample_indices Initial detected sample indices
  • selected_channels Channel indices
  • params Realignment parameters

Return: Vector of realigned discrete sample indices

Runs EM iterations: Template update -> Event shift -> Template update until shifts == 0.

function computeSubSampleShifts

static std::vector< double > computeSubSampleShifts(
    arma::fmat const & voltage_matrix,
    std::span< int64_t const > event_sample_indices,
    std::span< int const > selected_channels,
    TemplateRealignerParams const & params
)

Compute continuous sub-sample alignment offsets for a set of events against their template.

Parameters:

  • voltage_matrix Continuous voltage matrix
  • event_sample_indices Discrete event sample indices
  • selected_channels Channel indices
  • params Realignment parameters

Return: Vector of continuous sub-sample shifts (one per event)


Updated on 2026-11-13 at 20:25:41 +0000