Generate discrete sine wave
object™ generates a real or complex, multichannel sinusoidal signal with independent
amplitude, frequency, and phase in each output channel.
For both real and complex sinusoids, the Amplitude, Frequency, and PhaseOffset properties can be scalars or length-N vectors, where N is the number of channels in the output. When you specify at least one of these properties as a length-N vector, scalar values specified for the other properties are applied to each of the N channels.
To generate a discrete-time sinusoidal signal:
Create the dsp.SineWave object and set its properties.
Call the object with arguments, as if it were a function.
To learn more about how System objects work, see What Are System Objects? (MATLAB).
sine = dsp.SineWave
sine = dsp.SineWave(Name,Value)
sine = dsp.SineWave(amp,freq,phase,Name,Value)
creates a sine wave
object that generates a real-valued sinusoid with an amplitude of 1, a frequency of 100
Hz, and a phase offset of 0. By default, the sine wave object generates only one
sine = dsp.SineWave
Example: sine = dsp.SineWave('Amplitude',2);
creates a sine wave object with each specified property set to the specified value.
Enclose each property name in single quotes.
sine = dsp.SineWave(
Unless otherwise indicated, properties are nontunable, which means you cannot change their
values after calling the object. Objects lock when you call them, and the
release function unlocks them.
If a property is tunable, you can change its value at any time.
For more information on changing property values, see System Design in MATLAB Using System Objects (MATLAB).
Amplitude— Amplitude of sine wave
1(default) | scalar | vector
Amplitude of the sine wave, specified as one of the following:
This property is tunable only when you set Method to either
Frequency— Frequency of sine wave
100(default) | scalar | vector
Frequency of the sine wave in Hz, specified as one of the following:
PhaseOffset— Phase offset of sine wave
0(default) | scalar | vector
Phase offset of the sine wave in radians, specified as one of the following:
ComplexOutput— Flag that indicates whether waveform is real or complex
Flag that indicates whether the waveform is real or complex, specified as either:
false –– The waveform output is real.
true –– The waveform output is complex.
Method— Method used to generate sinusoids
'Trigonometric function'(default) |
Method used to generate sinusoids, specified as one of the following:
'Trigonometric function' –– The object computes the
sinusoid by sampling the continuous-time function.
'Table lookup' –– The object precomputes the unique samples
of every output sinusoid at the start of the simulation, and recalls the samples
from memory as needed.
'Differential' –– The object 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.
TableOptimization— Optimize table of sine values for speed or memory
Optimize table of sine values for speed or memory, specified as either:
'Speed' –– The table contains k
elements, where k is the number of input samples in one full
period of the sine wave. The period of each sinusoid must be an integer multiple
of 1/Fs, where Fs is the value of the SampleRate property value. That is, each
element of the Frequency property must be of the form
Fs/m, where m is an
integer greater than
'Memory' –– The table contains k/4
This property applies only when you set the
SampleRate— Sample rate of output signal
1000(default) | positive scalar
Sample rate of output signal in Hz, specified as a positive scalar.
SamplesPerFrame— Number of samples per frame
1(default) | positive integer
Number of consecutive samples from each sinusoid to buffer into the output frame, specified as a positive integer.
OutputDataType— Data type of the sine wave output
Data type of the sine wave output, specified as
CustomOutputDataType— Output word and fraction lengths
For versions earlier than R2016b, use the
function to run the System object™ algorithm. The arguments to
step are the object you created, followed by
the arguments shown in this section.
y = step(obj,x) and
y = obj(x) perform equivalent operations.
sineOut = sine()
creates the sine wave
sineOut = sine()
sineOut— Sine wave output
Sine wave output, returned as a vector or matrix. The SamplesPerFrame property determines the number of
rows in the output matrix. If the Frequency or the PhaseOffset property is a vector, the length of the
vector determines the number of columns (channels) in the output matrix. If the
Frequency or the
PhaseOffset properties is
a scalar, then the number of channels in the output matrix is 1.
The OutputDataType property sets the data type of the output.
To use an object function, specify the
object as the first input argument. For
example, to release system resources of a System
|Run System object algorithm|
|Create duplicate System object|
|Number of inputs required to call the System object|
|Number of outputs from calling the System object|
|Determine if System object is locked|
|Release resources and allow changes to System object property values and input characteristics|
|Reset internal states of System object|
Note: If you are using R2016a or an earlier release, replace each call to the object with the equivalent
step syntax. For example,
Generate a sine wave with an amplitude of 2, frequency of 10 Hz, and an initial phase of 0.
sine1 = dsp.SineWave(2,10); sine1.SamplesPerFrame = 1000; y = sine1(); plot(y)
Generate two sine waves offset by a phase of pi/2 radians.
sine2 = dsp.SineWave; sine2.Frequency = 10; sine2.PhaseOffset = [0 pi/2]; sine2.SamplesPerFrame = 1000; y = sine2(); plot(y)
A real-valued, discrete-time sinusoid is defined as:
A complex sinusoid is defined as:
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.
At each sample time, the algorithm 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 algorithm avoids the imprecision of computing the sine of very large numbers. This constraint also 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 algorithm optimizes the table of sine values for
table constructed for each channel contains ki
elements. When the optimization is for
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. However, the method 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, which need to be optimized for execution speed.
The lookup table for this object is constructed from double-precision floating-point
values. 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 data type to values greater than 53 bits does not improve the precision of your
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. 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.
Usage notes and limitations:
This object has no tunable properties for code generation.
See System Objects in MATLAB Code Generation (MATLAB Coder).