AM synthesis code
This commit is contained in:
parent
e97b5244ef
commit
a00dde9ad4
@ -2,6 +2,17 @@ import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import scipy as sp
|
||||
|
||||
def add_noise_snr_db(s, SNR):
|
||||
var_s = np.cov(s)
|
||||
var_noise = var_s/(10**(SNR/10))
|
||||
noise = var_noise**0.5 * np.random.randn(len(s))
|
||||
return s + noise
|
||||
|
||||
def add_noise_db(s, noise_db):
|
||||
var_noise = 10**(noise_db/10)
|
||||
noise = var_noise**0.5 * np.random.randn(len(s))
|
||||
return s + noise
|
||||
|
||||
|
||||
def lowpass(data, f_cutoff, f_s):
|
||||
nyq = 0.5*f_s
|
||||
@ -12,13 +23,12 @@ def lowpass(data, f_cutoff, f_s):
|
||||
|
||||
def dsb_am(m, f_c, t):
|
||||
omega_c = 2*np.pi*f_c
|
||||
c = np.cos(omega_c*t)
|
||||
return 0.5*m*c + c
|
||||
return (1 + m)*np.cos(omega_c*t)
|
||||
|
||||
def dsb_sc(m, f_c, t):
|
||||
omega_c = 2*np.pi*f_c
|
||||
c = np.cos(omega_c*t)
|
||||
return 0.5*m*c
|
||||
return m*c
|
||||
|
||||
def product_demod(s, f_baseband, f_c, f_s, t):
|
||||
omega_c = 2*np.pi*f_c
|
||||
@ -37,9 +47,18 @@ def main():
|
||||
t = np.arange(0,1,T_s)
|
||||
f = np.linspace(0,f_s,len(t))
|
||||
|
||||
m = np.sin(omega_m*t) + np.sin(omega_m/2*t)
|
||||
m = 0.5*(np.sin(omega_m*t) + np.sin(omega_m/2*t))
|
||||
s_am = dsb_am(m, f_c, t)
|
||||
s_sc = dsb_sc(m, f_c, t)
|
||||
|
||||
plt.subplot(311)
|
||||
plt.plot(t, m, label="Message Signal")
|
||||
plt.subplot(312)
|
||||
plt.plot(t, s_am, label="DSB-AM Modulated Signal")
|
||||
plt.subplot(313)
|
||||
plt.plot(t, s_sc, label="DSB-SC Modulated Signal")
|
||||
plt.show()
|
||||
|
||||
m_am_demod = product_demod(s_am, f_m, f_c, f_s, t)
|
||||
m_sc_demod = product_demod(s_sc, f_m, f_c, f_s, t)
|
||||
|
||||
@ -49,5 +68,13 @@ def main():
|
||||
plt.legend(loc="upper right")
|
||||
plt.show()
|
||||
|
||||
m_am_noisy_demod = product_demod(add_noise_snr_db(s_am, 3), f_m, f_c, f_s, t)
|
||||
m_sc_noisy_demod = product_demod(add_noise_snr_db(s_sc, 3), f_m, f_c, f_s, t)
|
||||
|
||||
plt.plot(t, m)
|
||||
plt.plot(t, m_am_noisy_demod)
|
||||
plt.plot(t, m_sc_noisy_demod)
|
||||
plt.show()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
Loading…
Reference in New Issue
Block a user