-
Notifications
You must be signed in to change notification settings - Fork 0
/
iir.py
32 lines (26 loc) · 1.19 KB
/
iir.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import numpy, scipy.io.wavfile,matplotlib.pyplot as plt, scipy.signal
from mysignal import play, tone
from freqencyAnalysis import plotfft
def make_filter(zeros, poles):
a=numpy.polynomial.polynomial.polyfromroots(zeros)
a=numpy.real(a)
if poles:
b=numpy.polynomial.polynomial.polyfromroots(poles)
b=numpy.real(b)
return lambda x: scipy.signal.lfilter(a[::-1],b[::-1],x)
else:
return lambda x: numpy.convolve(x,a)
def make_notch(freq, sampling_freq, param=0.937):
direction=numpy.e**(1j*numpy.pi*freq/sampling_freq*2)
return make_filter([direction, numpy.conj(direction)], [param * direction, param * numpy.conj(direction)])
def make_peak(freq, sampling_freq, param=0.937):
direction=numpy.e**(1j*numpy.pi*freq/sampling_freq*2)
return make_filter([], [param * direction, param * numpy.conj(direction)])
def plot_filter_response(filter, sampling_frequency, resolution, logscale=True):
impulse_response=filter([1]+[0]*(resolution-1))
plotfft(impulse_response,sampling_frequency,logscale)
if __name__=="__main__":
fs = 500
#iir=make_filter([-1,1],[0.937j,-0.937j])
iir=make_peak(50, fs)
plot_filter_response(iir, fs, 1024,False)