The spectrum analyser applet simulates the operation of an FFT spectrum analyser, with its input signal provided by a function generator. The controls on the lower panel of the applet allow the user to choose the function generator settings.
The settings available are as follows:
The spectrum display shows the magnitude spectrum across the frequency baseband from zero frequency to the folding or Nyquist frequency of 4000 Hz. The magnitude scale is linear, and is adjusted automatically based on the largest component of the spectrum.
The spectral magnitude of any given frequency component is calculated by squaring and adding the sine and cosine coefficients for that frequency and taking the square root, giving the modulus of the complex FFT value. The phase spectrum, corresponding to the argument of the complex FFT value, can in principle be determined from the sine and cosine coefficients, but is not shown. (In most practical applications of the FFT, only the magnitude spectrum is of interest and phase information is not required).
A few simple demonstrations using the applet will clarify what the spectrum display shows.
Try generating a sine wave with a frequency of 1000 Hz (the default setting when the applet starts). Leave the DC level and random noise options unchecked at this stage. The number of samples doesn't matter much for this demonstration; you could leave it at the default value of 256.
Click the "Plot signal" button. The sine wave signal should be plotted on the upper graph.
The period of the 1000 Hz sine wave is 1 ms. Since the signal is sampled 256 times at intervals of 1 / 8000 s (0.125 ms), the sample length (duration of the sampled signal) is
256 × 0.125 ms = 32 ms
The signal plot should therefore comprise 32 complete cycles of the sine wave .
Now click the "Spectrum" button. Once the FFT has been evaluated, the signal spectrum should be plotted on the lower graph. This should consist of a single vertical line or bar corresponding to the sine wave frequency of 1000 Hz, i.e. a quarter of the way along the frequency scale from 0 to 4000 Hz. At all other frequencies across the frequency baseband, the spectral magnitude is zero.
The plot shows that a pure sine wave signal has a spectrum consisting of a single "line" or peak at the frequency of the wave - the simplest possible type of frequency spectrum. Such a spectrum is sometimes described as monochromatic (literally, "single colour") by analogy with the optical spectrum of a light source, such as a laser, which produces light of a single specific frequency.
Repeat this with a cosine wave of the same frequency (1000 Hz). The spectrum should appear identical, since a cosine wave is the same as a sine wave apart from a phase difference of 90o and the plot does not depend on the phase of the signal.
Try further sine or cosine waves with frequencies in the range 0 to 4000 Hz and confirm that in each case the spectrum consists of a single line or peak at the appropriate position on the frequency scale. You may find that in some cases the spectrum is shown as a narrow band of frequencies rather than a single sharp line. This effect is called spectral leakage, and will be discussed later.
Later, we will look at what happens if we break the "frequency limit" of 4000 Hz, producing the effect known as aliasing.
In the previous section, we applied the FFT to a pure sinusoidal signal. Such a signal contains only one frequency component, equal to the frequency of the sine wave itself, so the resulting spectrum consists of a single sharp peak (except where spectral leakage occurs, as mentioned above).
We will now have a look at the spectra of other important types of waveform: square, triangular and sawtooth waves.
It can be shown theoretically (from Fourier's theorem) that a square wave can be built up (synthesised) by adding sine waves with frequencies equal to the "fundamental" frequency of the square wave itself, together with an infinite number of odd harmonics, the amplitude of the Nth harmonic being 1/N of the amplitude of the fundamental component.
Conversely, if we decompose a square wave into its sinusoidal components using the Fast Fourier Transform, we should get a spectrum consisting of the fundamental plus the odd harmonics of the square wave frequency, with amplitudes in the ratios
1 : 1/3 : 1/5 : 1/7 : 1/9 : ...
respectively. (In practice, because the square wave is sampled at a finite rate, we can determine its frequency components only up to the limiting folding frequency).
To demonstrate this, first generate a square wave signal: select a square wave with a frequency of 500 Hz and choose 256 samples. (Leave the DC level and random noise options unchecked as before).
Click the "Plot signal" button, then once the signal has been plotted, click "Spectrum".
The spectrum should contain components at odd harmonics of the 500 Hz fundamental frequency (500, 1500, 2500 and 3500 Hz, i.e. 1×, 3×, 5× and 7× the fundamental frequency). The amplitudes of the 3rd, 5th and 7th harmonics should be roughly 1/3, 1/5 and 1/7 of the fundamental.
Components above 4000 Hz (the 9th, 11th, 13th ... harmonics) undergo aliasing, which we will discuss and demonstrate later.
Generate and plot the spectra of square waves of other frequencies and check that the appropriate harmonics are shown.
Application of Fourier's theorem to a triangular wave shows that the spectrum should consist of a fundamental component and odd harmonics, as for a square wave, but with the amplitudes of the harmonics decreasing more rapidly, in the ratio 1/N2 rather than 1/N.
To confirm this, generate a triangular waveform of frequency 500 Hz, with 256 samples as before. Display the spectrum of this signal and confirm that it consists of the 500 Hz fundamental, together with odd harmonics with amplitudes in the ratios 1 : 1/9 : 1/25 : etc..
Generate a 500 Hz sawtooth wave and plot its spectrum.
You should find that the spectrum consists not only of odd harmonics (1st, 3rd, 5th etc.) but also even harmonics (2nd, 4th, 6th etc.), with amplitudes decreasing in the ratio 1 / N: in other words, the amplitude of the 2nd harmonic (at a frequency of 1000 Hz) should be 1/2 the fundamental, the 3rd harmonic (1500 Hz) is 1/3 of the fundamental, and so on.
You might want to try other frequencies of sawtooth wave to confirm this pattern.
Select a sine wave signal of frequency 1000 Hz. Check the "Add DC level" check box, enter a value of 0.5 (volts) in the text field for the amount of the DC offset, and plot the signal. The graph should show a sine wave shifted upwards by the 0.5 V offset (remember the sine wave itself has an amplitude of 1 V).
You should find that the spectrum consists of a line at 1000 Hz, due to the sine wave, together with a line at zero frequency, with a height of 0.5 V. This zero frequency component is due to the DC level. In terms of the spectrum of a signal, any constant (DC) level shows up as a zero frequency component.
Confirm this by adding varying amounts of DC (both positive and negative) to the signal. Note that since the plot shows only the magnitude of the various frequency components, changing the sign of any DC offset has no effect on the spectrum.
Real-life signals often contain appreciable amounts of "noise", i.e. a randomly-varying voltage superimposed on the signal, arising from various sources. To illustrate the effect of noise on the spectrum of a signal, we will generate a simple sine wave with added random noise.
Select a sine wave of frequency 1000 Hz. Check the "Add random noise" checkbox and enter a value of 0.5 (volts). (Ensure the "Add DC level" checkbox is unchecked to keep things simple).
Plot the signal and its spectrum. The signal plot should still look reasonably periodic, roughly resembling a sine wave, despite the added random noise. This is because the signal/noise ratio is fairly high (2 : 1).
The spectrum of the "noisy sine wave" should consist of a strong 1000 Hz peak, corresponding to the frequency of the sine wave, together with components of small, randomly-varying amplitudes spread across the entire frequency spectrum. When the FFT is applied to a signal containing random noise, the noise amplitude tends to be spread more or less evenly across the frequency baseband, giving an approximately flat background level from which the true signal components can be seen emerging.
Now generate another noisy sine wave, with the same parameters as before, except with the noise amplitude increased to 2 V.
The signal plot is no longer clearly recognisable as being periodic because of the low signal/noise ratio (1 : 2). In the spectrum plot, however, because the noise power is spread out across the spectrum, the underlying sine wave component at 1000 Hz stands out from the noise background.
In real life, the signal will probably not be a simple sine wave, but contain many frequency components. The spectrum of such a signal obtained using the FFT will reveal the more important components, while weaker features may be "swamped" in the noise background. This demonstration illustrates the effectiveness of the FFT in picking out periodic (repeating) components of signal, even when it is affected by relatively high noise levels.
One requirement of the FFT algorithm is that the number of samples of the signal (N, say) must be equal to a power of 2, i.e.
N = 2n
where n is a positive integer (n = 1, 2, 3, ...). In cases where the actual number of samples N does not satisfy this requirement, the sampled signal can be "padded out" with extra samples, arbitrarily set to a value of zero, to make up the required number (the next higher power of 2). The time taken to compute the FFT of a signal increases significantly as N is made larger, since this affects the number of calculations that have to be performed.
If the signal is sampled at time intervals h, then the sample length, i.e the total duration of the sampled signal, is simply Nh. For example, if we sample a signal 256 ( = 28) times at intervals of 20 µs, the sample length will be
Nh = 256 × 20 µs = 5.12 ms
According to the Sampling Theorem, the highest frequency component of a sampled signal that can be correctly identified without aliasing is the folding or "Nyquist" frequency
fN = fs / 2 = 1 / 2h
where fs is the sampling rate (number of samples per second) and h is the sampling interval (time interval between successive samples). The FFT procedure gives the amplitudes of the sine and cosine components of the signal over the frequency "baseband" from 0 to fN.
For example, for a sampling rate fs = 50 kHz (50000 samples/s), the folding frequency fN is 25 kHz. The spectrum of the sampled signal obtained using the FFT therefore extends over a frequency range from 0 to 25 kHz.
Note that this frequency baseband is fixed by the sampling process and not by the frequencies present in the signal. If the signal contains any components with frequencies beyond the baseband, these will undergo aliasing and the spectrum obtained from the sampled signal will be incorrect.
The zero frequency component in the spectrum of a signal is simply the DC component of the signal. The other frequency components are all multiples (harmonics) of the fundamental frequency. In order to apply Fourier's theorem, we must treat the signal as being periodic, with a period equal to the sample length. In other words, the signal, rather than existing only during the time while it was being sampled, is assumed to consist of endless repetitions of the captured segment of the signal.
If the assumed period of the sampled signal is the sample length Nh, then the fundamental frequency (the assumed frequency of repetition of the sampled signal) must be 1 / Nh (since frequency = 1 / period). The frequency components in the spectrum computed by the FFT are integer multiples (harmonics) of this frequency: 1 / Nh, 2 / Nh, 3 / Nh ... etc. The number of frequency components covering the baseband is N / 2.
The spectral resolution, i.e. the frequency difference between adjacent frequency components, is therefore equal to 1 / Nh.
As the name suggests, spectral resolution indicates the level of detail or resolution in the frequency spectrum. If the spectral resolution is too coarse (i.e. too large), fine detail in the spectrum may be lost. To resolve fine detail in the spectrum of a signal, we need to make Nh large, i.e. to sample the signal over a longer time. This can be done either by taking a large number of samples N, or by making the sampling interval h large, or perhaps both. If we make the sampling interval too large, however, aliasing of higher-frequency components of the signal may occur. On the other hand, increasing the number of samples has the drawback of increasing the time taken for the processor to compute the FFT, and the amount of memory required to store the data during processing.
When a signal is sampled for spectral analysis, values for N and h must be chosen so that the sample length is large enough to give adequate frequency resolution in the signal spectrum, without the computation time becoming unacceptably long or introducing unwanted aliasing of higher-frequency components.
A signal is sampled 256 times at 20 µs intervals. The sample length is
Nh = 256 × 20 µs = 5.12 ms
and the spectral resolution is therefore
1 / Nh = 1 / 5.12 ms = 200 Hz (approx).
The number of harmonics (frequency channels) determined by the FFT is
N / 2 = 256 / 2 = 128
The sampling rate is
fs = 1 / h = 1 / 20 µs = 50 k samples/second
and the folding frequency is
fN = fs / 2 = 25 kHz
The frequency spectrum determined using the FFT will thus contain 128 components with frequencies ranging from 0 to 25 kHz, at intervals of approximately 200 Hz.
A speech signal with a bandwidth of 4 kHz is to be sampled so that its spectrum can be determined using the FFT. The required spectral resolution is 10 Hz. Select suitable values for the sampling interval and number of samples.
According to the Sampling Theorem, the largest sampling interval we can use for a signal containing frequency components up to 4 kHz is
h = 1 / (2 × 4 kHz) = 0.125 ms
To achieve a spectral resolution of 10 Hz, the sample length must be at least
Nh = 1 / 10 Hz = 100 ms
Thus the number of samples must be at least N where
N = 0.1 s / h = 100 ms / 0.125 ms = 800
Since N must be a power of 2, we have to round it up from 800 to the next power of 2, which is 210 = 1024. This gives us a spectral resolution slightly better than required.
To illustrate the foregoing discussion, set up a sine wave signal with a frequency of 1000 Hz and with a random noise level of 1 V (ensure the Add random noise checkbox is checked). The purpose of the noise component is to give a rich spectral content to show the varying levels of detail in the spectrum depending on the number of samples used. Try plotting the signal and its corresponding spectrum for various numbers of samples (32, 64, 128, 256 and 512). It should be easy to see that as the number of samples is increased, the frequency resolution in the spectrum improves, i.e. the number of frequency components shown increases, while the interval between them becomes smaller.
|
Since the sampling interval h is fixed at 0.125 ms, the spectral resolution is given by 1 / Nh = 8000 Hz / N where N is the number of samples. The table shows the spectral resolution (in Hz) obtained for each choice of the number of samples N. |
|
The spectrum analyser applet uses the FFT to compute and display the spectrum of a simulated signal sampled at a rate of 8000 samples / second. The highest frequency which can be correctly reproduced, in accordance with the sampling theorem, is half the sampling rate, or 4000 Hz. (The 8000 samples/s rate chosen for the demonstration is the standard sampling rate for speech signals in digital telecommunications).
So what happens if we try to plot the spectrum of a signal containing components above the 4000 Hz maximum? Let's find out.
Set up a sine wave signal with a frequency of 7000 Hz. Choose 256 samples. (The number of samples doesn't really matter, but if we choose a reasonably large number, the better frequency resolution will show things more clearly). Disable random noise and the added DC level to keep things simple.
Plot the signal. It should look like a perfectly good sine wave, although if you count the number of cycles and work out the apparent frequency you should begin to suspect something funny is happening.
Now plot the spectrum. A nice peak appears, which you would expect by now with a sine wave signal. But hold on a minute! This is supposed to be a 7000 Hz signal, and the frequency scale only goes up to 4000 Hz. How come a peak shows in the spectrum? It looks as though our 7000 Hz signal has magically been changed into a lower frequency sine wave, to give a spectral line in the 0 - 4000 Hz baseband.
As an experiment, change the sine wave frequency setting from 7000 to 1000 Hz and replot the signal. Apart from a subtle phase shift, the signal trace should look no different. What's more, if you click the "Spectrum" button, you'll find that there is no change in the spectrum plot at all. What is going on?
The answer is that our 7000 Hz sine wave is being undersampled, and the sampled version of the signal actually looks just like a lower frequency sine wave - one with a frequency of 1000 Hz, in fact. That's why there was no difference in the spectra of the 7000 Hz and 1000 Hz signals.
This effect is called aliasing. An "alias" is an alternate identity; often, a false one. If a frequency component of a signal is undersampled (i.e. sampled at a rate below the minimum prescribed by the sampling theorem), it appears as though it has a lower "alias frequency". In this demonstration, the sampling rate is 8000 samples/s and the maximum frequency that can be correctly reconstructed from samples is half this value, i.e. 4000 Hz. The 7000 Hz signal we sampled appears as an alias frequency of 1000 Hz.
Aliasing is potentially a very serious problem in any system that processes sampled signals. It means that higher-frequency components of a complex signal could appear as though they were of lower frequency, and it would be impossible to reconstruct correctly the information contained in the original signal. If this happened in digital telephone systems, for example, speech would sound strange and probably unintelligible. Digital audio recordings which failed to sample the sound signal at a sufficient rate would result in extremely poor reproduction; similar problems would occur wherever the requirements of the sampling theorem failed to be met.
What can be done to avoid or prevent aliasing? Briefly, there are two approaches:
The Java 1.1 version of the spectrum analyser applet allows a digital anti-aliasing filter to be applied to the sampled signal to remove frequency components above the folding (Nyquist) frequency, so that the computed spectrum is unaffected by aliasing.
You might like to experiment further with the phenomenon of aliasing, by sampling sine waves of various frequencies above the 4000 Hz limit. Try to work out the relationship between the true signal frequency and its "alias" frequency. (As a hint, the 4000 Hz limit is sometimes called the "folding frequency"). The occurrence of aliasing with a sinusoidal input signal is illustrated graphically by the aliasing demo applet.
You could also try looking at the spectrum of a square wave with a frequency of 2500 Hz. Why would this cause aliasing, if the signal frequency is within the 4000 Hz limit? The answer is that although the square wave signal itself does indeed have a frequency of 2500 Hz, when it is resolved into its sine and cosine wave frequency components, these comprise not only a 2500 Hz fundamental but also harmonics at 3×, 5×, ... etc. the fundamental frequency. These higher harmonics exceed the 4000 Hz frequency limit and so are subject to aliasing; you may have trouble trying to sort out which peak in the spectrum belongs to which harmonic!
We saw earlier that the frequency spectrum of a 1000 Hz sine (or cosine) wave consists of a single sharp line. As you may already have found, however, sine waves of other frequencies do not in general have such "clean" spectra. To illustrate this, generate a 1100 Hz sine wave and plot its spectrum. Instead of a single line, the spectrum contains many frequency components, spread over a range of frequencies centred around 1100 Hz.
This spreading out of spectral energy across several frequency "channels" is called spectral leakage. Spectral leakage affects any frequency component of a signal which does not exactly coincide with a frequency channel. Since the frequency components of an arbitrary signal are unlikely to satisfy this requirement, spectral leakage is more likely to occur than not with real-life sampled signals.
To demonstrate this, generate a square wave signal with a frequency of 400 Hz and plot its spectrum. The square wave has frequency components at the fundamental frequency (400 Hz) and at odd harmonic frequencies (1200 Hz, 2000 Hz, 2800 Hz etc.). Of the components within the frequency baseband (0 - 4000 Hz), only the 5th harmonic at 2000 Hz coincides with one of the frequency channels shown in the spectrum. You should find that all the spectral components of the 400 Hz square wave are subject to leakage, with the exception of the 2000 Hz component.
Spectral leakage occurs when a frequency component of a signal does not slot exactly into one of the frequency channels in the spectrum computed using the discrete Fourier transform. These "frequency channels", the frequencies represented by lines in the spectrum, are exact integer multiples (harmonics) of the fundamental frequency 1/Nh. A sine wave with a frequency coinciding with one of these frequency channels has the property that you can fit an exact integer number of sine wave cycles into the complete sample length of the sampled signal. (The number of cycles is just the harmonic number).
To illustrate this, plot a 1000 Hz and then a 1100 Hz sine wave (don't bother to plot the spectra). Since the sampled 1000 Hz sine wave comprises exactly 32 cycles, as discussed earlier, you should find that in the time domain plot, the beginning and end of the 1000 Hz sine wave match up, rather like a wallpaper pattern. For the 1100 Hz sine wave, however, there are 35.2 cycles of the sampled waveform, and it is the odd 0.2 of a cycle left over at the end that causes spectral leakage. This is because the discrete Fourier transform actually finds the frequency components of a signal which consists of endlessly repeated copies of the sampled signal - like patterned wallpaper on a wall of infinite width. Those frequency components of the signal for which there is a "pattern match" at the two ends give a smooth join where adjacent sections meet. More generally, however, this is not achieved, and there is a mismatch where adjacent sections join. The sudden jump or discontinuity created by the pattern mismatch gives rise to the spurious components in the spectrum of the signal, causing a particular frequency component of the signal to appear not as a single sharp line but as a spread of frequencies, roughly centred around where the frequency component should be located, somewhere between the two nearest frequency channels either side.
Remember that "real life" signals are not simple sine waves; a speech waveform, for example, contains many components of different frequencies, and it is extremely unlikely that there will be a smooth match at the beginning and end of the sampled signal. Spectral leakage is therefore almost certainly going to affect the spectrum of any signal of practical interest.
Spectral leakage causes energy (amplitude) from distinct spectral features to "leak" into adjacent frequency channels, giving rise to spurious components in the frequency spectrum of the signal. The only way to avoid such leakage entirely would be to arrange that all the frequency components of the signal being examined coincide exactly with frequency channels in the computed spectrum. This, however, is impractical for an arbitrary signal containing many (usually unknown) frequency components.
While spectral leakage cannot in general be eliminated completely, its effects can be reduced. This is done by applying a tapered window function to the sampled signal. The sampled values of the signal are multiplied by a function which tapers toward zero at either end, so that the sampled signal, rather than starting and stopping abruptly, "fades" in and out like some music CD tracks. This reduces the effect of the discontinuities where the mismatched sections of the signal join up, and hence also the amount of leakage. There is, however, a downside to the use of a tapered window function: lines in the spectrum of a signal become broadened, making it difficult to distinguish separate frequency components, and therefore effectively reducing the spectral resolution.
A number of tapered window functions have been devised. Some are more effective in reducing spectral leakage, at the expense of loss of spectral detail, while others to try to achieve a compromise between these conflicting requirements. A popular choice is the Hanning (or von Hann) window function, which is essentially one cycle of an inverted cosine function, shifted and scaled so that it has a maximum value of 1 in the middle and tapers to zero at either end. The Java 1.1 version of the spectrum analyser applet allows a tapered data window function to be applied to the sampled signal prior to determining the FFT of the signal.
FFT Spectrum Analyser applet: 1.02 version | 1.1 version