【发布时间】:2015-04-16 15:43:02
【问题描述】:
我正在尝试过滤/平滑从采样频率为 50 kHz 的压力传感器获得的信号。示例信号如下所示:
我想在MATLAB中得到一个loess得到的平滑信号(我画的不是同一个数据,数值不同)。
我使用 matplotlib 的 psd() 函数计算了功率谱密度,下面还提供了功率谱密度:
我已经尝试使用以下代码并获得了过滤后的信号:
import csv
import numpy as np
import matplotlib.pyplot as plt
import scipy as sp
from scipy.signal import butter, lfilter, freqz
def butter_lowpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='low', analog=False)
return b, a
def butter_lowpass_filter(data, cutoff, fs, order=5):
b, a = butter_lowpass(cutoff, fs, order=order)
y = lfilter(b, a, data)
return y
data = np.loadtxt('data.dat', skiprows=2, delimiter=',', unpack=True).transpose()
time = data[:,0]
pressure = data[:,1]
cutoff = 2000
fs = 50000
pressure_smooth = butter_lowpass_filter(pressure, cutoff, fs)
figure_pressure_trace = plt.figure(figsize=(5.15, 5.15))
figure_pressure_trace.clf()
plot_P_vs_t = plt.subplot(111)
plot_P_vs_t.plot(time, pressure, linewidth=1.0)
plot_P_vs_t.plot(time, pressure_smooth, linewidth=1.0)
plot_P_vs_t.set_ylabel('Pressure (bar)', labelpad=6)
plot_P_vs_t.set_xlabel('Time (ms)', labelpad=6)
plt.show()
plt.close()
我得到的输出是:
我需要更多的平滑,我尝试改变截止频率但仍然无法获得满意的结果。我无法通过 MATLAB 获得相同的平滑度。我确信它可以在 Python 中完成,但是怎么做呢?
你可以找到数据here。
更新
我应用了 statsmodels 的 lowess 平滑,这也不能提供令人满意的结果。
【问题讨论】:
标签: python numpy scipy filtering smoothing