Pretty much all of Fall 2024

This commit is contained in:
Aidan Sharpe
2024-11-10 14:46:30 -05:00
parent 87f9c55360
commit faa05b88f9
116 changed files with 8295 additions and 1683 deletions

View File

@ -0,0 +1,51 @@
import numpy as np
f_1 = 5
f_2 = 5E3
omega_1 = 2*np.pi*f_1
omega_2 = 2*np.pi*f_2
A_c = 10
a = 1
def m(t):
return 0.2*np.cos(omega_1*t) + 0.8*np.cos(omega_2*t)
def dm_dt(t):
return -0.2*omega_1*np.sin(omega_1*t) - 0.8*omega_2*np.sin(omega_2*t)
def ddm_dtt(t):
return -0.2*omega_1**2*np.cos(omega_1*t) - 0.8*omega_2**2*np.cos(omega_2*t)
def g(t):
return A_c + A_c*m(t)
# first derivative of g(t)
def dg_dt(t):
return A_c*dm_dt(t)
# second derivative of g(t)
def ddg_dtt(t):
return A_c**2*ddm_dtt(t)
# use Newton's method to find the maximum of the function
def newton_method(t):
for i in range(10):
t = t-dg_dt(t)/ddg_dtt(t)
print("Newton method:", t, np.min(m(t)))
return np.min(g(t))
# Sample g(t) at the maxima and minima of the carrier signal
def sample_method():
f_s = 4*max(f_1, f_2)
T_s = 1/f_s
T_0 = 1/min(f_1, f_2)
t = np.arange(0,T_0,T_s)
t = t[np.argmin(m(t))]
print("Sampling method:", t, m(t))
return t
if __name__ == '__main__':
newton_method(0.1)

View File

@ -0,0 +1,37 @@
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
A_c = 1
f_1 = 500
T_1 = 1/f_1
omega_1 = 2*np.pi*f_1
f_c = 20000
T_c = 1/f_c
omega_c = 2*np.pi*f_c
def m(t):
return 2*np.cos(2*omega_1*t) + 3*np.cos(3*omega_1*t)
def s(t):
return A_c*m(t)*np.cos(omega_c*t)
f_s = 40*f_c
T_s = 1/f_s
t = np.arange(0,T_1,T_s)
plt.plot(t,s(t))
plt.plot(t,m(t))
plt.show()
S = sp.fft.fft(s(t))[35:45]
f = t*f_s/T_1
f = f[35:45]
for i in range(len(f)):
print(f[i], S[i].real)
plt.stem(f,S.real/1600)
plt.show()

View File

@ -0,0 +1,56 @@
# ECOMMS Homework 2 - Aidan Sharpe
## Problem 1
```python
import numpy as np
f_c = 1250
f_m = 125
A_c = 10
a = 1
def g(t):
return A_c * (1 + a*(0.2*np.cos(2*np.pi*f_m*t) + 0.5*np.sin(2*np.pi*f_c*t)))
# First derivative of g(t)
def dg_dt(t):
return -0.2*2*f_m*np.pi*A_c*a*np.sin(2*np.pi*f_m*t) \
+ 2*np.pi*0.5*f_c*A_c*np.cos(2*np.pi*f_c*t)
# Second derivative of g(t)
def ddg_dtt(t):
return -0.2*(2*np.pi*f_m)**2*A_c*a*np.cos(2*np.pi*f_m*t) \
- 0.5*(2*np.pi*f_c)**2*A_c*a*np.sin(2*np.pi*f_c*t)
# Use Newton's method to find the maximum of the function
def newton_method(t):
for i in range(10):
t = t - dg_dt(t)/ddg_dtt(t)
print("Newton method:", t, np.max(g(t)))
return np.max(g(t))
def dc_dt(t):
return A_c*a*np.pi*f_c*np.cos(2*np.pi*f_c*t)
# Sample g(t) at the maxima and minima of the carrier signal
def sample_method():
samples = f_c / f_m
n = np.arange(samples)
t = (2*n + 1) / (4*f_c)
t = t[np.argmax(g(t))]
print("Sampling method:", t, g(t))
return t
if __name__ == '__main__':
t_max = sample_method()
A_max = newton_method(t_max)
a_coeff = (A_max - A_c) / A_c
print("Value of a where positive modulation is 90%:", 0.9/a_coeff)
```
### Sampling Method
$t_max = 0.0002$, $g_max = 16.9754$
### Newton Method
$t_max = 0.000199206$, $g_max = 16.9755$
Positive modulation is 90% when $a=1.2902$

