python产生fir滤波器_Python中使用FIR滤波器firwin后信号的相移

所以,在我最后两个问题之后,让我谈谈我的实际问题。也许有人在我的理论程序中发现了错误,或者我在编程中做错了什么。在……里面

[En]

So, after my last two questions, let me talk about my practical problems. Maybe someone found an error in my theoretical program, or I did something wrong in programming. In

我使用scipy.signal(使用firwin函数)在Python中实现带通滤波器。我的原始信号包括两个频率(w_1=600Hz,w_2=800Hz)。可能会有更多的频率所以我需要一个带通滤波器。在

在这个例子中,我想过滤掉大约600hz的频带,所以我取了600+/-20Hz作为截止频率。当我实现滤波器并使用lfilter在时域中再现信号时,正确的频率被过滤了。在

为了消除相移,我用scipy.signal.freqz绘制了频率响应图,返回值为firwin的h作为分子,1作为预定义的denumerator。

如freqz文档中所述,我还绘制了相位(=doc中的角度),并且能够查看频率响应图,以获得滤波信号频率600hz的相移。在

所以相位延迟t

tΒp=—(Tetha(w))/(w)

不幸的是,当我把这个相位延迟加到滤波信号的时间数据中时,它并没有得到与原始600hz信号相同的相位。在

我加了密码。奇怪的是,在消除一些代码以将其保持在最小之前,过滤后的信号以正确的幅度开始–现在情况甚至更糟。年#年中

[En]

I added the password. Oddly enough, before eliminating some of the code to keep it to a minimum, the filtered signal starts with the correct amplitude-now the situation is even worse. In #

Filtering test

from math import *

import numpy as np

from scipy import signal

from scipy.signal import firwin, lfilter, lti

from scipy.signal import freqz

import matplotlib.pyplot as plt

import matplotlib.colors as colors

Nb of frequencies in the original signal

nfrq = 2

F = [60,80]

Sampling:

nitper = 16

nper = 50.

fmin = np.min(F)

fmax = np.max(F)

T0 = 1./fmin

dt = 1./fmax/nitper

sampling frequency

fs = 1./dt

nyq_rate= fs/2

nitpermin = nitper*fmax/fmin

Nit = int(nper*nitpermin+1)

tps = np.linspace(0.,nper*T0,Nit)

dtf = fs/Nit

Build analytic signal

s = completeSignal(F,Nit,tps)

scomplete = np.zeros((Nit))

omg1 = 2.piF[0]

omg2 = 2.piF[1]

scomplete=scomplete+np.sin(omg1tps)+np.sin(omg2tps)

ssingle = singleSignals(nfrq,F,Nit,tps)

ssingle=np.zeros((nfrq,Nit))

ssingle[0,:]=ssingle[0,:]+np.sin(omg1*tps)

ssingle[1,:]=ssingle[0,:]+np.sin(omg2*tps)

Construction of the desired bandpass filter

lowcut = (60-2) # desired cutoff frequencies

highcut = (60+2)

ntaps = 451 # the higher and closer the signal frequencies, the more taps for the filter are required

taps_hamming = firwin(ntaps,[lowcut/nyq_rate, highcut/nyq_rate], pass_zero=False)

Use lfilter to get the filtered signal

filtered_signal = lfilter(taps_hamming, 1, scomplete)

The phase delay of the filtered signal

delay = ((ntaps-1)/2)/fs

plt.figure(1, figsize=(12, 9))

Plot the signals

plt.plot(tps, scomplete,label=”Original signal with %s freq” % nfrq)

plt.plot(tps-delay, filtered_signal,label=”Filtered signal %s freq ” % F[0])

plt.plot(tps, ssingle[0,:],label=”original signal %s Hz” % F[0])

plt.grid(True)

plt.legend()

plt.xlim(0,1)

plt.xlabel(‘Time (s)’)

plt.ylabel(‘Amplitude’)

Plot the frequency responses of the filter.

plt.figure(2, figsize=(12, 9))

plt.clf()

First plot the desired ideal response as a green(ish) rectangle.

rect = plt.Rectangle((lowcut, 0), highcut – lowcut, 5.0,facecolor=”#60ff60″, alpha=0.2,label=”ideal filter”)

plt.gca().add_patch(rect)

actual filter

w, h = freqz(taps_hamming, 1, worN=1000)

plt.plot((fs * 0.5 / np.pi) * w, abs(h), label=”designed rectangular window filter”)

plt.xlim(0,2*F[1])

plt.ylim(0, 1)

plt.grid(True)

plt.legend()

plt.xlabel(‘Frequency (Hz)’)

plt.ylabel(‘Gain’)

plt.title(‘Frequency response of FIR filter, %d taps’ % ntaps)

plt.show()’

Original: https://blog.csdn.net/weixin_39748858/article/details/111515140
Author: weixin_39748858
Title: python产生fir滤波器_Python中使用FIR滤波器firwin后信号的相移

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/525041/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球