FIR Filters & Convolution

Time-domain and FFT-based convolution, moving-average filters, and general FIR filtering.

pyminidsp.convolution_num_samples(signal_len, kernel_len)[source]

Compute the output length of a full linear convolution.

Compute the output length of a full linear convolution. For input length N and kernel length M, the result is N + M - 1.

Parameters:
  • signal_len (int)

  • kernel_len (int)

Return type:

int

pyminidsp.convolution_time(signal, kernel)[source]

Time-domain full linear convolution.

Returns:

numpy array of length signal_len + kernel_len - 1.

Parameters:
  • signal (ArrayLike)

  • kernel (ArrayLike)

Return type:

ndarray[tuple[Any, …], dtype[float64]]

Time-domain full linear convolution (direct sum-of-products).

\[\text{out}[n] = \sum_{k=0}^{M-1} \text{signal}[n-k] \cdot \text{kernel}[k]\]

with out-of-range signal samples treated as zero.

Parameters:
  • signal (ArrayLike) – Input signal.

  • kernel (ArrayLike) – FIR kernel.

Returns:

Array of length len(signal) + len(kernel) - 1.

Return type:

ndarray[tuple[Any, …], dtype[float64]]

pyminidsp.moving_average(signal, window_len)[source]

Causal moving-average FIR filter.

Returns:

numpy array of the same length as the input.

Parameters:
  • signal (ArrayLike)

  • window_len (int)

Return type:

ndarray[tuple[Any, …], dtype[float64]]

Causal moving-average FIR filter with zero-padded startup.

\[\text{out}[n] = \frac{1}{W} \sum_{k=0}^{W-1} \text{signal}[n-k]\]

where out-of-range samples (n - k < 0) are treated as zero.

Parameters:
  • signal (ArrayLike) – Input signal.

  • window_len (int) – Moving-average window length (must be > 0).

Returns:

Array of the same length as the input.

Return type:

ndarray[tuple[Any, …], dtype[float64]]

pyminidsp.fir_filter(signal, coeffs)[source]

Apply a causal FIR filter with arbitrary coefficients.

Returns:

numpy array of the same length as the input.

Parameters:
  • signal (ArrayLike)

  • coeffs (ArrayLike)

Return type:

ndarray[tuple[Any, …], dtype[float64]]

Apply a causal FIR filter with arbitrary coefficients.

\[\text{out}[n] = \sum_{k=0}^{T-1} \text{coeffs}[k] \cdot \text{signal}[n-k]\]

with out-of-range signal samples treated as zero.

Parameters:
  • signal (ArrayLike) – Input signal.

  • coeffs (ArrayLike) – FIR coefficients.

Returns:

Array of the same length as the input.

Return type:

ndarray[tuple[Any, …], dtype[float64]]

pyminidsp.design_lowpass_fir(num_taps, cutoff_freq, sample_rate, kaiser_beta=5.0)[source]

Design a Kaiser-windowed sinc lowpass FIR filter.

Parameters:
  • num_taps (int) – Number of filter coefficients (filter order + 1).

  • cutoff_freq (float) – Cutoff frequency in Hz.

  • sample_rate (float) – Sampling rate in Hz.

  • kaiser_beta (float) – Kaiser window shape parameter (default 5.0).

Returns:

numpy array of length num_taps containing the filter coefficients.

Return type:

ndarray[tuple[Any, …], dtype[float64]]

Design a Kaiser-windowed sinc lowpass FIR filter.

Returns a set of FIR filter coefficients that can be passed directly to fir_filter(). The Kaiser window’s beta parameter controls the trade-off between stopband attenuation and transition width.

Parameters:
  • num_taps (int) – Number of filter coefficients (filter order + 1).

  • cutoff_freq (float) – Cutoff frequency in Hz.

  • sample_rate (float) – Sampling rate in Hz.

  • kaiser_beta (float) – Kaiser window shape parameter (default 5.0).

Returns:

Array of length num_taps.

Return type:

ndarray[tuple[Any, …], dtype[float64]]

coeffs = md.design_lowpass_fir(65, cutoff_freq=4000.0,
                                sample_rate=44100.0, kaiser_beta=8.0)
filtered = md.fir_filter(signal, coeffs)
pyminidsp.convolution_fft_ola(signal, kernel)[source]

Full linear convolution using FFT overlap-add.

Returns:

numpy array of length signal_len + kernel_len - 1.

Parameters:
  • signal (ArrayLike)

  • kernel (ArrayLike)

Return type:

ndarray[tuple[Any, …], dtype[float64]]

Full linear convolution using FFT overlap-add. Produces the same output as convolution_time() but is faster for longer kernels by processing blocks in the frequency domain.

Parameters:
  • signal (ArrayLike) – Input signal.

  • kernel (ArrayLike) – FIR kernel.

Returns:

Array of length len(signal) + len(kernel) - 1.

Return type:

ndarray[tuple[Any, …], dtype[float64]]