You are on page 1of 22

%P3.

1
function [X] = dtft(x,nx,w)
% Computes Discrete-time Fourier Transform
% [X] = dtft(x,n,w)
%
% X = DTFT values are computed at w frequencies
% x = finite duration sequence over n
% n = sample position vector
% w = frequency location vector
M = 250;
k = -M:M;
% k = 501 points
X = x*(exp(-j*pi/M)).^(nx'*k); % Computes DTFT

%P3.2 colordef white; clear; clc;


M = 250;
k = -M:M;
w = (pi/M)*k;%[0, pi]axis divided into 501 points.
[x,nx] = stepseq(0,-50,50); % sequence x1a(n)
[x2,nx2] = stepseq(20,-50,50); % sequence x1b(n)
[x,nx] = sigadd(x,nx,x2,nx2);
x2 = 2*0.8.^nx2;
[x,nx] = sigmult(x,nx,x2,nx2);
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); angX = angle(X);
figure(1);
subplot(3,2,1); stem(nx,x,'k');
xlabel('n'); title('Given Sequence'); ylabel('x1(n)')
subplot(3,2,3); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,5); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')
[x,nx] = stepseq(0,0,100); % sequence x1a(n)
[x2,nx2] = stepseq(50,0,100); % sequence x1b(n)
[x,nx] = sigadd(x,nx,x2,nx2);
x2 = 0.9.^nx2;
[x,nx] = sigmult(x,nx,x2,nx2);
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); angX = angle(X);
figure(1);
subplot(3,2,2); stem(nx,x,'k');
xlabel('n'); title('Given Sequence'); ylabel('x2(n)')
subplot(3,2,4); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,6); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')

x = [4,3,2,1,2,3,4];
nx = 0:length(x)-1;
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); angX = angle(X);
figure(2);
subplot(3,2,1); stem(nx,x,'k');
xlabel('n'); title('Given Sequence'); ylabel('x3(n)')
subplot(3,2,3); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,5); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')
x = [4,3,2,1,1,2,3,4];
nx = 0:length(x)-1;
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); angX = angle(X);
figure(2);
subplot(3,2,2); stem(nx,x,'k');
xlabel('n'); title('Given Sequence'); ylabel('x4(n)')
subplot(3,2,4); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,6); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')

x = 4:-1:-4;
nx = 0:length(x)-1;
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); angX = angle(X);
figure(3);
subplot(3,2,1); stem(nx,x,'k');
xlabel('n'); title('Given Sequence'); ylabel('x5(n)')
subplot(3,2,3); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,5); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')
x = [4,3,2,1,-1,-2,-3,-4];
nx = 0:length(x)-1;
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); angX = angle(X);
figure(3);
subplot(3,2,2); stem(nx,x,'k');
xlabel('n'); title('Given Sequence'); ylabel('x6(n)')
subplot(3,2,4); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,6); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')

clear; clc;
%P3.3
M = 250;
k = -M:M;
w = (pi/M)*k;

% [0, pi] axis divided into 501 points.

[x,nx] = stepseq(0,-10,10); % sequence x1(n)


x=3*0.9^3*x;
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); angX = angle(X);
figure(1);
subplot(3,2,1); stem(nx,x,'k');
xlabel('n'); title('Given Sequence'); ylabel('x1(n)')
subplot(3,2,3); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,5); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')
[x,nx] = stepseq(2,-10,10); % sequence x2(n)
[x,nx] = sigmult(x,nx,2*0.8.^(nx+2),nx);
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); angX = angle(X);
subplot(3,2,2); stem(nx,x,'k');
xlabel('n'); title('Given Sequence'); ylabel('x2(n)')
subplot(3,2,4); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,6); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')
[x,nx] = stepseq(0,-10,10); % sequence x3(n)
[x,nx] = sigmult(x,nx,0.5.^nx,nx);
[x,nx] = sigmult(x,nx,nx,nx);
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); angX = angle(X);
figure(2);
subplot(3,2,1); stem(nx,x,'k');
xlabel('n'); title('Given Sequence'); ylabel('x3(n)')
subplot(3,2,3); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,5); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')
[x,nx] = stepseq(2,-10,10); % sequence x4(n)
[x,nx] = sigmult(x,nx,-0.7.^(nx-1),nx);
[x,nx] = sigmult(nx+2,nx,x,nx);
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); angX = angle(X);
subplot(3,2,2); stem(nx,x,'k');
xlabel('n'); title('Given Sequence'); ylabel('x4(n)')
subplot(3,2,4); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,6); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')

