Summary: This development of these labs was supported by the National Science Foundation under Grant No. DUE-0511635. Any opinions, conclusions or recommendations expressed in this material are those of the authors and do not necessarily reflect the views of the National Science Foundation.
Note: You are viewing an old version of this document. The latest version is available here.
In this lab, we will look at the effect of filtering signals using a frequency domain implementation of an LTI system, i.e., multiplying the Fourier transform of the input signal with the frequency response of the system. In particular, we will filter sound signals, and investigate both low-pass and high-pass filters. Recall that a low-pass filter filters out high frequencies, allowing only the low frequencies to pass through. A high-pass filter does the opposite.
help <command>, online help for a command.
fft, Fast Fourier Transform.
ifft, Inverse Fourier Transform.
sound, plays sound unscaled (clips input to [-1,1]).
soundsc, plays sound scaled (scales input to [-1,1]).
wavread, reads in WAV file. The sampling rate of the WAV file can also be retrieved, for example, [x, Fs] = wavread('filename.wav'), where x is the sound vector and Fs is the sampling rate.
All of the sounds for this lab can be downloaded from the Sound Resources page.
When working in MATLAB, the continuous-time Fourier transform cannot be done by the computer exactly, but a digital approximation is done instead. The approximation uses the discrete Fourier transform (more on that in EE 341).
There are a couple important differences between the discrete Fourier transforms on the computer and the continuous Fourier transforms you are working with in class: finite frequency range and discrete frequency samples. The frequency range is related to the sampling frequency of the signal. In the example below, where we find the Fourier transform of the "fall" signal, the sampling frequency is Fs=8000, so the frequency range is [-4000,4000] Hz (or 2*pi times that for w in radians). The frequency resolution depends on the length of the signal (which is also the length of the frequency representation).
The MATLAB command for finding the Fourier transform of a signal is fft (for Fast Fourier Transform (FFT)). In this class, we only need the default version.
>> load fall %load in the signal
>> x = fall;
>> X = fft(x);
The fft command in MATLAB returns an uncentered result. To view the frequency content in the same way as we are used to seeing it in class, you need to plot only the first half of the result (positive frequencies only) OR use the MATLAB command fftshift which toggles between centered and uncentered versions of the frequency domain. The code below will allow you to view the frequency content both ways.
>> N = length(x);
>> pfreq = [0:N/2]*Fs/N;
>> Xpos=X(1:N/2+1);
>> plot(pfreq,abs(Xpos));
>> figure;
>> freq = [-(N/2-1):N/2]*Fs/N;
>> plot(freq,abs(fftshift(X)));
Note that we are using abs in the plot to view the magnitude since the Fourier transform of the signal is complex valued. (Type X(2) to see this. Note that X(1) is the DC term, so this will be real valued.)
Try looking at the frequency content of a few other signals. Note that the fall signal happens to have an even length, so N/2 is an integer. If the length is odd, you may have indexing problems, so it is easiest to just omit the last sample, as in x=x(1:length(x)-1);.
After you make modifications of a signal in the frequency domain, you typically want to get back to the time domain. The MATLAB command ifft will accomplish this task.
>> xnew = real(ifft(X));
You need the real command because the inverse Fourier transform returns a vector that is complex-valued, since some changes that you make in the frequence domain could result in that. If your changes maintain complex symmetry in the frequency domain, then the imaginary components should be zero (or very close), but you still need to get rid of them if you want to use the sound command to listen to your signal.
An ideal low-pass filter eliminates high frequency components entirely, as in:
This low-pass filter can be implemented in MATLAB using what we know about the Fourier transform. Remember that multiplication in the Frequency domain equals convolution in the time domain. If our signal and filter are both in the frequency domain, we can simply multiply them to produce the result of the system.
x with the FFT and the filter definition above.
The cutoff of the low-pass filter is defined by the constant a. The low-pass filter equation above defines the filter H in the frequency domain. Because the definition assumes the filter is centered around w = 0, the vector w is defined as such.
>> load fall %load in the signal
>> x = fall;
>> X = fft(x); % get the Fourier transform (uncentered)
>> N = length(X);
>> a = 100*2*pi;
>> w = (-N/2+1:(N/2)); % centered frequency vector
>> H = a ./ (a + i*w); % centered version of H
>> plot(w*Fs/N,abs(H))
The plot will show the form of the frequency response of a system that we are used to looking at, but we need to shift it to match the form that the fft gave us for x.
>> Hshift = fftshift(H); % uncentered version of H
>> Y = X .* Hshift'; % filter the signal
' operator transposes vectors/matrices in MATLAB.
>> y = real(ifft(Y));
>> sound(x, Fs) % original sound
>> sound(y, Fs) % low-pass-filtered sound
The filter reduced the signal amplitude, which you can hear when you use the sound command but not with the soundsc which does automatic scaling. Replay the sounds with the soundsc and see what other differences there are in the filtered vs. original signals. What changes could you make to the filter to make a greater difference?
>> y = y * (max(abs(x))/max(abs(y)))
Use wavread to load the sound castanets44m.wav. Perform low-pass filtering with the filter defined above, starting with a = 500*2*pi, but also try different values.
Play the original and the low-passed version of the sound. Plot their frequency content (Fourier transforms) as well.
x(t) using the following MATLAB command,
>> x = repmat([zeros(1, 99) 1], 1, 5);
x(t) and y(t) separately using the subplot command. These should be plotted versus the time vector. Label the axes and title each graph appropriately.
An ideal high-pass filter eliminates low frequency components entirely, as in:
The high-pass filter can be implemented in MATLAB much the same way as the low-pass filter. Perform high-pass filtering with the filter defined above on the sound castanets44m.wav. Start with a = 2000*2*pi, but also try different values.
Play the original and the high-passed version of the sound. The filtered signal may be to be scaled so that both have the same range on the Y-axis. Plot their frequency responses as well.
Fs = 8000 Hz). The mixed sound is created from bassdrum.wav, hatclosed.wav, and shake.mat.
mixedsig = shake + 10*rainstick.
mixedsig and process it to get (approximately) only the trumpet sound (shake) out? Try to do something easy but approximate first, and then if you have more time, see how good a reproduction of shake you can get. You may find it helpful to look at Fourier domain of the sounds, but you may not use rainstick.mat or shake.mat in your solution.