This commit is contained in:
Aidan Sharpe
2025-02-14 22:27:10 -05:00
parent d9cec13c2e
commit 82b435b6a2
39 changed files with 867 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
% Lab 2
% Question 2 on periodicity
% Define the samples axis (0 to 50, including 50)
n=0:1:50;
% Define four cosines at different frequencies
x1=cos(2*pi*n/7);
x2=cos(2*pi*n/21);
x3=cos(2*pi*n/17.5);
x4=cos(2*n/7);
% Plot each of the cosines in a separate subplot
subplot(411)
stem(n,x1,'linewidth',2)
subplot(412)
stem(n,x2,'linewidth',2)
subplot(413)
stem(n,x3,'linewidth',2)
subplot(414)
stem(n,x4,'linewidth',2)
xlabel('n');

View File

@@ -0,0 +1,44 @@
import numpy as np
import matplotlib.pyplot as plt
def main():
# Define the samples axis (0 to 51, excluding 51)
n = np.arange(51)
# Define the four frequencies used
f_1 = 1/7
f_2 = 1/21
f_3 = 1/17.5
f_4 = 1/(7*np.pi)
# Convert frequencies to angular frequency
omega_1 = 2*np.pi*f_1
omega_2 = 2*np.pi*f_2
omega_3 = 2*np.pi*f_3
omega_4 = 2*np.pi*f_4
# Sample a cosine of each of the
x_1 = np.cos(omega_1*n)
x_2 = np.cos(omega_2*n)
x_3 = np.cos(omega_3*n)
x_4 = np.cos(omega_4*n)
# Plot each of the cosines in a separate subplot
plt.subplot(411)
plt.stem(n, x_1)
plt.subplot(412)
plt.stem(n, x_2)
plt.subplot(413)
plt.stem(n, x_3)
plt.subplot(414)
plt.stem(n, x_4)
plt.xlabel("n")
plt.show()
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,56 @@
clear x
n=0:1:12;
x(n+1)=0;
x(6)=1;
x(9)=-2;
x(12)=6;
figure(1)
stem(n,x,'linewidth',2)
xlabel('n');
ylabel('x(n)');
clear x
n=0:1:15;
x(n+1)=0;
for m=0:15
if (m < 3)
x(m+1) = 0;
elseif (m >=3 && m < 8)
x(m+1) = 1;
elseif (m >= 8 && m < 12)
x(m+1) = -1;
end
end
figure(2)
stem(n,x,'linewidth',2)
xlabel('n');
ylabel('x(n)');
clear x
n=0:1:20;
x(n+1)=n;
figure(3)
stem(n,x,'linewidth',2)
xlabel('n');
ylabel('x(n)');
clear x
n=0:1:10;
x(1)=0;
for m=1:10
x(m+1)=m*m;
end
figure(4)
stem(n,x,'linewidth',2)
xlabel('n');
ylabel('x(n)');
clear x
n=0:1:30;
for m=0:30
x(m+1)=3*(m-3)*(m-3)*exp(-0.3*m)*sin(2*m/3);
end
figure(5)
stem(n,x,'linewidth',2)
xlabel('n');
ylabel('x(n)');

View File

@@ -0,0 +1,56 @@
import numpy as np
import matplotlib.pyplot as plt
def delta(n):
return np.where(n==0, 1, 0)
def u(n):
return np.heaviside(n, 1)
def main():
# Part a
n = np.arange(13)
x = delta(n-5) - 2*delta(n-8) + 6*delta(n-11)
plt.stem(n, x)
plt.xlabel("$n$")
plt.ylabel(r"$x[n] = \delta(n-5) - 2\delta(n-8) + 6\delta(n-11)$")
plt.show()
# Part b
n = np.arange(16)
x = u(n-3) - 2*u(n-8) + u(n-12)
plt.stem(n, x)
plt.xlabel("$n$")
plt.ylabel("$x[n] = u(n-3) - 2u(n-8) + u(n-12)$")
plt.show()
# Part c
n = np.arange(21)
x = n*u(n)
plt.stem(n, x)
plt.xlabel("$n$")
plt.ylabel("$x[n] = n u(n)$")
plt.show()
# Part d
n = np.arange(11)
x = n**2 * u(n)
plt.stem(n, x)
plt.xlabel(r"$n$")
plt.ylabel(r"$x[n] = n^2 u(n)$")
plt.show()
# Part e
n = np.arange(31)
x = 3 * (n-3)**2 * np.exp(-0.3*n) * np.sin(2*n/3) * u(n)
plt.stem(n, x)
plt.xlabel("$n$")
plt.ylabel(r"$x[n] = 3(n-3)^2 e^{-0.3n} \sin(2n/3) u(n)$")
plt.show()
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,20 @@
clear x
a=pi/4;
n=0:1:50;
x(n+1)=5*cos(a*a*n);
figure(1)
stem(n,x,'linewidth',2)
xlabel('n');
ylabel('x(n)');
clear x
a=5;
c=abs(0.5+0.5*j);
n=0:1:50;
for m=0:50
x(m+1)=a*c^m;
end
figure(2)
stem(n,x,'linewidth',2)
xlabel('n');
ylabel('x(n)');

