Consultant evaluation and lab 3
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
% value of a
|
||||
a=0.9;
|
||||
|
||||
% signal x(n)
|
||||
for n=0:200
|
||||
x(n+1) = a^n;
|
||||
end
|
||||
figure(1)
|
||||
n=0:1:200;
|
||||
stem(n,x)
|
||||
xlabel('n');
|
||||
ylabel('x(n)');
|
||||
|
||||
% question part (e)
|
||||
n=1;
|
||||
d=[1 -a];
|
||||
[h1,w]=freqz(n,d,256);
|
||||
h1mag=abs(h1);
|
||||
figure(2)
|
||||
plot(w,h1mag,'b','linewidth',2)
|
||||
xlabel('Frequency');
|
||||
ylabel('Magnitude Response');
|
||||
|
||||
% partial dtft for k =3
|
||||
[h2,w]=freqz(x(1:4),1,256);
|
||||
h2mag=abs(h2);
|
||||
hold
|
||||
plot(w,h2mag,'r','linewidth',2)
|
||||
|
||||
% partial dtft for k =10
|
||||
[h3,w]=freqz(x(1:11),1,256);
|
||||
h3mag=abs(h3);
|
||||
plot(w,h3mag,'m','linewidth',2)
|
||||
|
||||
% partial dtft for k =20
|
||||
[h4,w]=freqz(x(1:21),1,256);
|
||||
h4mag=abs(h4);
|
||||
plot(w,h4mag,'k','linewidth',2)
|
||||
|
||||
% supremum coefficients of the error
|
||||
for k=1:200
|
||||
[hk,w]=freqz(x(1:k+1),1,256);
|
||||
ek=abs(h1-hk);
|
||||
coeff(k)=max(ek);
|
||||
end
|
||||
figure(3)
|
||||
k=1:1:200;
|
||||
stem(k,coeff)
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import numpy as np
|
||||
import scipy as sp
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
DATA_POINTS = 200
|
||||
|
||||
def u(n):
|
||||
return np.heaviside(n, 1)
|
||||
|
||||
def main():
|
||||
a = 0.9
|
||||
n = np.arange(DATA_POINTS)
|
||||
x = a**n * u(n)
|
||||
|
||||
# Plot samples of x[n]
|
||||
plt.figure()
|
||||
plt.stem(n, x)
|
||||
plt.xlabel("n")
|
||||
plt.ylabel(r"$x[n]$")
|
||||
|
||||
# Plot the analytical DTFT
|
||||
numerator = 1
|
||||
denominator = [1, -a]
|
||||
omega, h_0 = sp.signal.freqz(numerator, denominator, 256)
|
||||
|
||||
plt.figure()
|
||||
plt.plot(omega, np.abs(h_0), label="Analytical DTFT")
|
||||
plt.xlabel("Frequency")
|
||||
plt.ylabel("Magnitude Response")
|
||||
|
||||
# Plot the truncated DTFT for K = 3, 10, 20
|
||||
for K in (3, 10, 20):
|
||||
omega, h = sp.signal.freqz(x[:K], 1, 256)
|
||||
plt.plot(omega, np.abs(h), label=f"Truncated DTFT ($K = {K}$)")
|
||||
plt.legend()
|
||||
|
||||
# Calculate the maximum error between the truncated DTFT
|
||||
# and the analytical DTFT for values of K from 1 to 200
|
||||
k = np.arange(DATA_POINTS)
|
||||
coeffs = np.zeros_like(k, dtype=np.float32)
|
||||
|
||||
for i_k in k:
|
||||
omega, h_k = sp.signal.freqz(x[:i_k], 1, 256)
|
||||
err_k = np.abs(h_0 - h_k)
|
||||
coeffs[i_k] = np.max(err_k)
|
||||
|
||||
# Plot the maximum error previously calculated
|
||||
plt.figure()
|
||||
plt.stem(k, coeffs)
|
||||
plt.xlabel("k")
|
||||
plt.ylabel("Supremum coefficients of the error")
|
||||
plt.show()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,54 @@
|
||||
% value of a
|
||||
a=0.9;
|
||||
|
||||
% signal x(n)
|
||||
for n=0:200
|
||||
x(n+1) = n*(a^n);
|
||||
end
|
||||
figure(1)
|
||||
n=0:1:200;
|
||||
stem(n,x)
|
||||
xlabel('n');
|
||||
ylabel('x(n)');
|
||||
|
||||
% question part (e)
|
||||
n=[0 a];
|
||||
d=[1 -2*a a*a];
|
||||
[h1,w]=freqz(n,d,256);
|
||||
h1mag=abs(h1);
|
||||
figure(2)
|
||||
plot(w,h1mag,'b','linewidth',2)
|
||||
xlabel('Frequency');
|
||||
ylabel('Magnitude Response');
|
||||
|
||||
% partial dtft for k =3
|
||||
[h2,w]=freqz(x(1:4),1,256);
|
||||
h2mag=abs(h2);
|
||||
hold
|
||||
plot(w,h2mag,'r','linewidth',2)
|
||||
|
||||
% partial dtft for k =10
|
||||
[h3,w]=freqz(x(1:11),1,256);
|
||||
h3mag=abs(h3);
|
||||
plot(w,h3mag,'m','linewidth',2)
|
||||
|
||||
% partial dtft for k =20
|
||||
[h4,w]=freqz(x(1:21),1,256);
|
||||
h4mag=abs(h4);
|
||||
plot(w,h4mag,'g','linewidth',2)
|
||||
|
||||
% partial dtft for k =40
|
||||
[h5,w]=freqz(x(1:41),1,256);
|
||||
h5mag=abs(h5);
|
||||
plot(w,h5mag,'k','linewidth',2)
|
||||
|
||||
% supremum coefficients of the error
|
||||
for k=1:200
|
||||
[hk,w]=freqz(x(1:k+1),1,256);
|
||||
ek=abs(h1-hk);
|
||||
coeff(k)=max(ek);
|
||||
end
|
||||
figure(3)
|
||||
k=1:1:200;
|
||||
stem(k,coeff)
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import numpy as np
|
||||
import scipy as sp
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
|
||||
DATA_POINTS = 200
|
||||
|
||||
|
||||
def u(n):
|
||||
return np.heaviside(n, 1)
|
||||
|
||||
|
||||
def main():
|
||||
a = 0.9
|
||||
n = np.arange(DATA_POINTS)
|
||||
x = n*(a**n)*u(n)
|
||||
|
||||
# Plot samples of x[n]
|
||||
plt.figure()
|
||||
plt.stem(n, x)
|
||||
plt.xlabel("n")
|
||||
plt.ylabel(r"$x[n]$")
|
||||
|
||||
# Plot the analytical DTFT
|
||||
numerator = [0, a]
|
||||
denominator = [1, -2*a, a**2]
|
||||
omega, h_0 = sp.signal.freqz(numerator, denominator, 256)
|
||||
|
||||
plt.figure()
|
||||
plt.plot(omega, np.abs(h_0), label="Analytical DTFT")
|
||||
plt.xlabel("Frequency")
|
||||
plt.ylabel("Magnitude Response")
|
||||
|
||||
# Plot the truncated DTFT for K = 3, 10, 20
|
||||
for K in (3, 10, 20):
|
||||
omega, h = sp.signal.freqz(x[:K], 1, 256)
|
||||
plt.plot(omega, np.abs(h), label=f"Truncated DTFT ($K = {K}$)")
|
||||
plt.legend()
|
||||
|
||||
# Calculate the maximum error between the truncated DTFT
|
||||
# and the analytical DTFT for values of K from 1 to 200
|
||||
k = np.arange(DATA_POINTS)
|
||||
coeffs = np.zeros_like(k, dtype=np.float32)
|
||||
|
||||
for i_k in k:
|
||||
omega, h_k = sp.signal.freqz(x[:i_k], 1, 256)
|
||||
err_k = np.abs(h_0 - h_k)
|
||||
coeffs[i_k] = np.max(err_k)
|
||||
|
||||
# Plot the maximum error previously calculated
|
||||
plt.figure()
|
||||
plt.stem(k, coeffs)
|
||||
plt.xlabel("k")
|
||||
plt.ylabel("Supremum coefficients of the error")
|
||||
plt.show()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,52 @@
|
||||
% value of a
|
||||
wc=0.4*pi;
|
||||
|
||||
% signal x(n)
|
||||
for n=-50:50
|
||||
if (n == 0)
|
||||
x(n+51) = wc/pi;
|
||||
else
|
||||
x(n+51) = sin(wc*n)/(pi*n);
|
||||
end
|
||||
end
|
||||
figure(1)
|
||||
n=-50:1:50;
|
||||
stem(n,x)
|
||||
xlabel('n')
|
||||
ylabel('x(n)')
|
||||
|
||||
% Ideal lpf
|
||||
n=0;
|
||||
for w=0:0.05*pi:pi;
|
||||
n=n+1;
|
||||
wfreq(n)=w;
|
||||
if (w <= wc)
|
||||
hlpf(n)=1;
|
||||
else
|
||||
hlpf(n)=0;
|
||||
end
|
||||
end
|
||||
figure(2)
|
||||
plot(wfreq,hlpf,'b','linewidth',2)
|
||||
xlabel('Frequency')
|
||||
ylabel('DTFT of Ideal Lowpass Filter')
|
||||
|
||||
% partial dtft for k =10
|
||||
k=10;
|
||||
[h2]=freqz(x(51-k:51+k),1,wfreq);
|
||||
h2mag=abs(h2);
|
||||
hold
|
||||
plot(wfreq,h2mag,'r','linewidth',2)
|
||||
|
||||
% partial dtft for k =20
|
||||
k=20;
|
||||
[h3]=freqz(x(51-k:51+k),1,wfreq);
|
||||
h3mag=abs(h3);
|
||||
plot(wfreq,h3mag,'m','linewidth',2)
|
||||
|
||||
% partial dtft for k =30
|
||||
k=30;
|
||||
[h4]=freqz(x(51-k:51+k),1,wfreq);
|
||||
h4mag=abs(h4);
|
||||
plot(wfreq,h4mag,'k','linewidth',2)
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
for n=0:99
|
||||
y1(n+1) = (1 - (0.95)^(n+1))/0.05;
|
||||
y2(n+1) = n + 1;
|
||||
end
|
||||
figure(1)
|
||||
n=0:1:99;
|
||||
stem(n,y1)
|
||||
xlabel('n')
|
||||
ylabel('y(n)')
|
||||
figure(2)
|
||||
n=0:1:99;
|
||||
stem(n,y2)
|
||||
xlabel('n')
|
||||
ylabel('y(n)')
|
||||
|
||||
Reference in New Issue
Block a user