%% Filtering Through a Variable Bandwidth Bandpass FIR Filter
% *Note*: This example runs only in R2016b or later. If you are using an earlier 
% release, replace each call to the function with the equivalent |step| syntax. 
% For example, myObject(x) becomes step(myObject,x). 
% 
% This example shows you how to tune the center frequency and the bandwidth 
% of the FIR filter.
%%
Fs = 44100; % Input sample rate
% Define a bandpass variable bandwidth FIR filter:
vbw = dsp.VariableBandwidthFIRFilter('FilterType','Bandpass',...
    'FilterOrder',100,...
    'SampleRate',Fs,...
    'CenterFrequency',1e4,...
    'Bandwidth',4e3);
tfe = dsp.TransferFunctionEstimator('FrequencyRange','onesided');
aplot = dsp.ArrayPlot('PlotType','Line',...
    'XOffset',0,...
    'YLimits',[-120 5], ...
    'SampleIncrement', 44100/1024,...
    'YLabel','Frequency Response (dB)',...
    'XLabel','Frequency (Hz)',...
    'Title','System Transfer Function');
FrameLength = 1024;
sine = dsp.SineWave('SamplesPerFrame',FrameLength);
for i=1:500
    % Generate input
    x = sine() + randn(FrameLength,1);
    % Pass input through the filter
    y = vbw(x);
    % Transfer function estimation
    h = tfe(x,y);
    % Plot transfer function
    aplot(20*log10(abs(h)))
    % Tune bandwidth and center frequency of the FIR filter
    if (i==250)
        vbw.CenterFrequency = 5000;
        vbw.Bandwidth = 2000;
    end
end

%% 
% Copyright 2012 The MathWorks, Inc.