Summary: Fourier series, sums of cosines. 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.
In this lab, we will look at the Fourier series representation of periodic signals using MATLAB. In particular, we will study the truncated Fourier series reconstruction of a periodic function.
abs, compute the complex magnitude.
angle, compute the phase angle.
clear, clears all variables.
help <command>, online help.
whos, list all variables and their sizes.
In this section, we'll reconstruct the periodic function x(t), shown in Figure 1, by synthesizing a periodic signal from a variable number of Fourier Series coefficients, and observe similarities and differences in the synthesized signal.
![]() |
gibbs.m to put your code in for this problem.
Ck.m. Take a look at the contents of the function. This function takes one argument
stem instead of plot to emphasize that the coefficients are a function of integer-valued (not continuous)
Coeff.tif.
t=-5:.01:5. Stack the three plots in a single figure using the subplot command and include your name in the title of the figure. Save the figure as FourTrunc.tif
gibbs.m
Coeff.tif
FourTrunc.tif
As you add more cosines you'll note that you do get closer to the square wave (in terms of squared error), but that at the edges there is some undershoot and overshoot that becomes shorter in time, but the magnitude of the undershoot and overshoot stay large. This persistent undershoot and overshoot at edges is called Gibbs Phenomenon.
In general, this kind of "ringing" occurs at discontinuities if you try to synthesize a sharp edge out of too few low frequencies. Or, if you start with a real signal and filter out its higher frequencies, it is "as if" you had synthesized the signal from low frequencies. Thus, low-pass filtering (a filter that only passes low-frequencies) will also cause this kind of ringing.
For example, when compressing an audio signal, higher frequencies are usually removed (that is, the audio signal is low-pass filtered). Then, if there is an impulse edge or "attack" in the music, ringing will occur. However, the ringing (called "pre-echo" in audio) can be heard only before the attack, because the attack masks the ringing that comes after it (this masking effect happens in your head). High-quality MP3 systems put a lot of effort into detecting attacks and processing the signals to avoid pre-echo.
We have seen that we can approximate a square wave with the Fourier series, but can we approximate something more interesting, say a musical instrument? Many instruments produce very periodic waveforms.
sigsynth.m to put your code in for this problem.
trumpet.mat from the Sound Resources page. The sample rate, Fs, of the trumpet is 11,025 Hz. Play this sound with the sound command (remember to include the correct sample rate).
Fs = 11025; % our sample rate is 11025 Hz
Y = fft(trumpet, 512); % take the fft of trumpet
Ymag = abs(Y); % take the mag of Y
f = Fs * (0:256)/512; % get a meaningful axis
plot(f, Ymag(1:257)); % plot Ymag (only half the points are needed)
xlabel('Frequency (Hz)')
ylabel('Magnitude')
You should now see a series of peaks (these are the harmonics of the instrument).
addcosines.m that takes in three vectors: time vector t, frequency vector freq, and magnitude vector mag. Have your new function use a for-loop to add together cosines, one for each frequency/magnitude pair in the freq and mag vectors. Remember to normalize your output vector after you add up all the cosines (the output should be between -1 and 1), like in the Functions in MATLAB and the Groove Station lab. Use the data you collected from the frequency plot of the trumpet sound with your new function to sum cosines at the noted frequencies.
mag(i)*cos(2*pi*freq(i)*t);. Remember your time vector will have the form 0:1/Fs:time_in_seconds.
soundsc will normalize the input before it plays the sound.
t = 0:1/Fs:1; % one second time vector at 11025 Hz
freq = [100 150];
mag = [1 2];
synthwaves.tif.
sigsynth.m
addcosines.m
synthwaves.tif
You probably noticed in the last problem that even though the wave forms looked fairly different, the sound was similar. Let's look into this a bit deeper with a simpler sound.
phasefun.m to put your code in for this problem.
sig1. Use Fs = 8000 (remember that you can reproduce only frequencies that are less than Fs/2).
sig2 exactly the same as the first one, except time delay the second cosine by a half cycle (half of its period).
phasesigs.tif. What did the time delay do to the phase?
soundsc, do they sound different?
sig2 with a few different delays and compare the sound to the first signal.
sig3 that is one cosine at some frequency. Now add sig3 with a timed delayed version of itself and call it sig4. Use a quarter cycle delay.
sig3 and sig4. Play them with soundsc, do they sound different to you?
phasefun.m
phasesigs.tif
Show the TA ALL m-files that you created or edited and the files below.
gibbs.m
Coeff.tif
FourTrunc.tif
sigsynth.m
addcosines.m
synthwaves.tif
phasefun.m
phasesigs.tif
any wav files created