from
scipy
import
signal
import
matplotlib.pyplot as plt
import
numpy as np
samp_freq
=
1000
notch_freq
=
50.0
quality_factor
=
20.0
b_notch, a_notch
=
signal.iirnotch(notch_freq, quality_factor, samp_freq)
freq, h
=
signal.freqz(b_notch, a_notch, fs
=
samp_freq)
fig
=
plt.figure(figsize
=
(
8
,
6
))
plt.plot(freq
*
samp_freq
/
(
2
*
np.pi),
20
*
np.log10(
abs
(h)),
'r'
, label
=
'Bandpass filter'
, linewidth
=
'2'
)
plt.xlabel(
'Frequency [Hz]'
, fontsize
=
20
)
plt.ylabel(
'Magnitude [dB]'
, fontsize
=
20
)
plt.title(
'Notch Filter'
, fontsize
=
20
)
plt.grid()
f1
=
15
f2
=
50
n
=
np.linspace(
0
,
1
,
1000
)
noisySignal
=
np.sin(
2
*
np.pi
*
15
*
n)
+
np.sin(
2
*
np.pi
*
50
*
n)
+
\
np.random.normal(
0
, .
1
,
1000
)
*
0.03
fig
=
plt.figure(figsize
=
(
8
,
6
))
plt.subplot(
211
)
plt.plot(n, noisySignal, color
=
'r'
, linewidth
=
2
)
plt.xlabel(
'Time'
, fontsize
=
20
)
plt.ylabel(
'Magnitude'
, fontsize
=
18
)
plt.title(
'Noisy Signal'
, fontsize
=
20
)
outputSignal
=
signal.filtfilt(b_notch, a_notch, noisySignal)
plt.subplot(
212
)
plt.plot(n, outputSignal)
plt.xlabel(
'Time'
, fontsize
=
20
)
plt.ylabel(
'Magnitude'
, fontsize
=
18
)
plt.title(
'Filtered Signal'
, fontsize
=
20
)
plt.subplots_adjust(hspace
=
0.5
)
fig.tight_layout()
plt.show()