Sine Wave - Generate continuous or discrete sine wave - Simulink (original) (raw)

Generate continuous or discrete sine wave

Libraries:
DSP System Toolbox / Sources
DSP System Toolbox HDL Support / Sources

Description

The Sine Wave block generates a multichannel real or complex sinusoidal signal, with independent amplitude, frequency, and phase in each output channel. The block supports floating point and signed fixed-point data types.

The block generates a real sinusoidal signal when you set the Output complexity parameter to Real. The real sinusoidal output 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.

The block generates a complex exponential signal when you set the Output complexity parameter to Complex. This complex exponential signal is defined by an expression of the type

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 following real sinusoids, set the block parameters as shown:

Examples

expand all

This example compares the different methods of generating sample-based sine waves from the Sine Wave block in DSP System Toolbox.

This example compares the different methods of generating frame-based sine waves from the Sine Wave block in DSP System Toolbox™.

Ports

Output

expand all

Output a sinusoidal signal as a scalar or vector. For more information about output complexity, see Description. For information about multichannel support, see Generating Multichannel Outputs.

Tip

To output fixed-point data types, you must set Sample mode to Discrete andComputation method to Table lookup.

Data Types: single | double | fixed point
Complex Number Support: Yes

Parameters

expand all

Main

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 theFrequency and Phase offset parameters.

Tip

This parameter is tunable (Simulink) only when the Computation method is Trigonometric fcn orDifferential.

Tunable: Yes

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 theAmplitude and Phase offset parameters. You can specify positive, zero, or negative frequencies.

Tip

This parameter is tunable (Simulink) when you set:

Tunable: Yes

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 theAmplitude and Frequency parameters.

Tip

This parameter is tunable (Simulink) when you set:

Tunable: Yes

Specify the sampling mode as Continuous orDiscrete:

The type of waveform to generate: Real specifies a real sine wave, Complex specifies a complex exponential.

The method by which discrete-time sinusoids are generated:Trigonometric fcn, Table lookup, or Differential. For more information on each of the available options, see Algorithms.

Dependencies

This parameter is only visible when you set the Sample mode to Discrete.

Note

To generate fixed-point sinusoids, you must set theComputation method toTable lookup.

Optimizes the table of sine values forSpeed orMemory. 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.

Dependencies

This parameter is only visible when you set theComputation method parameter toTable lookup.

The period with which the sine wave is sampled,Ts, as a finite scalar, greater than zero. The output frame period of the block is_M_ Ts, where you specify M in the Samples per frame parameter.

Dependencies

To enable this parameter, set Sample mode toDiscrete.

The number of consecutive samples from each sinusoid to buffer into the output frame, M, specified as a positive scalar integer. This parameter is not tunable.

The block output is an M_-by-N matrix with frame period_M _T_s, where you specify _T_s in theSample time parameter.

Dependencies

To enable this parameter, set Sample mode toDiscrete.

This parameter determines the behavior of the Sine Wave block when an enabled subsystem is reenabled. 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).

Dependencies

This parameter only applies when the Sine Wave block is located inside an enabled subsystem and theStates when enabling parameter of theEnable (Simulink) block is set to reset.

Data Types

Select how you would like to specify the data type properties of theOutput data type. You can choose:

Data Type Assistant

Select how you would like to specify the data type properties of theOutput data type. You can choose:

For more information, see Specify Data Types Using Data Type Assistant (Simulink).

Specify the method for scaling your fixed-point data to avoid overflow conditions and minimize quantization errors. For more information, seeSpecify Data Types Using Data Type Assistant (Simulink).

Dependencies

To enable this parameter, set Mode toFixed point

Specify the bit size of the word that holds the quantized integer as a positive integer from 2 to 128. For more information, see Specify Data Types Using Data Type Assistant (Simulink).

Dependencies

To enable this parameter, set Mode toFixed point

Specify the fraction length of the fixed-point data type as a positive or negative integer scalar.

Dependencies

To enable this parameter, set Mode toFixed point andScaling to Binary point.

Select the data type override mode for this signal. You can select:

Dependencies

To enable this parameter, set Mode toBuilt in or Fixed point. For more information, see Specify Data Types Using Data Type Assistant (Simulink).

Block Characteristics

Data Types double | fixed point integer single
Direct Feedthrough no
Multidimensional Signals no
Variable-Size Signals no
Zero-Crossing Detection no

Algorithms

expand all

When you select Discrete from the Sample mode parameter, the secondary Computation method parameter provides three options for generating the discrete sinusoid:Trigonometric fcn, Table lookup, and Differential.

The trigonometric function method computes the sinusoid in the_i_th channel,_y_i, by sampling the continuous function

with a period of Ts, where you specify_Ts_ in the Sample time parameter. This mode of operation has the same benefits and liabilities as the Continuous sample mode.

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/(fiTs) = ki must be an integer value for every channel i = 1, 2, ..., N.

When the Optimize table for parameter is set toSpeed, the table constructed for each channel contains ki elements. When theOptimize table for parameter is set toMemory, 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 method is recommended for models that are intended to emulate or generate code for DSP hardware, and that therefore must be optimized for execution speed.

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 theOutput or User-defined data type to values greater than 53 bits does not improve the precision of your output.

Tip

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

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 using the following identities.

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

where you specify Ts in theSample 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_Ai_sin[2π_fi_(t+Ts)+ϕi_] is then computed from the values of sin(2π_fi t+ϕi) and cos(2π_fi_ t+ϕ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).

Extended Capabilities

expand all

The Sine Wave block references absolute simulation time when configured in continuous sample mode.

Note

For a sine wave generator that is optimized for HDL code generation and has hardware-friendly control signals, see NCO (DSP HDL Toolbox), and Generate Sine Wave (DSP HDL Toolbox). The DSP HDL Toolbox™ blocks provide HDL-optimized algorithms with hardware-friendly control signals and simulate the latency of the HDL algorithm in Simulink.

HDL Coder™ provides additional configuration options that affect HDL implementation and synthesized logic.

HDL Architecture

This block has one default HDL architecture.

Restrictions

For HDL code generation, you must select the following Sine Wave block settings:

Output:

Complex Data Support

This block supports code generation for complex signals.

Version History

Introduced before R2006a