python实现hp 滤波
时间: 2023-05-11 16:03:30 浏览: 283
HP滤波是一种高通滤波器,可以用于去除信号中的低频成分。以下是Python实现HP滤波的代码:
```python
import numpy as np
from scipy.signal import butter, filtfilt
def butter_highpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='high', analog=False)
return b, a
def highpass_filter(data, cutoff, fs, order=5):
b, a = butter_highpass(cutoff, fs, order=order)
y = filtfilt(b, a, data)
return y
```
其中,butter_highpass函数用于生成高通滤波器的系数,highpass_filter函数用于对数据进行滤波。使用方法如下:
```python
# 生成测试数据
fs = 1000
t = np.arange(0, 1, 1/fs)
x = np.sin(2*np.pi*50*t) + np.sin(2*np.pi*150*t) + np.sin(2*np.pi*300*t)
# 对数据进行滤波
cutoff = 100
order = 5
y = highpass_filter(x, cutoff, fs, order=order)
```
这里生成了一个包含50Hz、150Hz和300Hz三个频率的信号,然后使用高通滤波器去除了低于100Hz的成分。
阅读全文