This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Generate Fixed-Point FIR Code Using MATLAB Function Block

Program the MATLAB Function Block

The following 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. In this example, you perform the following tasks in the sequence shown:

  1. Place a MATLAB Function block in a new model. You can find the block in the Simulink User-Defined Functions library.

  2. Save your model as cgen_fi.

  3. Double-click the MATLAB Function block in your model to open the MATLAB Function Block Editor. Type or copy and paste the following MATLAB® code, including comments, into the Editor:

    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;
    % Plot the outputs only in simulation.
    % This does not generate C code.
    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 by typing the following code at the MATLAB command line:

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

Create the Model

  1. Add blocks to your model to create the following system.

  2. Set the block parameters in the model to these Fixed-Point FIR Code Example Parameter Values.

  3. From the model menu, select Simulation > Model Configuration Parameters and set the following parameters.

    Stop time0
    Solverdiscrete (no continuous states)

    Click Apply to save your changes.

Define the fimath Object Using the Model Explorer

  1. Open the Model Explorer for the model.

  2. Click the cgen_fi > MATLAB Function node in the Model Hierarchy pane. The dialog box for the MATLAB Function block appears in the Dialog pane of the Model Explorer.

  3. Select Specify other for the MATLAB Function block fimath parameter on the MATLAB Function block dialog box. You can then create the following fimath object in the edit box:


    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.

Run the Simulation

  1. Run the simulation by selecting your model and typing Ctrl+T. While the simulation is running, information outputs to the MATLAB command line. You can look at the plots of the noisy signal and the filtered signal.

  2. Next, build embeddable C code for your model by selecting the model and typing Ctrl+B. While the code is building, information outputs to the MATLAB command line. A folder called coder_fi_grt_rtw is created in your current working folder.

  3. Navigate to coder_fi_grt_rtw > cgen_fi.c. In this file, you can see the code generated from your model. Search for the following comment in your code:

    /* codegen_fi doc model example */
    This search brings you to the beginning of the section of the code that your MATLAB Function block generated.

Was this topic helpful?