1

使用bandpass Butterworth filter对信号数据进行滤波去噪

在信号处理中,有些信号会包含大量的噪声,需要用一些滤波器去噪,本文介绍使用bandpass Butterworth filter进行去噪。
直接使用sciPypython库可以实现噪声滤波

步骤1:导入所需python模块

from scipy.signal import filtfilt
from scipy import stats
import CSV
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy

步骤2:读取数据及可视化数据

def plot():
    data = pd.read_csv(filePath)
    signal_data = data[['data']]
    signal_data = np.array(data)
    time = np.linspace(0, 0.02, 52)
    plt.plot(time, signal_data)
    plt.show()

数据的样子如下:

而执行plot()函数得到的图形为:

步骤3:执行滤波器滤波

def bandPass_filter(signal):
    fs = 4000.0
    lowcut = 20.0
    highcut = 50.0
    nyqs = 0.5* fs
    low = lowcut / nyqs
    high = highcut/ nyqs
    
    order=2
    
    b, a = scipy.signal.butter(order, [low, high], 'bandpass', analog = False) # b: 滤波器的分子系数向量,a: 滤波器的分母系数向量
    y = scipy.signal.filtfilt(b, a, signal, axis=0)  # 滤波函数
    return(y)

data = pd.read_csv(filePath)
signal_data = data[['data']]
signal_data = np.array(data)
time = np.linspace(0, 0.02, 52)
filtered_signal= bandPass_filter(signal_data)
plt.plot(time, filtered_signal)
plt.show()

结果显示:

完整的代码以及将两种效果放入一张图中显示

from scipy.signal import filtfilt
from scipy import stats
import csv
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy
def plot():
    data = pd.read_csv(filePath)
    signal_data = data[['data']]
    signal_data = np.array(signal_data)
    time = np.linspace(0, 0.2, 52)
    plt.plot(time, signal_data)
    plt.show()
    filtered_signal= bandPass_filter(signal_data)
    plt.plot(time, filtered_signal)
    plt.show()
#syntax for bandpass filter
def bandPass_filter(signal):
    fs = 4000.0
    lowcut = 20.0
    highcut = 50.0
    nyqs = 0.5* fs
    low = lowcut / nyqs
    high = highcut/ nyqs
    
    order=2
    
    b, a = scipy.signal.butter(order, [low, high], 'bandpass', analog = False)
    y = scipy.signal.filtfilt(b, a, signal, axis=0)
    return(y)
plot()

结果显示:

posted @ 2023-05-18 17:13  Bonne_chance  阅读(706)  评论(0编辑  收藏  举报
1