# The following example uses a QPSK signal with rc pulse shaping, and time alignment at M = 15.
#
import matplotlib.pyplot as plt
import numpy as np
from sk_dsp_comm import digitalcom as dc
Ns = 8
Rs = 1.
fsin = Ns*Rs
Tsin = 1 / fsin
N = 200
ts = 1
x, b, data = dc.mpsk_bb(N+12, Ns, 4, 'rc')
x = x[12*Ns:]
xxI = x.real
M = 15
fsout = fsin * (M-1) / M
Tsout = 1. / fsout
xI = dc.farrow_resample(xxI, fsin, fsin)
tx = np.arange(0, len(xI)) / fsin
yI = dc.farrow_resample(xxI, fsin, fsout)
ty = np.arange(0, len(yI)) / fsout
plt.plot(tx - Tsin, xI)
plt.plot(tx[ts::Ns] - Tsin, xI[ts::Ns], 'r.')
plt.plot(ty[ts::Ns] - Tsout, yI[ts::Ns], 'g.')
plt.title(r'Impact of Asynchronous Sampling')
plt.ylabel(r'Real Signal Amplitude')
plt.xlabel(r'Symbol Rate Normalized Time')
plt.xlim([0, 20])
plt.grid()
plt.show()
#
# Consider a sum of sinusoids example:
#
plt.figure(figsize=(6,8))
fs0 = 100
t_fr0 = np.arange(1000)/fs0
x_fr0 = np.cos(2*np.pi*12/100*t_fr0) + 2*np.sin(2*np.pi*50/100*t_fr0)
fs1 = 8
n_fr1 = t_fr0[::fs0//fs1]
x_fr1 = x_fr0[::fs0//fs1]
fs2 = 18
n_fr2 = dc.farrow_resample(n_fr1, fs1, fs2, i_ord=3)
x_fr2 = dc.farrow_resample(x_fr1, fs1, fs2, i_ord=3)
n_fr3 = dc.farrow_resample(n_fr1, fs1, fs2, i_ord=1)
x_fr3 = dc.farrow_resample(x_fr1, fs1, fs2, i_ord=1)
plt.subplot(311)
plt.plot(t_fr0,x_fr0,'--',label='True')
plt.plot(n_fr1,x_fr1,'r.',label='Input @ $f_s = 8$ sps')
plt.legend()
plt.title(r'Starting Point')
plt.xlabel(r'Time (s)')
plt.ylabel(r'Amplitude')
plt.ylim(-3,0)
plt.xlim(3,4)
plt.grid()
plt.subplot(312)
plt.plot(t_fr0,x_fr0,'--',label='True')
plt.plot(n_fr2,x_fr2,'m.',label='Resample Cubic @ $f_s = 18$ sps')
plt.legend()
plt.title(r'Using farrow_resample with I_ord = 3')
plt.xlabel(r'Time (s)')
plt.ylabel(r'Amplitude')
plt.ylim(-3,0)
plt.xlim(3,4)
plt.grid()
plt.subplot(313)
plt.plot(t_fr0,x_fr0,'--',label='True')
plt.plot(n_fr3,x_fr3,'c.',label='Resample Linear @ $f_s = 18$ sps')
plt.legend()
plt.title(r'Using farrow_resample with I_ord = 1')
plt.xlabel(r'Time (s)')
plt.ylabel(r'Amplitude')
plt.ylim(-3,0)
plt.xlim(3,4)
plt.grid()
plt.tight_layout()
plt.show()
#
# * The last two plots show upsampling using a Farrow interpolator of I_ord = 3 and 1
# * The upsampling factor is 18/8 in both cases
# * Small linear interpolation errors are evident in the third ploit compared to the second plot
