Main Content

Generate Fixed-Point FIR Code Using MATLAB Function Block

This example shows how to create a fixed-point, lowpass, direct form FIR filter in Simulink®. To create the FIR filter, you use Fixed-Point Designer™ software and the MATLAB Function block.

Open the Model

Open the cgen_fi model.

open_system('cgen_fi')

The model contains a MATLAB Function block that implements a lowpass, direct form FIR filter. Double-click the MATLAB Function block to examine the dffirdemo function.

function [yout,zf] = dffirdemo(b,x,zi) %#codegen
%cgen_fi doc model example
%Initialize the output signal yout and the final conditions zf
Ty = numerictype(1,12,8);
yout = fi(zeros(size(x)),'numerictype',Ty);
zf = zi;

% FIR filter code
for k = 1:length(x)
  % Update the states: z = [x(k);z(1:end-1)]  
  zf(:) = [x(k);zf(1:end-1)];
  % Form the output: y(k) = b*z
  yout(k) = b*zf;
end

% Plot the outputs only in simulation.
% This does not generate C code.
figure;
subplot(211);plot(x); title('Noisy Signal');grid;
subplot(212);plot(yout); title('Filtered Signal');grid;function [yout,zf] = dffirdemo(b, x, zi) %#codegen
%codegen_fi doc model example
%Initialize the output signal yout and the final conditions zf
Ty = numerictype(1,12,8);
yout = fi(zeros(size(x)),'numerictype',Ty);
zf = zi;

% FIR filter code
for k=1:length(x);
  % Update the states: z = [x(k);z(1:end-1)]  
  zf(:) = [x(k);zf(1:end-1)];
  % Form the output: y(k) = b*z
  yout(k) = b*zf;
end

% Plot the outputs only in simulation.
% This does not generate C code.
figure;
subplot(211);plot(x); title('Noisy Signal');grid;
subplot(212);plot(yout); title('Filtered Signal');grid;

Prepare the Inputs

Define the filter coefficients b, noise x, and initial conditions zi.

b = fi_fir_coefficients;
load mtlb
x = mtlb;
n = length(x);
noise = sin(2*pi*2140*(0:n-1)'./Fs);
x = x + noise;
zi = zeros(length(b),1);

Define fimath Object Using the Property Inspector

1. Select the MATLAB Function block and open the Property Inspector.

2. Select Specify Other for the MATLAB Function fimath parameter. Create this fimath object in the edit box:

fimath('RoundingMethod','Floor',...
    'OverflowAction','Wrap',...
    'ProductMode','KeepLSB',...
    'ProductWordLength',32,...
    'SumMode','KeepLSB',...
    'SumWordLength',32)

The fimath object you define here is associated with fixed-point inputs to the MATLAB Function block as well as the fi object you construct within the block.

By selecting Specify other for the MATLAB Function block fimath, you ensure that your model always uses the fimath properties you specified.

Simulate the Model

Simulate the model. You can look at the plots of the noisy signal and the filtered signal.

sim('cgen_fi')

Figure contains 2 axes objects. Axes object 1 with title Noisy Signal contains an object of type line. Axes object 2 with title Filtered Signal contains an object of type line.

ans = 
  Simulink.SimulationOutput:
                 noisyx: [4001x1 embedded.fi] 
                   tout: [1x1 double] 
                   yout: [4001x1 embedded.fi] 
                     zf: [13x1 embedded.fi] 

     SimulationMetadata: [1x1 Simulink.SimulationMetadata] 
           ErrorMessage: [0x0 char] 

Generate Code

Build embeddable C code for your model by selecting the model and typing Ctrl+B. A folder called cgen_fi_grt_rtw is created in your current working directory.

Inspect the file cgen_fi_grt_rtw > cgen_fi.c to view the code generated from the model.

See Also

| | |

Related Topics