Waveform expressions in the FFT spectrum analyser demonstration


The waveform is expressed as a function of time t (seconds) and is constructed using fairly standard notation. The arguments of the functions sin and cos are in radians; the value of piis represented by pi. A sine wave of frequency f (Hz), for example, is expressed as

sin(2*pi*f*t)

with an appropriate numerical value substituted for f. Note that multiplication must be indicated explicitly by an asterisk.

The overall amplitude of the waveform function is immaterial since the plot is automatically scaled to accommodate the maximum sampled value; however, the relative amplitudes of different components are significant. This means that the following expressions will yield identical results in terms of the graphical display of the waveform and its spectrum:

sin(1000*pi*t) + 0.5*sin(2000*pi*t)

2*sin(1000*pi*t) + sin(2000*pi*t)

Additional functions

In addition to the standard sin and cos functions, the following non-standard functions can also be used in waveform expressions:

sqr()
Square wave function.
Example: sqr(2000*pi*t) gives a 1000 Hz square wave.
tri()
Triangular wave function.
Example: tri(2000*pi*t) gives a 1000 Hz triangular wave.
saw()
Sawtooth wave function.
Example: tri(2000*pi*t) gives a 1000 Hz sawtooth wave.
noise()
Random noise function.
Example: noise() (note the function has no argument).
Generates pseudo-random values with an approximately normal (Gaussian) distribution with zero mean and unit standard deviation. The noise amplitude can be controlled simply by multiplying by a constant: for example, 0·2*noise().
Note that this function may take some time to evaluate on a slower machine.

Examples

The above functions can be added, multiplied etc. to obtain the required waveform, as illustrated by the examples listed below.

Waveform Expression
1V, 1000 Hz sine wave with 0·5V DC level sin(2000*pi*t) + 0.5
1V, 1000 Hz sine wave with 0·5V random noise sin(2000*pi*t) + 0.5*noise()
Sum of 1V, 1000 Hz sine wave and 2V, 2000 Hz sine wave sin(2000*pi*t) + 2*sin(4000*pi*t)
Product of 1000 Hz and 250 Hz sine waves sin(2000*pi*t)*sin(500*pi*t)
250 Hz square wave sqr(500*pi*t)
1500 Hz triangular wave with Hanning window function tri(3000*pi*t)*hann(t)

Tapered data window function

Function hann()
Hanning window function.
Usage: hann(t)
Note the argument must simply be t, with no multiplying frequency factor.

The Hanning window function is widely used to reduce spectral leakage in FFT-based spectrum analysers. To apply this tapered data window, the waveform expression should be multiplied by the hann(t) function.

Example

With a sampling rate of 8000 sample/s and 256 samples selected, generate a 1100 Hz sine wave using the expression

sin(2200*pi*t)

Plot the spectrum and note that there is significant spectral leakage (signal amplitude "leaking" out into adjacent frequency channels, rather than being confined to a single sharp spectral line).

Now modify the waveform expression to read

sin(2200*pi*t)*hann(t)

so that the waveform tapers at either end. Plot the signal and its spectrum and note how the leakage is significantly reduced. The application of a tapered data window prior to the FFT has the side-effect of the broadening of major spectral features, and hence an effective reduction in spectral resolution.

The Java 1.1 spectrum analyser applet offers a selection of tapered data window functions which can be applied to the sampled signal.


Back to Java/JavaScript FFT spectrum analyser demo

DSP home page