📄 matlabforsignalprocessing.m
字号:
%% 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
f1=fir1(7,0.25)
freqz(f1)
%% Try a 70-tap filter, visualize:
% more reasonable passband attenuation,
% but not enough sharp transition at
% Passband-Stopband boundary
f1=fir1(70,0.25)
freqz(f1)
%% Try a 170-filter, visualize:
% Good passband attenuation,
% and sharp transition at boundary,
% End of your function-based method!
f1=fir1(170,0.25)
freqz(f1)
%% This was the command-line method of design
%% Repeat the same design using Filter Design GUI: fdatool
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!
% Instead:
%% Start by constructing a design object (fdesign)
% and tell it what kind of filter you want
% In this case a Lowpass filter
fd=fdesign.lowpass
%% 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)
get(fd,'Description')
%% So, you impose requirements first, like you should
% put in the desired behavior of your filter as
% parameters of fdesign object you just created.
fd.Fpass=0.24
fd.Fstop=0.26
fd.Apass=1;
fd.Astop=50
%% 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
designmethods(fd)
%% How about only FIR
designmethods(fd,'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!
design(fd,'equiripple')
%% Capture your design as a dfilt object
df3=design(fd,'equiripple');
%% Inquire about the filter structure and its properties
info(df3)
%% Go to Analysis Menu
% Visualize the filter in terms of impulse response,
% group delay, pole-zero and other analyses easily
% using filter visualization tool
fvtool(df3)
%% Perform trade-off analysis between Stopband attenuation
% and filter order easily
fd.Astop=40
df4=design(fd,'equiripple')
fvtool(df4);
%% 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
help dfilt
%% Construct a filter object by passing your coefficients
% f1 to the dfilt constructor by choosing for example
% a Direct-Form FIR filter (dffir)structure
df1=dfilt.dffir(f1)
fvtool(df1)
%% Automatically generate a Simulink model composed of
% delays, gains and sums from your dfilt object
realizemdl(df1)
%% Change your filter structure to Direct-Form Transposed
df2=convert(df1,'dffirt')
%% Look at a direct-form transposed filter structure
% in the Simulink model
realizemdl(df2)
%% Automaticaly generate VHDL/Verilog code
% with Filter Design HDL Coder
generatehdl(df2)
%% Turn the filter into a fixed-point filter
df2.Arithmetic='fixed'
%% Examine range of filter coefficients and verify proper autoscaling
% furnished automatically by the dfilt object
max_num=max(df2.Numerator)
min_num=min(df2.Numerator)
%% Look at the basic filter properties
get(df2)
%% Get a detailed report of how filter is implemented
info(df2)
%% Now set the constraints of a real-world hardware
df2.FilterInternals='SpecifyPrecision'
%% 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, ...
'InputFracLength',13,...
'ProductWordLength', 24, ...
'AccumWordLength', 28, ...
'OutputWordLength', 16);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -