Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.1k views
in Technique[技术] by (71.8m points)

python - Applying a bandpass filter with firwin

I want to apply a bandpass filter with firwin to a three-tone signal. The three tones are 7, 11, and 30 hz, respectively. Specifically, I need to modify the cutoff so that the first and last tones(7 and 30hz) get filtered out. To my understanding, f1 and f2 should be the edges of the bandpass filter. If I set them to 10 and 20 hz, it gives me an error telling me I can't set the cutoff higher than fs/2. I'm not understanding the error because I previously set fs to 100hz. Is there any way I can set the cutoffs so that only the 11hz gets passed?

f1, f2 = 0.5, 0.8 
# f1, f2 = 10.0, 20.0

bandpass_coeff = signal.firwin(N_taps, [f1, f2], pass_zero=False) 

freqzPlot(bandpass_coeff, 'bandpass filter with firwin ' + str(N_taps) + ' taps')

filterHighest(Fs, Fc, bandpass_coeff)


def generateThreeTones(Fs, interval, freq1 = 7.0, amp1 = 5.0, phase1 = 0.0, freq2 = 11.0, amp2 = 3.0, phase2 = 5.0, freq3 = 30.0, amp3 = 4.0, phase3 = 11.0):

dt = 1.0/Fs                          # sampling period (increment in time)
time = np.arange(0, interval, dt)    # time vector over interval



# generate the sin signal
x = amp1*np.sin(2*math.pi*freq1*time+phase1)+amp2*np.sin(2*math.pi*freq2*time+phase2)+amp3*np.sin(2*math.pi*freq3*time+phase3)
return time, x
question from:https://stackoverflow.com/questions/65916827/applying-a-bandpass-filter-with-firwin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should pass Fs to signal.firwin. Something like:

bandpass_coeff = signal.firwin(N_taps, [f1, f2], pass_zero=False,fs=Fs)

Otherwise the default value will be used for fs, which is 2 according to the documentation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...