[x,nx] = stepseq(0,-10,10); % sequence x5(n)


[x,nx] = sigmult(cos(0.1*pi*nx),nx,x,nx);
[x,nx] = sigmult(5*(-0.9).^nx,nx,x,nx);
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); angX = angle(X);
figure(3);
subplot(3,2,1); stem(nx,x,'k');
xlabel('n'); title('Given Sequence'); ylabel('x5(n)')
subplot(3,2,3); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,5); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')

%P3.4
colordef white; clear; clc; M = 250;
% [0, pi] axis divided into 501 points.

k = -M:M;

w = (pi/M)*k;

N = 5; nx = [-N-10:N+10];
x = stepseq(-N,-N-10,N+10)-stepseq(N+1,-N-10,N+10);
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); magX = magX./max(magX); angX = angle(X); figure(1);
subplot(3,2,1); stem(nx,x,'k');
xlabel('n'); title('Given Sequence at N = 5'); ylabel('x1(n)')
subplot(3,2,3); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,5); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')
N = 15; nx = [-N-10:N+10];
x = stepseq(-N,-N-10,N+10)-stepseq(N+1,-N-10,N+10);
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); magX = magX./max(magX); angX = angle(X); figure(1);
subplot(3,2,2); stem(nx,x,'k');
xlabel('n'); title('Given Sequence at N = 15'); ylabel('x1(n)')
subplot(3,2,4); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,6); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')

N = 25; nx = [-N-10:N+10];
x = stepseq(-N,-N-10,N+10)-stepseq(N+1,-N-10,N+10);
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); magX = magX./max(magX); angX = angle(X); figure(2);
subplot(3,2,1); stem(nx,x,'k');
xlabel('n'); title('Given Sequence at N = 25'); ylabel('x1(n)')
subplot(3,2,3); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,5); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')
N = 100; nx = [-N-10:N+10];
x = stepseq(-N,-N-10,N+10)-stepseq(N+1,-N-10,N+10);
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); magX = magX./max(magX); angX = angle(X); figure(2);
subplot(3,2,2); stem(nx,x,'k');
xlabel('n'); title('Given Sequence at N = 100'); ylabel('x1(n)')
subplot(3,2,4); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,6); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')

%P3.4
colordef white; clear;
M = 250;
k = -M:M;
points.

clc;
w = (pi/M)*k;

% [0, pi] axis divided into 501

N = 5; nx = [-N-10:N+10];
x = stepseq(-N,-N-10,N+10)-stepseq(N+1,-N-10,N+10);
x = (1-abs(nx)/N).*x;
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); magX = magX./max(magX); angX = angle(X); figure(1);
subplot(3,2,1); stem(nx,x,'k');
xlabel('n'); title('Given Sequence at N = 5'); ylabel('x1(n)')
subplot(3,2,3); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,5); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')
N = 15; nx = [-N-10:N+10];
x = stepseq(-N,-N-10,N+10)-stepseq(N+1,-N-10,N+10);
x = (1-abs(nx)/N).*x;
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); magX = magX./max(magX); angX = angle(X); figure(1);
subplot(3,2,2); stem(nx,x,'k');
xlabel('n'); title('Given Sequence at N = 15'); ylabel('x1(n)')
subplot(3,2,4); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,6); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')

N = 25; nx = [-N-10:N+10];
x = stepseq(-N,-N-10,N+10)-stepseq(N+1,-N-10,N+10);
x = (1-abs(nx)/N).*x;
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); magX = magX./max(magX); angX = angle(X); figure(2);
subplot(3,2,1); stem(nx,x,'k');
xlabel('n'); title('Given Sequence at N = 25'); ylabel('x1(n)')
subplot(3,2,3); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,5); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')
N = 100; nx = [-N-10:N+10];
x = stepseq(-N,-N-10,N+10)-stepseq(N+1,-N-10,N+10);
x = (1-abs(nx)/N).*x;
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); magX = magX./max(magX); angX = angle(X); figure(2);
subplot(3,2,2); stem(nx,x,'k');
xlabel('n'); title('Given Sequence at N = 100'); ylabel('x1(n)')
subplot(3,2,4); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,6); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')

%P3.6
colordef white; clear;
M = 250;
k = -M:M;
points.

clc;
w = (pi/M)*k;

% [0, pi] axis divided into 501

