-
Notifications
You must be signed in to change notification settings - Fork 1
/
fft.py
48 lines (34 loc) · 1.06 KB
/
fft.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
"""
filter out the noise
"""
import numpy as np
from scipy.signal import freqz
from scipy.signal import butter, lfilter
import scipy.io.wavfile as wavfile
def butter_bandpass(lowcut, highcut, fs, order=5):
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='band')
return b, a
def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
y = lfilter(b, a, data)
return y
def read_wav_file(wave_file):
return wavfile.read(wave_file)
if __name__ == "__main__":
# Sample rate and desired cutoff frequencies (in Hz).
fs = 5000.0
# telephony system standard of 300Hz to 3.4kHz
lowcut = 300
highcut = 3400
# Filter a noisy signal.
rate, data = wavfile.read("test_speak.wav")
data_ch1 = data[:, 0]
print(data_ch1[:5])
data_ch1 = data_ch1/max(data_ch1)
print(data_ch1[:5])
print(rate)
y = butter_bandpass_filter(data_ch1, lowcut, highcut, fs, order=6)
wavfile.write("out.wav", 44100, y)