View File

@ -0,0 +1,42 @@
import numpy as np
f_c = 1250
f_m = 125
A_c = 10
a = 1
def g(t):
return A_c * (1 + a*(0.2*np.cos(2*np.pi*f_m*t) + 0.5*np.sin(2*np.pi*f_c*t)))
# first derivative of g(t)
def dg_dt(t):
return -0.2*2*f_m*np.pi*A_c*a*np.sin(2*np.pi*f_m*t) + 2*np.pi*0.5*f_c*A_c*np.cos(2*np.pi*f_c*t)
# second derivative of g(t)
def ddg_dtt(t):
return -0.2*(2*np.pi*f_m)**2*A_c*a*np.cos(2*np.pi*f_m*t) - 0.5*(2*np.pi*f_c)**2*A_c*a*np.sin(2*np.pi*f_c*t)
# use Newton's method to find the maximum of the function
def newton_method(t):
for i in range(10):
t = t - dg_dt(t)/ddg_dtt(t)
print("Newton method:", t, np.max(g(t)))
return np.max(g(t))
def dc_dt(t):
return A_c*a*np.pi*f_c*np.cos(2*np.pi*f_c*t)
# Sample g(t) at the maxima and minima of the carrier signal
def sample_method():
samples = f_c / f_m
n = np.arange(samples)
t = (2*n + 1) / (4*f_c)
t = t[np.argmax(g(t))]
print("Sampling method:", t, g(t))
return t
if __name__ == '__main__':
t_max = sample_method()
A_max = newton_method(t_max)
a_coeff = (A_max - A_c) / A_c
print("Value of a where positive modulation is 90%:", 0.9/a_coeff)

View File

@ -0,0 +1,33 @@
import numpy as np
f_m = 0.2
f_c = 2*f_m
A_c = 1
a = 1
def g(t):
return A_c*np.cos(2*np.pi*f_m*t) + A_c*np.cos(2*np.pi*f_c*t)
# first derivative of g(t)
def dg_dt(t):
return -(2*np.pi*f_m)*A_c*np.sin(2*np.pi*f_m*t) + -(2*np.pi*f_c)*A_c*np.sin(2*np.pi*f_c*t)
# second derivative of g(t)
def ddg_dtt(t):
return -(2*np.pi*f_m)**2*A_c*np.cos(2*np.pi*f_m*t) + -(2*np.pi*f_c)**2*A_c*np.cos(2*np.pi*f_c*t)
# use Newton's method to find the maximum of the function
def newton_method(t):
for i in range(3):
t = t - dg_dt(t)/ddg_dtt(t)
print(f"Iteration {i+1}: {t}\t{g(t)}")
def dc_dt(t):
return A_c*a*np.pi*f_c*np.cos(2*np.pi*f_c*t)
if __name__ == '__main__':
T_c = 1/f_c
t_min = T_c/2
newton_method(t_min)
newton_method(0)

View File

@ -0,0 +1,24 @@
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
f_c = 1
f_m = f_c/4
omega_m = 2*np.pi*f_m
omega_c = 2*np.pi*f_c
T_c = f_c
t = np.arange(0,10,T_c/10)
m = np.cos(omega_m * t)
plt.plot(t, m)
plt.show()
D_p = np.pi
D_f = np.pi
S_p = np.cos(omega_c + D_p*m)
plt.plot(t,S_p)
plt.show()

View File

@ -0,0 +1,26 @@
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
# Modulation index
beta = 2.0
# Number of impulses is 2*(beta+1) + 1.
# For beta=2, the number of impulses is 3 either side of the center frequency
# plus 1 for the center frequency, for a total of 7 impulses.
# Message frequency
f_m = 15E+3
# Transmission bandwidth
B_T = 2*(beta+1)*f_m
n = np.arange(0,10,1)
bessel_values = sp.special.jv(n,beta)
bessel_power = np.cumsum(bessel_values)
plt.stem(n, bessel_values)
plt.plot(n, bessel_power)
plt.hlines(0.98*bessel_power[-1], xmin=n[0], xmax=n[-1])
plt.show()
#

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

