Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Define an inline function to generate a signal f1(t) = exp(-|t|)cos(wt) with

ID: 3637673 • Letter: 1

Question


1. Define an inline function to generate a signal f1(t) = exp(-|t|)cos(wt) with radian frequency corresponding to period of T = 0.25.
2. Using the inline function, generate an array f1 of values of f1(t) and plot f1(t). Make sure the
range of t and the grid step are chosen reasonably to represent the important features of f1(t).  
3. Generate an array f2 representing a time-shifted signal f1(t-1). Plot f2(t).
4. Generate an array f3 representing f1(2(t-1)). Plot f3(t).
5. Generate an array f4 representing 2f1(2(t-1)). Plot f4(t).
6. Evaluate numerically energy of the signals f1, f2, f3, f4.
7. Use symbolic operations to find energy of the signals f1, f2, f3, f4.
8. Given the input f(t), the system output is y(t)=2 (d/dt)*f(t)+3*integral(f(x)dx . Use symbolic
operations to find the system response for f1(t) input

Explanation / Answer

When a Simulink model contains an S-function and a corresponding TLC block target file exists for that S-function, Real-Time Workshop inlines the S-function. Inlining an S-function can produce more efficient code by eliminating the S-function API layer from the generated code.

For S-functions that can perform a variety of tasks, inlining them gives you the opportunity to generate code only for the current mode of operation set for each instance of the block. As an example of this, if an S-function accepts an arbitrary signal width and loops through each element of the signal, you would want to generate inlined code that has loops when the signal has two or more elements, but generates a simple nonlooped calculation when the signal has just one element.

Level 1 C MEX S-functions (written to an older form of the S-function API) that are not inlined will cause the generated code to make calls to all seven of these functions even if the routine is empty for the particular S-function.

Initialize the sizes array

Initialize the sample times array

Initialize the states

Compute the outputs

Update discrete states

Compute the derivatives of continuous states

Clean up when the simulation terminates

Level 2 C MEX S-functions (i.e., those written to the current S-function API) that are not inlined make calls to the above functions, with the following exceptions:

mdlInitializeConditions is called only if MDL_INITIALIZE_CONDITIONS is declared with #define.

mdlStart is called only if MDL_START is declared with #define.

mdlUpdate is called only if MDL_UPDATE is declared with #define.

mdlDerivatives is called only if MDL_DERIVATIVES is declared with #define.

By inlining an S-function, you can eliminate the calls to these possibly empty functions in the simulation loop. This can greatly improve the efficiency of the generated code.

To inline an S-function called sfunc_name, you create a custom S-function block target file called sfunc_name.tlc and place it in the same directory as the S-function MEX-file. Then, at build time, the target file is executed instead of setting up function calls into the S-function .c file. The S-function target file "inlines" the S-function by directing the Target Language Compiler to insert only the statements defined in the target file.

In general, inlining an S-function is especially useful when

The time required to execute the contents of the S-function is small in comparison to the overhead required to call the S-function.

Certain S-function routines are empty (e.g., mdlUpdate).

The behavior of the S-function changes between simulation and code generation. For example, device driver I/O S-functions might read from the MATLAB workspace during simulation, but read from an actual hardware address in the generated code.

Function Purpose mdlInitializeSizes

Initialize the sizes array

mdlInitializeSampleTimes

Initialize the sample times array

mdlInitializeConditions

Initialize the states

mdlOutputs

Compute the outputs

mdlUpdate

Update discrete states

mdlDerivatives

Compute the derivatives of continuous states

mdlTerminate

Clean up when the simulation terminates