-
Notifications
You must be signed in to change notification settings - Fork 0
/
HW1_ex2_Group14.py
222 lines (83 loc) · 3.22 KB
/
HW1_ex2_Group14.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python
# coding: utf-8
import os
import pathlib
from scipy.io import wavfile
import numpy as np
import tensorflow as tf
import time
import math
from subprocess import Popen
from scipy import signal
Popen('sudo sh -c "echo performance >'
'/sys/devices/system/cpu/cpufreq/policy0/scaling_governor"',
shell=True).wait()
def mfcc(filename, num_mel_bins, mel_lower_frequency, mel_upper_frequency, resample, i, linear_to_mel_weight_matrix = None):
#Resampling
if resample:
rate=8000
input_rate, audio = wavfile.read(filename)
audio = signal.resample_poly(audio, 1, 2)
tf_audio = tf.convert_to_tensor(audio, dtype=tf.float32)
frame_length = 128
frame_step = 64
else:
rate=16000
input_rate, audio = wavfile.read(filename)
tf_audio = tf.convert_to_tensor(audio, dtype=tf.float32)
frame_length = 256
frame_step = 128
#STFT
stft = tf.signal.stft(tf_audio, frame_length, frame_step,
fft_length=frame_length)
spectrogram = tf.abs(stft)
mel_coefficients = 10
if i==0:
num_spectrogram_bins = spectrogram.shape[-1]
linear_to_mel_weight_matrix = tf.signal.linear_to_mel_weight_matrix(
num_mel_bins, num_spectrogram_bins, rate,
mel_lower_frequency, mel_upper_frequency)
mel_spectrogram = tf.tensordot(spectrogram, linear_to_mel_weight_matrix, 1)
log_mel_spectrogram = tf.math.log(mel_spectrogram + 1.e-6)
mfccs = tf.signal.mfccs_from_log_mel_spectrograms(log_mel_spectrogram)[..., :mel_coefficients]
if i==0:
return mfccs, linear_to_mel_weight_matrix
else:
return mfccs
mel_lower_freq = 0
mel_upper_freq = 4000
num_mel_bins = 32
resample = False
data_dir = 'yes_no/'
files_list = os.listdir(data_dir)
MFCCfast_execTime = 0
MFCCslow_execTime = 0
num_files = len(files_list)
SNR = 0
for i, filename in enumerate(files_list):
if i==0:
start = time.time()
mfccSlow, mat1 = mfcc(data_dir+filename, 40, 20, 4000, False, i)
end = time.time()
MFCCslow_execTime += (end-start)
start = time.time()
mfccFast, mat2 = mfcc(data_dir+filename, num_mel_bins, mel_lower_freq, mel_upper_freq, resample, i)
end = time.time()
MFCCfast_execTime += (end-start)
else:
start = time.time()
mfccSlow = mfcc(data_dir+filename, 40, 20, 4000, False, i, mat1)
end = time.time()
MFCCslow_execTime += (end-start)
start = time.time()
mfccFast = mfcc(data_dir+filename, num_mel_bins, mel_lower_freq, mel_upper_freq, resample, i,mat2)
end = time.time()
MFCCfast_execTime += (end-start)
SNR += 20 * math.log10(np.linalg.norm(mfccSlow)/np.linalg.norm(mfccSlow-mfccFast + 1e-6))
MFCCslow_execTime /= num_files
MFCCfast_execTime /= num_files
SNR/= num_files
print("Average time for MFFCs slow: {:.1f} ms".format(MFCCslow_execTime*1000))
print("Average time for MFFCs fast: {:.1f} ms".format(MFCCfast_execTime*1000))
print("SNR: {:.2f} dB".format(SNR))
print("\n\n\n")