%% Remove Noise from Tone Scale
% Remove additive noise from an audio tone scale using an octaveFilter.
% Create audioOscillator and audioDeviceWriter objects with default properties. Create an octaveFilter object with the center frequency set to 100 Hz.

osc = audioOscillator;
deviceWriter = audioDeviceWriter;
octFilt = octaveFilter(100);

%
% In an audio stream loop, listen to a tone created by your audio oscillator. The tone contains additive Gaussian noise.

for i = 1:400
    x = osc();
    x1 = x + 0.1*randn(512,1);
    deviceWriter(x1);
    if rem(i,100)==0
        osc.Frequency = osc.Frequency*2;
    end
end

%
% Create a spectrum analyzer to view your filtered and unfiltered signals.

scope = dsp.SpectrumAnalyzer( ...
    'PlotAsTwoSidedSpectrum',false, ...
    'FrequencyScale','Log', ...
    'FrequencyResolutionMethod','WindowLength', ...
    'Title','Octave-Band Filtering', ...
    'ShowLegend',true, ...
    'SpectralAverages',10, ...
    'ChannelNames',{'Original signal','Filtered signal'});

%
% Reset the frequency of your audio oscillator to its default, 100 Hz.

osc.Frequency = 100;

%
% In an audio stream loop, filter the corrupted tone using your octave-band filter. When the tone changes frequency in the loop, change the center frequency of your octave filter to match. As a best practice, release your audio device once done.

for i = 1:400
    x = osc();
    x1 = x + 0.1*randn(512,1);
    x2 = octFilt(x1);
    deviceWriter(x2);
    if rem(i,100)==0
        osc.Frequency = osc.Frequency*2;
        octFilt.CenterFrequency = octFilt.CenterFrequency*2;
    end
    scope([x1,x2])
end

release(deviceWriter)
release(scope)

% Copyright 2012 The MathWorks, Inc.