%% Extract Low Frequency Subband from Speech
% Use a halfband analysis filter bank and interpolation filter to extract the low frequency subband from a speech signal.
% 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 dsp.AudioFileReader and audioDeviceWriter System objects are not supported in MATLAB Online.
% Set up the audio file reader, the analysis filter bank, the audio device writer, and the interpolation filter. The sampling rate of the audio data is 22050 Hz. The halfband filter has an order of 21 and a transition width of 2 kHz.

afr = dsp.AudioFileReader('speech_dft.mp3','SamplesPerFrame',1024);

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

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

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

ap = audioDeviceWriter('SampleRate',afr.SampleRate);

%
% View the magnitude response of the halfband filter.

fvtool(IIRHalfbandDecim)

%
% Read the speech signal from the audio file in frames of 1024 samples. Filter the speech signal into lowpass and highpass subbands with a halfband frequency of 5512.5 Hz. Reconstruct a lowpass approximation of the speech signal by interpolating the lowpass subband. Play the filtered output.

while ~isDone(afr)
  audioframe = afr();
  xlo = IIRHalfbandDecim(audioframe);
  ylow = IIRHalfbandInterp(xlo);
  ap(ylow);
end

%
% Wait until the audio file ends, and then close the input file and release the audio output resource.

release(afr);           
release(ap);