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.
2. Define two possible windows, w1 and w2, in which the signal is not zero.
 w1s 230 w1e 300 w1 w1s w1e w2s 0 w2e 499 w2 w2s w2e
3. Set the window width to one of the two possible widths.
4. Define the signal and sampling frequencies.
5. Use the sin function to define the signal inside and outside the selected window.
6. Plot the signal.
7. Use the dft function to calculate the Discrete Fourier Transform of x.
8. Plot the resulting function in the frequency domain.
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).
 n1 9 r 0.5 trw 2
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.
3. Use the floor and length functions to show that the interval length is the largest number L for which:
4. Plot the spectrum with a normalized frequency axis (1 represents the sampling frequency).
5. Calculate the average power in the spectrum.
This is approximately the mean square value of signal x.
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.
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.
3. Plot the first 100 samples of the two signals.
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.
 n2 40 hw 5 PsP1 pspectrum y1 n2 r hw L1 length PsP1 l1 0 L1
5. Use the match and max functions to find the peak sample(s) of the power spectrum.
6. Plot the spectrum in decibels. Use markers to mark the common frequency and the maximum decibel value.
The plot shows that all the signal power occurs at the common frequencies fc and 1-fc.
7. Calculate the noise power.
8. Calculate the noise power gain in dB.
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.

2 pulseh L1
0.5
Or: pulseh
0.5 L1 2
10. Compare in decibels the theoretical and actual heights.
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.
2. Plot the spectrum in decibels.
 L2 length CsP l2 0 L2 dB2 Re 10 log CsP db2max max dB2
3. Plot the cross spectrum, keeping in mind that it is complex and therefore the log should be applied to the magnitude.
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.
 Coh coherence y1 y2 n2 r hw L3 length Coh l3 0 L3
2. Plot the coherence function. Use markers to mark the common frequency and the maximum value.
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.
 SNR snr y1 y2 n2 r hw L4 length SNR l4 0 L4
4. Plot the signal-to-noise ratio function. Use markers to mark the common frequency and the maximum value.
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.