DSP Blockset | ![]() ![]() |
Generate a continuous or discrete sine wave.
Library
Description
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
where A is specified by the Amplitude parameter, f is specified in hertz by the Frequency parameter, and is specified in radians by the Phase 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
Sections of This Reference Page
Generating Multi-Channel Outputs
For both real and complex sinusoids, the Amplitude, Frequency, and Phase parameter values (A, f, and ) can be scalars or length-N vectors, where N is the desired number of channels in the output. If at least one of these parameters is specified 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:
Output Sample Time and Samples Per Frame
In all discrete modes (see below), the block buffers the sampled sinusoids into frames of size M, where M is specified by the Samples per frame parameter. The output is a frame-based M-by-N matrix with frame period M*Ts, where Ts is specified by the Sample time parameter. For M=1, the output is sample-based.
Sample Mode
The Sample mode parameter specifies the block's sampling property, which can be Continuous or Discrete:
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 blocks in the DSP Blockset do not accept continuous-time inputs.
Discrete Computational Methods
When Discrete is selected from the Sample mode parameter, the secondary Computation method parameter provides three options for generating the discrete sinusoid:
Trigonometric Fcn. The trigonometric function method computes the sinusoid in the ith channel, yi, by sampling the continuous function
with a period of Ts, where Ts is specified by the Sample time parameter. This mode of operation shares the same benefits and liabilities as the Continuous sample mode described above.
If the period of every sinusoid in the output is evenly divisible by the sample period, meaning that 1/(fiTs) = ki is an integer for every output yi, then the sinusoidal output in the ith channel is a repeating sequence with a period of ki samples. 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 look-up method at the expense of many more floating-point operations.
Table Lookup. The table look-up 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 if 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/(fiTs) = ki must be an integer value for every channel i = 1, 2, ..., N. When the Optimize table for parameter is set to Speed, the table constructed for each channel contains ki elements. When the Optimize table for parameter is set to Memory, the table constructed for each channel contains ki/4 elements.
For long output sequences, the table look-up method requires far fewer floating-point operations than any of the other methods, but may 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.
Differential. The differential method uses an incremental (differential) algorithm rather than one based on absolute time. The 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.
The update equations for the sinusoid in the ith channel, yi, can therefore be written in matrix form (for real output) as
where Ts is specified by the Sample time parameter. Since Ts is constant, the right-hand matrix is a constant and can be computed once at the start of the simulation. The value of Aisin[2fi(t+Ts)+
i] is then computed from the values of sin(2
fit+
i) and cos(2
fit+
i) by a simple matrix multiplication at each time step.
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).
Examples
The dspsinecomp
demo provides a comparison of all the available sine generation methods.
Dialog Box
ufix
, sint
, uint
, sfrac
, and ufrac
functions from the Fixed-Point Blockset. This parameter is only visible if User-defined is selected for the Output data type parameter.Supported Data Types
To learn how to convert to the above data types in MATLAB and Simulink, see Supported Data Types and How to Convert to Them.
See Also
Chirp |
DSP Blockset |
Complex Exponential |
DSP Blockset |
Signal From Workspace |
DSP Blockset |
Signal Generator |
Simulink |
Sine Wave |
Simulink |
sin |
MATLAB |
Also see the following topics:
![]() | Signal To Workspace | Singular Value Decomposition | ![]() |