-
Notifications
You must be signed in to change notification settings - Fork 0
/
realtime_IIR.py
76 lines (59 loc) · 1.88 KB
/
realtime_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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
This script is a benchmark to test different FIR filter on real time implementation (sample by sample)
Author. Gabriel Galeote Checa
email: [email protected]
GNU license
"""
import matplotlib.pylab as plt
import numpy as np
from scipy import signal
import iir
#Set your sampling frequency
fs = 2000 # Hertz
def generate_sine_wave(freq, sample_rate, duration, magnitude):
x = np.linspace(0, duration, fs * duration, endpoint=False)
frequencies = x * freq
y = magnitude * np.sin((2 * np.pi) * frequencies)
return x, y
# Generate 2 sinusoidal waves with a frequencies of 1 and 50 Hz for test
f1 = 1
f2 = 50
magnitude_1 = 1
magnitude_2 = 0.5
DURATION = 5 # Seconds
x, sine1 = generate_sine_wave(f1, fs, DURATION, magnitude_1)
_, noise = generate_sine_wave(f2, fs, DURATION, magnitude_2)
mysignal = sine1 + noise
plt.figure(1)
plt.plot(mysignal)
# ----------- IIR filter --------------
order = 20
fs = 2000
f1 = 0.5 /(fs * 2)
f2 = 45 /(fs * 2)
cutoff =[0.000125, 0.01125]
coeff = signal.butter(order, cutoff, 'bandpass', output='sos')
# If the order of the filter is 1, is one IIR 2nd order filter otherwise, it is a chain of IIR filters.
SAMPLES = DURATION * fs
myFilter = iir.IIR_filter(coeff)
y = np.zeros(SAMPLES)
for i in range(SAMPLES):
y[i] = myFilter.filter(mysignal[i])
# Now plot original and filtered signal
plt.figure(2)
plt.subplot(211)
plt.plot(mysignal)
plt.subplot(212)
plt.plot(y)
# Calculate the fourier trasnform
unfilteredfft = np.fft.fft(mysignal)
IIRfilteredfft = np.fft.fft(y)
T = 1/fs
f = np.linspace(0, fs, SAMPLES)
plt.figure(3)
plt.subplot(211)
plt.plot(f[:SAMPLES // 2], np.abs(unfilteredfft)[:SAMPLES // 2] * 1 / SAMPLES)
plt.subplot(212)
plt.plot(f[:SAMPLES // 2], np.abs(IIRfilteredfft)[:SAMPLES // 2] * 1 / SAMPLES) # 1 / N is a normalization factor
np.savetxt('coefficients.dat', coeff, fmt='%f', delimiter='')
plt.show()