-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloaddat.m
More file actions
211 lines (168 loc) · 5.97 KB
/
Copy pathloaddat.m
File metadata and controls
211 lines (168 loc) · 5.97 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
function [ x, y, Fs ] = loaddat( folds, dataset_dir, features, da, DA_SNR )
if ~exist('features', 'var')
features = 1;
end
if ~exist('da', 'var')
da = 0;
end
if ~exist('DA_SNR', 'var') %% SNR for data augmentation
DA_SNR = 30;
end
recompute_all = false;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~exist('Gget_desc_onefile')
addpath('./timbretoolbox')
addpath('./timbretoolbox/_tools')
addpath('./timbretoolbox/_tools_sf')
addpath('./timbretoolbox/classes')
end
fs_target = 44100;
% === PARAMETERS
do_s.b_TEE = 1; % descriptors from the Temporal Energy Envelope
do_s.b_STFTmag = 1; % descriptors from the STFT magnitude
do_s.b_STFTpow = 1; % descriptors from the STFT power
do_s.b_Harmonic = 1; % descriptors from Harmonic Sinusoidal Modeling representation
do_s.b_ERBfft = 1; % descriptors from ERB representation (ERB being computed using FFT)
do_s.b_ERBgam = 1; % descriptors from ERB representation (ERB being computed using Gamma Tone Filter)
config_s.xcorr_nb_coeff = 12; % === defines the number of auto-correlation coefficients that will be sued
config_s.threshold_harmo= 0.3; % === defines the threshold [0,1] below which harmonic-features are not computed
config_s.nb_harmo = 20; % === defines the number of harmonics that will be extracted
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
da = round(da);
x = [];
y = [];
index = 0;
d = dir(dataset_dir);
for i = 1:length(d)
if d(i).name(1) == '.' || length(d(i).name) < 5
continue;
end
if ~strcmpi(d(i).name(end-2:end), 'wav')
continue;
end
fname = d(i).name(1:end-4);
tmp = regexpi(fname, '_', 'split');
fn = sprintf('%s_%s', tmp{1}, tmp{2});
%fprintf(1, '%s\n', fn);
%isFold( fn, folds )
%% check if is in fold
if ~isFold( fn, folds )
%fprintf(1, 'skip %s...\n', fn);
continue;
end
index = index+1;
if ~isempty(regexpi(fname, 'toot', 'match'))
y(index) = 1; %% toot
else
y(index) = 2; %% quack
end
[s, Fs] = audioread(sprintf('%s/%s', dataset_dir, d(i).name));
%normalize signal
s = s-mean(s);
s = s / (1.2*max(abs(s)));
if features == 1 %% signal
feats = s;
x(index,:) = feats;
elseif features == 2 %% MFCC
%[s, Fs] = audioread(sprintf('%s/%s', dataset_dir, d(i).name));
[ mfcc, t ] = my_mfcc( s, Fs );
feats = mfcc(:,1:end).'; %reshape(mfcc.', [1 numelem(mfcc)]);
x(index,:,:) = feats;
elseif features == 3 %% STFT
M = 1025;
H = 512;
w = hann(M, 'periodic');
rec = 2;
%[s, Fs] = audioread(sprintf('%s/%s', dataset_dir, d(i).name));
[ Sw, ~, ~] = my_stft(s, w, rec);
feats = abs(Sw(1:H,:)).^0.5;
[nf, nt] = size(feats);
x(index,:,:) = feats;
elseif features == 4 %% TTB
%%% load file to improve speed
ttbfile = sprintf('%s/ttb/%s_ttb.mat', dataset_dir, fname);
if exist(ttbfile, 'file') && ~recompute_all
tmp = load(ttbfile);
feats = tmp.ttb_vec;
else
warning('off')
%% TTB
wav_file = './tmpfile2.wav';
audiowrite(wav_file, s, Fs);
if (Fs ~= fs_target) % invalid sampling rate
wav_file2 = './tmpfile.wav';
system(sprintf('sox "%s" -r %d "%s"', wav_file, fs_target, wav_file2));
%fprintf(1, 'resampling %s...\n', wav_file);
wav_file = wav_file2;
end
desc_tmp = Gget_desc_onefile(wav_file,do_s, config_s);
modeling_tmp = Gget_temporalmodeling_onefile(desc_tmp);
ttb_vec = real(struct2array(modeling_tmp));
feature_name = fieldnames(modeling_tmp);
save(ttbfile, 'ttb_vec', 'feature_name', 'Fs');
feats = ttb_vec;
warning('on')
end
x(index,:) = feats;
end
%%%%%%%%%%% DATA AUGMENTATION
if da > 0
%fprintf(1, 'data augmentation...\n')
lbl = y(index); %% same label
%% ref signal
s0 = s;
for ii = 1:da
%% add noise
s = sigmerge(s0, randn(size(s)), DA_SNR);
%% random circshift
s = circshift(s, randi(length(s)));
%normalize signal
s = s-mean(s);
s = s / (1.2*max(abs(s)));
if features == 1 %% signal
feats = s;
x(index,:) = feats;
elseif features == 2 %% MFCC
[ mfcc, t ] = my_mfcc( s, Fs );
feats = mfcc(:,1:end).'; %reshape(mfcc.', [1 numelem(mfcc)]);
x(index,:,:) = feats;
elseif features == 3 %% STFT
M = 1025;
H = 512;
w = hann(M, 'periodic');
rec = 2;
[ Sw, ~, ~] = my_stft(s, w, rec);
feats = abs(Sw(1:H,:)).^0.5;
[nf, nt] = size(feats);
x(index,:,:) = feats;
elseif features == 4 %% TTB
%%% load file to improve speed
ttbfile = sprintf('%s/ttb/%s_ttb_d%d.mat', dataset_dir, fname, ii);
if exist(ttbfile, 'file') && ~recompute_all
tmp = load(ttbfile);
feats = tmp.ttb_vec;
else
warning('off')
%% TTB
wav_file = './tmpfile2.wav';
audiowrite(wav_file, s, Fs);
if (Fs ~= fs_target) % invalid sampling rate
wav_file2 = './tmpfile.wav';
system(sprintf('sox "%s" -r %d "%s"', wav_file, fs_target, wav_file2));
%fprintf(1, 'resampling %s...\n', wav_file);
wav_file = wav_file2;
end
desc_tmp = Gget_desc_onefile(wav_file,do_s, config_s);
modeling_tmp = Gget_temporalmodeling_onefile(desc_tmp);
ttb_vec = real(struct2array(modeling_tmp));
feature_name = fieldnames(modeling_tmp);
save(ttbfile, 'ttb_vec', 'feature_name', 'Fs');
feats = ttb_vec;
warning('on')
end
x(index,:) = feats;
end % features
end %% da
end
end
end