|On this page…|
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:
Place a MATLAB Function block in a new model. You can find the block in the Simulink User-Defined Functions library.
Save your model as cgen_fi.
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; 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;
Define the filter coefficients b, noise x, and initial conditions zi by typing the following code at the MATLAB command line:
b=fidemo.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);
Add blocks to your model to create the following system.
Set the block parameters in the model to these Fixed-Point FIR Code Example Parameter Values.
From the model menu, select Simulation > Model Configuration Parameters and set the following parameters.
|Solver||discrete (no continuous states)|
Click Apply to save your changes.
Open the Model Explorer for the model.
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.
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:
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.
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.
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.
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.