-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadTrainingData.m
More file actions
189 lines (146 loc) · 7.71 KB
/
LoadTrainingData.m
File metadata and controls
189 lines (146 loc) · 7.71 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
%% EECS351 load training data
% audioNormalization_YW courtesy Yi-Wen Chen
% https://www.mathworks.com/matlabcentral/fileexchange/69958-audio-normalization-by-matlab
clear;
close all;
window=round(0.04*48000);
overlap=round(0.025*48000);
energyThreshold = 0.1;
zcrThreshold = 5;
cd EN\; %change folder to english training clips folder
ENfiles = dir('**/*.mp3'); %specify which files to read
trainLabels={}; %empty labels for EN, CN training data
i=1;
trainindex = 1;
for file = ENfiles'
temp=audioNormalization_YW(audioread(strcat(ENfiles(i).folder, ...
'\',file.name)),0.5); %normalize input audio
if(length(temp) >= 200000)
temp=temp(1:200000,:);
trainLabels(trainindex,:)={'EN'}; %assign label to all english training data
[mfcctemp,deltatemp,deltadeltatemp]=mfcc(temp,48000); %get mfcc's, deltas, deltadeltas
mfcctemp(1:25,:)=[]; %zero first 20 samples to account for weird delta spike
deltatemp(1:25,:)=[];
ftemp=pitch(temp,48000,WindowLength=window,OverlapLength=overlap);
%extract voiced segments courtesy MATLAB
%https://www.mathworks.com/help/audio/ug/speaker-identification-using-pitch-and-mfcc.html
[segments,~] = buffer(temp,window,overlap,"nodelay");
ste = sum((segments.*hamming(window,"periodic")).^2,1);
isSpeech = ste(:) > energyThreshold;
zcr = zerocrossrate(temp,WindowLength=window,OverlapLength=overlap);
isVoiced = zcr < zcrThreshold;
voicedSpeech = isSpeech & isVoiced;
ftemp(~voicedSpeech) = [];
%smoothing
inrange = (25 < ftemp) & (ftemp < 375);
removeSaturation = ftemp(inrange);
removeSaturation(1:round(length(ftemp)/10))=[];
instdrange = (removeSaturation > mean(removeSaturation)-2*std(removeSaturation)) & ...
(removeSaturation < mean(removeSaturation)+2*std(removeSaturation));
removeOutliers = removeSaturation(instdrange);
smooth=movmean(removeOutliers,5,'Endpoints','discard');
%smooth=lowpass(smooth,8000,48000);
%feature calculation
delta=diff(smooth);
p2p=peak2peak(smooth);
avgdelta = mean(abs(delta));
meddelta = median(abs(delta));
dft=abs(fft(smooth));
scalarfeatures = [p2p avgdelta meddelta];
mfcc1d=reshape(mfcctemp.',1,[]); %resize array to 1d for training
mfcc1d=[mfcc1d scalarfeatures];
mfccTrain(1:length(mfcc1d),trainindex)=mfcc1d(:); %add array to train array
trainindex=trainindex+1;
end
i=i+1;
end
cd '..\CN\' %change folder to chinese training clips
CNfiles = dir('**\*.mp3'); %specify files to read
for file = CNfiles'
temp=audioNormalization_YW(audioread(strcat(CNfiles(i-length(ENfiles)).folder ...
,'\',file.name)),0.5); %normalize audio
if(length(temp) >= 200000)
temp=temp(1:200000,:);
trainLabels(trainindex,:)={'CN'}; %assign label to all english training data
[mfcctemp,deltatemp,deltadeltatemp]=mfcc(temp,48000); %get mfcc's, deltas, deltadeltas
mfcctemp(1:25,:)=[]; %zero first 20 samples to account for weird delta spike
deltatemp(1:25,:)=[];
ftemp=pitch(temp,48000,WindowLength=window,OverlapLength=overlap);
%extract voiced segments courtesy MATLAB
%https://www.mathworks.com/help/audio/ug/speaker-identification-using-pitch-and-mfcc.html
[segments,~] = buffer(temp,window,overlap,"nodelay");
ste = sum((segments.*hamming(window,"periodic")).^2,1);
isSpeech = ste(:) > energyThreshold;
zcr = zerocrossrate(temp,WindowLength=window,OverlapLength=overlap);
isVoiced = zcr < zcrThreshold;
voicedSpeech = isSpeech & isVoiced;
ftemp(~voicedSpeech) = [];
%smoothing
inrange = (25 < ftemp) & (ftemp < 375);
removeSaturation = ftemp(inrange);
removeSaturation(1:round(length(ftemp)/10))=[];
instdrange = (removeSaturation > mean(removeSaturation)-2*std(removeSaturation)) & ...
(removeSaturation < mean(removeSaturation)+2*std(removeSaturation));
removeOutliers = removeSaturation(instdrange);
smooth=movmean(removeOutliers,5,'Endpoints','discard');
%smooth=lowpass(smooth,8000,48000);
%feature calculation
delta=diff(smooth);
p2p=peak2peak(smooth);
avgdelta = mean(abs(delta));
meddelta = median(abs(delta));
dft=abs(fft(smooth));
scalarfeatures = [p2p avgdelta meddelta];
mfcc1d=reshape(mfcctemp.',1,[]); %resize array to 1d for training
mfcc1d=[mfcc1d scalarfeatures];
mfccTrain(1:length(mfcc1d),trainindex)=mfcc1d(:); %add array to train array
trainindex=trainindex+1;
end
i=i+1;
end
cd '..\HN\'
HNfiles = dir('**\*.mp3');
for file = HNfiles' %repeat for hindi
temp=audioNormalization_YW(audioread(strcat(HNfiles(i-length(ENfiles) ...
-length(CNfiles)).folder,'\',file.name)),0.5);
temp=resample(temp,3,2);
if(length(temp) >= 200000)
temp=temp(1:200000,:);
trainLabels(trainindex,:)={'HN'}; %assign label to all english training data
[mfcctemp,deltatemp,deltadeltatemp]=mfcc(temp,48000); %get mfcc's, deltas, deltadeltas
mfcctemp(1:25,:)=[]; %zero first 20 samples to account for weird delta spike
deltatemp(1:25,:)=[];
ftemp=pitch(temp,48000,WindowLength=window,OverlapLength=overlap);
%extract voiced segments courtesy MATLAB
%https://www.mathworks.com/help/audio/ug/speaker-identification-using-pitch-and-mfcc.html
[segments,~] = buffer(temp,window,overlap,"nodelay");
ste = sum((segments.*hamming(window,"periodic")).^2,1);
isSpeech = ste(:) > energyThreshold;
zcr = zerocrossrate(temp,WindowLength=window,OverlapLength=overlap);
isVoiced = zcr < zcrThreshold;
voicedSpeech = isSpeech & isVoiced;
ftemp(~voicedSpeech) = [];
%smoothing
inrange = (25 < ftemp) & (ftemp < 375);
removeSaturation = ftemp(inrange);
removeSaturation(1:round(length(ftemp)/10))=[];
instdrange = (removeSaturation > mean(removeSaturation)-2*std(removeSaturation)) & ...
(removeSaturation < mean(removeSaturation)+2*std(removeSaturation));
removeOutliers = removeSaturation(instdrange);
smooth=movmean(removeOutliers,5,'Endpoints','discard');
%smooth=lowpass(smooth,8000,48000);
%feature calculation
delta=diff(smooth);
p2p=peak2peak(smooth);
avgdelta = mean(abs(delta));
meddelta = median(abs(delta));
dft=abs(fft(smooth));
scalarfeatures = [p2p avgdelta meddelta];
mfcc1d=reshape(mfcctemp.',1,[]); %resize array to 1d for training
mfcc1d=[mfcc1d scalarfeatures];
mfccTrain(1:length(mfcc1d),trainindex)=mfcc1d(:); %add array to train array
trainindex=trainindex+1;
end
i=i+1;
end
mfccTrain=mfccTrain.'; %transpose matrix for training