📜  scipy.signal lfilter - Python (1)

📅  最后修改于: 2023-12-03 14:47:18.650000             🧑  作者: Mango

scipy.signal lfilter - Python

The scipy.signal.lfilter function in Python is used for linear filtering of one-dimensional arrays using a digital filter. It performs the convolution of an input signal with a filter, which can be FIR (Finite Impulse Response) or IIR (Infinite Impulse Response).

Syntax
scipy.signal.lfilter(b, a, x, axis=-1, zi=None)

The lfilter function takes the following parameters:

  • b: Coefficients of the numerator filter polynomial (b[0], b[1], ..., b[M]).
  • a: Coefficients of the denominator filter polynomial (a[0], a[1], ..., a[N]).
  • x: Input signal to be filtered.
  • axis: Axis along which the filter is applied.
  • zi: Initial conditions for the filter delays.
Returns

The function returns the filtered array with the same shape as the input signal x.

Example
import numpy as np
from scipy import signal

# Create a 1D input signal
x = np.array([1, 2, 3, 4, 5])

# Define the filter coefficients
b = np.array([1, 2, 1])
a = np.array([1, -1/2])

# Apply the filter
y = signal.lfilter(b, a, x)

print(y)  # Output: [1.   2.   3.25 4.5  3.25]

In the above example, we create a 1D input signal x. We also define the filter coefficients, b and a, which represent a second-order IIR filter. We then apply the lfilter function to filter the input signal x. The filtered output y is printed, which gives the filtered version of the input signal.

The lfilter function is useful in various signal processing applications, such as noise removal, smoothing, and frequency filtering. It allows us to design and apply both FIR and IIR filters to digital signals efficiently.