%% This is a MATLAB-script for the MathWorks Webinar entitled:
%% MATLAB for Signal Processing
% It demonstrates a comparison between
% function-based and object-based approaches to
% design of filters with Signal Processing and
% Filter Design Toolboxes.
% Furthermore, it showcases the use of filter objects
% (for example dfilt) as tools to capture a design,
% provide a complete array of analysis and visualization
% path to implementing the design as a Simulink model
% (for this you need Simulink & Signal Processing Blockset)
% and automatically generating HDL code for the filter
% for hardware implementation
% (for this you need Filter Design HDL Coder).
%% Function-based approach to filter design
% Choose design method first, iterate through parameters,
% Inspect to see if you meet spectral requirements,
% Iterate until your requirements are met
%% FIR1 method (Windowing Ideal lowpass impulse response)
%% Start with a 7-tap filter, Normalized Cutoff frequency
% at 0.25 of bandwidth.
% Visualize response using freqz command
%% Try a 70-tap filter, visualize:
% more reasonable passband attenuation,
% but not enough sharp transition at
% Passband-Stopband boundary
%% Try a 170-filter, visualize:
% Good passband attenuation,
% and sharp transition at boundary,
% End of your function-based method!
%% This was the command-line method of design
%% Repeat the same design using Filter Design GUI: fdatool
%% Now, examine an object-based approach to filter design
% Unlike function-based approach:
% Do not commit yourself to a design method yet!
% Do not go through parameters in an Ad Hoc manner!
%% Start by constructing a design object (fdesign)
% and tell it what kind of filter you want
% In this case a Lowpass filter
%% Look at how it is asking you to specify the filter:
% It is asking you to give the 4 characterizing
% elements of a lowpass filter design, which
% describe what spectral characteristics
% the designed filter should have!
% Passband Frequency
% Stopband Frequency
% Passband Ripple (dB)
% Stopband Attenuation (dB)
%% So, you impose requirements first, like you should
% put in the desired behavior of your filter as
% parameters of fdesign object you just created.
%% Now that you constrained the filter being designed
% to behave the way you want,
% ask the fdesign object what design methods are
% available to you to meet these requirements
%% First both IIR & FIR methods
%% How about only FIR
%% Now choose one of them and design the filter
% based on that method. You are guaranteed
% to meet your specifications since you chose
% a fdesign object-based approach,
% no need for trial-and-error inspections!
%% Capture your design as a dfilt object
%% Inquire about the filter structure and its properties
%% Go to Analysis Menu
% Visualize the filter in terms of impulse response,
% group delay, pole-zero and other analyses easily
% using filter visualization tool
%% Perform trade-off analysis between Stopband attenuation
% and filter order easily
%% You can also capture your original design based
%% on function-based approach as a dfilt filter object
% Just choose a filter structure from the list
%% Construct a filter object by passing your coefficients
% f1 to the dfilt constructor by choosing for example
% a Direct-Form FIR filter (dffir)structure
%% Automatically generate a Simulink model composed of
% delays, gains and sums from your dfilt object
%% Change your filter structure to Direct-Form Transposed
%% Look at a direct-form transposed filter structure
% in the Simulink model
%% Automaticaly generate VHDL/Verilog code
% with Filter Design HDL Coder
%% Turn the filter into a fixed-point filter
%% Examine range of filter coefficients and verify proper autoscaling
% furnished automatically by the dfilt object
%% Look at the basic filter properties
%% Get a detailed report of how filter is implemented
%% Now set the constraints of a real-world hardware
%% Hardware specifications: 16-bit data buses,
% a 24-bit multiplier and an accumulator with 4 guard bits and
% the input data comes from a 16-bit ADC.
set(df2, 'InputWordLength', 16, ...
'ProductWordLength', 24, ...
'AccumWordLength', 28, ...