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 )
0 commit comments