# Sine Wave

Generate sine wave, using simulation time as time source

Sources

## Description

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

 Note:   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.

### Time-Based Mode

The output of the Sine Wave block is determined by:

$y=amplitude×\mathrm{sin}\left(frequency×time+phase\right)+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.

#### Block Behavior in Continuous Mode

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.

#### Block Behavior in Discrete Mode

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}\left(t+\Delta t\right)=\mathrm{sin}\left(t\right)\mathrm{cos}\left(\Delta t\right)+\mathrm{sin}\left(\Delta t\right)\mathrm{cos}\left(t\right)\\ \mathrm{cos}\left(t+\Delta t\right)=\mathrm{cos}\left(t\right)\mathrm{cos}\left(\Delta t\right)-\mathrm{sin}\left(t\right)\mathrm{sin}\left(\Delta t\right)\end{array}$

In matrix form, these identities are:

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

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

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

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

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.

#### Methods to Handle Round-Off Errors in Discrete Mode

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

MethodRationale

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 `sin()` math library function to calculate block output.

1. On the Sine Wave block dialog box, set Time to ```Use external signal``` so that an input port appears on the block icon.

2. Connect a clock signal to this input port using a Digital Clock block.

3. Set the sample time of the clock signal to the sample time of the Sine Wave block.

Unlike the block algorithm, the `sin()` math library function computes block output at each time step independently of output values from other time steps, preventing the accumulation of round-off errors.

### Sample-Based Mode

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

$y=A\mathrm{sin}\left(2\pi \left(k+o\right)/p\right)+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.

### Parameter Dimensions

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.

## Data Type Support

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

## Parameters and Dialog Box

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.

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.

## Examples

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

• `sldemo_househeatsldemo_househeat`

• `sldemo_tonegen_fixptsldemo_tonegen_fixpt`

• `sldemo_VariableTransportDelaysldemo_VariableTransportDelay`

• `sldemo_zeroxingsldemo_zeroxing`

## Characteristics

 Sample Time Specified in the Sample time parameter Scalar Expansion Yes, of parameters Dimensionalized Yes Zero-Crossing Detection No