Generate sine wave, using simulation time as time source

Sources

The Sine Wave block outputs a sinusoidal waveform. The block can operate in time-based or sample-based mode.

This block is the same as the Sine
Wave Function block that appears in the Math Operations library.
If you select `Use external signal`

for the **Time** parameter
in the block dialog box, you get the Sine Wave Function block.

The output of the Sine Wave block is determined by:

$$y=amplitude\times \mathrm{sin}(frequency\times time+phase)+bias.$$

Time-based mode has two submodes: continuous mode or discrete
mode. The value of the **Sample time** parameter
determines whether the block operates in continuous mode or discrete
mode:

`0`

(the default) causes the block to operate in continuous mode.`>0`

causes the block to operate in discrete mode.

See Specify Sample Time in the online documentation for more information.

A **Sample time** parameter value of `0`

causes
the block to operate in continuous mode. When operating in continuous
mode, the Sine Wave block can become inaccurate due to loss of precision
as time becomes very large.

A **Sample time** parameter value greater than
zero causes the block to behave as if it were driving a Zero-Order
Hold block whose sample time is set to that value.

Using the Sine Wave block in this way, you can build models with sine wave sources that are purely discrete, rather than models that are hybrid continuous/discrete systems. Hybrid systems are inherently more complex and as a result take more time to simulate.

In discrete mode, this block uses a differential incremental algorithm instead of one based on absolute time. As a result, the block can be useful in models intended to run for an indefinite length of time, such as in vibration or fatigue testing.

The differential incremental algorithm computes the sine based on the value computed at the previous sample time. This method uses the following trigonometric identities:

$$\begin{array}{l}\mathrm{sin}(t+\Delta t)=\mathrm{sin}(t)\mathrm{cos}(\Delta t)+\mathrm{sin}(\Delta t)\mathrm{cos}(t)\\ \mathrm{cos}(t+\Delta t)=\mathrm{cos}(t)\mathrm{cos}(\Delta t)-\mathrm{sin}(t)\mathrm{sin}(\Delta t)\end{array}$$

In matrix form, these identities are:

$$\left[\begin{array}{c}\mathrm{sin}(t+\Delta t)\\ \mathrm{cos}(t+\Delta t)\end{array}\right]=\left[\begin{array}{cc}\mathrm{cos}(\Delta t)& \mathrm{sin}(\Delta t)\\ -\mathrm{sin}(\Delta t)& \mathrm{cos}(\Delta t)\end{array}\right]\left[\begin{array}{c}\mathrm{sin}(t)\\ \mathrm{cos}(t)\end{array}\right]$$

Because Δ*t* is constant, the following
expression is a constant:

$$\left[\begin{array}{cc}\mathrm{cos}(\Delta t)& \mathrm{sin}(\Delta t)\\ -\mathrm{sin}(\Delta t)& \mathrm{cos}(\Delta t)\end{array}\right]$$

Therefore, the problem becomes one of a matrix multiplication of the value of $$\mathrm{sin}(t)$$ by a constant matrix to obtain $$\mathrm{sin}(t+\Delta t)$$.

Discrete mode reduces but does not eliminate the accumulation
of round-off errors, for example, `(4*eps)`

. This
accumulation can happen because computation of the block output at
each time step depends on the value of the output at the previous
time step.

To handle round-off errors when the Sine Wave block operates in time-based discrete mode, use one of the following methods.

Method | Rationale |
---|---|

Insert a Saturation block directly downstream of the Sine Wave block. | By setting saturation limits on the Sine Wave block output, you can remove overshoot due to accumulation of round-off errors. |

Set up the Sine Wave block to use the On the Sine Wave block dialog box, set **Time**to`Use external signal` so that an input port appears on the block icon.Connect a clock signal to this input port using a Digital Clock block. Set the sample time of the clock signal to the sample time of the Sine Wave block.
| Unlike the block algorithm, the |

Sample-based mode uses the following formula to compute the output of the Sine Wave block.

