Académique Documents
Professionnel Documents
Culture Documents
LAB 4 REPORT
1.
INTRODUCTION 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.
2. SOME USEFUL MATLAB COMMANDS abs, compute the complex magnitude. angle, compute the phase angle. whos, list all variables and their sizes. clear, clears all variables. help <command>, online help.
0.5
-0.5
-1
0.5
1.5
2.5
3.5 x 10
4
0.5
-0.5
-1
20
40
60
80
100
120
140
160
180
200
Two signals seem to be different. In fact, since the trumpet sound has been compressed by a large value interval, we feel that its magnitude seem to expand from -1 to 1.To see it clearly, we must base on the zooming figure.
Question 4: To view the frequency spectrum of this sound, we have the code:
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')
60
50
agnitude M
40
30
20
10
1000
2000
3000
4000
5000
6000
F requency(H z)
Question 5: Using the "data cursor" tool in MATLAB's figure window to easily read graph data, we have frequency and its strength (magnitude) for ten of the strongest peaks:
(258.4, 11.8) (1335, 52.29) (2390, 15.72) (538.3, 37.75) (1593, 38.99) (2649, 15.21) (796.7, 65.11) (1852, 28.31) (1055, 52.24) (2110, 12.49)
Question 6-7:
Using the data we collected from the frequency plot of the trumpet sound (the seven pairs of value in question 5) with our new function to sum cosines at the noted frequencies. This code is in file addcosines_test.m.
freq = [1055 1335 1593 1852 2110 2390 2649]; % 7 frequencies mag =[52.24 52.29 38.99 28.31 12.49 15.72 15.21]; t = 0:1/Fs:length(trumpet)/Fs; y = addcosines(t, freq, mag); subplot(2,1,1);plot(trumpet(200:400));title('trumpet sound'); subplot(2,1,2);plot(y(200:400));title('addcosines sound');
trumpet sound 1
0.5
-0.5
-1
50
100
150
200
250
addcosines sound 1
0.5
-0.5
-1
50
100
150
200
250
Question 9: We use five pairs of value in question 5 to synthesis the signal and compare to the trumpet signal.
%%%% question 9 with fewer frequencies Fs = 11025; freq = [258.4 538.3 796.7 1055 1335]; mag = [11.8 37.75 65.11 52.24 52.29]; t = 0:1/Fs:length(trumpet)/Fs;
0.5
-0.5
-1
50
100
150
200
250
trumpet sound
1
0.5
-0.5
-1
50
100
150
200
250
50
100
150
200
250
In the case of fewer frequencies, the sound is larger than the synthesized sound in question 8. In contrast, the sound is smaller in the case of more frequencies.
Exercise 2: Question 1-2: We create a script file called phasefun.m to put our code in for this problem. Then, we pick two harmonic frequencies y1 and y2 and generate a signal from two cosines at these frequencies added together and call it sig1. Use Fs = 8000.
%%%% question 1-2 Fs = 8000; t = 0:1/Fs:1; y1 = 45*cos(2*pi*900*t); y2 = 34.5*cos(2*pi*1400*t); sig1 = y1+y2;
Question 3-4: We generate a second signal called sig2 exactly the same as the first one, except time delay the second cosine by a half cycle (half of its period).
%%%% question 3-4: sig2 = y1 + 34.5*cos(2*pi*1400*(t - 0.5*(1/1400))); figure subplot(2,1,1);plot(sig1(1:100));title('signal 1'); subplot(2,1,2);plot(sig2(1:100));title('signal 2'); wavwrite(sig1,Fs,'signal1');
wavwrite(sig2,Fs,'signal2');
signal 1
80 60 40 20 0 -20 -40 -60 -80 0 10 20 30 40 50 60 70 80 90 100
signal 2
80 60 40 20 0 -20 -40 -60 -80 0 10 20 30 40 50 60 70 80 90 100
These two signals are different. The time delay makes the phase of signal 2 be later than signal 1.
Question 5:
%%%% question 5 soundsc(sig1); soundsc(sig2);
We hear the same sound. That is because the time delay is so small.
Question 6: We use signal 2 with time delay the second cosine by period.
sig6 = y1 + 34.5*cos(2*pi*1400*(t - 0.75*(1/1400))); figure subplot(2,1,1);plot(sig1(1:100));title('signal 1'); subplot(2,1,2);plot(sig6(1:100));title('signal 6');
signal 1
80 60 40 20 0 -20 -40 -60 -80 0 10 20 30 40 50 60 70 80 90 100
signal 6
80 60 40 20 0 -20 -40 -60 -80 0 10 20 30 40 50 60 70 80 90 100
Question 7-8: We create a sig3 that is one cosine at some frequency. After that, we add sig3 with a timed delayed version of itself and call it sig4 using a quarter cycle delay. Then, we use subplot and plot a few periods of sig3 and sig4, and play them.
%%%% question 7-8 sig3 = 35*cos(2*pi*935*t); sig4 = sig3 + 35*cos(2*pi*935*(t - 0.25*(1/935))); figure subplot(2,1,1);plot(sig3(1:50));title('signal 3'); subplot(2,1,2);plot(sig4(1:50));title('signal 4'); soundsc(sig3); soundsc(sig4);
signal 3
40
20
-20
-40
10
15
20
25
30
35
40
45
50
signal 4
50
-50
10
15
20
25
30
35
40
45
50
The signal 4 has a little bigger sound than signal 3. Since the delay time is so small, we hear the sounds occurring at the same time.
Question 9: We cannot hear the sound difference clearly because our ears are not sensitive to be able to recognize. Exercise 3: Gibbs phenomena Question 1-2-3: We create a script file called gibbs.m. Then, we plot the magnitude and phase of the coefficients Ck.
k = -10:1:10; ck = Ck(k); mag = abs(ck); phase = angle(ck); figure subplot(2,1,1);stem(k,mag);title('magnitude');xlabel('w'); subplot(2,1,2);stem(k,phase);title('phase');xlabel('w');
magnitude
0.35 0.3 0.25 0.2 0.15 0.1 0.05 0 -10
-8
-6
-4
-2
10
w phase
2 1.5 1 0.5 0 -0.5 -1 -1.5 -2 -10 -8 -6 -4 -2 0 2 4 6 8 10
Question 4: This is our code for calculating the signal x(t) with a truncated Fourier series. It is in the file calcX.m.
function [out] = calcX(t,kmax) x = zeros(1,length(t)); w = 1; for k = 0:kmax x = x + 2*abs(Ck(k))*cos(k*w*t +angle(Ck(k))); end out = x;
Question 5: This is the test function with kmax = 5, 15, and 30 for the calculating x(t) in the file calcX_test.m.
t = -5:0.01:5; figure x1 = calcX(t,5); subplot(3,1,1);plot(t,x1);title('An+Trong: x1(t) with kMax = 5'); x2 = calcX(t,15); subplot(3,1,2);plot(t,x2);title('x2(t) with kMax = 15'); x3 = calcX(t,30); subplot(3,1,3);plot(t,x3);title('x3(t) with kMax = 30');
-4
-3
-2
-1
-4
-3
-2
-1
-4
-3
-2
-1