N = 5; nx = [-N-10:N+10];
nx = [-N-10:N+10]; x = stepseq(-N,-N-10,N+10)-stepseq(N+1,-N-10,N+10);
x = (0.5+0.5*cos(pi*nx/N)).*x;
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); magX = magX./max(magX); angX = angle(X); figure(1);
subplot(3,2,1); stem(nx,x,'k');
xlabel('n'); title('Given Sequence at N = 5'); ylabel('x1(n)')
subplot(3,2,3); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,5); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')
N = 15; nx = [-N-10:N+10];
nx = [-N-10:N+10]; x = stepseq(-N,-N-10,N+10)-stepseq(N+1,-N-10,N+10);
x = (0.5+0.5*cos(pi*nx/N)).*x;
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); magX = magX./max(magX); angX = angle(X); figure(1);
subplot(3,2,2); stem(nx,x,'k');
xlabel('n'); title('Given Sequence at N = 15'); ylabel('x1(n)')
subplot(3,2,4); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,6); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')

N = 25; nx = [-N-10:N+10];
nx = [-N-10:N+10]; x = stepseq(-N,-N-10,N+10)-stepseq(N+1,-N-10,N+10);
x = (0.5+0.5*cos(pi*nx/N)).*x;
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); magX = magX./max(magX); angX = angle(X); figure(2);
subplot(3,2,1); stem(nx,x,'k');
xlabel('n'); title('Given Sequence at N = 25'); ylabel('x1(n)')
subplot(3,2,3); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,5); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')
N = 100; nx = [-N-10:N+10];
nx = [-N-10:N+10]; x = stepseq(-N,-N-10,N+10)-stepseq(N+1,-N-10,N+10);
x = (0.5+0.5*cos(pi*nx/N)).*x;
X = dtft(x,nx,w);
% Computes Discrete-Time Fourier Transform
magX = abs(X); magX = magX./max(magX); angX = angle(X); figure(2);
subplot(3,2,2); stem(nx,x,'k');
xlabel('n'); title('Given Sequence at N = 100'); ylabel('x1(n)')
subplot(3,2,4); plot(w/pi,magX,'k');
xlabel('frequency in pi units'); title('Magnitude Part');
ylabel('Magnitude')
subplot(3,2,6); plot(w/pi,angX,'k');
xlabel('frequency in pi units'); title('Angle Part'); ylabel('Radians')

% P3.7
colordef white; clear;
M = 250;
k = -M:M;
points.

clc;
w = (pi/M)*k;

% [0, pi] axis divided into 501

n = -50:50;
x = exp(j*0.1*pi*n).*(stepseq(0,-50,50)-stepseq(20,-50,50));
[xe,xo,m] = evenoddcomplex(x,n);
XE = dtft(xe,m,w); XO = dtft(xo,m,w);
figure(1);
subplot(2,2,1); plot(w/pi,abs(XE),'k'); axis tight;
xlabel('frequency in pi units');
title('Conjugate Symmetric Part Fourier Transform'); ylabel('|XE|');
subplot(2,2,3); plot(w/pi,abs(XO),'k'); axis tight;
xlabel('frequency in pi units');
title('Conjugate Antisymmetric Part Fourier Transform'); ylabel('|XO|');
subplot(2,2,2); plot(w/pi,abs(real(dtft(x,n,w))),'k'); axis tight;
xlabel('frequency in pi units');
title('Real part of DTFT(x)'); ylabel('|Re(DTFT(x))|');
subplot(2,2,4); plot(w/pi,abs(j*imag(dtft(x,n,w))),'k');
axis tight;
xlabel('frequency in pi units'); title('Imaginary part of DTFT(x)');
ylabel('|Im(DTFT(x))|');

% P3.8
colordef white; clear;
M = 250;
k = -M:M;
points.

clc;
w = (pi/M)*k;

% [0, pi] axis divided into 501

