numpy - 使用 scipy 的低通冷杉滤波器参数

标签 numpy signal-processing scipy scientific-computing matplotlib

我正在尝试使用 scipy 编写一个简单的低通滤波器,但我需要帮助定义参数。

我的时间序列数据中有350万条记录需要过滤,数据采样频率为1000hz。

我正在使用 scipy 库中的 signal.firwin 和 signal.lfilter。

我在下面的代码中选择的参数根本不过滤我的数据。取而代之的是,下面的代码只是生成一些图形上看起来与完全相同的数据,但时间相位失真将图形向右移动略小于 1000 个数据点(1 秒)。

在另一个软件程序中,通过图形用户界面命令运行低通冷杉滤波器产生的输出对于每 10 秒(10,000 个数据点)段具有相似的平均值,但标准偏差大大降低,因此我们基本上失去了这个特定的噪声数据文件并将其替换为保留平均值的内容,同时显示不受高频噪声污染的长期趋势。另一个软件的参数对话框包含一个复选框,允许您选择系数的数量,以便“根据样本大小和采样频率进行优化”。 (我在 1000 hz 下收集了 350 万个样本,但我想要一个使用这些输入作为变量的函数。)

*谁能告诉我如何调整下面的代码,以消除所有高于 0.05 hz 的频率? * 我希望在图形中看到平滑的波浪,而不仅仅是我现在从下面的代码中得到的相同图形的时间失真。

class FilterTheZ0():
    def __init__(self,ZSmoothedPylab):
        #------------------------------------------------------
        # Set the order and cutoff of the filter
        #------------------------------------------------------
        self.n = 1000
        self.ZSmoothedPylab=ZSmoothedPylab
        self.l = len(ZSmoothedPylab)
        self.x = arange(0,self.l)
        self.cutoffFreq = 0.05

        #------------------------------------------------------
        # Run the filter
        #------------------------------------------------------
        self.RunLowPassFIR_Filter(self.ZSmoothedPylab, self.n, self.l
                                       , self.x, self.cutoffFreq)

    def RunLowPassFIR_Filter(self,data, order, l, x, cutoffFreq):
        #------------------------------------------------------
        # Set a to be the denominator coefficient vector
        #------------------------------------------------------
        a = 1
        #----------------------------------------------------
        # Create the low pass FIR filter
        #----------------------------------------------------
        b = signal.firwin(self.n, cutoff = self.cutoffFreq, window = "hamming")

        #---------------------------------------------------
        # Run the same data set through each of the various
        # filters that were created above.
        #---------------------------------------------------
        response = signal.lfilter(b,a,data)
        responsePylab=p.array(response)

        #--------------------------------------------------
        # Plot the input and the various outputs that are
        # produced by running each of the various filters
        # on the same inputs.
        #--------------------------------------------------

        plot(x[10000:20000],data[10000:20000])
        plot(x[10000:20000],responsePylab[10000:20000])
        show()

        return

最佳答案

截止频率归一化为奈奎斯特频率,它是采样率的一半。因此,当 FS = 1000 和 FC = 0.05 时,您需要截止值 = 0.05/500 = 1e-4。

from scipy import signal

FS = 1000.0                                          # sampling rate
FC = 0.05/(0.5*FS)                                   # cutoff frequency at 0.05 Hz
N = 1001                                             # number of filter taps
a = 1                                                # filter denominator
b = signal.firwin(N, cutoff=FC, window='hamming')    # filter numerator

M = FS*60                                            # number of samples (60 seconds)
n = arange(M)                                        # time index
x1 = cos(2*pi*n*0.025/FS)                            # signal at 0.025 Hz
x = x1 + 2*rand(M)                                   # signal + noise
y = signal.lfilter(b, a, x)                          # filtered output

plot(n/FS, x); plot(n/FS, y, 'r')                    # output in red
grid()

Output
滤波器输出延迟半秒(滤波器以抽头 500 为中心)。请注意,噪声添加的 DC 偏移由低通滤波器保留。此外,0.025 Hz 完全在通过范围内,因此从峰到峰的输出摆幅约为 2。

关于numpy - 使用 scipy 的低通冷杉滤波器参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4152457/

相关文章:

python - 从数据文件中获取带有列标题和数据列的 python 映射

python - 避免在 np.array 操作中使用 for 循环。 Python

c# - 从 .WAV 文件中读取 24 位样本

matlab - 如何在matlab中分别控制耳机左右声道在matlab中播放音频信号?

python - 在列表列表中查找最长递增子序列的最有效方法

python - 如果一列中的第三个字母是 W,如何在 Pandas 中删除一行?

Python/Numpy - 在对值的一定公差范围内设置值

python - 为什么我在 python 中的 Spearman 相关性中得到 nan

python - 如何将 NumPy 数组标准化到一定范围内?

python - 找出在 matplotlib 中绘制的两条曲线之间的区域(fill_between area)