🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEscipy

scipy Documentation

LOADING ENGINE...

signal.lfilter()

AI & DATA SCIENCE // signal-lfilter

scipy.signal.lfilter() applies a digital linear filter to a signal, defined by its numerator and denominator polynomial coefficients, implementing operations like smoothing, differencing, or more general IIR/FIR filtering.

Syntax

scipy.signal.lfilter(b, a, x)

Deep Dive Course

lfilter() implements a general linear filter defined by two coefficient arrays: b, the numerator, feedforward coefficients, and a, the denominator, feedback coefficients — setting a to just [1] produces a simple FIR, finite impulse response, filter that only depends on current and past input values, while a genuinely multi-element a produces an IIR, infinite impulse response, filter, which also feeds back its own past output values, letting a short set of coefficients implement effects that would otherwise require a very long FIR filter. It's the general building block underlying many specific filtering operations, like simple moving averages, exponential smoothing, and more sophisticated frequency-selective filters designed with functions like signal.butter().

1Understanding signal.lfilter()

lfilter() implements a general linear filter defined by two coefficient arrays: b, the numerator, feedforward coefficients, and a, the denominator, feedback coefficients — setting a to just [1] produces a simple FIR, finite impulse response, filter that only depends on current and past input values, while a genuinely multi-element a produces an IIR, infinite impulse response, filter, which also feeds back its own past output values, letting a short set of coefficients implement effects that would otherwise require a very long FIR filter. It's the general building block underlying many specific filtering operations, like simple moving averages, exponential smoothing, and more sophisticated frequency-selective filters designed with functions like signal.butter().

💡

A simple moving average filter can be implemented directly with lfilter() by setting b to an array of equal weights that sum to 1, and a to just [1] — this is a quick way to smooth noisy data without needing a specialized smoothing function.

editor.html
from scipy import signal
import numpy as np

x = np.array([1, 2, 3, 4, 5], dtype=float)
b = [1/3, 1/3, 1/3]
a = [1]
filtered = signal.lfilter(b, a, x)
print(np.round(filtered, 2))
localhost:3000

2Practical Example

Here is a real-world application of signal.lfilter() showing how it is used in production SciPy code.

editor.html
from scipy import signal
import numpy as np

x = np.array([1, 1, 1, 1, 1], dtype=float)
b = [1]
a = [1, -0.5]
filtered = signal.lfilter(b, a, x)
print(np.round(filtered, 3))
localhost:3000

3Best Practices

Follow these guidelines when working with signal.lfilter():

1. Use lfilter() directly for straightforward FIR filters, simple moving averages, basic differencing, by setting a to [1] and choosing b's coefficients appropriately

2. Design more sophisticated frequency-selective filters, low-pass, high-pass, etc., with a dedicated function like signal.butter(), then apply the resulting b/a coefficients with lfilter(), rather than hand-deriving filter coefficients yourself

3. Use signal.filtfilt() instead of lfilter() when you need zero-phase filtering, no time delay/shift introduced by the filter, since lfilter() alone introduces a phase shift

⚠️

Tip: A simple moving average filter can be implemented directly with lfilter() by setting b to an array of equal weights that sum to 1, and a to just [1] — this is a quick way to smooth noisy data without needing a specialized smoothing function.

editor.html
from scipy import signal
import numpy as np

x = np.array([1, 2, 3, 4, 5], dtype=float)
b = [1/3, 1/3, 1/3]
a = [1]
filtered = signal.lfilter(b, a, x)
print(np.round(filtered, 2))
localhost:3000

Examples

Example 01Basic Usage
from scipy import signal
import numpy as np

x = np.array([1, 2, 3, 4, 5], dtype=float)
b = [1/3, 1/3, 1/3]
a = [1]
filtered = signal.lfilter(b, a, x)
print(np.round(filtered, 2))
Example 02Advanced Example
from scipy import signal
import numpy as np

x = np.array([1, 1, 1, 1, 1], dtype=float)
b = [1]
a = [1, -0.5]
filtered = signal.lfilter(b, a, x)
print(np.round(filtered, 3))

Best Practices

  • Use lfilter() directly for straightforward FIR filters, simple moving averages, basic differencing, by setting a to [1] and choosing b's coefficients appropriately
  • Design more sophisticated frequency-selective filters, low-pass, high-pass, etc., with a dedicated function like signal.butter(), then apply the resulting b/a coefficients with lfilter(), rather than hand-deriving filter coefficients yourself
  • Use signal.filtfilt() instead of lfilter() when you need zero-phase filtering, no time delay/shift introduced by the filter, since lfilter() alone introduces a phase shift

Interview Question

What's the difference between an FIR filter and an IIR filter, in terms of how lfilter()'s a and b coefficients are set up?

Hint: Think about what happens to a's coefficients specifically for each type.

An FIR, finite impulse response, filter sets a to just [1], meaning the output at any point depends only on a weighted combination of current and past input values, with no feedback of the filter's own past outputs at all — a single impulse input eventually produces zero output again as it moves past the finite set of b coefficients. An IIR, infinite impulse response, filter instead uses a genuinely multi-element a array, which feeds a weighted combination of the filter's own past output values back into the calculation of each new output, which in principle can keep influencing the output indefinitely, letting IIR filters achieve certain frequency-selective effects with far fewer coefficients than an equivalent FIR filter would need, at the cost of being potentially unstable if not designed carefully.

Exercises

MediumPractice using signal.lfilter() in a real scenario.
View Solution
from scipy import signal
import numpy as np

x = np.array([1, 2, 3, 4, 5], dtype=float)
b = [1/3, 1/3, 1/3]
a = [1]
filtered = signal.lfilter(b, a, x)
print(np.round(filtered, 2))

Frequently Asked Questions

What's the difference between an FIR filter and an IIR filter, in terms of how lfilter()'s a and b coefficients are set up?

An FIR, finite impulse response, filter sets a to just [1], meaning the output at any point depends only on a weighted combination of current and past input values, with no feedback of the filter's own past outputs at all — a single impulse input eventually produces zero output again as it moves past the finite set of b coefficients. An IIR, infinite impulse response, filter instead uses a genuinely multi-element a array, which feeds a weighted combination of the filter's own past output values back into the calculation of each new output, which in principle can keep influencing the output indefinitely, letting IIR filters achieve certain frequency-selective effects with far fewer coefficients than an equivalent FIR filter would need, at the cost of being potentially unstable if not designed carefully.

Related Functions

signal-convolvesignal-detrendoptimize-root