Lab2-Spectral Analysis in Matlab
Lab2-Spectral Analysis in Matlab
Try: x = randn(1,100); w = 10; y = conv(ones(1,w)/w,x); avgs = y(10:99); plot(avgs) Ensemble averages: w = 10; for i = 1:w; X(i,:) = randn(1,100); end AVGS = mean(X); plot(AVGS) x = [-1 0 1]; y = [0 1 2]; xcorr(x,y) conv(x,fliplr(y)) xcov(x,y) xcov(x,x) xcov(x,y-1) What do you notice about these vectors? Explain why you see these results.
Example: Crosscorrelation
In a simple target ranging system, an outgoing signal x is compared with a returning signal y . We can model y by y (n) = x(n d) + where is an attenuation factor, d is a time delay, and is channel noise. If T is the return time for the signal, then x and y should be correlated at n = T . The target will be located at a distance of vT , where v is the channel speed of the signal. Try: x = [zeros(1,25),1,zeros(1,25)]; subplot(311), stem(x) y = 0.75*[zeros(1,20),x] + 0.1*randn(1,71); subplot(312), stem(y)
[c lags] = xcorr(x,y); subplot(313), stem(lags,c) Does this example show the expected behavior? Why or why not?
Yk+1 =
j =0
jk yj +1
where is a complex nth root of unity: = e2i/n This notation uses i for the complex unit, and j and k for indices that run from 0 to n 1. The subscripts j + 1 and k + 1 run from 1 to n, corresponding to the range usually associated with Matlab vectors. Data in the vector y are assumed to be separated by a constant interval in time or space dt = 1/F s. F s is called the sampling frequency of y . The coecient Yk+1 measures the amount of the frequency f = k (F s/n) that is present in the data in y . The vector Y is called the spectrum of y . The midpoint of Y (or the point just to the right of the midpoint, if n is even), corresponding to the frequency f = F s/2, is called the Nyquist point. The real part of the DFT is symmetric about the Nyquist point. The graphical user interface fftgui allows you to explore properties of the DFT. If y is a vector, fftgui(y) plots real(y), imag(y), real(fft(y)), and imag(fft(y)). You can use the mouse to move any of the points in any of the plots, and the points in the other plots respond. Try: Roots of unity edit z1roots
z1roots(3); z1roots(7); Explore the DFT: delta1 = [1 zeros(1,11)]; fftgui(delta1) delta2 = [0 1 zeros(1,10)]; fftgui(delta2) deltaNyq = [zeros(1,6),1,zeros(1,5)]; fftgui(deltaNyq) square = [zeros(1,4),ones(1,4),zeros(1,4)]; fftgui(square) t = linspace(0,1,50); periodic = sin(2*pi*t); fftgui(periodic)
size of an external buer. The FFT length is the length of the output, the computed DFT. The command Y=fft(y) returns the DFT Y of y . The window length length(y) and the FFT length length(Y) are the same. The command Y=fft(y,n) returns the DFT Y with length n. If the length of y is less than n, y is padded with trailing zeros to length n. If the length of y is greater than n, the sequence y is truncated. The FFT length is then the same as the padded/truncated version of the input y . Try: Vector data interpolation and the origins of the FFT edit fftinterp fftinterp hold off
Note: Several people discovered fast DFT algorithms independently, and many people have since joined in their development, but it was a 1965 paper by John Tukey of Princeton University and John Cooley of IBM Research that is generally credited as the starting point for the modern usage of the FFT. The Matlab fft function is based on FFTW, The fastest Fourier Transform in the West, developed by MIT graduate students Matteo Frigo and Steven G. Johnson. (http://www.tw.org)
f = (0:n-1)*(Fs/n) Fs/2
A plot of the power spectrum is called a periodogram. The rst half of the principal frequency range (from 0 to the Nyquist frequency F s/2 is sucient, because the second half is a reection of the rst half. Spectra are sometimes plotted with a principal frequency range from F s/2 to F s/2. Matlab provides the function fftshift to rearrange the outputs of fft and convert to a 0-centered spectrum. Try: Periodograms edit pgrams pgrams Whale call edit whalefft whalefft FFT Demos sigdemo1 playshow fftdemo phone playshow sunspots If youre not familiar with DTMF, try the following with a touch-tone telephone. While listening to the receiver, press two keys in the same row simultaneously, then press two keys in the same column (e.g., 1 & 2, then 1 & 4).
Aliasing
Discrete time signals sampled from time-periodic analog signals need not be time periodic, but they are always periodic in frequency, with a period equal to the sampling frequency. The resulting harmonics show up as spectral copies in frequency domain plots like the periodogram. If the sampling rate is too low, these spectral copies can overlap within the principal range, confusing the frequency analysis.
Power = Y.*conj(Y)/nfft; plot(f,Power) title(Periodogram) figure ryy = xcorr(y,y); Ryy = fft(ryy,512); plot(f, abs(Ryy)) title(DFT of Autocorrelation)
Non-parametric Methods
Non-parametric methods estimate the PSD directly from the signal itself. The simplest such method is the periodogram. An improved version of the periodogram is Welchs method. A more modern technique is the multitaper method (MTM). The following functions estimate the PSD P xx in units of power per radians per sample. The corresponding vector of frequencies w is computed in radians per sample, and has the same length as P xx. Periodogram method [Pxx w] = periodogram(x) Estimates the PSD using a periodogram. Optional inputs specify windows (default is rectangular), FFT length, PSD sample frequencies, and output frequency range. Welch method [Pxx w] = pwelch(x) Estimates the PSD using Welchs averaged periodogram method. The vector x is segmented into equal-length sections with overlap. Trailing entries not included in the nal segment are discarded. Each segment is windowed. Optional inputs specify windows (default is Hamming), overlap, FFT length, and PSD sample frequencies. Multitaper method [Pxx w] = pmtm(x, nw) Estimates the PSD using a sequence of 2 nw 1 orthogonal tapers (windows in the frequency domain). The quantity nw is the time-bandwidth product for the discrete prolate spheroidal sequences specifying the tapers. Optional inputs specify taper frequencies, FFT length, and PSD sample frequencies. Try:
t = 0:1/100:10-1/100; x = sin(2*pi*15*t) + sin(2*pi*30*t); periodogram(x,[],512,100); figure pwelch(x,[],512,100); figure pmtm(x,[],512,100); Comment on the three methods. Is one preferable to the other two? Why or why not?
Parametric Methods
Parametric methods can yield higher resolution than non-parametric methods in cases where the signal length is short. These methods use a dierent approach to spectral estimation: instead of estimating the PSD directly from the data, they model the data as the output of a linear system driven by white noise (an adaptive lter), and then attempt to estimate the parameters of that linear system. The most commonly used linear system model is the all-pole model, a system with all of its zeros at the origin in the z -plane. The output of such a system for white noise input is an autoregressive (AR) process. These methods are sometimes referred to as AR methods. AR methods give accurate spectra for data that is peaky, that is, data with a large PSD at certain frequencies. The data in many practical applications (such as speech) tends to have peaky spectra, so that AR models are often useful. In addition, the AR models lead to a system of linear equations that is relatively simple to solve. The following methods are summarized on the next page. The input p species the order of the autoregressive (AR) prediction model. Yule-Walker AR method [Pxx f] = pyulear(x,p,nfft,fs) Burg method [Pxx f] = pburg(x,p,nfft,fs) Covariance and modied covariance methods [Pxx f] = pcov(x,p,nfft,fs) [Pxx f] = pmcov(x,p,nfft,fs)
Try: edit pmethods pmethods(pyulear,25,1024) pmethods(pburg,25,1024) pmethods(pcov,5,512) pmethods(pmcov,5,512) Some of the factors to consider when choosing among parametric methods are summarized in the following table. See the documentation for further details. Burg Covariance Modied Covariance Yule-Walker
Characteristics Does not apply window to data Minimizes forward and backward prediction errors Does not apply window to data Minimizes forward prediction error Does not apply window to data Minimizes forward and backward prediction errors Applies window to data Minimizes forward prediction error
Advantages High resolution for short data records Always produces a stable model Better resolution High resolution for than Y-W for short short data records data records Extract frequencies Extract frequencies from mix of p or from mix of p or more sinusoids more sinusoids No spectral line-splitting Disadvantages Peak locations Can produce Can produce Performs relatively highly dependent unstable models unstable models poorly for short on initial phase data records Spectral lineFrequency bias Peak locations Frequency bias splitting for for estimates of slightly dependent for estimates of sinusoids in noise, sinusoids in noise on initial phase sinusiods in noise or when order is very large Frequency bias for Minor frequency sinusoids in noise bias for sinusiods in noise As good as other methods for large data records Always produces a stable model
Conditions for Nonsingularity p must be 1/2 input frame size p must be 2/3 input frame size Autocorrelation matrix always positive-denite, nonsingular
Subspace Methods
Subspace methods, also known as high-resolution methods or super-resolution methods, generate PSD estimates based on an eigenanalysis or eigendecomposition of the correlation matrix. These methods are best suited for line spectra - i.e., spectra of sinusoidal signals and are eective for detecting sinusoids buried in noise, especially when signal to noise ratios are low. The following functions estimate the pseudospectrum S (an indicateor of the presence of sinusoidal components in a signal) of the input signal x, and a vector w of normalized frequencies (in rad/sample) at which the pseudospectrum is evealuated. The input p controls the dimensions of the signal and noise subspaces used by the algorithms. Eigenvector method [S f] = peig(x,p,nfft,fs) Multiple Signal Classication (MUSIC) method [S f] = pmusic(x,p,nfft,fs) The MUSIC algorithm uses Schmidts eigenspace analysis method. The eigenvector uses a weighted version of the MUSIC algorithm. Try: edit ssmethod ssmethod(3) ssmethod(4)
1. Select the signal in the Signals list in SPTool. 2. Select the Create button under the Spectra list. Use the View button under the Spectra list in the SPTool GUI to display one or more selected spectra. Try: t = 0:1/100:10-1/100; x = sin(2*pi*15*t) + sin(2*pi*30*t); Import this signal into SPTool and view the spectrum using various methods.
Time-Varying Spectra
The spectral estimation methods described so far are designed for the analysis of signals with a constant spectrum over time. In order to nd time-varying spectra, dierent methods of analysis and visualization must be used. The time-dependent Fourier transform of a signal is a sequence of DFTs computed using a sliding window. A spectrogram is a plot of its magnitude versus time. [B f t] = specgram(x,nfft,fs,window,numoverlap) calculates the time-dependent Fourier transform for the signal in vector x and returns the DFT values B , the frequency vectors f , and the time vectors t. The spectrogram is computed as follows: 1. The signal is split into overlapping sections and applies to the window specied by the window parameter to each section. 2. It computes the discrete-time Fourier transform of each section with a length nf f t FFT to produce an estimate of the short-term frequency content of the signal; these transforms make up the columns of B . The quantity length(window) numoverlap species by how many samples specgram shifts the window. 3. For real input, specgram truncates to the rst nf f t/2 + 1 points for nf f t even and (nf f t + 1)/2 for nf f t odd. When called with no outputs, specgram displays the spectrogram. Try: Time-constant spectrum t = 0:1/100:10-1/100; x = sin(2*pi*15*t) + sin(2*pi*30*t); specgram(x,256,100,hann(21),15)
colorbar Time-varying spectrum load handel sound(y,Fs) specgram(y,512,Fs,kaiser(100,5),75) colorbar Spectrogram Demos specgramdemo xpsound
Wavelets
One of the drawbacks of the Fourier transform is that it captures frequency information about a signal without any reference to time. For stationary signals this is unimportant, but for time-varying or bursty signals, time can be critical to an analysis. The time-dependent Fourier transform computed by the specgram function is one solution to this problem. By applying a DFT to a sliding window in the time domain, specgram captures a signals frequency content at dierent times. The disadvantage of this method is that it is uniform on all time intervals: it does not adjust to local idiosyncrasies in the frequency content. As a result, more subtle nonstationary characteristics of a signal can go undetected.
Wavelet analysis uses a more adaptable method, and is capable of revealing trends, breakdown points, discontinuities in higher derivatives, and self-similarity that the DFT might miss. A wavelet is a waveform of eectively limited duration that has an average value of zero. The discrete wavelet transform (DWT) computes coecients of similarity between a signal and a sliding wavelet. The coecients are found with wavelets of dierent scales (widths that approximate dierent frequencies) to analyze the signal at dierent resolutions. In a wavelet analysis, a signal is iteratively decomposed into the sum of a lowpass approximation and a progressive sequence of highpass details. The Wavelet Toolbox adds wavelet analysis techniques to the signal processing techniques available in the Signal Processing Toolbox. wavemenu brings up a menu for accessing graphical tools in the Wavelet Toolbox. Try: wavemenu Select Wavelet 1-D and then File Example Analysis Basic Signals Frequency breakdown S is the signal a5 is the approximation dn are the details What is happening in this signal and how does the wavelet analysis show it?
(The material in this lab handout was put together by Paul Beliveau and derives principally from the MathWorks training document MATLAB for Signal Processing, 2006.)
c 2006GM