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 C Code

    Note:   To generate fixed-point code from MATLAB® you must have both the Fixed-Point Designer™ product and the MATLAB Coder™ product. You also must have a C compiler.

This example shows how to generate code for a simple function that multiples and accumulates two input values. This is the type of code that you could embed in external hardware. The function is

function acc = mult_acc(x,a,acc)
acc = accumpos(acc,x*a); 

This code defines the test bench inputs, sets up the required code generation properties, and generates the code. The test bench inputs are specified as fixed-point numbers. The x input is a random number, a is 0.9, and the accumulator, acc, is initialized to 0. The coder.HardwareImplementation object specifies properties of the external hardware that impact the generated code. The examples specifies a 40-bit accumulator. The coder.CodeConfig object has properties that directly affect code generation. The codegen command takes the function, the configuration object as the input arguments and generates embeddable C code.

x = fi(rand,true,16,15);
a = fi(0.9,true,16,15);
acc = fi(0,true,40,30);

hi = coder.HardwareImplementation;
hi. ProdHWDeviceType = 'Generic->Custom'
hi. TargetHWDeviceType = 'Generic->Custom'
hi.TargetBitPerLong = 40;
hi.ProdBitPerLong   = 40;

hc = coder.config('lib');
hc.HardwareImplementation = hi;
hc.GenerateReport         = true;

codegen mult_acc -config hc -args {x,a,acc}

The generated C code is

 * mult_acc.c
 * Code generation for function 'mult_acc'

/* Include files */
#include "rt_nonfinite.h"
#include "mult_acc.h"

/* Function Definitions */
void mult_acc(short x, short a, long *acc)
  *acc += x * a;

/* End of code generation (mult_acc.c) */

Was this topic helpful?