miniDSP
A small C library for audio DSP
Loading...
Searching...
No Matches
minidsp_fir.c File Reference

FIR filtering and convolution: direct time-domain methods and FFT overlap-add fast convolution. More...

#include "minidsp.h"
#include "minidsp_internal.h"

Go to the source code of this file.

Functions

static unsigned next_pow2 (unsigned n)
 Return the next power-of-two >= n (n must be > 0).
unsigned MD_convolution_num_samples (unsigned signal_len, unsigned kernel_len)
 Compute the output length of a full linear convolution.
void MD_convolution_time (const double *signal, unsigned signal_len, const double *kernel, unsigned kernel_len, double *out)
 Time-domain full linear convolution (direct sum-of-products).
void MD_moving_average (const double *signal, unsigned signal_len, unsigned window_len, double *out)
 Causal moving-average FIR filter with zero-padded startup.
void MD_fir_filter (const double *signal, unsigned signal_len, const double *coeffs, unsigned num_taps, double *out)
 Apply a causal FIR filter with arbitrary coefficients.
void MD_design_lowpass_fir (double *coeffs, unsigned num_taps, double cutoff_freq, double sample_rate, double kaiser_beta)
 Design a Kaiser-windowed sinc lowpass FIR filter.
void MD_convolution_fft_ola (const double *signal, unsigned signal_len, const double *kernel, unsigned kernel_len, double *out)
 Full linear convolution using FFT overlap-add (offline).

Detailed Description

FIR filtering and convolution: direct time-domain methods and FFT overlap-add fast convolution.

Definition in file minidsp_fir.c.

Function Documentation

◆ MD_convolution_fft_ola()

void MD_convolution_fft_ola ( const double * signal,
unsigned signal_len,
const double * kernel,
unsigned kernel_len,
double * out )

Full linear convolution using FFT overlap-add (offline).

Produces the same output as MD_convolution_time() but is faster for longer kernels by processing blocks in the frequency domain.

Parameters
signalInput signal of length signal_len.
signal_lenNumber of input samples (must be > 0).
kernelFIR kernel of length kernel_len.
kernel_lenNumber of FIR taps (must be > 0).
outOutput buffer of length signal_len + kernel_len - 1.

Definition at line 124 of file minidsp_fir.c.

◆ MD_convolution_num_samples()

unsigned MD_convolution_num_samples ( unsigned signal_len,
unsigned kernel_len )

Compute the output length of a full linear convolution.

For input length N and kernel length M, full convolution length is N+M-1.

Definition at line 18 of file minidsp_fir.c.

◆ MD_convolution_time()

void MD_convolution_time ( const double * signal,
unsigned signal_len,
const double * kernel,
unsigned kernel_len,
double * out )

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

Computes: out[n] = sum_{k=0}^{kernel_len-1} signal[n-k] * kernel[k] with out-of-range signal samples treated as zero.

Parameters
signalInput signal of length signal_len.
signal_lenNumber of input samples (must be > 0).
kernelFIR kernel of length kernel_len.
kernel_lenNumber of FIR taps (must be > 0).
outOutput buffer of length signal_len + kernel_len - 1.

Definition at line 25 of file minidsp_fir.c.

◆ MD_design_lowpass_fir()

void MD_design_lowpass_fir ( double * coeffs,
unsigned num_taps,
double cutoff_freq,
double sample_rate,
double kaiser_beta )

Design a Kaiser-windowed sinc lowpass FIR filter.

Generates a linear-phase lowpass filter with the specified cutoff frequency and Kaiser window shape. The coefficients are normalized to unity DC gain (they sum to 1.0).

\[ h[i] = 2\,f_c\;\mathrm{sinc}\!\bigl(2\,f_c\,(i - (N-1)/2)\bigr) \;\cdot\; w_{\mathrm{Kaiser}}[i] \]

where \(f_c = \mathrm{cutoff\_freq} / \mathrm{sample\_rate}\).

Parameters
coeffsOutput buffer of length num_taps (caller-allocated).
num_tapsFilter length (must be > 0).
cutoff_freq-6 dB cutoff frequency in Hz (must be > 0 and < sample_rate / 2).
sample_rateSampling rate in Hz (must be > 0).
kaiser_betaKaiser window \(\beta\) parameter (e.g. 10.0).
double h[65];
MD_design_lowpass_fir(h, 65, 4000.0, 48000.0, 10.0); // 4 kHz LPF
void MD_design_lowpass_fir(double *coeffs, unsigned num_taps, double cutoff_freq, double sample_rate, double kaiser_beta)
Design a Kaiser-windowed sinc lowpass FIR filter.
Definition minidsp_fir.c:88

Definition at line 88 of file minidsp_fir.c.

◆ MD_fir_filter()

void MD_fir_filter ( const double * signal,
unsigned signal_len,
const double * coeffs,
unsigned num_taps,
double * out )

Apply a causal FIR filter with arbitrary coefficients.

Computes: out[n] = sum_{k=0}^{num_taps-1} coeffs[k] * signal[n-k] with out-of-range signal samples treated as zero.

Parameters
signalInput signal of length signal_len.
signal_lenNumber of input samples (must be > 0).
coeffsFIR coefficients of length num_taps.
num_tapsNumber of FIR taps (must be > 0).
outOutput buffer of length signal_len.

Definition at line 68 of file minidsp_fir.c.

◆ MD_moving_average()

void MD_moving_average ( const double * signal,
unsigned signal_len,
unsigned window_len,
double * out )

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

Computes: out[n] = (1/window_len) * sum_{k=0}^{window_len-1} signal[n-k] where out-of-range samples (n-k < 0) are treated as zero.

Parameters
signalInput signal of length signal_len.
signal_lenNumber of input samples (must be > 0).
window_lenMoving-average window length (must be > 0).
outOutput buffer of length signal_len.

Definition at line 48 of file minidsp_fir.c.

◆ next_pow2()

unsigned next_pow2 ( unsigned n)
static

Return the next power-of-two >= n (n must be > 0).

Definition at line 11 of file minidsp_fir.c.