import numpy as np
from scipy import signal, interpolate
import matplotlib.pyplot as plt
# Slow fading series
N_samples = 200 # number of samples
log_mean = -80 # lognormal process mean [dB]
log_sigma = 5 # lognormal process variance [dB]
L_corr = 10 # correlation length [m]
ds = 1 # sampling wavelength
interp_rate = int(np.round(L_corr / ds)) # intrepolation rate
L_corr = interp_rate * ds # retain correlation length
# Gaussian series
gauss = np.random.randn(N_samples, 1)
gauss = [i[0] for i in gauss]
# The following hints are done according to [1] (project312):
d_axis1 = np.array([i for i in range(1, N_samples+1)])*L_corr - L_corr
d_axis2 = (np.arange(1, N_samples + 1, 1 / interp_rate) - 1)*L_corr
R_interpolated = interpolate.interp1d(d_axis1, gauss, bounds_error=False) # interpolation function
R_interpolated = R_interpolated(d_axis2)*log_sigma + log_mean # lognormal series [dB]
# Free space path loss (FSPL)
d = np.array([i for i in range(1, R_interpolated.shape[0]+1)])
L = (4*np.pi*d) / ds
FSPL = 20*np.log10(L)
# Log-normal path loss
LNPL = 10*2.0*np.log10(d)
# Plotting
plt.subplots(figsize=(12, 11))
ax1 = plt.subplot(212)
ax1.plot(30 - (R_interpolated+FSPL+LNPL))
ax1.set_xlabel("Traversed distance (m)")
ax1.set_ylabel('Signal level (dB)')
ax1.set_title('Signal attenuation in case of \n Log-distance path loss model')
ax1.grid(which='both', axis='both')
ax2 = plt.subplot(222)
ax2.plot(R_interpolated)
ax2.set_title('Random slow fading series')
ax2.set_xlabel('Traversed distance (m)')
ax2.set_ylabel('Slow variations \n with autocorrelation built in (dBm)')
ax2.grid(which='both', axis='both')
ax3 = plt.subplot(221)
ax3.plot(FSPL+LNPL, label="FSPL+LNPL")
ax3.plot(LNPL, label="LNPL")
ax3.plot(FSPL, label="FSPL")
ax3.set_xlabel('Traversed distance (m)')
ax3.set_ylabel('Path loss level (dB)')
ax3.set_title('Path loss')
ax3.grid(which='both', axis='both')
ax3.legend(loc="upper right")
plt.subplots_adjust(hspace = 0.3)
plt.savefig("slowfading.png")
# Reference
# 1. Fontan, F. P., Mayo, A., Marote, D., Prieto‐Cerdeira, R., Mariño, P., Machado, F., & Riera, N. (2008). Review of generative models for the narrowband # land mobile satellite propagation channel. International Journal of Satellite Communications and Networking, 26(4), 291-316.