-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
156 lines (121 loc) · 4.68 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import numpy as np
import sounddevice as sd
import soundfile as sf
import matplotlib.pyplot as plt
from scipy import signal
# Parametros 7: f1 = 5.7 kHz, f2 = 6 kHz, δ2 = 0.1
# Noise function
def noise_audio(voice, a1, a2):
n = np.arange(len(voice)) # Time axis
noise = a1 * np.cos(0.76 * np.pi * n) + a2 * np.cos(0.8 * np.pi * n)
noisy_voice = noise + voice
return noisy_voice # Return audio with noise
# Applying filters
def butterworth_filter(noisy_voice, cutoff, sample_rate, order):
b, a = signal.butter(N=order, Wn=cutoff, fs=sample_rate, btype='low', analog=False)
y = signal.lfilter(b, a, noisy_voice)
return y
def chebyshev1_filter(noisy_voice, cutoff, sample_rate, order, ripple):
b, a = signal.cheby1(order, ripple, cutoff, fs=sample_rate, btype='low', analog = False)
y = signal.lfilter(b, a, noisy_voice)
return y
def chebyshev2_filter(noisy_voice, cutoff, sample_rate, order, stop_attenuation):
b, a = signal.cheby2(order, stop_attenuation, cutoff, fs=sample_rate, btype='low', analog=False)
y = signal.lfilter(b, a, noisy_voice)
return y
def eliptic_filter(noisy_voice, cutoff, sample_rate, order, stop_attenuation, ripple):
b, a = signal.ellip(order, ripple, stop_attenuation, cutoff, fs=sample_rate, btype = 'low', analog = False)
y = signal.lfilter(b, a, noisy_voice)
return y
# Set-up
recording_time = 5 # seconds
sample_rate = 44100 # sample rate(Hz)
std_audio = "Audio padrao IIR" # Audio gravado
noisy_audio = "Audio com Ruído IIR" # Audio com ruido
butterworth_filtred_audio = "Audio filtrado com Butterworth" # Butterworth
filtred_audio_chebyshev_I = "Audio filtrado com Chebyshev I" # Chebyshev I
filtred_audio_chebyshev_II = "Audio filtrado com Chebyshev II" # Chebyshev II
filtred_audio_eliptic = "Audio filtrado com Elíptico" # Elíptico
a1 = 0.01
a2 = 0.01
# IIR filter settings
order = 4
cutoff = 1000 # Cut-Off Frequency (rad)
ripple = 3 # Ripple (dB)
stop_attenuation = 40 # Minimum attenuation within the rejection band (dB)
# Audio recording
print("Gravando..")
voice = sd.rec(int(recording_time * sample_rate), samplerate=sample_rate, channels=1)
sd.wait()
# Noisy audio
noisy_voice = noise_audio(voice.flatten(), 0.7, 0.7)
# Applying butteworth filter
btw_filtered_sign = butterworth_filter(noisy_voice, cutoff, sample_rate, order)
# Applying chebyshev I filter
cheb1_filtered_sign = chebyshev1_filter(noisy_voice, cutoff, sample_rate, order, ripple)
# Applying chebyshev II filter
cheb2_filtered_sign = chebyshev2_filter(noisy_voice, cutoff, sample_rate, order, stop_attenuation)
# Applying eliptic filter
elptc_filtered_sign = eliptic_filter(noisy_voice, cutoff, sample_rate, order, stop_attenuation, ripple)
# Save WAV file
file_name_wav1 = noisy_audio + ".wav"
sf.write(file_name_wav1, noisy_voice, sample_rate)
file_name_wav2 = std_audio + ".wav"
sf.write(file_name_wav2, voice.flatten(), sample_rate)
file_name_wav3 = butterworth_filtred_audio + ".wav"
sf.write(file_name_wav3, btw_filtered_sign, sample_rate)
file_name_wav4 = filtred_audio_chebyshev_I + ".wav"
sf.write(file_name_wav4, cheb1_filtered_sign, sample_rate)
file_name_wav5 = filtred_audio_chebyshev_II + ".wav"
sf.write(file_name_wav5, cheb2_filtered_sign, sample_rate)
file_name_wav6 = filtred_audio_eliptic + ".wav"
sf.write(file_name_wav6, elptc_filtered_sign, sample_rate)
print("""Gravado com sucesso.
Arquivo salvo com sucesso.""")
# Charts plot
t = np.linspace(0, recording_time, num=len(voice))
plt.figure(figsize=(14, 12))
# Original audio file
plt.subplot(6, 1, 1)
plt.plot(t, voice.flatten(), color="purple")
plt.title("Sinal Original")
plt.xlabel("Tempo(s)")
plt.ylabel("Amplitude")
plt.ylim(-0.20, 0.20)
# Noisy audio
plt.subplot(6, 1, 2)
plt.plot(t, noisy_voice, color="lightgreen")
plt.title("Sinal com Ruído")
plt.xlabel("Tempo (s)")
plt.ylabel("Amplitude")
# Butterworth filtered audio
plt.subplot(6, 1, 3)
plt.plot(t, btw_filtered_sign, color="lightblue")
plt.title("Sinal Filtrado - Butterworth")
plt.xlabel("Tempo (s)")
plt.ylabel("Amplitude")
plt.ylim(-0.20, 0.20)
# Chebyshev I filtered audio
plt.subplot(6, 1, 4)
plt.plot(t, cheb1_filtered_sign, color="red")
plt.title("Sinal Filtrado - Chebyshev I")
plt.xlabel("Tempo (s)")
plt.ylabel("Amplitude")
plt.ylim(-0.20, 0.20)
# Chebyshev II filtered audio
plt.subplot(6, 1, 5)
plt.plot(t, cheb2_filtered_sign, color="blue")
plt.title("Sinal Filtrado - Chebyshev II")
plt.xlabel("Tempo (s)")
plt.ylabel("Amplitude")
plt.ylim(-0.20, 0.20)
# Eliptic filtered audio
plt.subplot(6, 1, 6)
plt.plot(t, elptc_filtered_sign, color="pink")
plt.title("Sinal Filtrado - Elíptico")
plt.xlabel("Tempo (s)")
plt.ylabel("Amplitude")
plt.ylim(-0.20, 0.20)
plt.tight_layout()
plt.savefig("plots_IIR.png")
plt.show()