[英]Setting Wn for analog Bessel model in scipy.signal
I have been trying to implement an analog Bessel filter with a cutoff frequency 2kHz using scipy.signal, and I am confused about what value of Wn to set, as the documentation states Wn (for analog filters) should be set to angular frequency (12000 rad/s approximately).我一直在尝试使用 scipy.signal 实现一个截止频率为 2kHz 的模拟贝塞尔滤波器,我对要设置的 Wn 值感到困惑,因为文档状态 Wn(用于模拟滤波器)应设置为 angular 频率(12000大约弧度/秒)。 But if I implement this to my 1 second of dummy data, with half a second pulse sampled at 500 000 Hz, I get a string of 0s and nans.
但是,如果我对 1 秒的虚拟数据实施此操作,半秒脉冲以 500 000 Hz 采样,我会得到一串 0 和 nans。 What is it that I am missing?
我错过了什么?
import numpy as np
import scipy
import matplotlib.pyplot as plt
import scipy.signal
def make_signal(pulse_length, rate = 500000):
new_x = np.zeros(rate)
end_signal = 250000+pulse_length
new_x[250000:end_signal] = 1
data = new_x
print (np.shape(data))
# pad on both sides
data=np.concatenate((np.zeros(rate),data,np.zeros(rate)))
return data
def conv_time(t):
pulse_length = t * 500000
pulse_length = int(pulse_length)
return pulse_length
def make_data(ti): #give time in seconds
pulse_length=conv_time(ti)
print (pulse_length)
data = make_signal(pulse_length)
return data
time_scale = np.linspace(0,1,500000)
data = make_data(0.5)
[b,a] = scipy.signal.bessel(4, 12566.37, btype='low', analog=True, output='ba', norm='phase', fs=None)
output_signal = scipy.signal.filtfilt(b, a, data)
plt.plot(data[600000:800000])
plt.plot(output_signal[600000:800000])
When plotting response using freqs, it doesn't seem that bad to me;使用频率绘制响应时,对我来说似乎并没有那么糟糕。 where am I making a mistake?
我在哪里犯错了?
You are passing an analog filter to a function, scipy.signal.filtfilt
, that expects a digital (ie discrete time) filter.您将模拟滤波器传递给 function,
scipy.signal.filtfilt
,它需要一个数字(即离散时间)滤波器。 If you are going to use filtfilt
or lfilter
, the filter must be digital.如果要使用
filtfilt
或lfilter
,则过滤器必须是数字的。
To work with continuous time systems, take a look at the functions要使用连续时间系统,请查看函数
scipy.signal.impulse
( scipy.signal.impulse2
) scipy.signal.impulse
( scipy.signal.impulse2
)scipy.signal.step
( scipy.signal.step2
) scipy.signal.step
( scipy.signal.step2
)scipy.signal.lsim
( scipy.signal.lsim2
) scipy.signal.lsim
( scipy.signal.lsim2
) (The 2
versions solve the same mathematical problem as the version without 2
but use a different method. In most cases, the version without 2
is fine and is much faster than the 2
version.) (
2
版解决了与没有2
版相同的数学问题,但使用了不同的方法。在大多数情况下,没有2
的版本很好,并且比2
版快得多。)
Other related functions and classes are listed in the section Continuous-Time Linear Systems of the SciPy documentation.其他相关函数和类在 SciPy 文档的连续时间线性系统部分中列出。
For example, here's a script that plots the impulse and step responses of your Bessel filter:例如,这是一个绘制 Bessel 滤波器的脉冲和阶跃响应的脚本:
import numpy as np
from scipy.signal import bessel, step, impulse
import matplotlib.pyplot as plt
order = 4
Wn = 2*np.pi * 2000
b, a = bessel(order, Wn, btype='low', analog=True, output='ba', norm='phase')
# Note: the upper limit for t was chosen after some experimentation.
# If you don't give a T argument to impulse or step, it will choose a
# a "pretty good" time span.
t = np.linspace(0, 0.00125, 2500, endpoint=False)
timp, yimp = impulse((b, a), T=t)
tstep, ystep = step((b, a), T=t)
plt.subplot(2, 1, 1)
plt.plot(timp, yimp, label='impulse response')
plt.legend(loc='upper right', framealpha=1, shadow=True)
plt.grid(alpha=0.25)
plt.title('Impulse and step response of the Bessel filter')
plt.subplot(2, 1, 2)
plt.plot(tstep, ystep, label='step response')
plt.legend(loc='lower right', framealpha=1, shadow=True)
plt.grid(alpha=0.25)
plt.xlabel('t')
plt.show()
The script generates this plot:该脚本生成此 plot:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.