%% Two-Channel Filter Bank
% Use a halfband decimator and interpolator to implement a two-channel filter 
% bank. This example uses an audio file input and shows that the power spectrum 
% of the filter bank output does not differ significantly from the input.
% 
% *Note*: If you are using R2016a or an earlier release, replace each call 
% to the object with the equivalent step syntax. For example, obj(x) becomes 
% step(obj,x).
% 
% *Note*: The |audioDeviceWriter| System object(TM) is not supported in MATLAB Online.
% 
% Set up the audio file reader and audio device writer. Construct the IIR 
% halfband decimator and interpolator. Finally, set up the spectrum analyzer to 
% display the power spectra of the filter-bank input and output.

AF = dsp.AudioFileReader('speech_dft.mp3','SamplesPerFrame',1024);
AP = audioDeviceWriter('SampleRate',AF.SampleRate);

filterspec = 'Filter order and transition width';
Order = 51;
TW = 2000;

IIRHalfbandDecim = dsp.IIRHalfbandDecimator(...
    'Specification',filterspec,'FilterOrder',Order,...
    'TransitionWidth',TW,'SampleRate',AF.SampleRate);

IIRHalfbandInterp = dsp.IIRHalfbandInterpolator(...
    'Specification',filterspec,'FilterOrder',Order,...
    'TransitionWidth',TW,'SampleRate',AF.SampleRate/2,...
    'FilterBankInputPort',true);

SpecAna = dsp.SpectrumAnalyzer('SampleRate',AF.SampleRate,...
    'PlotAsTwoSidedSpectrum',false,'ReducePlotRate',false,...
    'ShowLegend',true,...
    'ChannelNames',{'Input signal','Filtered output signal'});
%% 
% Read the audio 1024 samples at a time. Filter the input to obtain the 
% lowpass and highpass subband signals decimated by a factor of two. This is the 
% analysis filter bank. Use the halfband interpolator as the synthesis filter 
% bank. Display the running power spectrum of the audio input and the output of 
% the synthesis filter bank. Play the output.
%%
while ~isDone(AF)
    audioInput = AF();
    [xlo,xhigh] = IIRHalfbandDecim(audioInput);
    audioOutput = IIRHalfbandInterp(xlo,xhigh);
    spectrumInput = [audioInput audioOutput];
    SpecAna(spectrumInput);
    AP(audioOutput);
end

release(AF);
release(AP);
release(SpecAna);
%%
% Copyright 2015 The MathWorks, Inc.