Time To First Spike Algorithm

time_to_first_spike(signal: ndarray, interval_length: int) ndarray[source]

Perform Time To First Spike (TTFS) encoding on the input signal.

This function implements a sparse temporal coding scheme where input intensity is encoded in the timing of the first spike within a fixed time window (interval_length). Stronger inputs produce earlier spikes.

The signal is divided into non-overlapping blocks of length interval_length. For each block, the mean intensity is computed and mapped to a spike latency using a logarithmic function (approximating an exponential decaying threshold). A single spike is placed at the corresponding time step within the block.

Note

  • TTFS requires normalized input signals between 0 and 1. If the input signal contains negative values, they are shifted to be non-negative and then normalized.

Refer to the Time-to-First-Spike (TTFS) for a detailed explanation of the Time-to-First-Spike Encoding Algorithm.

Code Example:

import numpy as np
from spikify.encoders.temporal.global_referenced import time_to_first_spike
signal = np.array([0.1, 0.2, 0.3, 1.0, 0.5, 0.3, 0.1, 0.2])
interval_length = 4
encoded_signal = time_to_first_spike(signal, interval_length)
Parameters:
  • signal (numpy.ndarray) – Input signal to encode (1D or 2D: timestamps × features).

  • interval_length (int) – Length of each time window (block) for latency mapping. Must evenly divide the signal length. Larger values give coarser temporal resolution but allow longer latency range.

Returns:

A numpy array representing the encoded spike train.

Return type:

numpy.ndarray

Raises:

ValueError – If signal is empty or interval_length does not divide signal length.