echo on
% use atan2 or angle instead atan to get phase
% push any key
pause
clc
%               ====== EXERCISE 1.1 ======
%
%               ****** 1.1.a ******
xi=[1 0 0 0 0 0 0 0];
n=0:1:7;
X=fft(xi);
subplot(221),stem(n,real(xi))
title('Real part of xi[n]'), xlabel('INDEX (n)')
subplot(223),stem(n,imag(xi))
title('Imag part of xi[n]'), xlabel('INDEX (n)')
subplot(222),stem(n,real(X))
title('Real part of DFT'), xlabel('INDEX (k)')
subplot(224),stem(n,imag(X))
title('Imag part of DFT'), xlabel('INDEX (k)')
pause
magn1=sqrt(mul(real(xi),real(xi))+mul(imag(xi),imag(xi)))
phase1=atan2(imag(xi),real(xi))
magn2=sqrt(mul(real(X),real(X))+mul(imag(X),imag(X)))
phase2=atan2(imag(X),real(X))
subplot(221),stem(n,magn1)
title('magnitude part of xi[n]'), xlabel('INDEX (n)')
subplot(223),stem(n,phase1)
title('phase part of xi[n]'), xlabel('INDEX (n)')
subplot(222),stem(n,magn2)
title('magnitude part of DFT'), xlabel('INDEX (k)')
subplot(224),stem(n,phase2)
title('phase part of DFT'), xlabel('INDEX (k)')
pause
clear
clc
%               ****** 1.1.b ******

x1=[1 1 1 1 1 1 1 1];
n=0:1:7;
X=fft(x1);
subplot(221),stem(n,real(x1))
title('Real part of x1[n]'), xlabel('INDEX (n)')
subplot(223),stem(n,imag(x1))
title('Imag part of x1[n]'), xlabel('INDEX (n)')
subplot(222),stem(n,real(X))
title('Real part of DFT'), xlabel('INDEX (k)')
subplot(224),stem(n,imag(X))
title('Imag part of DFT'), xlabel('INDEX (k)')
pause
magn1=sqrt(mul(real(x1),real(x1))+mul(imag(x1),imag(x1)))
phase1=atan(dev(imag(x1),real(x1)))
magn2=sqrt(mul(real(X),real(X))+mul(imag(X),imag(X)))
phase2=atan(dev(imag(X),real(X)))
subplot(221),stem(n,magn1)
title('magnitude part of x1[n]'), xlabel('INDEX (n)')
subplot(223),stem(n,phase1)
title('phase part of x1[n]'), xlabel('INDEX (n)')
subplot(222),stem(n,magn2)
title('magnitude part of DFT'), xlabel('INDEX (k)')
subplot(224),stem(n,phase2)
title('phase part of DFT'), xlabel('INDEX (k)')
pause
clear
clc
%               ****** 1.1.c-1 ******

xish1=[0 0 0 1 0 0 0 0];
n=0:1:7;
X=fft(xish1);
subplot(221),stem(n,real(xish1))
title('Real part of xish1[n]'), xlabel('INDEX (n)')
subplot(223),stem(n,imag(xish1))
title('Imag part of xish1[n]'), xlabel('INDEX (n)')
subplot(222),stem(n,real(X))
title('Real part of DFT'), xlabel('INDEX (k)')
subplot(224),stem(n,imag(X))
title('Imag part of DFT'), xlabel('INDEX (k)')
pause
magn1=sqrt(mul(real(xish1),real(xish1))+mul(imag(xish1),imag(xish1)))
phase1=atan(dev(imag(xish1),real(xish1)))
magn2=sqrt(mul(real(X),real(X))+mul(imag(X),imag(X)))
phase2=atan(dev(imag(X),real(X)))
subplot(221),stem(n,magn1)
title('magnitude part of xish1[n]'), xlabel('INDEX (n)')
subplot(223),stem(n,phase1)
title('phase part of xish1[n]'), xlabel('INDEX (n)')
subplot(222),stem(n,magn2)
title('magnitude part of DFT'), xlabel('INDEX (k)')
subplot(224),stem(n,phase2)
title('phase part of DFT'), xlabel('INDEX (k)')
pause
clear
clc
%               ****** 1.1.c-2 ******

xish2=[0 0 0 0 1 0 0 0];
n=0:1:7;
X=fft(xish2);
subplot(221),stem(n,real(xish2))
title('Real part of xish[n]'), xlabel('INDEX (n)')
subplot(223),stem(n,imag(xish2))
title('Imag part of xish2[n]'), xlabel('INDEX (n)')
subplot(222),stem(n,real(X))
title('Real part of DFT'), xlabel('INDEX (k)')
subplot(224),stem(n,imag(X))
title('Imag part of DFT'), xlabel('INDEX (k)')
pause
magn1=sqrt(mul(real(xish2),real(xish2))+mul(imag(xish2),imag(xish2)))
phase1=atan(dev(imag(xish2),real(xish2)))
magn2=sqrt(mul(real(X),real(X))+mul(imag(X),imag(X)))
phase2=atan(dev(imag(X),real(X)))
subplot(221),stem(n,magn1)
title('magnitude part of xish2[n]'), xlabel('INDEX (n)')
subplot(223),stem(n,phase1)
title('phase part of xish2[n]'), xlabel('INDEX (n)')
subplot(222),stem(n,magn2)
title('magnitude part of DFT'), xlabel('INDEX (k)')
subplot(224),stem(n,phase2)
title('phase part of DFT'), xlabel('INDEX (k)')
pause
clear
clc
%               ****** 1.1.d-1 ******

xb1=[1 1 1 0 0 0 0 0];
n=0:1:7;
X=fft(xb1);
subplot(221),stem(n,real(xb1))
title('Real part of xb[n]'), xlabel('INDEX (n)')
subplot(223),stem(n,imag(xb1))
title('Imag part of xb1[n]'), xlabel('INDEX (n)')
subplot(222),stem(n,real(X))
title('Real part of DFT'), xlabel('INDEX (k)')
subplot(224),stem(n,imag(X))
title('Imag part of DFT'), xlabel('INDEX (k)')
pause
magn1=sqrt(mul(real(xb1),real(xb1))+mul(imag(xb1),imag(xb1)))
phase1=atan(dev(imag(xb1),real(xb1)))
magn2=sqrt(mul(real(X),real(X))+mul(imag(X),imag(X)))
phase2=atan(dev(imag(X),real(X)))
subplot(221),stem(n,magn1)
title('magnitude part of xb1[n]'), xlabel('INDEX (n)')
subplot(223),stem(n,phase1)
title('phase part of xb1[n]'), xlabel('INDEX (n)')
subplot(222),stem(n,magn2)
title('magnitude part of DFT'), xlabel('INDEX (k)')
subplot(224),stem(n,phase2)
title('phase part of DFT'), xlabel('INDEX (k)')
pause
clear
clc
%               ****** 1.1.d-2 ******

xb2=[1 1 1 1 0 0 0 0];
n=0:1:7;
X=fft(xb2);
subplot(221),stem(n,real(xb2))
title('Real part of xb2[n]'), xlabel('INDEX (n)')
subplot(223),stem(n,imag(xb2))
title('Imag part of xb2[n]'), xlabel('INDEX (n)')
subplot(222),stem(n,real(X))
title('Real part of DFT'), xlabel('INDEX (k)')
subplot(224),stem(n,imag(X))
title('Imag part of DFT'), xlabel('INDEX (k)')
pause
magn1=sqrt(mul(real(xb2),real(xb2))+mul(imag(xb2),imag(xb2)))
phase1=atan(dev(imag(xb2),real(xb2)))
magn2=sqrt(mul(real(X),real(X))+mul(imag(X),imag(X)))
phase2=atan(dev(imag(X),real(X)))
subplot(221),stem(n,magn1)
title('magnitude part of xb2[n]'), xlabel('INDEX (n)')
subplot(223),stem(n,phase1)
title('phase part of xb2[n]'), xlabel('INDEX (n)')
subplot(222),stem(n,magn2)
title('magnitude part of DFT'), xlabel('INDEX (k)')
subplot(224),stem(n,phase2)
title('phase part of DFT'), xlabel('INDEX (k)')
pause
clear
clc
%               ****** 1.1.e ******

xbsy=[1 1 0 0 0 0 0 1];
n=0:1:7;
X=fft(xbsy);
subplot(221),stem(n,real(xbsy))
title('Real part of xbsy[n]'), xlabel('INDEX (n)')
subplot(223),stem(n,imag(xbsy))
title('Imag part of xbsy[n]'), xlabel('INDEX (n)')
subplot(222),stem(n,real(X))
title('Real part of DFT'), xlabel('INDEX (k)')
subplot(224),stem(n,imag(X))
title('Imag part of DFT'), xlabel('INDEX (k)')
pause
magn1=sqrt(mul(real(xbsy),real(xbsy))+mul(imag(xbsy),imag(xbsy)))
phase1=atan(dev(imag(xbsy),real(xbsy)))
magn2=sqrt(mul(real(X),real(X))+mul(imag(X),imag(X)))
phase2=atan(dev(imag(X),real(X)))
subplot(221),stem(n,magn1)
title('magnitude part of xbsy[n]'), xlabel('INDEX (n)')
subplot(223),stem(n,phase1)
title('phase part of xbsy[n]'), xlabel('INDEX (n)')
subplot(222),stem(n,magn2)
title('magnitude part of DFT'), xlabel('INDEX (k)')
subplot(224),stem(n,phase2)
title('phase part of DFT'), xlabel('INDEX (k)')
pause
clear
clc
echo on
clc

%         ******** exercise 1.2.a ***********
nn=0:20;
ss=cos(2*pi*nn/21);
SS=fft(ss);
kk=nn;
subplot(221),stem(kk,real(ss))
title('REAL PART of s[n]'), xlabel('INDEX (n)')
subplot(223),stem(kk,imag(ss))
title('IMAG PART of s[n]'), xlabel('INDEX (n)')
subplot(222),stem(kk,real(SS))
title('REAL PART of DFT'), xlabel('INDEX (k)')
subplot(224),stem(kk,imag(SS))
title('IMAG PART of DFT'), xlabel('INDEX (k)')
pause
clc
clear

%         ******** exercise 1.2.b-1 ***********
nn=0:20;
ss=sin(2*pi*nn/21);
SS=fft(ss);
kk=nn;
subplot(221),stem(kk,real(ss))
title('REAL PART of s[n]'), xlabel('INDEX (n)')
subplot(223),stem(kk,imag(ss))
title('IMAG PART of s[n]'), xlabel('INDEX (n)')
subplot(222),stem(kk,real(SS))
title('REAL PART of DFT'), xlabel('INDEX (k)')
subplot(224),stem(kk,imag(SS))
title('IMAG PART of DFT'), xlabel('INDEX (k)')
pause
clc
magn1=sqrt(mul(real(ss),real(ss))+mul(imag(ss),imag(ss)));
phase1=atan(dev(imag(ss),real(ss)));
magn2=sqrt(mul(real(SS),real(SS))+mul(imag(SS),imag(SS)));
phase2=atan(dev(imag(SS),real(SS)));
subplot(221),stem(kk,magn1)
title('Magnitude PART of s[n]'),xlabel('INDEX(n)')
subplot(223),stem(kk,phase1)
title('Phase PART of s[n]'),xlabel('INDEX(n)')
subplot(222),stem(kk,magn2)
title('Magnitude PART of s[k]'),xlabel('INDEX(k)')
subplot(224),stem(kk,phase2)
title('Phase PART of s[k]'),xlabel('INDEX(k)')
pause
clc
clear


%         ******** exercise 1.2.b-2 ***********
nn=0:20;
ss=cos(2*pi*nn/21+pi/4);
SS=fft(ss);
kk=nn;
subplot(221),stem(kk,real(ss))
title('REAL PART of s[n]'), xlabel('INDEX (n)')
subplot(223),stem(kk,imag(ss))
title('IMAG PART of s[n]'), xlabel('INDEX (n)')
subplot(222),stem(kk,real(SS))
title('REAL PART of DFT'), xlabel('INDEX (k)')
subplot(224),stem(kk,imag(SS))
title('IMAG PART of DFT'), xlabel('INDEX (k)')
pause
clc
magn1=sqrt(mul(real(ss),real(ss))+mul(imag(ss),imag(ss)));
phase1=atan(dev(imag(ss),real(ss)));
magn2=sqrt(mul(real(SS),real(SS))+mul(imag(SS),imag(SS)));
phase2=atan(dev(imag(SS),real(SS)));
subplot(221),stem(kk,magn1)
title('Magnitude PART of s[n]'),xlabel('INDEX(n)')
subplot(223),stem(kk,phase1)
title('Phase PART of s[n]'),xlabel('INDEX(n)')
subplot(222),stem(kk,magn2)
title('Magnitude PART of s[k]'),xlabel('INDEX(k)')
subplot(224),stem(kk,phase2)
title('Phase PART of s[k]'),xlabel('INDEX(k)')
pause
clc
clear

%         ******** exercise 1.2.c ***********
nn=0:20;
ss=cos(2*pi*nn/7);
SS=fft(ss);
kk=nn;
subplot(221),stem(kk,real(ss))
title('REAL PART of s[n]'), xlabel('INDEX (n)')
subplot(223),stem(kk,imag(ss))
title('IMAG PART of s[n]'), xlabel('INDEX (n)')
subplot(222),stem(kk,real(SS))
title('REAL PART of DFT'), xlabel('INDEX (k)')
subplot(224),stem(kk,imag(SS))
title('IMAG PART of DFT'), xlabel('INDEX (k)')
pause
clc
clear

%         ******** exercise 1.2.d ***********
nn=0:20;
ss=cos(2*pi*nn*3.1/21);
SS=fft(ss);
kk=nn;
subplot(221),stem(kk,real(ss))
title('REAL PART of s[n]'), xlabel('INDEX (n)')
subplot(223),stem(kk,imag(ss))
title('IMAG PART of s[n]'), xlabel('INDEX (n)')
subplot(222),stem(kk,real(SS))
title('REAL PART of DFT'), xlabel('INDEX (k)')
subplot(224),stem(kk,imag(SS))
title('IMAG PART of DFT'), xlabel('INDEX (k)')
pause
clc
clear

%         ******** exercise 1.2.e-1 ***********
nn=0:10;
ss=cos(2*pi*nn/11);
SS=fft(ss);
kk=nn;
subplot(221),stem(kk,real(ss))
title('REAL PART of s[n]'), xlabel('INDEX (n)')
subplot(223),stem(kk,imag(ss))
title('IMAG PART of s[n]'), xlabel('INDEX (n)')
subplot(222),stem(kk,real(SS))
title('REAL PART of DFT'), xlabel('INDEX (k)')
subplot(224),stem(kk,imag(SS))
title('IMAG PART of DFT'), xlabel('INDEX (k)')
pause
clc
clear

%         ******** exercise 1.2.e-2 ***********
nn=0:30;
ss=cos(2*pi*nn/31);
SS=fft(ss);
kk=nn;
subplot(221),stem(kk,real(ss))
title('REAL PART of s[n]'), xlabel('INDEX (n)')
subplot(223),stem(kk,imag(ss))
title('IMAG PART of s[n]'), xlabel('INDEX (n)')
subplot(222),stem(kk,real(SS))
title('REAL PART of DFT'), xlabel('INDEX (k)')
subplot(224),stem(kk,imag(SS))
title('IMAG PART of DFT'), xlabel('INDEX (k)')
pause
clc
clear
echo on
clc

%      *********** exercise 1.3.a *************
nn=0:15;
cc=exp(j*6*pi*nn/16);
CC=fft(cc);
kk=nn;
subplot(221),stem(kk,real(cc))
title('REAL PART of c[n]'), xlabel('INDEX (n)')
subplot(223),stem(kk,imag(cc))
title('IMAG PART of c[n]'), xlabel('INDEX (n)')
subplot(222),stem(kk,real(CC))
title('REAL PART of DFT'), xlabel('INDEX (k)')
subplot(224),stem(kk,imag(CC))
title('IMAG PART of DFT'), xlabel('INDEX (k)')
pause
clc
clear

%      *********** exercise 1.3.b *************
kk=0:15;
XX=exp(j*6*pi*kk/16);
xx=ifft(XX);
nn=kk;
subplot(221),stem(nn,real(xx))
title('REAL PART of inverse DFT'), xlabel('INDEX (n)')
subplot(223),stem(nn,imag(xx))
title('IMAG PART of inverse DFT'), xlabel('INDEX (n)')
subplot(222),stem(nn,real(XX))
title('REAL PART of X[k]'), xlabel('INDEX (k)')
subplot(224),stem(kk,imag(XX))
title('IMAG PART of X[k]'), xlabel('INDEX (k)')
pause
clc
clear

%      *********** exercise 1.3.c *************
nn=0:15;
cc=exp(j*5*pi*nn/16);
CC=fft(cc);
kk=nn;
subplot(221),stem(kk,real(cc))
title('REAL PART of c[n]'), xlabel('INDEX (n)')
subplot(223),stem(kk,imag(cc))
title('IMAG PART of c[n]'), xlabel('INDEX (n)')
subplot(222),stem(kk,real(CC))
title('REAL PART of DFT'), xlabel('INDEX (k)')
subplot(224),stem(kk,imag(CC))
title('IMAG PART of DFT'), xlabel('INDEX (k)')
pause
clc
clear

%      *********** exercise 1.3.d *************
nn=0:15;
cc=exp(j*6*pi*nn/16);
xx=imag(cc);
XX=fft(xx);
kk=nn;
subplot(221),stem(kk,real(xx))
title('REAL PART of sin(wt)'), xlabel('INDEX (n)')
subplot(223),stem(kk,imag(xx))
title('IMAG PART of sin(wt)'), xlabel('INDEX (n)')
subplot(222),stem(kk,real(XX))
title('REAL PART of DFT of sin(wt)'), xlabel('INDEX (k)')
subplot(224),stem(kk,imag(XX))
title('IMAG PART of DFT of sin(wt)'), xlabel('INDEX (k)')
pause
clc
clear
