Modified Hough Spiker Algorithm

modified_hough_spiker(signal: ndarray, window_length: int, cutoff: float | ndarray, threshold: float | int | list[float, int] | ndarray, width: int | None = None, window_type: Literal['barthann', 'brthan', 'bth', 'bartlett', 'bart', 'brt', 'blackman', 'black', 'blk', 'blackmanharris', 'blackharr', 'bkh', 'bohman', 'bman', 'bmn', 'boxcar', 'box', 'ones', 'rect', 'rectangular', 'cosine', 'halfcosine', 'exponential', 'poisson', 'flattop', 'flat', 'flt', 'hamming', 'hamm', 'ham', 'hann', 'han', 'lanczos', 'sinc', 'nuttall', 'nutl', 'nut', 'parzen', 'parz', 'par', 'taylor', 'taylorwin', 'triangle', 'triang', 'tri', 'tukey', 'tuk'] = 'hann', pass_zero: bool | str = True, scale: bool = True, fs: float | None = None) ndarray[source]

Perform Modified Hough Spiker Algorithm (MHSA) encoding on the input signal.

The Modified Hough Spiker Algorithm (MHSA) is basically an improved version of the original Hough Spiker Algorithm (HSA). The original HSA is very strict: it only allows a spike to be created if the current piece of the signal is exactly as big as or bigger than the filter pattern at every single point in that window. If even one tiny spot dips below the filter, no spike is detected.

MHSA relaxes this rule a little bit to make it more practical and flexible. Instead of demanding perfection everywhere, it allows a small amount of “shortfall” — places where the signal is slightly below the filter. It measures how much the signal falls short in those spots (by adding up the differences where the filter is higher than the signal), and if that total shortfall is small enough (below a chosen limit called the threshold), it still decides to create a spike there. Then, just like in the original, it subtracts the filter pattern from the signal to remove the detected spike pattern so the algorithm can keep looking for the next one.

Note

  • MHSA requires non-negative inputs; automatic shifting and normalization to [0, 1] is applied per feature.

  • The FIR filter is designed using scipy.signal.firwin with the specified cutoff, window type, etc.

  • For multi-feature signals, the same filter shape is applied across all features, but scaling is performed independently per feature based on its amplitude.

Refer to the Modified Hough Spiker (MHSA) Encoding for a detailed explanation of the Modified Hough Spiker Algorithm.

Code Example:

import numpy as np
from spikify.encoders.temporal.deconvolution import modified_hough_spiker
signal = np.array([0.1, 0.2, 0.3, 1.0, 0.5, 0.3, 0.1])
window_length = 3
threshold = 0.5
cutoff = 0.1
encoded_signal, fir_coeffs, shift, norm, = modified_hough_spiker(signal, window_length, cutoff, threshold)
Parameters:
  • signal (numpy.ndarray) – Input signal to encode (1D or 2D: time × features or channels).

  • window_length (int) – Length of the FIR filter (number of coefficients).

  • cutoff (float | numpy.ndarray) – Cutoff frequency(ies) for the FIR filter design (normalized 0 to 1, where 1 = Nyquist). Scalar or an array of cutoff frequencies (that is, band edges).

  • threshold (float | int | list[float | int] | numpy.ndarray) – Threshold factor for spike detection. Scalar or per-feature sequence.

  • width (int | None) – Transition width for FIR filter design (optional, used with certain window types).

  • window_type (str) – Window function for FIR filter design (e.g., ‘hann’, ‘hamming’, ‘blackman’, ‘boxcar’).

  • pass_zero (bool | str) – Whether the filter should be low-pass (True) or high-pass (False/’highpass’).

  • scale (bool) – Set to True to scale the coefficients so that the frequency response is exactly unity at a certain frequency.

  • fs (float | None) – Sampling frequency (used for physical frequency units in cutoff; optional).

Returns:

  • spikes: A numpy array representing the encoded spike train. (values in {0, +1})

  • fir_bank: Final filter coefficients used, shape (window_length, features or channels).

  • shift: Per-feature shift values subtracted to make signal non-negative, shape (features or channels,).

  • norm: Per-feature normalization values used to scale signal to [0, 1], shape (features or channels,).

Return type:

tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, numpy.ndarray]

Raises:

ValueError – If the input signal is empty or if the threshold dimensions do not match the signal features or if the window_length is greater than the signal lenght.