python带通滤波,python中的fft带通滤波器

What I try is to filter my data with fft. I have a noisy signal recorded with 500Hz as a 1d- array. My high-frequency should cut off with 20Hz and my low-frequency with 10Hz.

What I have tried is:

fft=scipy.fft(signal)

bp=fft[:]

for i in range(len(bp)):

if not 10

bp[i]=0

ibp=scipy.ifft(bp)

What I get now are complex numbers. So something must be wrong. What? How can I correct my code?

解决方案

It's worth noting that the magnitude of the units of your bp are not necessarily going to be in Hz, but are dependent on the sampling frequency of signal, you should use scipy.fftpack.fftfreq for the conversion. Also if your signal is real you should be using scipy.fftpack.rfft. Here is a minimal working example that filters out all frequencies less than a specified amount:

import numpy as np

from scipy.fftpack import rfft, irfft, fftfreq

time = np.linspace(0,10,2000)

signal = np.cos(5*np.pi*time) + np.cos(7*np.pi*time)

W = fftfreq(signal.size, d=time[1]-time[0])

f_signal = rfft(signal)

# If our original signal time was in seconds, this is now in Hz

cut_f_signal = f_signal.copy()

cut_f_signal[(W<6)] = 0

cut_signal = irfft(cut_f_signal)

We can plot the evolution of the signal in real and fourier space:

import pylab as plt

plt.subplot(221)

plt.plot(time,signal)

plt.subplot(222)

plt.plot(W,f_signal)

plt.xlim(0,10)

plt.subplot(223)

plt.plot(W,cut_f_signal)

plt.xlim(0,10)

plt.subplot(224)

plt.plot(time,cut_signal)

plt.show()

4b28c5a45606a93af015a3b6541711fb.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值