n = -50:50;
x = exp(j*0.1*pi*n).*(stepseq(0,-50,50)-stepseq(20,-50,50));
[xe,xo,m] = evenoddcomplex(x,n);
xr = real(x);
xi = imag(x);
XE = dft(xe,length(m));
XO = dft(xo,length(m));
fXE = idft(XE,length(XE)); fXO = idft(XO,length(XO));
figure(1);
subplot(2,2,1); plot(m,fXE,'k'); axis tight;
xlabel('frequency in pi units'); title('Conjugate Symmetric Part Inverse
Fourier Transform'); ylabel('|XE-1|');
subplot(2,2,3); plot(m,fXO,'k'); axis tight;
xlabel('frequency in pi units'); title('Conjugate Antisymmetric Part
Inverse Fourier Transform'); ylabel('|XO-1|');
subplot(2,2,2); plot(m,xr,'k'); axis tight;
xlabel('frequency in pi units'); title('Real part of x(n)');
ylabel('Re((x(n))');
subplot(2,2,4); plot(m,xi,'k');
axis tight;
xlabel('frequency in pi units'); title('Imaginary part of x(n)');
ylabel('Im(x(n))');

% P3.9
colordef white; clear;
M = 250;
k = -M:M;
points.
w0 = pi/2; figure(1);

clc;
w = (pi/M)*k;

% [0, pi] axis divided into 501

N = 5;
n = [-N-10:N+10];
x = stepseq(-N,-N-10,N+10)-stepseq(N+1,-N-10,N+10);
x = cos(w0*n).*x;
X = dtft(x,n,w);
subplot(3,3,1); plot(w/pi,abs(X),'k');
xlabel('frequency in pi units'); title('Fourier Transform Magnitude N =
5'); ylabel('|X|');
subplot(3,3,4); plot(w/pi,angle(X),'k');
xlabel('frequency in pi units'); title('Fourier Transform Phase N = 5');
ylabel('\theta');
N = 15;
n = [-N-10:N+10];
x = stepseq(-N,-N-10,N+10)-stepseq(N+1,-N-10,N+10);
x = cos(w0*n).*x;
X = dtft(x,n,w);
subplot(3,3,2); plot(w/pi,abs(X),'k');
xlabel('frequency in pi units'); title('Fourier Transform Magnitude N =
15'); ylabel('|X|');
subplot(3,3,5); plot(w/pi,angle(X),'k');
xlabel('frequency in pi units'); title('Fourier Transform Phase N = 15');
ylabel('\theta')
N = 25;
n = [-N-10:N+10];
x = stepseq(-N,-N-10,N+10)-stepseq(N+1,-N-10,N+10);
x = cos(w0*n).*x;
X = dtft(x,n,w);
subplot(3,3,3); plot(w/pi,abs(X),'k');
xlabel('frequency in pi units'); title('Fourier Transform Magnitude N =
25'); ylabel('|X|');
subplot(3,3,6); plot(w/pi,angle(X),'k');
xlabel('frequency in pi units'); title('Fourier Transform Phase N = 25');
ylabel('\theta')
N = 100;
n = [-N-10:N+10];
x = stepseq(-N,-N-10,N+10)-stepseq(N+1,-N-10,N+10);
x = cos(w0*n).*x;
X = dtft(x,n,w);
subplot(3,2,5); plot(w/pi,abs(X),'k');
xlabel('frequency in pi units'); title('Fourier Transform Magnitude N =
100'); ylabel('|X|');
subplot(3,2,6); plot(w/pi,angle(X),'k');
xlabel('frequency in pi units'); title('Fourier Transform Phase N =
100'); ylabel('\theta')

% P3.11
colordef white; clear;
M = 250;
k = -M:M;
points.
w0 = pi/2; figure(1);
n = -50:50;

clc;
w = (pi/M)*k;

% [0, pi] axis divided into 501

h = (0.9).^(abs(n));
H = dtft(h,n,w);
subplot(3,2,1); plot(w/pi,abs(H));
xlabel('frequency in pi units');
ylabel('|H|'); title('Magnitude
Response');
subplot(3,2,2); plot(w/pi,angle(H)/pi);
xlabel('frequency in pi units');
ylabel('\Theta');
title('Phase
Response');
h = sinc(0.2*n).*((stepseq(-20,-50,50)-stepseq(20,-50,50)));
H = dtft(h,n,w);
subplot(3,2,3); plot(w/pi,abs(H));
xlabel('frequency in pi units');
ylabel('|H|'); title('Magnitude
Response');
subplot(3,2,4); plot(w/pi,angle(H)/pi);
xlabel('frequency in pi units');
ylabel('\Theta');
title('Phase
Response');
h = sinc(0.2*n).*((stepseq(0,-50,50)-stepseq(40,-50,50)));
H = dtft(h,n,w);
subplot(3,2,5); plot(w/pi,abs(H));
xlabel('frequency in pi units');
ylabel('|H|'); title('Magnitude
Response');
subplot(3,2,6); plot(w/pi,angle(H)/pi);
xlabel('frequency in pi units');
ylabel('\Theta');
title('Phase
Response');

figure(2);
h = ((0.5).^n+(0.4).^n).*stepseq(0,-50,50);
H = dtft(h,n,w);
subplot(3,2,1); plot(w/pi,abs(H),'k');
xlabel('frequency in pi units');
ylabel('|H|'); title('Magnitude
Response');
subplot(3,2,2); plot(w/pi,angle(H)/pi,'k');
xlabel('frequency in pi units');
ylabel('\Theta');
title('Phase
Response');
h = (0.5).^(abs(n)).*cos(0.1*pi*n);
H = dtft(h,n,w);
subplot(3,2,3); plot(w/pi,abs(H),'k');
xlabel('frequency in pi units');
ylabel('|H|'); title('Magnitude
Response');
subplot(3,2,4); plot(w/pi,angle(H)/pi,'k');
xlabel('frequency in pi units');
ylabel('\Theta');
title('Phase
Response');

% P3.13
colordef white; clear;
M = 250;
k = -M:M;
points.
w0 = pi/2; figure(1);

clc;
w = (pi/M)*k;

% [0, pi] axis divided into 501

alpha=20;
wc=0.5*pi; n = 0:40;
h = sin(wc*(n-alpha))./(pi*(n-alpha));
h(21) = 1/exp(1);
subplot(3,1,1); plot(n,h,'k');
xlabel('n');
ylabel('h(n)'); title('Truncated Impulse Response');
H = dtft(h,n,w);
subplot(3,1,2); plot(w/pi,abs(H),'k');
xlabel('frequency in pi units');
ylabel('|H|'); title('Magnitude
Response');
subplot(3,1,3); plot(w/pi,angle(H)/pi,'k');
xlabel('frequency in pi units');
ylabel('\Theta');
title('Phase
Response');

% P3.15
function [H]=freqresp(b,a,w)
% Frequency response function from difference equation
% [H] = freqresp(b,a,w)
% H = frequency response array evaluated a w frequencies
% b = numerator coefficient array
% a = denominator coefficient array (a(1)=1)
% w = frequency location array
m = 0:length(b)-1;
l = 0:length(a)-1;
num = b*exp(-j*m'*w);
den = a*exp(-j*l'*w);
H = num./den;

% P3.19
% Seal Analgica
Dt=0.0005; t=-.5:Dt:0.5;
xa=3*cos(20*pi*t);
subplot(2,2,1); plot(t,xa,'k'); xlabel('Frequency');
ylabel('Amplitude');
title('Analog Input Signal x_a(t)');
% Seal en tiempo discreto
Ts=0.01;
n=-50:1:50;
nTs=n*Ts;
x=3*cos(20*pi*nTs);
subplot(2,2,3); stem(n,x,'k'); xlabel('Frequency'); ylabel('Amplitude');
title('Discrete Input Signal x(n)');
% x(n)*h(n)
h=((0.5).^n).*stepseq(0,-50,50);
[y,n]=conv_m(x,n,h,n);
subplot(2,2,2); stem(n,y,'k'); xlabel('Frequency'); ylabel('Amplitude');
title('Discrete Output Signal y(n)');
% Seal de salida reconstruida ya(t)
Dt=0.0005; t=-0.50:Dt:0.50;
Ts=0.01;
Fs=1/Ts;
nTs=n*Ts;
ya=y*sinc(Fs*(ones(length(n),1)*t-nTs'*ones(1,length(t))));
subplot(2,2,4); plot(t,ya,'k'); xlabel('Frequency');
ylabel('Amplitude');
title('Analog Output Signal y_a(t)');

figure(2);
% Seal Analgica
Dt=0.0005; t=-.5:Dt:0.5;
xa=3*[t>=0];
subplot(2,2,1); plot(t,xa,'k'); xlabel('Frequency');
ylabel('Amplitude');
title('Seal analogica de entrada xa(t)');
% Seal en tiempo discreto
Ts=0.01;
n=-50:1:50;nTs=n*Ts;
x=3*[nTs>=0];
subplot(2,2,3); stem(n,x,'k'); xlabel('Frequency');
ylabel('Amplitude');
title('Seal discretizada de entrada x(n)');
% x(n)*h(n)
h = ((0.5).^n).*stepseq(0,-50,50); [y,n]=conv_m(x,n,h,n);
subplot(2,2,2); stem(n,y,'k'); xlabel('Frequency');
ylabel('Amplitude');
title('Seal discretizada de salida y(n)');
% Seal de salida reconstruida ya(t)
Dt=0.0005; t=-0.50:Dt:0.50;
Ts=0.01;
Fs=1/Ts;nTs=n*Ts;
ya=y*sinc(Fs*(ones(length(n),1)*t-nTs'*ones(1,length(t))));
subplot(2,2,4); plot(t,ya,'k'); xlabel('Frequency');
ylabel('Amplitude');
title('Analog Output Signal y_a(t)');

You might also like