在 Scipy 中使用低通数字巴特沃斯滤波器去除噪声 – Python
在本文中,任务是使用低通数字巴特沃斯滤波器编写一个用于噪声去除的Python程序。
什么是噪音?
噪声基本上是电子信号中不需要的部分。它通常是由于设计错误、连接松动、开关故障等造成的。
如果我们的信号中有噪声怎么办?
为了去除不需要的信号/噪声,我们使用不同类型和规格的滤波器。通常,在行业中,我们需要通过用信号对其进行测试来选择最适合的滤波器,以确定在给定用例中用于去除噪声的最佳滤波器。
我们现在要做什么?
我们现在将实施低通数字巴特沃斯滤波器,以去除正弦波组合的不需要的信号/噪声。
过滤器规格:
- 由 25 Hz 和 50 Hz 组成的信号
- 采样频率 1kHz。
- 在 35Hz 下订购 N=10 以去除 50Hz 音调。
循序渐进:
第 1 步:导入库
Python3
# import required library
import numpy as np
import scipy.signal as signal
import matplotlib.pyplot as plt
Python3
# Specifications of the filter
f1 = 25 # Frequency of 1st signal
f2 = 50 # Frequency of 2nd signal
N = 10 # Order of the filter
# Generate the time vector of 1 sec duration
t = np.linspace(0, 1, 1000) # Generate 1000 samples in 1 sec
# Generate the signal containing f1 and f2
sig = np.sin(2*np.pi*f1*t) + np.sin(2*np.pi*f2*t)
Python3
# Display the signal
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
ax1.plot(t, sig)
ax1.set_title('25 Hz and 50 Hz sinusoids')
ax1.axis([0, 1, -2, 2])
# Design the Butterworth filter using
# signal.butter and output='sos'
sos = signal.butter(50, 35, 'lp', fs=1000, output='sos')
Python3
# Filter the signal by the filter using signal.sosfilt
# Use signal.sosfiltfilt to get output inphase with input
filtered = signal.sosfiltfilt(sos, sig)
# Display the output signal
ax2.plot(t, filtered)
ax2.set_title('After 35 Hz Low-pass filter')
ax2.axis([0, 1, -2, 2])
ax2.set_xlabel('Time [seconds]')
plt.tight_layout()
plt.show()
Python3
# import required library
import numpy as np
import scipy.signal as signal
import matplotlib.pyplot as plt
# Given
f1 = 25 # Frequency of 1st signal
f2 = 50 # Frequency of 2nd signal
N = 10 # Order of the filter
# Generate the time vector of 1 sec duration
# START CODE HERE ### (≈ 1 line of code)
# Generate 1000 samples in 1 sec
t = np.linspace(0, 1, 1000)
# Generate the signal containing f1 and f2
# START CODE HERE ### (≈ 1 line of code)
sig = np.sin(2*np.pi*f1*t) + np.sin(2*np.pi*f2*t)
# Display the signal
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
ax1.plot(t, sig)
ax1.set_title('25 Hz and 50 Hz sinusoids')
ax1.axis([0, 1, -2, 2])
# Design the Butterworth filter using signal.butter and output='sos'
# START CODE HERE ### (≈ 1 line of code)
sos = signal.butter(50, 35, 'lp', fs=1000, output='sos')
# Filter the signal by the filter using signal.sosfilt
# START CODE HERE ### (≈ 1 line of code)
# Use signal.sosfiltfilt to get output inphase with input
filtered = signal.sosfiltfilt(sos, sig)
# Display the output signal
ax2.plot(t, filtered)
ax2.set_title('After 35 Hz Low-pass filter')
ax2.axis([0, 1, -2, 2])
ax2.set_xlabel('Time [seconds]')
plt.tight_layout()
plt.show()
第 2 步:定义规格
蟒蛇3
# Specifications of the filter
f1 = 25 # Frequency of 1st signal
f2 = 50 # Frequency of 2nd signal
N = 10 # Order of the filter
# Generate the time vector of 1 sec duration
t = np.linspace(0, 1, 1000) # Generate 1000 samples in 1 sec
# Generate the signal containing f1 and f2
sig = np.sin(2*np.pi*f1*t) + np.sin(2*np.pi*f2*t)
第 3 步:绘制带有噪声的原始信号
蟒蛇3
# Display the signal
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
ax1.plot(t, sig)
ax1.set_title('25 Hz and 50 Hz sinusoids')
ax1.axis([0, 1, -2, 2])
# Design the Butterworth filter using
# signal.butter and output='sos'
sos = signal.butter(50, 35, 'lp', fs=1000, output='sos')
输出:
步骤 4:去除噪声后的信号图
蟒蛇3
# Filter the signal by the filter using signal.sosfilt
# Use signal.sosfiltfilt to get output inphase with input
filtered = signal.sosfiltfilt(sos, sig)
# Display the output signal
ax2.plot(t, filtered)
ax2.set_title('After 35 Hz Low-pass filter')
ax2.axis([0, 1, -2, 2])
ax2.set_xlabel('Time [seconds]')
plt.tight_layout()
plt.show()
输出:
第 5 步:实施
蟒蛇3
# import required library
import numpy as np
import scipy.signal as signal
import matplotlib.pyplot as plt
# Given
f1 = 25 # Frequency of 1st signal
f2 = 50 # Frequency of 2nd signal
N = 10 # Order of the filter
# Generate the time vector of 1 sec duration
# START CODE HERE ### (≈ 1 line of code)
# Generate 1000 samples in 1 sec
t = np.linspace(0, 1, 1000)
# Generate the signal containing f1 and f2
# START CODE HERE ### (≈ 1 line of code)
sig = np.sin(2*np.pi*f1*t) + np.sin(2*np.pi*f2*t)
# Display the signal
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
ax1.plot(t, sig)
ax1.set_title('25 Hz and 50 Hz sinusoids')
ax1.axis([0, 1, -2, 2])
# Design the Butterworth filter using signal.butter and output='sos'
# START CODE HERE ### (≈ 1 line of code)
sos = signal.butter(50, 35, 'lp', fs=1000, output='sos')
# Filter the signal by the filter using signal.sosfilt
# START CODE HERE ### (≈ 1 line of code)
# Use signal.sosfiltfilt to get output inphase with input
filtered = signal.sosfiltfilt(sos, sig)
# Display the output signal
ax2.plot(t, filtered)
ax2.set_title('After 35 Hz Low-pass filter')
ax2.axis([0, 1, -2, 2])
ax2.set_xlabel('Time [seconds]')
plt.tight_layout()
plt.show()
输出: