class sdr.MovingAverager(sdr.FIR)

Implements a moving average FIR filter.

Notes

A discrete-time moving average with length \(L\) is an FIR filter with impulse response

\[h[n] = \frac{1}{L}, \quad 0 \le n \le L - 1 .\]

Examples

Create an FIR moving average filter and an IIR leaky integrator filter.

In [1]: fir = sdr.MovingAverager(30)

In [2]: iir = sdr.LeakyIntegrator(1 - 2 / 30)

Compare the step responses.

In [3]: plt.figure(figsize=(8, 4)); \
   ...: sdr.plot.step_response(fir, N=100, label="Moving Averager"); \
   ...: sdr.plot.step_response(iir, N=100, label="Leaky Integrator");
   ...: 
../../_images/sdr_MovingAverager_1.png

Compare the magnitude responses.

In [4]: plt.figure(figsize=(8, 4)); \
   ...: sdr.plot.magnitude_response(fir, label="Moving Averager"); \
   ...: sdr.plot.magnitude_response(iir, label="Leaky Integrator"); \
   ...: plt.ylim(-35, 5);
   ...: 
../../_images/sdr_MovingAverager_2.png

Compare the output of the two filters to a Gaussian random process.

In [5]: x = np.random.randn(1_000) + 2.0; \
   ...: y_fir = fir(x); \
   ...: y_iir = iir(x)
   ...: 

In [6]: plt.figure(figsize=(8, 4)); \
   ...: sdr.plot.time_domain(y_fir, label="Moving Averager"); \
   ...: sdr.plot.time_domain(y_iir, label="Leaky Integrator");
   ...: 
../../_images/sdr_MovingAverager_3.png

Constructors

MovingAverager(length: int, streaming: bool = False)

Creates a moving average FIR filter.

Special methods

__call__(x: ArrayLike, ...) → NDArray

Filters the input signal \(x[n]\) with the FIR filter.

__len__() → int

Returns the filter length \(N + 1\).

Streaming mode only

reset()

Resets the filter state. Only useful when using streaming mode.

flush() → NDArray

Flushes the filter state by passing zeros through the filter. Only useful when using streaming mode.

property streaming : bool

Indicates whether the filter is in streaming mode.

property state : NDArray

The filter state consisting of the previous \(N\) inputs.

Methods

impulse_response(N: int | None = None) → NDArray

Returns the impulse response \(h[n]\) of the FIR filter. The impulse response \(h[n]\) is the filter output when the input is an impulse \(\delta[n]\).

step_response(N: int | None = None) → NDArray

Returns the step response \(s[n]\) of the FIR filter. The step response \(s[n]\) is the filter output when the input is a unit step \(u[n]\).

frequency_response(...) → tuple[numpy.ndarray[Any, numpy.dtype[numpy.float64]], numpy.ndarray[Any, numpy.dtype[numpy.complex128]]]
frequency_response(freqs: float, ...) → complex
frequency_response(freqs, ...) → ndarray[Any, dtype[complex128]]

Returns the frequency response \(H(\omega)\) of the FIR filter.

group_delay(...) → tuple[NDArray, NDArray]

Returns the group delay \(\tau_g(\omega)\) of the FIR filter.

phase_delay(...) → tuple[NDArray, NDArray]

Returns the phase delay \(\tau_{\phi}(\omega)\) of the FIR filter.

Properties

property taps : NDArray

The feedforward taps \(h[n]\) with length \(N + 1\).

property order : int

The order of the FIR filter \(N\).

property delay : int

The delay of the FIR filter \(d = \lfloor \frac{N + 1}{2} \rfloor\) in samples.