%% 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, audio device writer, and interpolation filter. The sampling rate of the audio data is 22050 Hz. The order of the halfband filter is 52, with a transition width of 2 kHz.

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

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

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

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

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

%
% View the magnitude response of the halfband filter.

fvtool(firhalfbanddecim)

%
% 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 = firhalfbanddecim(audioframe);
  ylow = firhalfbandinterp(xlo);
  adw(ylow);
end

%
% Wait until the audio file is played to the end, then close the input file and release the audio output resource.

release(afr);           
release(adw);