Convert dsp.FIRFilter Object to Fixed-Point
Convert a dsp.FIRFilter
System object™, which filters a high-frequency sinusoid signal, to fixed-point using the
fiaccel function. 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, 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
At the command line, create a
coder.FixPtConfigobject, and specify the test bench asmyFIRFilter_tb.m.fixptcfg = coder.config('fixpt'); fixptcfg.TestBenchName = 'myFIRFilter_tb';
Generate fixed-point code for
myFIRFilter.musing thefiaccelfunction.Since no input types are specified, the fixed-point conversion process simulates the test bench to infer types. To specify input types, use thefiaccel -float2fixed fixptcfg myFIRFilter
-argsoption when you enter thefiaccelcommand.The fixed-point conversion process automatically proposes and applies fixed-point data types and generates a fixed-point function,
myFIRFilter_fixpt. To view the fixed-point function, click%#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, 14, 12, fm); end function fm = get_fimath() fm = fimath('RoundingMethod', 'Floor',... 'OverflowAction', 'Wrap',... 'ProductMode','FullPrecision',... 'MaxProductWordLength', 128,... 'SumMode','FullPrecision',... 'MaxSumWordLength', 128); end
Click View Report to see the project summary details and links to the fixed-point MATLAB® code and conversion report.