Functions > Signal Processing > Spectral Analysis > Example: Spectral Analysis
Example: Spectral Analysis
Use the coherence, cspectrum, pspectrum, and snr functions to calculate the coherence, cross spectrum, power spectrum, and signal-to-noise ratio for a signal or pairs of signals.
A description of this averaging procedure is found in Signal Processing Algorithms by Samuel Stearns and Ruth David (Prentice-Hall, Inc.).
Power Spectrum - Using DFT
For long time series where only few data points have values corresponding to a signal, it is desirable for the power spectrum to show the frequencies of the signal, but not the noise or zero values.
1. Define the window width.
Click to copy this expression
Click to copy this expression
2. Define two possible windows, w1 and w2, in which the signal is not zero.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
3. Set the window width to one of the two possible widths.
Click to copy this expression
4. Define the signal and sampling frequencies.
Click to copy this expression
Click to copy this expression
5. Use the sin function to define the signal inside and outside the selected window.
Click to copy this expression
Click to copy this expression
6. Plot the signal.
Click to copy this expression
7. Use the dft function to calculate the Discrete Fourier Transform of x.
Click to copy this expression
Click to copy this expression
8. Plot the resulting function in the frequency domain.
Click to copy this expression
Click to copy this expression
For a pure sine wave signal, the magnitude of the DFT coefficients provides a good estimate of its power spectrum. But in this case, the DFT of the signal is the frequency content of the structured signal convolved with the transform of a rectangular window (a sinc function).
Setting W= w2 (Step 3) widens the window in which the sine wave signal exists from 230–300 to 0–499 (a factor of 7.143x), results in increasing the magnitude of the two peaks from 35 to 250 (also a factor of 7.143x).
The frequencies at which the peaks occur remain unchanged.
Power Spectrum - Using pspectrum
The pspectrum function breaks the original time series into overlapping segments. Each of these segments is transformed by a DFT, and the coefficients of the transformed magnitudes are averaged. If the length of the segments is of the same scale as the phenomena to be analyzed in the signal, then the pspectrum function returns a good approximation to the magnitude of the components of the interesting parts of the original time series.
In general, the length of the signal should be at least n + 1.
1. Define the length of the signal, the overlap fraction, and the window type (tapered rectangular).
Click to copy this expression
Click to copy this expression
Click to copy this expression
2. Use the pspectrum function to compute the power spectrum of x computed by dividing x into n1 overlapping segments with overlap fraction r and windowing each segment with a tapered rectangular window.
Click to copy this expression
3. Use the floor and length functions to show that the interval length is the largest number L for which:
Click to copy this expression
4. Plot the spectrum with a normalized frequency axis (1 represents the sampling frequency).
Click to copy this expression
Click to copy this expression
Click to copy this expression
5. Calculate the average power in the spectrum.
Click to copy this expression
This is approximately the mean square value of signal x.
Click to copy this expression
pspectrum with White Noise
Calculate the power spectrum of a signal that contains white noise. Generate two such signals, since the second signal is needed for cross spectrum calculations. Use a Hamming window to taper the data block.
1. Define the signal width.
Click to copy this expression
Click to copy this expression
2. Use the sin and rnd functions to define two signals with a common frequency fc. The rnd function returns a vector of uniformly distributed random numbers between 0 and the specified number and thus ensures that the signals remain different.
Click to copy this expression
Click to copy this expression
Click to copy this expression
3. Plot the first 100 samples of the two signals.
Click to copy this expression
4. Use the pspectrum function to get the power spectrum of y1 computed by dividing it into 40 overlapping segments with overlap fraction of 0.5 and windowing each segment with a Hamming window.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
5. Use the match and max functions to find the peak sample(s) of the power spectrum.
Click to copy this expression
Click to copy this expression
Click to copy this expression
6. Plot the spectrum in decibels. Use markers to mark the common frequency and the maximum decibel value.
Click to copy this expression
Click to copy this expression
Click to copy this expression
The plot shows that all the signal power occurs at the common frequencies fc and 1-fc.
7. Calculate the noise power.
Click to copy this expression
8. Calculate the noise power gain in dB.
Click to copy this expression
9. Calculate the height using the fact that the above two frequency samples divided by the spectrum length should give the average power in the sine component, which is 0.5.
Click to copy this expression
Or:
Click to copy this expression
10. Compare in decibels the theoretical and actual heights.
Click to copy this expression
Click to copy this expression
The cspectrum Function
Typically, the cross spectrum is used to detect similarities in two signals, for example comparing a known speech waveform (say the vowel 'a') with an unknown speech waveform (say the word 'apple') to see if the known waveform is present in the unknown waveform. Just as the power spectrum of a time series is similar to its auto-correlation, the cross spectrum for two time series is similar to their cross-correlation. For stationary random sequences, the spectrum functions return the same values as the correlation functions.
1. Apply the cspectrum function to signals y1 and y2, using 40 overlapping segments with overlap fraction of 0.5 and windowing each segment with a tapered rectangular window.
Click to copy this expression
2. Plot the spectrum in decibels.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
3. Plot the cross spectrum, keeping in mind that it is complex and therefore the log should be applied to the magnitude.
Click to copy this expression
The cross spectrum of y1 and y2 shows a peak at the common frequencies fc and 1- fc.
The coherence and snr Functions
The coherence function measures the linear dependence of one signal on another, is equal to the squared magnitude of the cross spectrum of two signals divided by both power spectra, and ranges in value from zero to one. Values of 1 for the coherence function tend to indicate that both signals have strong noise-free components in that frequency band, while values of 0 indicate that there is mostly noise in that frequency band.
The signal-to-noise ratio snr function is equal to the coherence function divided by one minus the coherence function, and is maximized where the two signals have strong components in any component bands, proportional to the ratio between the strengths of the signals and the strength of sensor or background noise.
1. Apply the coherence function to signals y1 and y2, using 40 overlapping segments with overlap fraction of 0.5 and windowing each segment with a Hamming window.
Click to copy this expression
Click to copy this expression
Click to copy this expression
2. Plot the coherence function. Use markers to mark the common frequency and the maximum value.
Click to copy this expression
Click to copy this expression
3. Apply the snr function to signals y1 and y2, using 40 overlapping segments with overlap fraction of 0.5 and windowing each segment with a Hamming window.
Click to copy this expression
Click to copy this expression
Click to copy this expression
4. Plot the signal-to-noise ratio function. Use markers to mark the common frequency and the maximum value.
Click to copy this expression
Click to copy this expression
Both the coherence and snr functions exhibit a maximum value at the single frequency components of the two signals with added noise represented by the two smaller bars on either side.
Was this helpful?