~~DISCUSSION~~ ===== Fourier series, transforms, power spectra ===== Goals: * Refresh your memory of basic trigonometry and become comfortable plotting periodic functions in MATLAB * Understand the central idea of the Fourier transform and implement your own decomposition and reconstruction code * Appreciate the fundamental issue of spectral leakage, and how analysis parameters and signal properties affect it * Gain a basic familiarity with different methods for spectral estimation, and use some nonparametric estimators to obtain power spectral densities (PSDs) for some interesting neural signals Resources (optional, for reference): * {{:analysis:course:weeks_ch4_sinusoids.pdf|Chapter 4}} from Weeks, "Digital Signal Processing Using MATLAB & Wavelets" * {{:analysis:course:weeks_ch6_fourierintro.pdf|Chapter 6}} from Weeks, "Digital Signal Processing Using MATLAB & Wavelets" * [[http://www.mathworks.com/help/signal/ug/spectral-analysis.html|MATLAB's guide to spectral analysis]] ==== Introductory remarks ==== From very early electrical recordings of the human brain (e.g. Berger, 1929) it has been clear that //oscillations// are a salient feature of brain activity. The functional relevance, underlying mechanisms and clinical applications of oscillatory activity continue to be active areas of research; all of these endeavours rely critically on accurate characterization and quantification of the data. A fundamental tool in the analysis of oscillations is **Fourier analysis**, which is introduced in this module. ==== Generating and plotting basic sinusoids ==== To study oscillations we require //periodic// functions of time, such as ''sin(t)'', which repeat its values in regular intervals. Recall that to plot this sort of function in MATLAB, we first define the time axis (commonly a variable with the name ''tvec''), pass this to the function we wish to plot as an argument, and plot the result: %% plot a simple sinusoid Fs = 100; % in samples per second (Hz) t0 = 0; t1 = 1; % start and end times tvec = t0:1./Fs:t1; % construct time axis f = 2; % frequency of sine to plot y = sin(2*pi*f*tvec); % note sin() expects arguments in radians, not degrees (see also ''sind()'') stem(tvec,y); Apart from its frequency (2Hz in the above example), a sinusoid is further characterized by its //phase// and //amplitude//, which are readily manipulated as follows: phi = pi/2; subplot(221) y = sin(2*pi*f*tvec + phi); % a phase shift stem(tvec,y); hold on; plot(tvec,cos(2*pi*f*tvec),'r--','LineWidth',2) % notice, cosine is simply phase shifted sine legend('sin (phase-shifted)', 'cos'); a = 2; subplot(222) y = a.*sin(2*pi*f*tvec + phi); % amplitude change stem(tvec,y); % note scale of y axis! ==== Sums of sinusoids and harmonic series ==== What happens if we sum some sinusoids together? A compact representation for this is a //harmonic series//: $$S(t)=\sum_{n=1}^{N}{a_{n} cos(2 \pi n f_{0} t+\phi_{n})}$$ Note that each sinusoid of this series has a frequency that is an integer multiple $n$ of some base frequency $f_0$. This is a compact representation because we only specify the amplitude $a_n$ and phase $\phi_n$ for each of the series, with the frequencies fixed. Figure 4.7 in the Weeks chapter linked to above is a sum of four sinusoids, with a base frequency of 2: %% harmonic series example mag = [0.1 0 1.3 0.5]; % magnitudes for each term pha = [-pi/6 0 pi 2*pi/3]; % phases for each term f = 2; % base frequency signal_out = zeros(size(tvec)); for ii = 1:numel(mag) % note, the book chapter uses i, not best practice! this_signal = mag(ii)*cos(2*pi*f*ii*tvec + pha(ii)); plot(tvec,this_signal,'r:'); hold on; signal_out = signal_out + this_signal; % build the sum end plot(tvec,signal_out,'LineWidth',2); ☛ Why is it not a good idea to use the variable name ''i''? (Hint: for the same reason you should not define a variable named ''pi''...) It looks like we can create some interesting signals (blue) by summing simple sinusoids (thin red lines)! In fact, the central insight underlying Fourier analysis is that we can use a sum (series) of sinusoids to approximate //any// signal to arbitrary precision. An important corollary to this is that any signal can be //decomposed// into a series of sinusoids. Let's demonstrate this. ==== Decomposing and reconstructing a signal ==== We will use the MATLAB function ''fft()'' (for **F**ast **F**ourier **T**ransform; a technical point discussed in the Weeks chapter is that this in fact computes something called the Discrete Fourier Transform or DFT) to obtain the amplitudes and phases ($a_n$ and $\phi_n$ in the equation above) of a randomly generated signal, and then piece the signal back together by plugging them in to a harmonic series: %% rng('default'); % reset random number generator to reproducible state, so your plot will look like mine! x = round(rand(1,8)*10); % generate a length 8 vector of integers between 0 and 10 xlen = length(x); % get magnitudes and phases of Fourier series X = fft(x); Xmag = abs(X); % magnitudes, a_n Xphase = angle(X); % phases, phi_n n = 0:xlen-1; t = 0:0.05:xlen-1; % a finer timescale to show the smooth signal later for iH = xlen-1:-1:0 % reconstruct each harmonic s(iH+1,:) = Xmag(iH+1)*cos(2*pi*n*iH/xlen + Xphase(iH+1))/xlen; sm(iH+1,:) = Xmag(iH+1)*cos(2*pi*t*iH/xlen + Xphase(iH+1))/xlen; % detail: xlen appears here because the fundamental frequency used by fft() depends on this end ssum = sum(s); % coarse timescale (original points) smsum = sum(sm); % fine timescale (to see full signal) figure; plot(n, x, 'go', t, smsum, 'b', n, ssum, 'r*'); legend({'original','sum - all','sum - points only'}); You should get: {{ :analysis:course-w16:fourier.png?nolink&600 |}} Notice that the reconstruction is essentially perfect. We retrieved the original 8 points by plugging in the coefficients returned by ''fft()'' into a series of sinusoids. The set of magnitudes and phases that describe the harmonic series that reconstruct the signal are known as the //magnitude spectrum// and //phase spectrum// respectively. The square of the magnitude spectrum is referred to as the signal //power// at different frequencies. ==== Interpreting the output of MATLAB's fft() function ==== You might have noticed the magnitude and phase spectra above have a peculiar form. Let's explore this a little further using a signal with known frequency and phase content: Fs = 20; % in samples per second (Hz) t0 = 0; t1 = 1; % start and end times tvec = t0:1/Fs:t1-(1/Fs); % construct time axis; generate exactly 20 samples f = 2; % signal frequency y = sin(2*pi*f*tvec); % construct signal, a 2Hz sine wave sampled at 20Hz for 1s yfft = fft(y,length(y)); yfft_mag = abs(yfft); yfft_ph = angle(yfft); stem(yfft_mag) The result: {{ :analysis:course:week4_fig2.png?600 |}} Some issues are apparent: * ''fft()'' did not return a frequency axis, so the output is not straightforward to interpret. * Whatever the coefficients returned, there is not a single peak as we would expect from the pure input signal: in fact there are two peaks. To understand this, recall from [[analysis:course-w16:week4|sampling theory]] that the largest frequency that can be detected in a signal sampled at ''Fs'' is ''Fs/2'', the //Nyquist frequency//. Thus, we would expect the frequency axis from ''fft()'' to go from 0 to ''Fs/2'' at most. In addition, it turns out the Fourier transform is defined for //real// as well as for //complex// signals, and it returns spectra for both these components. Since we are only interested in real signals, we get a symmetric spectrum back (note that if we did not use ''abs()'' on the output of ''fft()'' we would get some imaginary components as well). To construct a frequency axis that takes both these ideas into account, we can do: Npoints = length(y); F = [-Npoints/2:Npoints/2-1]./Npoints; % construct frequency axis yfft_mag = fftshift(yfft_mag); % align output, see note below stem(F,yfft_mag); xlabel('Frequency (Fs^{-1})'); ''fftshift()'' cuts the second (complex) half of the spectrum and pastes it backwards at the beginning, so that our frequency axis is now correct; it is in units of ''1 / Fs'' so 0.1 corresponds to the 2Hz we put in. For analysis purposes we don't care about the complex part of the spectrum, and this is usually ignored to yield what is referred to as the "single-sided spectrum". Because we would never actually use ''fft()'' directly to estimate spectral content (see the section on spectral estimation below) we won't do more work to get rid of it now. Notice also the superscript in the ''xlabel'': MATLAB can interpret basic [[http://web.ift.uib.no/Teori/KURS/WRK/TeX/symALL.html|LaTeX math symbols]] for figure text. A final point about the output of ''fft()'' is that it contains the magnitude spectrum at specific frequencies. For instance, we have the 0.1*Fs point, but not 0.125*Fs! This will become important later. ==== Zero-padding the FFT ==== The above example was constructed nicely so that our signal contained exactly two full periods. This will not be true for real world signals. What happens if we don't have this perfect input? ☛ Change the ''tvec'' variable above to contain one more sample, like so: tvec = t0:1/Fs:t1; This means that our signal is now no longer an integer number of periods. The resulting two-sided spectrum is: {{ :analysis:course-w16:fourier2.png?nolink&600 |}} Notice that: - The peaks now appear at a frequency not exactly equal to the true frequency - Other non-zero components have appeared To explain (1), recall that ''fft()'' evaluates the magnitudes of specific frequencies only. Inspection of the spectrum above indicates that we don't have a frequency bin that is exactly 0.1 (the true frequency of our signal). Let's fix that using the second, optional, argument of ''fft()''. tvec = t0:1/Fs:t1; nPoints = [length(tvec) 64 256 1024]; for iP = 1:length(nPoints) % repeat fft with different numbers of points nP = nPoints(iP); subplot(2,2,iP); y = sin(2*pi*f*tvec); yfft = fft(y,nP); yfft_mag = abs(yfft); yfft_ph = angle(yfft); F = [-nP/2:nP/2-1]./nP; yfft_mag = fftshift(yfft_mag); plot(F,yfft_mag,'kx',F,yfft_mag,'k'); title(sprintf('%d point FFT',nP)); xlabel('Frequency (Fs^{-1})'); end This gives: {{ :analysis:course:week4_fig4.png?600 |}} As we increase the number of points to evaluate the FFT over, we get increased frequency resolution, and indeed the peaks of the spectrum converge to 0.1 as expected. Under the hood, this is in fact accomplished by //padding the input signal with zeros// before the DFT is computed; doing this does not change the spectral content (the zeros are not periodic), but allows a much longer harmonic series, with a smaller fundamental frequency, to be evaluated. A typical value to use for the number of points to evaluate the FFT is the next power of 2 (after however many samples your signal contains). This is because part of what makes the FFT fast is that it can easily divide the signal in half. But non-power of 2 values also work. ☛ What happens if you try to evaluate the FFT using a number of points //smaller// than that in your signal? As we increase the number of points to evaluate the FFT, we can obtain coefficients for frequencies of arbitrary precision. For this reason, the power spectrum is often referred to as //power spectral density// or PSD. ☛ Demonstrate that you get the same effect by zero-padding the original 21-point signal (rather than making the signal longer, as in the above example). ==== Spectral leakage ==== It is clear from the above figure that even if we increase the frequency resolution of our FFT with zero-padding, we still have an imperfect estimate of the true frequency content of our signal. In particular, the estimate around the true frequency has a nonzero width -- this part of the magnitude spectrum is referred to as the //main lobe// -- and we have nonzero spectral content for other frequencies as well (the //side lobes//). To explore the source of this **spectral leakage**, let's make our signal longer while evaluating the FFT over the same number of points: tvec = t0:1/Fs:t1-(1/Fs); nRepeats = [1 2 4 8]; nP = 1024; for iP = 1:length(nRepeats) subplot(2,2,iP); y = sin(2*pi*f*tvec); y = repmat(y,[1 nRepeats(iP)]); % repeat the signal a number of times yfft = fft(y,nP); yfft_mag = abs(yfft); yfft_ph = angle(yfft); F = [-nP/2:nP/2-1]./nP; yfft_mag = fftshift(yfft_mag); plot(F,yfft_mag,'kx',F,yfft_mag,'k'); title(sprintf('%d repeats',nRepeats(iP))); xlabel('Frequency (Fs^{-1})'); end The result: {{ :analysis:course:week4_fig5.png?600 |}} Notice that the magnitude spectrum converges to the true frequency as we increase the length of the signal. However, for any signal of finite length, there will always be some spectral leakage. This occurs because we are effectively cutting off the signal abruptly; other than changing the length of the signal (which is not always an option) we can attempt to minimize spectral leakage through using less abrupt cutoffs, discussed in the next section. ==== Windowing ==== Computing a FFT over a window of finite size is as if we are taking a finite-size window (for instance '' w(n) = 1 if 0 <= n <= N; 0 otherwise''; note that this defines a //rectangular// window) and multiplying this with a hypothetical infinite signal. It turns out that the spectrum of the windowed signal equals the //convolution// of the signal's spectrum and the window's spectrum; this occurs because of the [[http://en.wikipedia.org/wiki/Convolution_theorem|convolution theorem]] that states that multiplication in the time domain equals convolution in the frequency domain. (If the idea of convolution is new to you, you can think of it as a kind of "blurring": for instance, convolving a signal with a 5-point rectangular pulse is equivalent to taking the running average with a 5-point moving window. For a graphical exploration of this idea, see [[http://pages.jh.edu/~signals/convolve/ | this website]] or look at the MATLAB doc for the ''conv()'' function.) Thus, it becomes important to understand the spectral properties of the windows used for Fourier analysis. Let's plot a few: nP = 25; nPFFT = 1024; windows = {'rectwin','triang','hamming','hanning','blackman'}; cols = 'rgbcmyk'; for iW = 1:length(windows) eval(cat(2,'wn = ',windows{iW},'(nP);')); % make sure you understand this wn = wn./sum(wn); subplot(211); % plot the window plot(wn,cols(iW),'LineWidth',2); hold on; subplot(212); yfft = fft(wn,nPFFT); yfft_mag = abs(yfft); yfft_ph = angle(yfft); F = [-nPFFT/2:nPFFT/2-1]./nPFFT; yfft_mag = fftshift(yfft_mag); h(iW) = plot(F,yfft_mag,cols(iW),'LineWidth',2); hold on; end xlabel('Frequency (Fs^{-1})'); legend(h,windows); ☛ What does the ''eval()'' statement in the above code do? The result: {{ :analysis:course:week4_fig6.png?600 }} Inspection of the magnitude spectra of the different windows shows that although the rectangular window has the narrowest mainlobe (a desirable characteristic; narrower means a better estimate of the true underlying frequency) it also has the largest sidelobes (an undesirable characteristic; these sidelobe frequencies are not actually present in the signal!). As you can see, different windows represent different tradeoffs between these properties. ☛ Change the y axis to log scale to better visualize the differences (recall that you can use ''get(gca)'' to access the properties of the current axes). ☛ Modify the code in the Spectral Leakage section, above, to use a Hamming window instead of a rectangular window. Verify that the sidelobes are now smaller, at the expense of a slightly wider mainlobe. Note that the integral of the windows should be 1, to preserve power estimates, as guaranteed by the normalization statement in the code above. ==== Robust spectral estimation methods ===== The above method of spectral estimation, obtaining the Fourier coefficients by applying the DFT to the entire data set, is also referred to as constructing a //periodogram//. MATLAB has a function for this: [Pxx,F] = periodogram(y,[],nP,Fs); plot(F,Pxx); xlabel('Frequency (Hz)'); Note that this plots the //one-sided// spectrum, and that the units on the frequency axis are now in Hz. In addition, rather than plotting the magnitudes of the sinusoid components as before, we now have //power//, which is simply the magnitude squared. It is easy to change the window: hold on; [Pxx,F] = periodogram(y,hanning(length(y)),nP,Fs); plot(F,Pxx,'r'); Note again the trading off of mainlobe width against sidelobe magnitude. It turns out that the periodogram, although usable, is not a very good spectral estimator in some ways; it is //biased//: its variance does not tend to zero as data length goes to infinity ([[http://www.mathworks.com/help/signal/ug/spectral-analysis.html|details]]). This makes later statistical comparisons difficult, and the spectrum look noisy, as we'll demonstrate in a later section. A useful approach to address these issues is to cut the signal up into smaller segments, estimate the spectrum for each segment, and combine the estimates, a process known as [[http://en.wikipedia.org/wiki/Bartlett's_method|Bartlett's method]]. An example of a spectral estimator that uses this approach is //Welch's method//, which uses segments (or windows) that can overlap. Type ''help pwelch'' and read the overall description of what this function does. Based on the discussion above, this should make sense. Here is an illustration of how ''pwelch()'' performs: Fs = 20; % in samples per second (Hz) t0 = 0; t1 = 1; f = 2; nRepeats = 4; tvec = t0:1/Fs:t1-(1/Fs); nP = 1024; y = sin(2*pi*f*tvec); y = repmat(y,[1 nRepeats]); [Pxx,F] = periodogram(y,rectwin(length(y)),nP,Fs); plot(F,Pxx); hold on; wSize = 40; [Pxx,F] = pwelch(y,rectwin(wSize),wSize/2,nP,Fs); plot(F,Pxx,'r'); xlabel('Frequency (Hz)'); Note that the Welch spectrum sacrifices some frequency resolution, because of the smaller window that results from cutting up the data, but our estimate is now more robust. ==== Application to real data ==== As in the previous module, let's load some Emotiv data: fn = 'Seth1.edf'; [hdr,data] = edfread(fn); We also need to define a few things: our sampling frequency ''Fs'', and which channel we want to plot. Pick one and assign it to the ''iCh'' variable. Now we can compute the spectrum: wSize = 1024; [Pxx,F] = periodogram(data(iCh,:),hamming(length(data(iCh,:))),length(data(iCh,:)),Fs); plot(F,10*log10(Pxx),'k'); xlabel('Frequency (Hz)'); ylabel('Power (dB)'); xlim([0 150]); Note that we are plotting not the raw power spectrum but rather the ''10*log10()'' of it; this is a convention similar to that of the definition of the decibel (dB), the unit of signal power also applied to sound waves. Regardless, it doesn't look very good! The estimates look very noisy, a characteristic of the periodogram method. ☛ Edit the above code to compute a Welch spectrum instead, using a Hamming window of the same size and 50% overlap. Much better! ☛ Compute the PSD of "white noise", i.e. a random signal where each sample is drawn independently from the interval (0,1) with equal probability. Is it 1/f? How would you describe its shape? //Hint//: use the MATLAB function ''rand()''.