Skip to content

Commit 261b98c

Browse files
authored
Initial upload of files
Butterworth bandpass filter function
1 parent 79935a9 commit 261b98c

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

butterworth_bandpass.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from scipy import signal
2+
from matplotlib import pyplot as plt
3+
from matplotlib import style
4+
import numpy as np
5+
6+
7+
# import sampling rate and time stamp from noise_signal module
8+
from noise_signal import t, my_signal
9+
10+
# Critcal Frequencies
11+
lowcut = 200
12+
highcut = 700
13+
14+
sig = my_signal
15+
time = t
16+
17+
def band_butter(sig, time, crit_low, crit_high, order):
18+
19+
# sampling frequency
20+
Fs = len(time)/max(time)
21+
22+
# nyquist frequency
23+
Fn = 0.5*Fs
24+
25+
# normalize critical frequencies
26+
low = lowcut/Fn
27+
high = highcut/Fn
28+
29+
# filter coefficients
30+
b,a = signal.butter(order,[low,high],btype='band')
31+
32+
# filtered signal
33+
sig_filt = signal.lfilter(b,a,sig)
34+
35+
# frequency response
36+
w,h = signal.freqz(b,a,worN=2000)
37+
38+
# Plots
39+
style.use('ggplot')
40+
style.use('dark_background')
41+
42+
plt.title('Filter Frequency Response')
43+
plt.plot((Fs*0.5/np.pi)*w, abs(h),label = f'order={order}', color = 'orange')
44+
plt.xlabel('Frequency (Hz)')
45+
plt.ylabel('Gain')
46+
plt.grid()
47+
plt.legend(loc='best')
48+
49+
f,plt_arr= plt.subplots(2,sharex=True)
50+
51+
plt_arr[0].plot(time, sig, color ='red')
52+
plt_arr[0].set_title('Input Signal', color ='red')
53+
plt_arr[0].set_ylabel('Amplitude')
54+
55+
plt_arr[1].plot(time, sig_filt,color='cyan')
56+
plt_arr[1].set_title('Filtered Signal', color ='cyan')
57+
plt_arr[1].set_ylabel('Amplitude')
58+
plt_arr[1].set_xlabel('time (seconds)')
59+
60+
plt.show()
61+
62+
band_butter(my_signal, t, lowcut, highcut, 6)

noise_signal.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import numpy as np
2+
3+
# generate signal
4+
fs =5000
5+
T = 0.1
6+
t = np.linspace(0.0,T,T*fs)
7+
8+
x1 = 0.11*np.sin(2*np.pi*0.89*np.sqrt(t)+0.65) # amp = 0.1 freq = 2.2
9+
x2 = 0.03*np.sin(2*np.pi*150*t+0.2) # amp = 0.03 freq = 300
10+
x3 = 0.015*np.sin(2*np.pi*600*t) # amp = 0.2 freq = 900
11+
x4 = 0.015*np.sin(2*np.pi*2000*t) # amp = 0.2 freq = 900
12+
13+
my_signal = x1+x2+x3+x4

0 commit comments

Comments
 (0)