View File

@ -1,5 +1,5 @@
import numpy as np
import sounddevice as sd
#import sounddevice as sd
import matplotlib.pyplot as plt
def normalize_signal(signal):
@ -10,12 +10,37 @@ def normalize_signal(signal):
normalized_signal -= 1
return normalized_signal
snr = 10
f = 466.16
f_s = 16000
T_0 = 1/f
t = np.arange(0,0.01,1/f_s)
t = np.arange(0,T_0,1/f_s)
s = 0.5 * np.sin(2*np.pi*f*t)
sd.play(normalize_signal(s), samplerate=f_s, blocking=True)
# Convert signal covariance
var_s = np.cov(s)
# Calculate required noise variance
var_snr_10 = var_s/(10**(10/10))
var_snr_20 = var_s/(10**(20/10))
var_snr_30 = var_s/(10**(30/10))
# Genearate noise
noise_snr_10 = (var_snr_10**0.5) * np.random.randn(len(s))
noise_snr_20 = (var_snr_20**0.5) * np.random.randn(len(s))
noise_snr_30 = (var_snr_30**0.5) * np.random.randn(len(s))
# Add signal and noise
m_snr_10 = s+noise_snr_10
m_snr_20 = s+noise_snr_20
m_snr_30 = s+noise_snr_30
plt.plot(t,s, label="Pure A$\sharp$ Tone")
plt.plot(t,m_snr_10, label="Corrupted A$\sharp$ Tone (SNR=10dB)")
plt.plot(t,m_snr_20, label="Corrupted A$\sharp$ Tone (SNR=20dB)")
plt.plot(t,m_snr_30, label="Corrupted A$\sharp$ Tone (SNR=30dB)")
plt.legend()
plt.title("Pure and Corrupted A$\sharp$ Tones")
plt.xlabel("Time (s)")
plt.show()
#sd.play(normalize_signal(s), samplerate=f_s, blocking=True)

View File

@ -0,0 +1,33 @@
import numpy as np
import matplotlib.pyplot as plt
import scipy as sp
f_s = 8E3
T_s = 1/f_s
t = np.arange(-5,5,T_s)
f = np.linspace(0,f_s,len(t))
omega = 2*np.pi*f
def u(t):
return np.heaviside(t, 1)
w = u(t) - u(t-0.6) + u(t-0.7) - u(t-1)
W_c = 1j*(np.exp(-1j*omega*0.6) + np.exp(-1j*omega) - np.exp(-1j*omega*0.7) - 1)/omega
W_d = sp.fft.fft(w)
print(W_c[:10])
print(W_d[:10])
plt.plot(t,w)
plt.show()
plt.subplot(211)
plt.plot(W_c)
plt.subplot(212)
plt.plot(W_d)
plt.show()

View File

@ -0,0 +1,54 @@
import numpy as np
import matplotlib.pyplot as plt
import scipy as sp
f_m = 5E3
f_c = 25E3
f_s = 50*f_c
T_m = 1/f_m
T_c = 1/f_c
T_s = 1/f_s
A_c = 10
A_m = 1
t = np.arange(0,2*T_m,T_s)
f = t*f_s/(2*T_m)
# ===== AM =====
s = A_c * (1 + A_m*np.cos(2*np.pi*f_m*t)) * np.cos(2*np.pi*f_c*t)
var_s = np.cov(s)
SNR = 10
var_snr = var_s/(10**(SNR/10))
noise_snr = (var_snr**0.5) * np.random.randn(len(s))
m = s+noise_snr
S = sp.fft.fft(s)
M = sp.fft.fft(m)
plt.subplot(211)
plt.stem(f, S)
plt.subplot(212)
plt.stem(f, M)
plt.show()
# ===== FM =====
beta_f = 10
s = A_c*np.cos(2*np.pi*f_c*t + beta_f*A_m*np.sin(2*np.pi*f_m*t))
var_snr = var_s/(10**(SNR/10))
noise_snr = (var_snr**0.5) * np.random.randn(len(s))
m = s+noise_snr
S = sp.fft.fft(s)
M = sp.fft.fft(m)
plt.subplot(211)
plt.stem(f, S)
plt.subplot(212)
plt.stem(f, M)
plt.show()

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.