Sine Wave - Generate continuous or discrete sine wave

Library

Signal Processing Sources

dspsrcs4

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 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

Sections of This Reference Page

Generating Multichannel Outputs

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:

Output Sample Time and Samples Per Frame

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 a frame-based M-by-N matrix with frame period M*Ts, where you specify Ts in 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:

Discrete Computational Methods

When you select Discrete 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 you specify Ts in the Sample time parameter. This mode of operation shares the same benefits and liabilities as the 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.

Table Lookup

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/(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 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.

Differential

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.

The update equations for the sinusoid in the ith channel, yi, can therefore be written in matrix form as

where you specify Ts in 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[2πfi(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

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

Opening this dialog box causes a running simulation to pause. See Changing Source Block Parameters During Simulation in the online Simulink documentation for details.

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 when Computation method is to Trigonometric fcn or Differential.

Frequency

A length-N vector containing frequencies, in rad/s, 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 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 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. This parameter is disabled when you select Continuous from the Sample mode parameter. For details, see Discrete Computational Methods.

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, Ts. The block's output frame period is M*Ts, where you specify 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. When the value of this parameter is 1, the block outputs a sample-based signal.

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 in out of the following ways:

Choose one of the built-in data types from the list.

Choose Fixed-point to specify the output data type and scaling in the Word length, Set fraction length in output to, and Fraction length parameters.

Choose User-defined to specify the output data type and scaling in the User-defined data type, Set fraction length in output to, and Fraction length parameters.

Choose Inherit via back propagation to set the output data type and scaling to match the next block downstream.

Word length

Specify the word length, in bits, of the fixed-point output data type. This parameter is only visible when you select Fixed-point for the Output data type parameter.

User-defined data type

Specify any built-in or fixed-point data type. You can specify fixed-point data types using the following Simulink Fixed Point functions: sfix, ufix, sint, uint, sfrac, and ufrac. This parameter is only visible when you select User-defined for the Output data type parameter.

Set fraction length in output to

Specify the scaling of the fixed-point output by either of the following two methods:

Choose Best precision to have the output scaling automatically set such that the output signal has the best possible precision.

Choose User-defined to specify the output scaling in the Fraction length parameter.

This parameter is only visible when you select Fixed-point for the Output data type parameter, or when you select User-defined and the specified output data type is a fixed-point data type.

Fraction length

For fixed-point output data types, specify the number of fractional bits, or bits to the right of the binary point. This parameter is only visible when you select Fixed-point or User-defined for the Output data type parameter and User-defined for the Set fraction length in output to parameter.

Supported Data Types

See Also

ChirpSignal Processing Blockset
Complex ExponentialSignal Processing Blockset
Signal From WorkspaceSignal Processing Blockset
Signal GeneratorSimulink
Sine WaveSimulink
sinMATLAB

  


 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS