Lab 5 DSP
Lab 5 DSP
Lab 5 DSP
--------------------------LAB 05----------------------
DISCRETE FOURIER TRANSFORM
Objectives
To study the concept of Discrete Fourier Transform using MATLAB.
Task # 1: Write a script in MATLAB for data sequence is given as follows: xn = {1 0 0 1}
a) Compute its DFT function Xk.(also attached its manual solution)
SOLUTION:
MATLAB CODE:
%DISCRETE FOURIER TRANSFORM
n=[0:3];
x=[1 0 0 1];
Xk=fft(x)
mag=abs(Xk)
angx=phase(Xk)
OUTPUT:
Xk = 2.0000 + 0.0000i 1.0000 + 1.0000i 0.0000 + 0.0000i 1.0000 - 1.0000i
mag = 2.0000 1.4142 0 1.4142
phase= 0 0.7854 0 -0.7854
b) Compute the IDFT of part (a) and compare it with original DTS.
SOLUTION:
MATLAB CODE:
%DISCRETE FOURIER TRANSFORM
n=[0:3];
x=[1 0 0 1];
Xk=fft(x)
mag=abs(Xk)
angx=phase(Xk)
Xn=ifft(Xk)%CONVERSION OF DFT TO DTS
OUTPUT:
Xn = 1 0 0 1
Task#2: Write a script in MATLAB for simulate Xk of DTS xn = {1 0 0 1}and also plot magnitude
and phase spectrums of Xk.
SOLUTION:
MATLAB CODE:
n=[0:3];
x=[1 0 0 1];
Xk=fft(x)
mag=abs(Xk)
angx=phase(Xk)
subplot 211
stem(mag,'r')
title('MAGNITUDE OF DFT SIGNAL')
subplot 212
stem(angx,'g')
Task # 3: Write a script in MATLAB for evaluating DFT of DTS sequence an where a=0.8
and define at n=0 to 35
a) Compute its DFT function Xk.
SOLUTION:
MATLAB CODE:
%DISCRETE FOURIER TRANSFORM
n=[0:35];
a=0.8;
x=a.^n
Xk=fft(x)
mag=abs(Xk)
ang=phase(Xk)
OUTPUT:
Xk =
Columns 1 through 6
4.9984 + 0.0000i 3.2980 - 2.1595i 1.8182 - 2.0040i 1.2073 - 1.5721i 0.9341 - 1.2407i 0.7941
- 1.0018i
Columns 7 through 12
0.7141 - 0.8245i 0.6645 - 0.6877i 0.6319 - 0.5782i 0.6096 - 0.4876i 0.5937 - 0.4107i 0.5821
- 0.3436i
Columns 13 through 18
0.5736 - 0.2839i 0.5673 - 0.2296i 0.5626 - 0.1794i 0.5593 - 0.1322i 0.5571 - 0.0870i 0.5558
- 0.0432i
Columns 19 through 24
0.5554 + 0.0000i 0.5558 + 0.0432i 0.5571 + 0.0870i 0.5593 + 0.1322i 0.5626 + 0.1794i
0.5673 + 0.2296i
Columns 25 through 30
0.5736 + 0.2839i 0.5821 + 0.3436i 0.5937 + 0.4107i 0.6096 + 0.4876i 0.6319 + 0.5782i
0.6645 + 0.6877i
Columns 31 through 36
0.7141 + 0.8245i 0.7941 + 1.0018i 0.9341 + 1.2407i 1.2073 + 1.5721i 1.8182 + 2.0040i
3.2980 + 2.1595i
mag =
Columns 1 through 12
4.9984 3.9421 2.7059 1.9821 1.5531 1.2783 1.0907 0.9563 0.8565 0.7806
0.7219 0.6759
Columns 13 through 24
0.6400 0.6120 0.5905 0.5747 0.5638 0.5575 0.5554 0.5575 0.5638 0.5747
0.5905 0.6120
Columns 25 through 36
0.6400 0.6759 0.7219 0.7806 0.8565 0.9563 1.0907 1.2783 1.5531 1.9821
2.7059 3.9421
ang =
Columns 1 through 12
Columns 13 through 24
-0.4595 -0.3846 -0.3086 -0.2320 -0.1549 -0.0775 0 0.0775 0.1549 0.2320 0.3086
0.3846
Columns 25 through 36
0.4595 0.5332 0.6052 0.6747 0.7410 0.8026 0.8571 0.9005 0.9254 0.9159
0.8340 0.5797
OUTPUT:
Task # 4: Write a script in MATLAB for considering 3 sinusoidal signals with their phase and
magnitudes.
X1(t)=5cos(1000πt), X2(t)=5cos(2000πt+0.25π) and X3(t)=5cos(3200πt+0.5π)
a) Create a DTS of sum of these 3 signals as x(n)= X1(t)+ X2(t)+ X3(t) by using a
sampling frequency of 8KHz.
SOLUTION:
MATLAB CODE:
%X1(t)=5*cos(1000*pi*t), X2(t)=5*cos(2000*pi*t+0.25*pi), X3(t)=5*cos(3200*pi*t+0.5*pi)
Fs=8000;
f1=500/8000;
f2=1000/8000;
f3=1600/8000;
n=[0:0.01:5];
%Xn=X1(n)+X2(n)+X3(n)
xn=5*cos(2*pi*f1*n)+5*cos(2*pi*f2*n+0.25*pi)+5*cos(2*pi*f3*n+0.5*pi)
OUTPUT:
xn =
Columns 1 through 12
8.5355 8.4448 8.3538 8.2625 8.1709 8.0791 7.9870 7.8948 7.8023 7.7096 7.6167 7.5237
Columns 13 through 24
7.4304 7.3371 7.2435 7.1499 7.0561 6.9622 6.8683 6.7742 6.6801 6.5859 6.4916 6.3973
Columns 25 through 36
6.3030 6.2087 6.1144 6.0200 5.9257 5.8314 5.7372 5.6430 5.5489 5.4549 5.3609 5.2671
Columns 37 through 48
5.1733 5.0797 4.9862 4.8928 4.7996 4.7066 4.6137 4.5210 4.4286 4.3363 4.2442 4.1524
Columns 49 through 60
4.0608 3.9695 3.8784 3.7876 3.6971 3.6069 3.5169 3.4273 3.3380 3.2490 3.1604 3.0721
Columns 61 through 72
2.9842 2.8967 2.8095 2.7227 2.6363 2.5504 2.4648 2.3797 2.2950 2.2107 2.1269 2.0436
Columns 73 through 84
1.9607 1.8784 1.7965 1.7151 1.6342 1.5538 1.4739 1.3946 1.3158 1.2376 1.1599 1.0827
Columns 85 through 96
1.0062 0.9302 0.8548 0.7800 0.7057 0.6321 0.5591 0.4867 0.4150 0.3438 0.2733 0.2035
0.1343 0.0657 -0.0021 -0.0693 -0.1359 -0.2017 -0.2669 -0.3314 -0.3952 -0.4583 -0.5207 -0.5823
-0.6433 -0.7035 -0.7630 -0.8218 -0.8798 -0.9371 -0.9937 -1.0495 -1.1046 -1.1589 -1.2125 -1.2652
-1.3173 -1.3685 -1.4190 -1.4687 -1.5177 -1.5658 -1.6132 -1.6598 -1.7056 -1.7507 -1.7949 -1.8383
-1.8810 -1.9228 -1.9639 -2.0041 -2.0436 -2.0822 -2.1200 -2.1571 -2.1933 -2.2288 -2.2634 -2.2972
-2.3302 -2.3624 -2.3938 -2.4244 -2.4542 -2.4832 -2.5114 -2.5387 -2.5653 -2.5911 -2.6160 -2.6402
-2.6635 -2.6861 -2.7079 -2.7288 -2.7490 -2.7684 -2.7870 -2.8048 -2.8218 -2.8380 -2.8535 -2.8682
-2.8821 -2.8952 -2.9075 -2.9191 -2.9300 -2.9400 -2.9493 -2.9579 -2.9657 -2.9727 -2.9791 -2.9846
-2.9895 -2.9936 -2.9969 -2.9996 -3.0015 -3.0028 -3.0033 -3.0031 -3.0022 -3.0006 -2.9984 -2.9954
-2.9918 -2.9875 -2.9825 -2.9769 -2.9706 -2.9636 -2.9560 -2.9478 -2.9389 -2.9294 -2.9193 -2.9086
-2.8973 -2.8853 -2.8728 -2.8596 -2.8459 -2.8316 -2.8168 -2.8014 -2.7854 -2.7689 -2.7518 -2.7342
-2.7161 -2.6974 -2.6783 -2.6586 -2.6385 -2.6178 -2.5967 -2.5751 -2.5530 -2.5305 -2.5075 -2.4841
-2.4602 -2.4359 -2.4112 -2.3861 -2.3605 -2.3346 -2.3083 -2.2816 -2.2545 -2.2271 -2.1994 -2.1712
-2.1428 -2.1140 -2.0849 -2.0555 -2.0257 -1.9957 -1.9654 -1.9349 -1.9040 -1.8729 -1.8415 -1.8099
-1.7781 -1.7461 -1.7138 -1.6813 -1.6486 -1.6158 -1.5827 -1.5495 -1.5161 -1.4826 -1.4489 -1.4151
-1.3811 -1.3471 -1.3129 -1.2786 -1.2443 -1.2098 -1.1753 -1.1407 -1.1061 -1.0714 -1.0366 -1.0019
-0.9671 -0.9323 -0.8975 -0.8627 -0.8279 -0.7931 -0.7584 -0.7237 -0.6890 -0.6544 -0.6199 -0.5854
-0.5510 -0.5167 -0.4825 -0.4484 -0.4144 -0.3806 -0.3469 -0.3133 -0.2798 -0.2465 -0.2134 -0.1804
-0.1477 -0.1151 -0.0827 -0.0504 -0.0185 0.0133 0.0449 0.0762 0.1073 0.1381 0.1687 0.1991
0.2291 0.2589 0.2884 0.3177 0.3466 0.3752 0.4035 0.4315 0.4592 0.4866 0.5136 0.5403
0.5666 0.5926 0.6182 0.6435 0.6684 0.6929 0.7170 0.7407 0.7641 0.7870 0.8096 0.8317
0.8534 0.8747 0.8956 0.9161 0.9361 0.9557 0.9748 0.9935 1.0117 1.0295 1.0468 1.0636
1.0800 1.0959 1.1113 1.1263 1.1407 1.1547 1.1682 1.1812 1.1937 1.2057 1.2171 1.2281
1.2386 1.2485 1.2580 1.2669 1.2753 1.2832 1.2905 1.2973 1.3036 1.3094 1.3146 1.3193
1.3235 1.3271 1.3302 1.3327 1.3347 1.3362 1.3371 1.3375 1.3373 1.3366 1.3354 1.3336
1.3312 1.3283 1.3249 1.3209 1.3164 1.3113 1.3057 1.2995 1.2928 1.2856 1.2778 1.2695
1.2606 1.2512 1.2412 1.2308 1.2197 1.2082 1.1961 1.1835 1.1704 1.1567 1.1425 1.1278
1.1126 1.0969 1.0806 1.0638 1.0466 1.0288 1.0105 0.9917 0.9724 0.9527 0.9324 0.9116
0.8904 0.8687 0.8465 0.8239 0.8007 0.7771 0.7531 0.7286 0.7036 0.6782 0.6524 0.6261
0.5994 0.5722 0.5447 0.5167 0.4883 0.4595 0.4302 0.4006 0.3706 0.3402 0.3094 0.2783
0.2467 0.2148 0.1826 0.1500 0.1170 0.0837 0.0501 0.0161 -0.0182 -0.0528 -0.0878 -0.1230
-0.1585 -0.1944 -0.2305 -0.2669 -0.3035 -0.3405 -0.3777 -0.4151 -0.4528 -0.4908 -0.5289 -0.5673
-0.6059 -0.6448 -0.6838 -0.7230 -0.7625 -0.8021 -0.8418 -0.8818 -0.9219 -0.9622 -1.0026 -1.0431
-1.0838 -1.1246 -1.1655 -1.2066 -1.2477 -1.2889 -1.3302 -1.3716 -1.4131 -1.4546 -1.4962 -1.5378
b) Compute a DFT for x(n) also plot magnitude and phase spectrums of X(k).
SOLUTION:
MATLAB CODE:
%X1(t)=5*cos(1000*pi*t), X2(t)=5*cos(2000*pi*t+0.25*pi), X3(t)=5*cos(3200*pi*t+0.5*pi)
Fs=8000;
f1=500/8000;
f2=1000/8000;
f3=1600/8000;
n=[0:0.01:5];
%Xn=X1(n)+X2(n)+X3(n)
xn=5*cos(2*pi*f1*n)+5*cos(2*pi*f2*n+0.25*pi)+5*cos(2*pi*f3*n+0.5*pi);
Xk=fft(xn);
MAG=abs(Xk);
ang=phase(Xk);
subplot 211
stem(MAG)
title('MAGNITUDE OF DFT SIGNAL AFTER SAMPLING')
subplot 212
stem(ang)
title('Phase of DFT SIGNAL AFTER SAMPLING')
OUTPUT:
Task # 5: Write a script in MATLAB to design the DFT algorithm by using its generalized formula
and also compare the result by using MATLAB built-in function.
k=0,1,2,………N-1
SOLUTION:
MATLAB CODE:
x = [1 0 0 1];
N = length(x);
X = zeros(4,1)
for k = 0:N-1
for n = 0:N-1
X(k+1) = X(k+1) + x(n+1)*exp(-j*pi/2*n*k)
end
end
t = 0:N-1
subplot(311)
stem(t,x);
xlabel('Time (s)');
ylabel('Amplitude');
title('Time domain - Input sequence')
subplot(312)
stem(t,X)
xlabel('Frequency');
ylabel('|X(k)|');
title('Frequency domain - Magnitude response')
subplot(313)
stem(t,angle(X))
xlabel('Frequency');
ylabel('Phase');
title('Frequency domain - Phase response')
X % to check |X(k)|
angle(X) % to check phase
OUTPUT:
Task # 6:Write a script in MATLAB to design the IDFT algorithm by using its generalized
formula and also compare the result by using MATLAB built-in function.
n=0,1,2,………N-1
SOLUTION:
MATLAB CODE:
%MATLAB CODE FOR IDFT
X =[2.0000 + 0.0000i 1.0000 + 1.0000i 0.0000 + 0.0000i 1.0000 - 1.0000i]
N=length(X);
x=zeros(4,1);
for k = 0:N-1
for n = 0:N-1
x(k+1) =[ x(k+1) + X(n+1)*exp(j*pi/2*n*k)]/N
end
end
n = 0:N-1
subplot(311)
stem(n,x);
xlabel('Time (s)');
ylabel('Amplitude');
title('Time domain - Input sequence')
subplot(312)
stem(n,x)
xlabel('Time');
ylabel('|X(k)|');
title('Time domain - Magnitude response')
subplot(313)
stem(n,angle(x))
xlabel('Time');
ylabel('Phase');
title('Time domain - Phase response')
x % to check |X(k)|
angle(x) % to check phase
Conclusion
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------