Main Content

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
Create a test bench, 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

  1. Open the Fixed-Point Converter app.

    • MATLAB® Toolstrip: On the Apps tab, under Code Generation, click the app icon.

    • MATLAB command prompt: Enter

      fixedPointConverter

  2. To add the entry-point function myFIRFilter to the project, browse to the file myFIRFilter.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.

  3. Click Next to go to the Define Input Types step.

    The app screens myFIRFilter.m for code violations and readiness issues. The app does not find issues in myFIRFilter.m.

  4. On the Define Input Types page, to add myFIRFilter_tb as a test file, browse to myFIRFilter_tb.m, and then click Autodefine Input Types.

    The app determines from the test file that the type of input is double(1024 x 1) and the type of num is double(1 x 131).

  5. Click Next to go to the Convert to Fixed Point step.

  6. 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.

  7. 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
    
  8. Click Next to see the project summary details and links to the fixed-point MATLAB code and conversion report.

Related Topics