Convert dsp.FIRFilter
Object to Fixed-Point
Using the Fixed-Point Converter App
Convert a dsp.FIRFilter
System object™, which filters a high-frequency sinusoid signal, to fixed-point using the
Fixed-Point Converter app. This example requires Fixed-Point Designer™ and DSP System Toolbox™ licenses.
Create DSP Filter Function and Test Bench
Create a myFIRFilter
function from a dsp.FIRFilter
System object.
By default, System objects are configured to use full-precision fixed-point arithmetic. To gather range data and get data type proposals from the Fixed-Point Converter app, configure the System object to use ‘Custom’ settings.
Save the function to a local writable folder.
function output = myFIRFilter(input, num) persistent lowpassFIR; if isempty(lowpassFIR) lowpassFIR = dsp.FIRFilter('NumeratorSource', 'Input port', ... 'FullPrecisionOverride', false, ... 'ProductDataType', 'Full precision', ... % default 'AccumulatorDataType', 'Custom', ... 'CustomAccumulatorDataType', numerictype(1,16,4), ... 'OutputDataType', 'Custom', ... 'CustomOutputDataType', numerictype(1,8,2)); end output = lowpassFIR(input, num); end
myFIRFilter_tb
, for the filter. The test bench
generates a signal that gathers range information for conversion. Save the test
bench.% Test bench for myFIRFilter % Remove high-frequency sinusoid using an FIR filter. % Initialize f1 = 1000; f2 = 3000; Fs = 8000; Fcutoff = 2000; % Generate input SR = dsp.SineWave('Frequency',[f1,f2],'SampleRate',Fs,... 'SamplesPerFrame',1024); % Filter coefficients num = fir1(130,Fcutoff/(Fs/2)); % Visualize input and output spectra plot = spectrumAnalyzer('SampleRate',Fs,... 'PlotAsTwoSidedSpectrum',false,... 'ShowLegend',true,'YLimits',[-120 30],... 'Title','Input Signal (Channel 1) Output Signal (Channel 2)'); % Stream for k = 1:100 input = sum(SR(),2); % Add the two sinusoids together filteredOutput = myFIRFilter(input, num); % Filter plot([input,filteredOutput]); % Visualize end
Convert the Function to Fixed-Point
Open the Fixed-Point Converter app.
MATLAB® Toolstrip: On the Apps tab, under Code Generation, click the app icon.
MATLAB command prompt: Enter
fixedPointConverter
To add the entry-point function
myFIRFilter
to the project, browse to the filemyFIRFilter.m
, and then click Open.By default, the app saves information and settings for this project in the current folder in a file named
myFirFilter.prj
.Click Next to go to the Define Input Types step.
The app screens
myFIRFilter.m
for code violations and fixed-point conversion readiness issues. The app does not find issues inmyFIRFilter.m
.On the Define Input Types page, to add
myFIRFilter_tb
as a test file, browse tomyFIRFilter_tb.m
, and then click Autodefine Input Types.The app determines from the test file that the type of
input
isdouble(1024 x 1)
and the type ofnum
isdouble(1 x 131)
.Click Next to go to the Convert to Fixed Point step.
On the Convert to Fixed Point page, click Analyze to collect range information.
The Variables tab displays the collected range information and type proposals. Manually edit the data type proposals as needed.
Click Convert to apply the proposed data types to the function.
The Fixed-Point Converter app applies the proposed data types and generates a fixed-point function,
myFIRFilter_fixpt
.%#codegen function output = myFIRFilter_fixpt(input, num) fm = get_fimath(); persistent lowpassFIR; if isempty(lowpassFIR) lowpassFIR = dsp.FIRFilter('NumeratorSource', 'Input port', ... 'FullPrecisionOverride', false, ... 'ProductDataType', 'Full precision', ... % default 'AccumulatorDataType', 'Custom', ... 'CustomAccumulatorDataType', numerictype(1, 16, 14), ... 'OutputDataType', 'Custom', ... 'CustomOutputDataType', numerictype(1, 8, 6)); end output = fi(lowpassFIR(input, num), 1, 16, 14, fm); end function fm = get_fimath() fm = fimath('RoundingMethod', 'Floor',... 'OverflowAction', 'Wrap',... 'ProductMode','FullPrecision',... 'MaxProductWordLength', 128,... 'SumMode','FullPrecision',... 'MaxSumWordLength', 128); end
Click Next to see the project summary details and links to the fixed-point MATLAB code and conversion report.