# codegen

Generate MATLAB code for tunable gain surfaces

## Syntax

``code = codegen(GS)``

## Description

example

````code = codegen(GS)` generates MATLAB® code for the tunable surface `GS`. The generated code is a function that takes the scalar values of the scheduling variables and returns the scalar-valued or matrix-valued gain, depending on `GS`. ```

## Examples

collapse all

Create a tunable surface that represents a scalar gain with a bilinear dependence on two scheduling variables. Suppose that the scheduling variables are `alpha`, ranging from 0-15 degrees, and `V`, ranging from 300-600 m/s. The tunable surface covers a linearly spaced grid in this operating range.

```[alpha,V] = ndgrid(0:3:15,300:50:600); domain = struct('alpha',alpha,'V',V); shapefcn = @(x,y) [x,y,x*y]; GS0 = tunableSurface('K',1,domain,shapefcn);```

Usually, you use `GS0` to parameterize a scheduled gain and tune the surface coefficients with systune. For this example, instead of tuning, manually set the coefficients to non-zero values.

`GS = setData(GS0,[100,28,40,10]);`

Generate MATLAB code that computes the scalar gain as a function of scheduling variables.

`code = codegen(GS)`
```code = 'function Gain_ = fcn(alpha_,V_) %#codegen % Type casting ZERO = zeros(1,1,'like',alpha_+V_); alpha_ = cast(alpha_,'like',ZERO); V_ = cast(V_,'like',ZERO); % Tuned gain surface coefficients Coeffs = cast([100 28 40 10],'like',ZERO); Offsets = cast([7.5 450],'like',ZERO); Scalings = cast([7.5 150],'like',ZERO); % Normalization alpha_ = (alpha_ - Offsets(1))/Scalings(1); V_ = (V_ - Offsets(2))/Scalings(2); % Compute weighted sum of terms Y = [ alpha_ , V_ , alpha_*V_ ]; Gain_ = Coeffs(1); for i=1:numel(Y) Gain_ = Gain_ + Coeffs(i+1) * Y(i); end ' ```

The resulting code is a function, `fcn`, that takes two scheduling variables and returns a scalar gain. The function includes the `%#codegen `directive, so that it can be used for further code generation, such as implementing a tuned gain schedule in hardware.

The function includes four sections. The first section ensures that the scheduling variables are cast to the same type. The second section encodes the gain coefficients and the offsets and scalings that the software extracts from `GS`. These values are hard-coded into `fcn`, which can compute the gain surface without reference to G`S`. The third section uses these values to compute the normalized scheduling variables. (See `tunableSurface` for more information about normalization.)

The last section computes the gain by summing up all the terms in the polynomial expression for the gain surface.

## Input Arguments

collapse all

Tunable gain surface, specified as a `tunableSurface` object.

## Output Arguments

collapse all

Generated code for the gain surface, returned as a character array. The code contains a function, `Gain_ = fcn(x1_,x2_,...,xN_)`, that computes the gain from the scheduling variables `x1_,x2_,...,xN_` of `GS`. The expression relating the gain to the scheduling variables, the coefficients of the expression, and the normalization of the scheduling variables are all taken from `GS`, and the code can run without further reference to `TS`. The returned function includes the `%#codegen` directive so that it can be used for further code generation, such as implementing a tuned gain schedule in hardware.

When you use `writeBlockValue` to write tuned gain-surface coefficients from an `slTuner` interface to a MATLAB Function block, it uses this generated MATLAB code.
{"ft_lang_label":"__label__en","ft_lang_prob":0.78678095,"math_prob":0.96988255,"size":2172,"snap":"2020-34-2020-40","text_gpt3_token_len":568,"char_repetition_ratio":0.12684502,"word_repetition_ratio":0.00625,"special_character_ratio":0.27992633,"punctuation_ratio":0.18390805,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9972141,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-08T05:34:59Z\",\"WARC-Record-ID\":\"<urn:uuid:689dec9d-3685-40eb-a356-e2699b200cef>\",\"Content-Length\":\"78150\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:043314c0-cfee-4097-a066-73aa112f64b2>\",\"WARC-Concurrent-To\":\"<urn:uuid:10fe5be6-2f54-4005-830a-d3ba4ca417cf>\",\"WARC-IP-Address\":\"23.66.56.59\",\"WARC-Target-URI\":\"https://uk.mathworks.com/help/control/ref/tunablesurface.codegen.html\",\"WARC-Payload-Digest\":\"sha1:X6P4S7W3IOLBHB3OR2BMFLLF3VP7MRPA\",\"WARC-Block-Digest\":\"sha1:MVFVXY5VT6I4WV5UE2LXKEER7JG5SPTF\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439737289.75_warc_CC-MAIN-20200808051116-20200808081116-00412.warc.gz\"}"}