Note: You must have both the DSP System Toolbox™ and Simulink® Coder™ products installed on your computer to complete the procedures in this section.
ex_codegen_dsp model implements a simple
adaptive filter to remove noise from a signal while simultaneously
identifying a filter that characterizes the noise frequency content.
To open this model, enter
Run the model and observe the output in both scopes. This model saves the filter weights each time they adapt. You can plot the last set of coefficients using the following command:
To generate code from the model, you must first ensure that you have write permission in your current folder. The code generation process creates a new subfolder inside the current MATLAB® working folder. MATLAB saves all of the files created by the code generation process in that subfolder, including those which contain the generated C source code.
To start the code generation process, click the Build
Model icon (
) on your
model toolbar. After the model finishes generating code, the Code
Generation Report appears, allowing you to inspect the generated code.
You may also notice that the build process created a new subfolder
inside of your current MATLAB working folder. The name of this
folder consists of the model name, followed by the suffix
In the case of this example, the subfolder that contains the generated
C source code is named
If you want to build and run the generated code, you need to have access to a C compiler. For more information about which compilers are supported in the current release, see http://www.mathworks.com/support/compilers/current_release/.
To setup your compiler, run the following command:
After your compiler is setup, you can build and run the generated code. The ex_codegen_dspex_codegen_dsp model is currently configured to generate code only. To build the generated code, you must first make the following changes:
Open the Model Configuration Parameters dialog, navigate to the Code Generation tab, and clear the Generate Code Only checkbox.
Click OK to apply your changes and close the dialog box.
From the model toolbar, click the Build Model icon ( ).
Because you re-configured the model to generate and build code, the code generation process continues until the code is compiled and linked.
To run the generated code, enter the following command at the MATLAB prompt:
Running the generated code creates a MAT-file which contains
the same variables as those generated by simulating the model. The
variables in the MAT-file are named with a prefix of
After you run the generated code, you can load the variables from
the MAT-file by typing the following command at the MATLAB prompt:
You can now compare the variables from the generated code with the variables from the model simulation. To plot the last set of coefficients from the generated code, enter the following command at the MATLAB prompt:
The last set of coefficients from the generated code are shown in the following figure.
For further information on generating code from Simulink, see the Simulink Coder documentation.