Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Generate continuous or discrete sine wave

Sources

`dspsrcs4`

The Sine Wave block generates a multichannel real or complex
sinusoidal signal, with independent amplitude, frequency, and phase
in each output channel. A real sinusoidal signal is generated when
the **Output complexity** parameter is set to `Real`

,
and is defined by an expression of the type

$$y=A\mathrm{sin}\left(2\pi ft+\varphi \right)$$

where you specify *A* in the **Amplitude** parameter, *f* in
hertz in the **Frequency** parameter, and *ϕ* in
radians in the **Phase offset** parameter. A complex
exponential signal is generated when the **Output complexity** parameter
is set to `Complex`

, and is defined by an
expression of the type

$$y=A{e}^{j(2\pi ft+\varphi )}=A\left\{\mathrm{cos}\left(2\pi ft+\varphi \right)+j\mathrm{sin}\left(2\pi ft+\varphi \right)\right\}$$

For both real and complex sinusoids, the **Amplitude**, **Frequency**,
and **Phase offset** parameter values (*A*, *f*,
and *ϕ*) can be scalars or length-*N* vectors,
where *N* is the desired number of channels in
the output. When you specify at least one of these parameters as a
length-*N* vector, scalar values specified for
the other parameters are applied to every channel.

For example, to generate the three-channel output containing
the real sinusoids below, set **Output complexity** to `Real`

and
the other parameters as follows:

**Amplitude**=`[1 2 3]`

**Frequency**=`[1000 500 250]`

**Phase offset**=`[0 0 pi/2]`

