简体   繁体   English

频带通过python吗?

[英]Frequency band pass in python?

I would like to filter out unwanted frequencies and keep an only 60Hz signal. 我想过滤掉不需要的频率,并保持60Hz的信号。

Here is what I have done so far: 到目前为止,这是我所做的:

import numpy as np
from scipy.fftpack import rfft, irfft, fftfreq
#
time   = np.linspace(0,1,1000)

in_sig = np.cos(54*np.pi*time) + np.cos(60*np.pi*time)  + np.sin(66*np.pi*time);
high_freq = 62;
low_freq = 58;

freqs = fftfreq(len(in_sig), d=time[1]-time[0])
filt_sig = rfft(in_sig)

cut_filt_sig = filt_sig.copy()
cut_filt_sig[(freqs<low_freq)] = 0
cut_filt_sig[(freqs>high_freq)] = 0

cut_in_sig = irfft(cut_filt_sig)

from pylab import *
figure(figsize=(10, 6))
subplot(221);plot(time,in_sig); title('Input signal');
subplot(222);plot(freqs,filt_sig);xlim(0,100);title('FFT of the input signal');

subplot(223);plot(time,cut_in_sig); title('Filtered signal');
xlabel('Time (s)')
subplot(224);plot(freqs,cut_filt_sig);xlim(0,100); title('FFT of the filtered signal');
xlabel('Freq. (Hz)')

show()

Plotted results 绘制结果

As I can see the filtered signal has lower amplitudes at the edges, I assume it could be due to applied rectangular window. 如我所见,滤波后的信号在边缘处具有较低的幅度,我认为这可能是由于应用了矩形窗口所致。 What windows would you recommend to use to improve to output? 您将建议使用哪些窗口来改善输出?

The issue likely comes from numpy's linspace() . 问题可能来自numpy的linspace() The default mode is to include the endpoint stop . 默认模式是包括端点stop So time is 0, 1/999, 2/999, ..., 1 . 所以time0, 1/999, 2/999, ..., 1 On the contrary, fft , handles of signal of length N as a periodic signal sampled at 0, T/N, ... , T(N-1)/N , thus avoiding the redundancy of the endpoint. 相反, fft将长度为N的信号作为周期信号以0, T/N, ... , T(N-1)/N进行采样,从而避免了端点的冗余。 The computed DFT therefore use a frame of length T=1000/999. 因此,所计算的DFT使用长度为T = 1000/999的帧。 Hence the frequencies of the DFT are k*999/1000, not k. 因此,DFT的频率为k * 999/1000,而不是k。 Since the length of the frame is not a multiple of the period of the signal (1/6s), a problem named spectral leakage occurs. 由于帧的长度不是信号周期的倍数(1 / 6s),因此会发生称为频谱泄漏的问题。

To avoid the spectral leakage, the length of the frame can be shortened to a multiple of the period, by removing the endpoint: 为了避免频谱泄漏,可以通过删除端点将帧的长度缩短为周期的倍数:

time   = np.linspace(0,1,1000,endpoint=False)

It returns time as 0, 1/1000, ....999/1000, handled by the DFT as a frame of length 1, that is a multiple of the period of the input signal (1/6s). 它返回time 0、1 / 1000,.... 999/1000,由DFT处理为长度为1的帧,该长度是输入信号周期(1 / 6s)的倍数。

无光谱泄漏

If the length of the frame is not a multiple of the period of the signal, the input signal can be windowed so as to partly mitigate the effect related to the discontinuity at the edge of the frame, but spurous frequencies still exist. 如果帧的长度不是信号周期的倍数,则可以对输入信号进行加窗处理 ,以部分缓解与帧边缘处的不连续性有关的影响,但是仍然存在杂散频率。 Finally, the actual frequencies can be properly computed by estimating the frequency of a peak as its mean frequency wih respect to power density. 最后,通过将峰值的频率估计为相对于功率密度的平均频率,可以适当地计算实际频率。 See my answer to Why are frequency values rounded in signal using FFT? 请参阅我的答案, 为什么频率值会使用FFT在信号中取整?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM