-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataAugmentation.m
More file actions
460 lines (386 loc) · 17.7 KB
/
DataAugmentation.m
File metadata and controls
460 lines (386 loc) · 17.7 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
% Fish Bombing Detection FYP; DATA AUGMENTATION
% Brendon Soong, 11/2/2021
clc
%% (1) Random Sequential Augmentations (StretchTime, ShiftPitch, ControlVolume, AddNoise, ShiftTime)
% Read in an audio signal
[audioIn,fs] = audioread("AudioFishBomb.wav");
% Create an audioDataAugmenter object
augmenter1 = audioDataAugmenter( ...
"AugmentationMode","sequential", ...
"NumAugmentations",5000, ...
...
"TimeStretchProbability",0.8, ...
"SpeedupFactorRange", [1.3,1.4], ...
...
"PitchShiftProbability",0.3, ...
...
"VolumeControlProbability",0.8, ...
"VolumeGainRange",[-5,5], ...
...
"AddNoiseProbability",0.7, ...
...
"TimeShiftProbability",0.8, ...
"TimeShiftRange", [-500e-3,500e-3]);
% Call augment on the audio to create 5 augmentations. The augmented audio is returned in a table with variables Audio and AugmentationInfo. The number of rows in the table is defined by NumAugmentations.
data1 = augment(augmenter1,audioIn,fs);
% In the current augmentation pipeline, augmentation parameters are assigned randomly from within the specified ranges. To determine the exact parameters used for an augmentation, inspect AugmentationInfo.
augmentationToInspect = 4;
% data.AugmentationInfo(augmentationToInspect)
% Listen to the augmentation you are inspecting. Plot time representation of the original and augmented signals.
augmentation = data1.Audio{augmentationToInspect};
sound(augmentation,fs)
t = (0:(numel(audioIn)-1))/fs;
taug = (0:(numel(augmentation)-1))/fs;
plot(t,audioIn,taug,augmentation)
legend("Original Audio","Augmented Audio")
ylabel("Amplitude")
xlabel("Time (s)")
% Create a new folder in your current folder to hold the augmented data set.
currentDir = pwd;
writeDirectory = fullfile(currentDir,'RandomSequentialAugmentation');
mkdir(writeDirectory)
% Normalize the audio to have a max absolute value of 1.
for i = 1:size(data1,1)
augmentedAudio = data1.Audio{i};
augmentedAudio = augmentedAudio/max(abs(augmentedAudio),[],'all');
augNum = num2str(i);
if numel(augNum)==1
iString = ['0',augNum];
else
iString = augNum;
end
audiowrite(fullfile(writeDirectory,sprintf('%s_RSA_%s.wav',"ReefCheck",iString)),augmentedAudio,fs);
fprintf("%d",i)
end
% ADD REMOVING NaN values here!!!!
%% (2) Specified Sequential Augmentations
% Read in an audio signal
[audioIn,fs] = audioread("AudioFishBomb.wav");
% Create an audioDataAugmenter object
augmenter2 = audioDataAugmenter( ...
"AugmentationMode","sequential", ...
"AugmentationParameterSource","specify", ...
"NumAugmentations",5000, ... % 5 Augmentations
"SpeedupFactor",[0.9,1.1,1.2], ...
"ApplyTimeStretch",true, ...
"ApplyPitchShift",true, ...
"SemitoneShift",[-2,-1,1,2], ...
"SNR",[10,15], ...
"ApplyVolumeControl",false, ...
"ApplyTimeShift",false);
% Call augment on the audio to create 5 augmentations. The augmented audio is returned in a table with variables Audio and AugmentationInfo. The number of rows in the table is defined by NumAugmentations.
data2 = augment(augmenter2,audioIn,fs);
% In the current augmentation pipeline, augmentation parameters are assigned randomly from within the specified ranges. To determine the exact parameters used for an augmentation, inspect AugmentationInfo.
augmentationToInspect = 4;
% data.AugmentationInfo(augmentationToInspect)
% Listen to the augmentation you are inspecting. Plot time representation of the original and augmented signals.
augmentation = data2.Audio{augmentationToInspect};
sound(augmentation,fs)
t = (0:(numel(audioIn)-1))/fs;
taug = (0:(numel(augmentation)-1))/fs;
plot(t,audioIn,taug,augmentation)
legend("Original Audio","Augmented Audio")
ylabel("Amplitude")
xlabel("Time (s)")
% Create a new folder in your current folder to hold the augmented data set.
currentDir = pwd;
writeDirectory = fullfile(currentDir,'SpecifiedSequentialAugmentation');
mkdir(writeDirectory)
% Normalize the audio to have a max absolute value of 1.
for i = 1:size(data2,1)
augmentedAudio = data2.Audio{i};
augmentedAudio = augmentedAudio/max(abs(augmentedAudio),[],'all');
augNum = num2str(i);
if numel(augNum)==1
iString = ['0',augNum];
else
iString = augNum;
end
audiowrite(fullfile(writeDirectory,sprintf('%s_SSA_%s.wav',"ReefCheck",iString)),augmentedAudio,fs);
end
%% (3) Random Independent Augmentations
% Read in an audio signal
[audioIn,fs] = audioread("AudioFishBomb.wav");
% Create an audioDataAugmenter object
augmenter3 = audioDataAugmenter( ...
"AugmentationMode","independent", ...
"AugmentationParameterSource","random", ...
"NumAugmentations",5000, ... % 5 Augmentations
"ApplyTimeStretch",false, ...
"ApplyPitchShift",false, ...
"ApplyVolumeControl",false, ...
"SNRRange",[0,20], ...
"TimeShiftRange",[-300e-3,300e-3]);
% Call augment on the audio to create 5 augmentations. The augmented audio is returned in a table with variables Audio and AugmentationInfo. The number of rows in the table is defined by NumAugmentations.
data3 = augment(augmenter3,audioIn,fs);
% In the current augmentation pipeline, augmentation parameters are assigned randomly from within the specified ranges. To determine the exact parameters used for an augmentation, inspect AugmentationInfo.
augmentationToInspect = 4;
% data.AugmentationInfo(augmentationToInspect)
% Listen to the augmentation you are inspecting. Plot time representation of the original and augmented signals.
augmentation = data3.Audio{augmentationToInspect};
sound(augmentation,fs)
t = (0:(numel(audioIn)-1))/fs;
taug = (0:(numel(augmentation)-1))/fs;
plot(t,audioIn,taug,augmentation)
legend("Original Audio","Augmented Audio")
ylabel("Amplitude")
xlabel("Time (s)")
% Create a new folder in your current folder to hold the augmented data set.
currentDir = pwd;
writeDirectory = fullfile(currentDir,'RandomIndependentAugmentation');
mkdir(writeDirectory)
% Normalize the audio to have a max absolute value of 1.
for i = 1:size(data3,1)
augmentedAudio = data3.Audio{i};
augmentedAudio = augmentedAudio/max(abs(augmentedAudio),[],'all');
augNum = num2str(i);
if numel(augNum)==1
iString = ['0',augNum];
else
iString = augNum;
end
audiowrite(fullfile(writeDirectory,sprintf('%s_RIA_%s.wav',"ReefCheck",iString)),augmentedAudio,fs);
end
%% (4) Specified Independent Augmentations
% Read in an audio signal
[audioIn,fs] = audioread("AudioFishBomb.wav");
% Create an audioDataAugmenter object
augmenter4 = audioDataAugmenter( ...
"AugmentationMode","independent", ...
"AugmentationParameterSource","specify", ...
"NumAugmentations",5000, ... % 15 Augmentations
"ApplyTimeStretch",false, ...
"ApplyPitchShift",false, ...
"VolumeGain", [1:10], ...
"SNR",[1:5], ...
"TimeShift",2);
% Call augment on the audio to create 5 augmentations. The augmented audio is returned in a table with variables Audio and AugmentationInfo. The number of rows in the table is defined by NumAugmentations.
data4 = augment(augmenter4,audioIn,fs);
% In the current augmentation pipeline, augmentation parameters are assigned randomly from within the specified ranges. To determine the exact parameters used for an augmentation, inspect AugmentationInfo.
augmentationToInspect = 15;
% data.AugmentationInfo(augmentationToInspect)
% Listen to the augmentation you are inspecting. Plot time representation of the original and augmented signals.
%augmentation = data4.Audio{augmentationToInspect};
%sound(augmentation,fs)
t = (0:(numel(audioIn)-1))/fs;
taug = (0:(numel(augmentation)-1))/fs;
plot(t,audioIn,taug,augmentation)
legend("Original Audio","Augmented Audio")
ylabel("Amplitude")
xlabel("Time (s)")
% Create a new folder in your current folder to hold the augmented data set.
currentDir = pwd;
writeDirectory = fullfile(currentDir,'SpecifiedIndependentAugmentation');
mkdir(writeDirectory)
% Normalize the audio to have a max absolute value of 1.
for i = 1:size(data4,1)
augmentedAudio = data4.Audio{i};
augmentedAudio = augmentedAudio/max(abs(augmentedAudio),[],'all');
augNum = num2str(i);
if numel(augNum)==1
iString = ['0',augNum];
else
iString = augNum;
end
audiowrite(fullfile(writeDirectory,sprintf('%s_SIA_%s.wav',"ReefCheck",iString)),augmentedAudio,fs);
end
%% (5) GENEREATE WHITE NOISE FOR TESTING NEURAL NETWORK
adsTrain = audioDatastore('C:\Users\soong\OneDrive\Desktop\DEEP LEARNING FYP\Training Data\explosion', 'IncludeSubfolders', false,'LabelSource', 'foldernames','FileExtensions','.wav');
fs = 24000;
duration = 1.6;
N = duration*fs;
% Generate noise
wNoise = 2*rand([N,100]) - 1; % White noise
bNoise = filter(1,[1,-0.999],wNoise); % Brown noise
bNoise = bNoise./max(abs(bNoise),[],'all');
pNoise = pinknoise([N,100]); % Pink noise
reduced_wNoise =0.015*wNoise;
reduced_bNoise =0.015*bNoise;
reduced_pNoise =0.015*pNoise;
figure
plot(wNoise(:,1))
hold on
plot(reduced_wNoise(:,1))
hold on
plot(Train(:,1))
title("Plot of White Noise, Reduced White Noise (0.015), Explosion Audio")
%% Write noise files to deep learning folder
currentDir = pwd;
writeDirectory = fullfile(currentDir,'ReducTesting Noise_not trunc');
mkdir(writeDirectory)
for i = 1:100
audiowrite(fullfile(writeDirectory,sprintf('ReducWhiteNoise_%d.wav',i)),reduced_wNoise(:,i),fs);
audiowrite(fullfile(writeDirectory,sprintf('ReducBrownNoise_%d.wav',i)),reduced_bNoise(:,i),fs);
audiowrite(fullfile(writeDirectory,sprintf('ReducPinkNoise_%d.wav',i)),reduced_pNoise(:,i),fs);
fprintf("%d \n",i)
end
%% (6) Re-Augmenting Reef Check Audio but just changing the amplitude, dont add noise (Specified Independent Augmentations)
% TRAINING AUDIO
% Read in an audio signal
[audioIn,fs] = audioread("AudioFishBomb.wav");
% Create an audioDataAugmenter object
augmenter6 = audioDataAugmenter( ...
"AugmentationMode","independent", ...
"AugmentationParameterSource","random", ...
"NumAugmentations",10000, ...
"TimeStretchProbability", 0, ...
"SpeedupFactorRange", [0 0], ...
"ApplyTimeStretch", false, ...
"SpeedupFactor", 0, ...
"PitchShiftProbability", 0, ...
"SemitoneShiftRange", [0 0], ...
"ApplyPitchShift", false, ...
"SemitoneShift", 0, ...
"VolumeControlProbability" ,1, ... %
"VolumeGainRange", [-10, -1], ... % "VolumeGain", [40], ...
"ApplyVolumeControl", true, ...
"AddNoiseProbability", 0, ...
"SNRRange", [0 0], ...
"ApplyAddNoise", false, ...
"SNR", 0, ...
"TimeShiftProbability", 0, ...
"TimeShiftRange", [0 0], ...
"ApplyTimeShift", false, ...
"TimeShift", 0);
% Call augment on the audio to create 5 augmentations. The augmented audio is returned in a table with variables Audio and AugmentationInfo. The number of rows in the table is defined by NumAugmentations.
data6 = augment(augmenter6,audioIn,fs);
% In the current augmentation pipeline, augmentation parameters are assigned randomly from within the specified ranges. To determine the exact parameters used for an augmentation, inspect AugmentationInfo.
augmentationToInspect = 1000;
% Listen to the augmentation you are inspecting. Plot time representation of the original and augmented signals.
augmentation6 = data6.Audio{augmentationToInspect};
sound(augmentation6,fs)
t = (0:(numel(audioIn)-1))/fs;
taug = (0:(numel(augmentation6)-1))/fs;
plot(t,audioIn,taug,augmentation6)
legend("Original Audio","Augmented Audio")
ylabel("Amplitude")
xlabel("Time (s)")
% Create a new folder in your current folder to hold the augmented data set.
currentDir = pwd;
writeDirectory = fullfile(currentDir,'Explosion');
mkdir(writeDirectory)
% Normalize the audio to have a max absolute value of 1.
for i = 1:size(data6,1)
augmentedAudio = data6.Audio{i};
augmentedAudio = augmentedAudio/max(abs(augmentedAudio),[],'all');
augNum = num2str(i);
if numel(augNum)==1
iString = ['0',augNum];
else
iString = augNum;
end
audiowrite(fullfile(writeDirectory,sprintf('%s_SIA_%s.wav',"Trial4Train",iString)),augmentedAudio,fs);
end
%% (7) Re-Augmenting Reef Check Audio but just changing the amplitude, dont add noise (Specified Independent Augmentations)
% TESTING AUDIO
% Read in an audio signal
[audioIn,fs] = audioread("AudioFishBomb.wav");
% Create an audioDataAugmenter object
augmenter7 = audioDataAugmenter( ...
"AugmentationMode","independent", ...
"AugmentationParameterSource","random", ...
"NumAugmentations",5000, ...
"TimeStretchProbability", 0, ...
"SpeedupFactorRange", [0 0], ...
"ApplyTimeStretch", false, ...
"SpeedupFactor", 0, ...
"PitchShiftProbability", 0, ...
"SemitoneShiftRange", [0 0], ...
"ApplyPitchShift", false, ...
"SemitoneShift", 0, ...
"VolumeControlProbability" ,1, ... %
"VolumeGainRange", [-20, -1], ... % "VolumeGain", [40], ...
"ApplyVolumeControl", true, ...
"AddNoiseProbability", 0, ...
"SNRRange", [0 0], ...
"ApplyAddNoise", false, ...
"SNR", 0, ...
"TimeShiftProbability", 0, ...
"TimeShiftRange", [0 0], ...
"ApplyTimeShift", false, ...
"TimeShift", 0);
% Call augment on the audio to create 5 augmentations. The augmented audio is returned in a table with variables Audio and AugmentationInfo. The number of rows in the table is defined by NumAugmentations.
data7 = augment(augmenter7,audioIn,fs);
% In the current augmentation pipeline, augmentation parameters are assigned randomly from within the specified ranges. To determine the exact parameters used for an augmentation, inspect AugmentationInfo.
augmentationToInspect = 500;
% Listen to the augmentation you are inspecting. Plot time representation of the original and augmented signals.
augmentation7 = data7.Audio{augmentationToInspect};
sound(augmentation7,fs)
t = (0:(numel(audioIn)-1))/fs;
taug = (0:(numel(augmentation7)-1))/fs;
plot(t,audioIn,taug,augmentation7)
legend("Original Audio","Augmented Audio")
ylabel("Amplitude")
xlabel("Time (s)")
% Create a new folder in your current folder to hold the augmented data set.
currentDir = pwd;
writeDirectory = fullfile(currentDir,'Explosion');
mkdir(writeDirectory)
% Normalize the audio to have a max absolute value of 1.
for i = 1:size(data7,1)
augmentedAudio = data7.Audio{i};
augmentedAudio = augmentedAudio/max(abs(augmentedAudio),[],'all');
augNum = num2str(i);
if numel(augNum)==1
iString = ['0',augNum];
else
iString = augNum;
end
audiowrite(fullfile(writeDirectory,sprintf('%s_SIA_%s.wav',"Trial4Val",iString)),augmentedAudio,fs);
end
%% (8) Augmenting UW Noise (TRAIN VolumeGainRange = -10 -1, Val = -15 -10
% Read in an audio signal
[audioIn,fs] = audioread("WindTurbine_Trunc.wav");
% Create an audioDataAugmenter object
augmenter8 = audioDataAugmenter( ...
"AugmentationMode","independent", ...
"AugmentationParameterSource","random", ...
"NumAugmentations",50, ...
"TimeStretchProbability", 0, ...
"SpeedupFactorRange", [0 0], ...
"ApplyTimeStretch", false, ...
"SpeedupFactor", 0, ...
"PitchShiftProbability", 0, ...
"SemitoneShiftRange", [0 0], ...
"ApplyPitchShift", false, ...
"SemitoneShift", 0, ...
"VolumeControlProbability" ,1, ... %
"VolumeGainRange", [-15, -10], ... % "VolumeGain", [40], ...
"ApplyVolumeControl", true, ...
"AddNoiseProbability", 0, ...
"SNRRange", [0 0], ...
"ApplyAddNoise", false, ...
"SNR", 0, ...
"TimeShiftProbability", 0, ...
"TimeShiftRange", [0 0], ...
"ApplyTimeShift", false, ...
"TimeShift", 0);
% Call augment on the audio to create 5 augmentations. The augmented audio is returned in a table with variables Audio and AugmentationInfo. The number of rows in the table is defined by NumAugmentations.
data8 = augment(augmenter8,audioIn,fs);
% In the current augmentation pipeline, augmentation parameters are assigned randomly from within the specified ranges. To determine the exact parameters used for an augmentation, inspect AugmentationInfo.
augmentationToInspect = 10;
% Listen to the augmentation you are inspecting. Plot time representation of the original and augmented signals.
augmentation8 = data8.Audio{augmentationToInspect};
sound(augmentation8,fs)
t = (0:(numel(audioIn)-1))/fs;
taug = (0:(numel(augmentation8)-1))/fs;
plot(t,audioIn,taug,augmentation8)
legend("Original Audio","Augmented Audio")
ylabel("Amplitude")
xlabel("Time (s)")
% Create a new folder in your current folder to hold the augmented data set.
currentDir = pwd;
writeDirectory = fullfile(currentDir,'Truncated_Augmented_Val');
mkdir(writeDirectory)
% Normalize the audio to have a max absolute value of 1.
for i = 1:size(data8,1)
augmentedAudio = data8.Audio{i};
augmentedAudio = augmentedAudio/max(abs(augmentedAudio),[],'all');
augNum = num2str(i);
if numel(augNum)==1
iString = ['0',augNum];
else
iString = augNum;
end
audiowrite(fullfile(writeDirectory,sprintf('%s_AugVal_%s.wav',"WindTurbine",iString)),augmentedAudio,fs);
end