View File

@@ -0,0 +1,32 @@
import numpy as np
import matplotlib.pyplot as plt
def u(n):
return np.heaviside(n, 1)
def main():
# Part a
a = np.pi/4
n = np.arange(51)
x = 5*np.cos(a**2 * n)
plt.stem(n, x)
plt.xlabel("$n$")
plt.ylabel(r"$x[n] = 5\cos(a^2 n)$")
plt.show()
# Part b
A = 5
b = (1 + 1j)/2
x = A*np.abs(b)**n * u(n)
plt.stem(n, x)
plt.xlabel("$n$")
plt.ylabel("$x[n] = A|b|^n u(n)$")
plt.show()
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,10 @@
energy(1)=1;
for k=2:500
energy(k)=energy(k-1) + (1/(k*k));
end
a=(pi*pi/6)*ones(length(energy),1);
plot(energy,'linewidth',2)
hold
plot(a,'r','linewidth',2)
xlabel('Time index');
ylabel('Energy');

View File

@@ -0,0 +1,26 @@
import numpy as np
import matplotlib.pyplot as plt
def u(n):
return np.heaviside(n, 1)
def main():
n = np.arange(101)
x = u(n-1)/n
# Avoid divide by zero
x[0] = 0
# Calculate the energy as a cumulative sum from n=0 to each sample
energy = np.cumsum(np.abs(x)**2)
plt.stem(n, energy)
plt.hlines(np.pi**2 / 6, n[0], n[-1], color='r')
plt.xlabel("Time index")
plt.ylabel("Energy")
plt.show()
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,30 @@
n=0:1:6;
x=[1 -5 4 -8 6 -3 -1];
figure(1)
stem(n,x,'linewidth',2)
xlabel('Time index');
ylabel('x(n)');
for m=0:6
u=mod(m+3,7);
y1(m+1)=x(u+1);
u=mod(m-4,7);
y2(m+1)=x(u+1);
u=mod(-m+2,7);
y3(m+1)=x(u+1);
end
figure(2)
stem(n,y1,'linewidth',2)
xlabel('Time index');
ylabel('x(n+3 mod 7)');
figure(3)
stem(n,y2,'linewidth',2)
xlabel('Time index');
ylabel('x(n-4 mod 7)');
figure(4)
stem(n,y3,'linewidth',2)
xlabel('Time index');
ylabel('x(-n+2 mod 7)');

View File

@@ -0,0 +1,35 @@
import numpy as np
import matplotlib.pyplot as plt
def main():
n = np.arange(7) # n ranges from 0 to 7 (not including 7)
x = np.array([1, -5, 4, -8, 6, -3, -1])
x_1 = x[(n+3) % 7] # x[ <n+3>_7 ]
x_2 = x[(n-4) % 7] # x[ <n-4>_7 ]
x_3 = x[(-n+2) % 7] # x[ <-n+2>_7 ]
plt.subplot(411)
plt.stem(n,x)
plt.ylabel("$x[n]$")
plt.subplot(412)
plt.stem(n,x_1)
plt.ylabel(r"$x[\langle n+3 \rangle_7]$")
plt.subplot(413)
plt.stem(n, x_2)
plt.ylabel(r"$x[\langle n-4\rangle_7]$")
plt.subplot(414)
plt.stem(n, x_3)
plt.ylabel(r"$x[\langle -n+2\rangle_7]$")
plt.xlabel("$n$")
plt.show()
if __name__ == "__main__":
main()