%% Specify Parameters of Custom Augmentation Method
% In this example, you add a custom augmentation method that applies median filtering to your audio.
% Read in an audio signal and listen to it.

[audioIn,fs] = audioread("Counting-16-44p1-mono-15secs.wav");
sound(audioIn,fs)

%
% Create a random sequential augmenter that adds noise with an SNR range of 5 dB to 10 dB. Set the probability of applying volume control, time stretching, pitch shifting, and time shifting to 0. Set NumAugmentations to 4 to create 4 separate augmentations.

aug = audioDataAugmenter('NumAugmentations',4, ...
    "AddNoiseProbability",1, ...
    "SNRRange",[5,10], ...
    "VolumeControlProbability",0, ...
    "TimeStretchProbability",0, ...
    "TimeShiftProbability",0, ...
    "PitchShiftProbability",0)

% Call addAugmentationMethod with an algorithm name and function handle. Specify the algorithm name as MedianFilter and the function handle as movmedian with a 3-element window length. The augmentation is added to the properties of your audioDataAugmenter object.

algorithmName = 'MedianFilter';
algorithmHandle = @(x)(movmedian(x,100));
addAugmentationMethod(aug,algorithmName,algorithmHandle)

aug

% Set the probability of applying median filtering to 80%.

aug.MedianFilterProbability = 0.8

%
% Call augment on the audio to create 4 augmentations.

data = augment(aug,audioIn,fs);

%
% You can check the parameter configuration of each augmentation using the AugmentationInfo table variable. If median filtering was applied for an augmentation, then AugmentationInfo lists the parameter as 'Applied'. If median filtering was not applied for an augmentation, then AugmentationInfo lists the parameter as 'Bypassed'.

augmentationToInspect = 2;
data.AugmentationInfo(augmentationToInspect)

% Listen to the audio you are inspecting. Plot the time-domain representation of the original and augmented signals.

augmentation = data.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)")

%
% You can specify additional parameters and corresponding parameter ranges (for use when AugmentationParamterSource is set to 'random') and parameter values (for use when AugmentationParamterSource is set to 'specify'). You must specify additional parameters, parameter ranges, and parameter values during your call to addAugmentationMethod.
% Call removeAugmentationMethod to remove the MedianFilter augmentation method. Call addAugmentationMethod again, this time specifying an augmentation parameter, parameter range, and parameter value. The augmentation and parameter range is added to the properties of your audioDataAugmenter object.

removeAugmentationMethod(aug,'MedianFilter')

algorithmName = 'MedianFilter';
augmentationParameter = 'MedianFilterWindowLength';
parameterRange = [1,200];
parameterValue = 100;

algorithmHandle = @(x,k)(movmedian(x,k));
addAugmentationMethod(aug,algorithmName,algorithmHandle, ...
    'AugmentationParameter',augmentationParameter, ...
    'ParameterRange',parameterRange, ...
    'ParameterValue',parameterValue)

aug

% In the current augmentation pipeline configuration, the parameter value is not applicable. ParameterValue is applicable when AugmetnationParameterSource is set to 'specify'. Set AugmentationParameterSource to 'specify' to enable the current parameter value.

aug.AugmentationParameterSource = 'specify'

%
% Set AugmentationParameterSource to random and then call augment.

aug.AugmentationParameterSource = "random";
data = augment(aug,audioIn,fs);

%
% If median filtering was applied for an augmentation, then AugmentationInfo lists the value applied.

augmentationToInspect = 3;
data.AugmentationInfo(augmentationToInspect)

% Listen to the audio you are inspecting. Plot the time-domain representation of the original and augmented signals.

augmentation = data.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)")

% Copyright 2019 The MathWorks, Inc.