-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathddk_updated.py
More file actions
222 lines (151 loc) · 5.58 KB
/
Copy pathddk_updated.py
File metadata and controls
222 lines (151 loc) · 5.58 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
import os
import sys
import numpy as np
import matplotlib.pyplot as plt
import tqdm
from scipy.io.wavfile import read
def log_energy(signal):
new_signal = np.power(signal,2)
sum_new_signal = np.sum(new_signal)
logE = np.log10(np.abs(sum_new_signal))
return logE
def multi_find(s, r):
s_len = len(s)
r_len = len(r)
_complete = []
if s_len < r_len:
n = -1
else:
for i in range(s_len):
# search for r in s until not enough characters are left
if s[i:i + r_len] == r:
_complete.append(i)
else:
i = i + 1
return(_complete)
def praat_f0_decode(fileTxt):
fid=open(fileTxt)
datam=fid.read()
end_line1=multi_find(datam, '\n')
F0=[]
ji=10
while (ji<len(end_line1)-1):
line1=datam[end_line1[ji]+1:end_line1[ji+1]]
cond=(line1=='2' or line1=='3' or line1=='4' or line1=='5' or line1=='6' or line1=='7' or line1=='8' or line1=='9' or line1=='10' or line1=='11' or line1=='12' or line1=='13' or line1=='14' or line1=='15')
if (cond): F0.append(float(datam[end_line1[ji+1]+1:end_line1[ji+2]]))
ji=ji+1
F0=np.asarray(F0)
return F0
def decode_Texgrid(file_textgrid, file_audio, type_segm, win_trans=0.04):
fid=open(file_textgrid)
data=fid.read()
fs, data_audio=read(file_audio)
if (type_segm=='Unvoiced' or type_segm=='Onset'):
pos=multi_find(data, '"U"')
elif (type_segm=='Voiced' or type_segm=='Offset'):
pos=multi_find(data, '"V"')
segments=[]
for j in range(len(pos)):
pos2=multi_find(data[0:pos[j]], '\n')
nP=len(pos2)
inicio=data[pos2[nP-3]:pos2[nP-2]]
fin=data[pos2[nP-2]:pos2[nP-1]]
inicioVal=int(float(inicio)*fs)
finVal=int(float(fin)*fs)-1
if (type_segm=='Onset' or type_segm=='Offset'):
segments.append(data_audio[int(finVal-win_trans*fs):int(finVal+win_trans*fs)])
else:
segments.append(data_audio[inicioVal:finVal])
return segments, fs
def Power(sig):
sig2=np.square(sig)
sumsig2=np.sum(sig2)/len(sig)
return sumsig2
def DDK_Features(fs, data, audio):
#fs,data = read(audio)
#Signal Normalization
data = data-np.mean(data)
data = data/float(np.max(np.abs(data)))
frame_size = 0.02 * fs
hop_size = 0.01 * fs
overlap = hop_size/frame_size
#print('Total number of frames: ', int(len(data)/hop_size))
# F0 Extraction with Praat
path = './'
os.system('./Toolkits/praat F0_Praat.praat '+'./wav/'+audio+' Toolkits/tempF0.txt 75 500 0.02')
F0 = praat_f0_decode('Toolkits/tempF0.txt')
frame_iterator = int((len(data)/frame_size/overlap))-1
logE = []
for i in range(frame_iterator):
frame = data[int(i*hop_size):int(i*hop_size+frame_size)]
logE.append(10.0**log_energy(frame))
logE = np.asarray(logE)
#Energy Features
avg_energy = 10*np.log10(np.mean(logE))
std_energy = 10*np.log10(np.std(logE))
max_energy = 10*np.log10(np.max(logE))
# Fundamental Frequency Features
avg_F0 = np.mean(F0[F0!=0])
std_F0 = np.std(F0[F0!=0])
max_F0 = np.max(F0)
time_step_F0 = 0
F0_min = 75
F0_max = 600
max_vuv_period = 0.02
average_vuv_period = 0.01
# Extract Voiced and Unvoiced features with Praat
os.system('./Toolkits/praat '+'vuv.praat'+' '+'./wav/'+audio+' '+'Toolkits/vuv.txt '
+str(F0_min)+' '+str(F0_max)+' '+str(time_step_F0)
+' '+str(max_vuv_period)+' '+str(average_vuv_period))
voiced_segments, fs = decode_Texgrid('Toolkits/vuv.txt', './wav/'+audio, 'Voiced')
unvoiced_segments, fs = decode_Texgrid('Toolkits/vuv.txt', './wav/'+audio, 'Unvoiced')
nr_voiced = len(voiced_segments)
nr_unvoiced = len(unvoiced_segments)
#DDK Features
DDK_rate = fs*float(nr_voiced)/len(data)
avg_dur_DDK = 1000*np.mean([len(voiced_segments[i])
for i in range(nr_voiced)])/float(fs)
regularity_DDK = 1000*np.std([len(voiced_segments[i])
for i in range(nr_voiced)])/float(fs)
#Silence Features
thr_len_pause = 0.14*float(fs)
thr_en_pause = 0.2
silence = []
for i in range(nr_unvoiced):
eu = log_energy(unvoiced_segments[i])
if (eu<thr_en_pause or len(unvoiced_segments[i])>thr_len_pause):
silence.append(unvoiced_segments[i])
Sil_rate=fs*float(len(silence))/len(data)
if (len(silence)>0):
Sil_avg_dur=1000*np.mean([len(silence[k])
for k in range(len(silence))])/float(fs)
Sil_std_dur=1000*np.std([len(silence[k])
for k in range(len(silence))])/float(fs)
else:
Sil_avg_dur=0.0
Sil_std_dur=0.0
#Encapsulate
feature_array = np.array([filename,avg_F0,std_F0,max_F0,avg_energy,std_energy,max_energy,
DDK_rate,avg_dur_DDK,regularity_DDK,
Sil_rate,Sil_avg_dur,Sil_std_dur])
feature_array = feature_array.reshape(1,len(feature_array))
return feature_array
if __name__=="__main__":
path = "./"
wav_dir = os.path.join(path,'wav')
titles = np.array(['Name','avg_F0','std_F0','max_F0','avg_E','std_E',
'max_E','DDK_rate','avgdur_DDK','reg_DDK',
'Sil_rate','Sil_avg_dur','Sil_std_dur'])
titles = titles.reshape(1,len(titles))
to_file = np.concatenate((titles,), axis=0)
num_files = len([f for f in os.listdir(wav_dir)if os.path.isfile(os.path.join(wav_dir, f))])
bar = tqdm.tqdm(total=num_files,desc='Feature Extraction')
for filename in os.listdir(wav_dir):
if filename.endswith('.wav'):
fs,audio = read(os.path.join(wav_dir,filename))
features = DDK_Features(fs, audio, filename)
#print(features)
to_file = np.concatenate((to_file,features), axis=0)
bar.update(1)
#Save as a .csv file
np.savetxt("DDK_Features.csv", to_file, delimiter=",", fmt='%s')