$$y=A\mathrm{sin}(2\pi (k+o)/p)+b$$

where

*A*is the amplitude of the sine wave.*p*is the number of time samples per sine wave period.*k*is a repeating integer value that ranges from 0 to*p*–1.*o*is the offset (phase shift) of the signal.*b*is the signal bias.

In this mode, Simulink^{®} sets *k* equal
to `0`

at the first time step and computes the block
output, using the preceding formula. At the next time step, Simulink increments *k* and
recomputes the output of the block. When *k* reaches *p*, Simulink resets *k* to `0`

before
computing the block output. This process continues until the end of
the simulation.

The sample-based method of computing block output at a given
time step does not depend on the output of the previous time steps.
Therefore, this mode avoids the accumulation of round-off errors.
Additionally, sample-based mode supports reset semantics in subsystems
that offer it. For example, if a Sine Wave block is in a resettable
subsystem that receives a reset trigger, the repeating integer `k`

resets
and the block output resets to its initial condition.

The numeric parameters of this block must have the same dimensions after scalar expansion.

If

**Interpret vector parameters as 1-D**is not selected, the block outputs a signal of the same dimensions and dimensionality as the parameters.If

**Interpret vector parameters as 1-D**is selected and the numeric parameters are row or column vectors, the block outputs a vector signal. Otherwise, the block outputs a signal of the same dimensionality and dimensions as the parameters.

The Sine Wave block accepts and outputs real signals of type `double`

.

For more information, see Data Types Supported by Simulink in the Simulink documentation.

**Sine type**Specify the type of sine wave that this block generates, either time- or sample-based. Some parameters in the dialog box appear depending on whether you select time-based or sample-based.

**Time**Specify whether to use simulation time as the source of values for the time variable or an external source. If you specify an external time source, the block displays an input port for the time source.

**Amplitude**Specify the amplitude of the signal. The default is

`1`

.**Bias**Specify the constant value added to the sine to produce the output of this block.

**Frequency**Specify the frequency, in radians per second. The default is

`1`

. This parameter appears only when you set**Sine type**to time-based.**Samples per period**Specify the number of samples per period. This parameter appears only when you set

**Sine type**to sample-based.**Phase**Specify the phase shift, in radians. The default is

`0`

. This parameter appears only when you set**Sine type**to time-based.You cannot configure this parameter to appear in the generated code as a tunable global variable if you set

**Time (t)**to`Use simulation time`

. For example, if you set**Default parameter behavior**to`Tunable`

or apply a storage class to a`Simulink.Parameter`

object, the**Phase**parameter does not appear in the generated code as a tunable global variable.To generate code so that you can tune the phase during execution, set

**Time (t)**to`Use external signal`

. You can provide your own time input signal or use a Digital Clock block to generate the time signal. For an example, see Tune Phase Parameter of Sine Wave Block During Code Execution (Simulink Coder).**Number of offset samples**Specify the offset (discrete phase shift) in number of sample times. This parameter appears only when you set

**Sine type**to sample-based.**Sample time**Specify the sample period. The default is

`0`

. If the sine type is sample-based, the sample time must be greater than 0. See Specify Sample Time in the online documentation for more information.**Interpret vector parameters as 1-D**If selected, column or row matrix values for numeric parameters result in a vector output signal. Otherwise, the block outputs a signal of the same dimensionality as the parameters. If you do not select this check box, the block always outputs a signal of the same dimensionality as the numeric parameters. See Determining the Output Dimensions of Source Blocks in the Simulink documentation. This parameter is not available when an external signal specifies time. In this case, if numeric parameters are column or row matrix values, the output is a 1-D vector.

The following Simulink examples show how to use the Sine Wave block:

Data Types | Double |

Sample Time | Specified in the |

Multidimensional Signals | No |

Variable-Size Signals | No |

Zero-Crossing Detection | No |

Code Generation | Yes |

Was this topic helpful?