$$y=\{\begin{array}{cc}\mathrm{sin}\left(2000\pi t\right)& \text{(channel1)}\\ 2\mathrm{sin}\left(1000\pi t\right)& \text{(channel2)}\\ 3\mathrm{sin}\left(500\pi t+\frac{\pi}{2}\right)& \text{(channel3)}\end{array}\text{}$$

In all discrete modes, the block buffers the sampled sinusoids
into frames of size M, where you specify M in the **Samples
per frame** parameter. The output is an M-by-N matrix with
frame period M*T_{s}, where you specify T_{s} in
the **Sample time** parameter.

The **Sample mode** parameter specifies the
block's sampling property, which can be `Continuous`

or `Discrete`

:

`Continuous`

In continuous mode, the sinusoid in the

*i*th channel,*y*, is computed as a continuous function,_{i}$$\begin{array}{ll}{y}_{i}={A}_{i}\mathrm{sin}\left(2\pi {f}_{i}t+{\varphi}_{i}\right)\hfill & \text{(real)}\hfill \\ \begin{array}{l}\\ \text{or}\\ \end{array}\hfill & \hfill \\ {y}_{i}={A}_{i}{e}^{j\left(2\pi {f}_{i}t+{\varphi}_{i}\right)}\hfill & \text{(complex)}\hfill \end{array}$$

and the block's output is continuous. In this mode, the block's operation is the same as that of a Simulink

^{®}Sine Wave block with**Sample time**set to`0`

. This mode offers high accuracy, but requires trigonometric function evaluations at each simulation step, which is computationally expensive. Additionally, because this method tracks absolute simulation time, a discontinuity will eventually occur when the time value reaches its maximum limit.Note also that many DSP System Toolbox™ blocks do not accept continuous-time inputs.

`Discrete`

In discrete mode, the block's discrete-time output can be generated by directly evaluating the trigonometric function, by table lookup, or by a differential method. The three options are explained below.

When you select `Discrete`

from the **Sample
mode** parameter, the secondary **Computation method** parameter
provides three options for generating the discrete sinusoid:

To generate fixed-point sinusoids, you must select ```
Table
Lookup
```

.

The trigonometric function method computes the sinusoid in the *i*th
channel, *y*_{i}, by sampling
the continuous function

$$\begin{array}{ll}{y}_{i}={A}_{i}\mathrm{sin}\left(2\pi {f}_{i}t+{\varphi}_{i}\right)\hfill & \text{(real)}\hfill \\ \begin{array}{l}\\ \text{or}\\ \end{array}\hfill & \hfill \\ {y}_{i}={A}_{i}{e}^{j\left(2\pi {f}_{i}t+{\varphi}_{i}\right)}\hfill & \text{(complex)}\hfill \end{array}$$

with a period of *T _{s}*,
where you specify

`Continuous`

sample
mode described above.At each sample time, the block evaluates the sine function at
the appropriate time value *within the first cycle* of
the sinusoid. By constraining trigonometric evaluations to the first
cycle of each sinusoid, the block avoids the imprecision of computing
the sine of very large numbers, and eliminates the possibility of
discontinuity during extended operations (when an absolute time variable
might overflow). This method therefore avoids the memory demands of
the table lookup method at the expense of many more floating-point
operations.

The table lookup method precomputes the *unique* samples
of every output sinusoid at the start of the simulation, and recalls
the samples from memory as needed. Because a table of finite length
can only be constructed when all output sequences repeat, the method
requires that the period of every sinusoid in the output be evenly
divisible by the sample period. That is, 1/(*f _{i}T_{s}*)
=

When the **Optimize table for** parameter is
set to `Speed`

, the table constructed for
each channel contains *k _{i}* elements.
When the

`Memory`

, the table constructed for each
channel contains For long output sequences, the table lookup method requires far fewer floating-point operations than any of the other methods, but can demand considerably more memory, especially for high sample rates (long tables). This is the recommended method for models that are intended to emulate or generate code for DSP hardware, and that therefore need to be optimized for execution speed.

The lookup table for this block is constructed from double-precision
floating-point values. Thus, when you use the `Table lookup`

computation
mode, the maximum amount of precision you can achieve in your output
is 53 bits. Setting the word length of the **Output** or **User-defined** data
type to values greater than 53 bits does not improve the precision
of your output.

The differential method uses an incremental algorithm. This algorithm computes the output samples based on the output values computed at the previous sample time (and precomputed update terms) by making use of the following identities.

$$\begin{array}{l}\mathrm{sin}\left(t+{T}_{s}\right)=\mathrm{sin}\left(t\right)\mathrm{cos}\left({T}_{s}\right)+\mathrm{cos}\left(t\right)\mathrm{sin}\left({T}_{s}\right)\\ \mathrm{cos}\left(t+{T}_{s}\right)=\mathrm{cos}\left(t\right)\mathrm{cos}\left({T}_{s}\right)-\mathrm{sin}\left(t\right)\mathrm{sin}\left({T}_{s}\right)\end{array}$$

The update equations for the sinusoid in the *i*th
channel, *y _{i}*, can therefore
be written in matrix form as

$$\left[\begin{array}{c}\mathrm{sin}\left\{2\pi {f}_{i}\left(t+{T}_{s}\right)+{\varphi}_{i}\right\}\\ \mathrm{cos}\left\{2\pi {f}_{i}\left(t+{T}_{s}\right)+{\varphi}_{i}\right\}\end{array}\right]=\left[\begin{array}{cc}\mathrm{cos}\left(2\pi {f}_{i}{T}_{s}\right)& \mathrm{sin}\left(2\pi {f}_{i}{T}_{s}\right)\\ -\mathrm{sin}\left(2\pi {f}_{i}{T}_{s}\right)& \mathrm{cos}\left(2\pi {f}_{i}{T}_{s}\right)\end{array}\right]\left[\begin{array}{c}\mathrm{sin}\left(2\pi {f}_{i}t+{\varphi}_{i}\right)\\ \mathrm{cos}\left(2\pi {f}_{i}t+{\varphi}_{i}\right)\end{array}\right]$$

where you specify *T _{s}* in
the

This mode offers reduced computational load, but is subject to drift over time due to cumulative quantization error. Because the method is not contingent on an absolute time value, there is no danger of discontinuity during extended operations (when an absolute time variable might overflow).

The ex_dspsinecomp and ex_dspsinecomp_frame examples provide a comparison of all the available sine generation methods for sample and frame based modes, respectively.

The **Main** pane of the Sine Wave block
dialog appears as follows.

**Amplitude**A length-

*N*vector containing the amplitudes of the sine waves in each of*N*output channels, or a scalar to be applied to all*N*channels. The vector length must be the same as that specified for the**Frequency**and**Phase offset**parameters. Tunable (Simulink) when**Computation method**is to`Trigonometric fcn`

or`Differential`

.**Frequency**A length-

*N*vector containing frequencies, in Hertz, of the sine waves in each of*N*output channels, or a scalar to be applied to all*N*channels. The vector length must be the same as that specified for the**Amplitude**and**Phase offset**parameters. You can specify positive, zero, or negative frequencies. Tunable (Simulink) when**Sample mode**is`Continuous`

or**Computation method**is`Trigonometric fcn`

.**Phase offset**A length-

*N*vector containing the phase offsets, in radians, of the sine waves in each of*N*output channels, or a scalar to be applied to all*N*channels. The vector length must be the same as that specified for the**Amplitude**and**Frequency**parameters. Tunable (Simulink) when**Sample mode**is`Continuous`

or**Computation method**is`Trigonometric fcn`

.**Sample mode**The block's sampling behavior,

`Continuous`

or`Discrete`

. This parameter is not tunable.**Output complexity**The type of waveform to generate:

`Real`

specifies a real sine wave,`Complex`

specifies a complex exponential. This parameter is not tunable.**Computation method**The method by which discrete-time sinusoids are generated:

`Trigonometric fcn`

,`Table lookup`

, or`Differential`

. This parameter is not tunable. For more information on each of the available options, see Discrete Computational Methods in the Description section.This parameter is only visible when you set the

**Sample mode**to`Discrete`

.### Note

To generate fixed-point sinusoids, you must set the

**Computation method**to`Table lookup`

.**Optimize table for**Optimizes the table of sine values for

`Speed`

or`Memory`

(this parameter is only visible when the**Computation method**parameter is set to`Table lookup`

). When optimized for speed, the table contains*k*elements, and when optimized for memory, the table contains*k*/4 elements, where*k*is the number of input samples in one full period of the sine wave.**Sample time**The period with which the sine wave is sampled,

*T*. The block's output frame period is_{s}*M***T*, where you specify_{s}*M*in the**Samples per frame**parameter. This parameter is disabled when you select`Continuous`

from the**Sample mode**parameter. This parameter is not tunable.**Samples per frame**The number of consecutive samples from each sinusoid to buffer into the output frame,

*M*.This parameter is disabled when you select

`Continuous`

from the**Sample mode**parameter.**Resetting states when re-enabled**This parameter only applies when the Sine Wave block is located inside an enabled subsystem and the

**States when enabling**parameter of the Enable block is set to`reset`

. This parameter determines the behavior of the Sine Wave block when the subsystem is re-enabled. The block can either reset itself to its starting state (`Restart at time zero`

), or resume generating the sinusoid based on the current simulation time (`Catch up to simulation time`

). This parameter is disabled when you select`Continuous`

from the**Sample mode**parameter.

The **Data Types** pane of the Sine Wave
block dialog appears as follows.

**Output data type**Specify the output data type for this block. You can select one of the following:

A rule that inherits a data type, for example,

`Inherit: Inherit via back propagation`

. When you select this option, the output data type and scaling matches that of the next downstream block.A built in data type, such as

`double`

An expression that evaluates to a valid data type, for example,

`fixdt(1,16)`

Click the

**Show data type assistant**button to display the**Data Type Assistant**, which helps you set the**Output data type**parameter.See Control Signal Data Types (Simulink) for more information.

### Note

The lookup table for this block is constructed from double-precision floating-point values. Thus, when you use the

`Table lookup`

computation mode, the maximum amount of precision you can achieve in your output is 53 bits. Setting the word length of the**Output**or**User-defined**data type to values greater than 53 bits does not improve the precision of your output.

This block supports HDL code generation using HDL Coder™. HDL Coder provides additional configuration options that affect HDL implementation and synthesized logic. For more information on implementations, properties, and restrictions for HDL code generation, see Sine Wave.

Double-precision floating point

Single-precision floating point

Fixed point (signed only)

8-, 16-, and 32-bit signed integers

Chirp | DSP System Toolbox |

Signal From Workspace | DSP System Toolbox |

Signal Generator | Simulink |

Sine Wave | Simulink |

`sin` | MATLAB |

Was this topic helpful?