Saturday, December 7, 2013

Interpreting the fourier transform

A friend of mine, who is a designer, asked me how the Fourier transform is interpreted. I'm turning my answer into a blog post in the hope that this might help non EE/math people in dealing with real-world signals.

Let's begin with a sampled signal. Suppose you sampled air pressure (mic) or maybe temperature (thermistor) at regular intervals, and you want to know if there is some periodicity in the data. The Fourier transform gives information about the frequencies in the signal and the algorithm commonly used to efficiently compute the discrete Fourier transform is Fast Fourier Transform (FFT). I've plotted the discrete Fourier transform (DFT) of 200 samples of a 30 Hz sine wave sampled at 1 kHz in MATLAB.

Here's the code I used:

Ts = 1e-3;
t = (0:1:200)*Ts;
x = sin(2*pi*30*t); % you'd get x from some real-world sensor

The number of points in the transform is the same as that in the signal (200 in both). And the plot is symmetric with respect to an imaginary vertical line at X = 100. This is as it should be for real valued signals (basically, all signals you might possibly capture from the real world). So lets ignore the right half and talk about the left half. The value of the transform at X=0 divided by the number of samples (200 here) gives the average or 'DC' content of the signal. Its the same as what you would get if you averaged all of your readings. 

Now, we'll see how we might infer that the signal we started with was a 30 Hz sine from the Fourier transform. There is a peak at X=7. Map this back to the analog domain using

analog_f = discrete_f * sample_rate / num_of_samples

We see a peak at X=7, the sample rate is 1000 Hz and we took 200 samples. So 7*1000/200 gives 35 Hz. But we started with a 30 Hz signal ! Well, unlike nearly every other language out there, MATLAB is NOT zero-indexed and so we should subtract 1 from location where the peak is before substituting it in the formula. 6*1000/200 gives 30 Hz. 

Each increment on the X-axis corresponds to 5 Hz ( X=1 corresponds to 5 Hz, X=2 corresponds to 10 Hz etc.), which limits the frequency resolution to 5 Hz. If you want a frequency resolution of 1 Hz (say you want to be able to tell the difference between 30 Hz and 31 Hz), the number of samples we took is not enough. The more the number of samples we take, the better the frequency resolution.

Lets look at some real-world data now.

This is from an accelerometer placed on the abdomen of someone breathing deeply. The accelerometer can measure tilt and we were trying to infer breathing rate from this data by observing periodicity in tilt. The peak in the Fourier transform appears small because of the large DC component. But it does give a pretty accurate breathing rate in this case.

Two caveats:

1. Fourier transform assumes that you have uniformly sampled the signal (as in samples taken at 0 ms,1 ms,2 ms,3 ms and so on). Things get a bit more complicated if you are sampling non-uniformly (0 ms, 1.5 ms, 2ms, 3 ms etc.). You shouldn't apply what I just described if samples were taken non-uniformly. 

2.  Fourier transform gives you no time resolution. It tells you nothing about "when" an event (such as a sine wave) happened. Consider this signal and its fourier transform. I've added a 25 Hz sine wave with a samller amplitude 75 Hz sine. 

The Fourier transform looks about right. Two peaks, one at 25Hz and a smaller one at 75 Hz. Now lets look at a 25 Hz sine followed by a 75 Hz sine.

The Fourier tranform appears a bit more distorted but seems OK. Like before, there is a peak at 25 Hz and one at 75 Hz. Looking at only the Fourier transform, it's impossible to say "when" the sine wave occurred. For the whole time you were taking samples ? Or for some small time in the middle ? You can't really say. It appears clear in the time domain but there you don't really know the frequency. 

When a certain frequency occurs can be important. For example, in an EEG, you might want to know when a certain event happened. And this is what Time-Frequency analysis is all about; trying to get both time and frequency resolution at the same time.

1 comment:

  1. Made some stuff clear, useful ready reference for someone who might do a FFT only once in a blue moon.