The Fourier transform is one of the most useful mathematical tools for many fields of science and engineering. The Fourier transform has applications in signal processing, physics, communications, geology, astronomy, optics, and many other fields. This technique transforms a function or set of data from the time or sample domain to the frequency domain. This means that the Fourier transform can display the frequency components within a time series of data. The Discrete Fourier Transform (DFT) transforms discrete data from the sample domain to the frequency domain. The Fast Fourier Transform (FFT) is an efficient way to do the DFT, and there are many different algorithms to accomplish the FFT. Matlab uses the FFT to find the frequency components of a discrete signal.
The following is an example of how to use the FFT to analyze an audio file in Matlab. The file in this example is the recording of a tuning fork resonating at the note A4. This shows how the Fourier transform works and how to implement the technique in Matlab.
%Fourier Transform of Sound File %Load File file = 'C:\MATLAB7\work\tuning_fork_A4'; [y,Fs,bits] = wavread(file); Nsamps = length(y); t = (1/Fs)*(1:Nsamps) %Prepare time data for plot %Do Fourier Transform y_fft = abs(fft(y)); %Retain Magnitude y_fft = y_fft(1:Nsamps/2); %Discard Half of Points f = Fs*(0:Nsamps/2-1)/Nsamps; %Prepare freq data for plot %Plot Sound File in Time Domain figure plot(t, y) xlabel('Time (s)') ylabel('Amplitude') title('Tuning Fork A4 in Time Domain') %Plot Sound File in Frequency Domain figure plot(f, y_fft) xlim([0 1000]) xlabel('Frequency (Hz)') ylabel('Amplitude') title('Frequency Response of Tuning Fork A4')
The sound file tuning_fork_A4 is opened using the wavread function, which returns the sampled data from the file, the sampling frequency, and the number of bits used in the A/D converter. Note that the file extension “.wav” does not have to be specified in the function call. The sampling frequency is important for interpreting the data, as shown below.
The FFT is performed using the “fft” function. Matlab has no “dft” function, as the FFT computes the DFT exactly. Only the magnitude of the FFT is saved, although the phase of the FFT is useful is some applications. The “fft” function allows the number of points outputted by the FFT to be specified, but for this example, we will use the same number of input and output points. In the next line, half of the points in the FFT are discarded. This is done for the purposes of this example, but for many applications, the entire spectrum is interesting. In the following line, the data that will be used for the abscissa is prepared by using the sampling frequency and the number of samples in the time domain. This step is important to determine the actual frequencies contained in the audio data.
Next, the original data are plotted in the time domain and the FFT of the data is plotted. The x-axis is limited to the range [0, 1000] in this plot to show more detail at the peak frequency. Notice that the frequency response contains a spike at approximately 440 Hz, which is the frequency of the note A4. There is also very little content at other frequencies, which is expected for a tuning fork. For other instruments, such as a guitar, harmonics at multiples of the peak frequency would be visible in the frequency response.
The Fourier transform is a useful tool in many different fields. Two-dimensional Fourier transforms are often used for images as well. Try the code above for yourself to see if you can get the same results.