-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexamples.py
84 lines (67 loc) · 2.26 KB
/
examples.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
77
78
79
80
81
82
83
84
import numpy as np
from typing import Sequence
def signal_generator(signal_band: Sequence[tuple], shape: tuple, fs: int) -> tuple[np.ndarray]:
"""Generate signals from a certain frequency and amplitude. Shape must be (samples, channels)"""
times = times = np.arange(length)/fs
signals = np.zeros(shape)
for freq, amp in signal_band:
signal = amp*np.sin(2*np.pi*freq*times)
signals += np.expand_dims(signal, axis=-1)
noises = np.random.randint(0, 1000, shape)/500
signals += noises
return times, signals
if __name__ == '__main__':
import matplotlib.pyplot as plt
from scipy.signal import welch
from iir import IIR
# Constant
fs = 250
length = 500
num_channel = 2
# Creating IIR instance
iir_filter = IIR(
num_channel=num_channel,
sampling_frequency=fs
)
iir_filter.add_filter(order=6, cutoff=(45, 55), filter_type='bandstop')
iir_filter.add_filter(order=4, cutoff=40, filter_type='lowpass')
iir_filter.add_filter(order=4, cutoff=3, filter_type='highpass')
# Signal generation
signal_band = [
(0.01, 200),
(8, 5),
(50, 20),
(80, 5)
]
times, signals = signal_generator(
signal_band, shape=(length, num_channel), fs=fs)
# Input as numpy array
filtered = iir_filter.filter(signals)
# Input as list
filtered = iir_filter.filter(signals.tolist())
# Plotting
labels = [f'CH{num+1}' for num in range(num_channel)]
plt.subplot(2, 2, 1)
plt.xlabel('Times (s)')
plt.ylabel('Amplitude')
plt.plot(times, signals)
plt.legend(labels, loc=1)
plt.subplot(2, 2, 2)
plt.xlabel('Times (s)')
plt.ylabel('Amplitude')
plt.plot(times, filtered)
plt.legend(labels, loc=1)
plt.subplot(2, 2, 3)
bins, power = welch(signals.T, fs=fs, nperseg=240, noverlap=200, nfft=500)
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude (dB)')
plt.plot(bins, 20*np.log10(power.T))
plt.legend(labels, loc=1)
plt.subplot(2, 2, 4)
bins, power = welch(filtered.T, fs=fs, nperseg=240, noverlap=200, nfft=500)
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude (dB)')
plt.plot(bins, 20*np.log10(power.T))
plt.legend(labels, loc=1)
plt.tight_layout()
plt.show()