Image('images/Pulse_Signal_Modeling.png',width='100%')

"""
Explanation: Signals
Using the signal primitives ss.rect(t,tau) and ss.tri(t,tau) synthesize the following signal and also plot the corresponding frequency spectra.
End of explanation
"""


Image('images/Shifting_Functions.png',width='100%')

"""
Explanation: In working the examples below it worth recalling how re-mapping the argument of a function can be used to shift a function right or left
End of explanation
"""


t = arange(-4,4,.01)
x5 = ss.tri(t-1,1) - ss.tri(t+1,1)
plot(t,x5)
ylabel(r'Amplitude')
xlabel(r'Times (s)')
title(r'The Finte Energy Signal $x_5(t)$')
grid();

"""
Explanation: Example: $x_5(t)$
End of explanation
"""


F, X5 = FT_approx(x5,t,4096)

plot(F, abs(X5))
xlim([-5,5])
title(r'Spectrum Magnitude $|X_5(f)|$')
ylabel(r'Magnitude')
xlabel(r'Frequency (Hz)')
grid();

"""
Explanation: The spectrum (Fourier transform)
$$
    X_5(f) = \int_{-\infty}^\infty x(t) e^{-j2\pi ft}\, dt 
$$
is numerically computed using FT_approx().
End of explanation
"""


# Complete this example by plottont $p(t)$ for $T = 1$ and plotting the spectrum.
# Overlay the spectrum of $\Pi(t/T)$ for $T=1$

# Code here

"""
Explanation: Example: The Bi-Phase Pulse
This pulse is used in Manchester encoding of a bit stream.
$$
    p(t) = \Pi\left(\frac{t-T/4}{T/2}\right) - \Pi\left(\frac{t-3T/4}{T/2}\right)
$$
When each bit in a bit stream is mapped to the time axis using this pulse shape

There is a transition for every bit making sychronization easier
The spectrum does not contain and zero frequency components
Unfortunately the spectrum is wide than the simple rectangle $\pm 1 \times \Pi\big(t/T\big)$ pulse shape
End of explanation
"""


Ns = 8 # samples per bit period
p_RC = ss.rc_imp(Ns,0.35) # 0.35 is a popular excess bandwidth factor
p_SRC = ss.sqrt_rc_imp(Ns,0.35)
n = arange(len(p_RC))
plot(n/Ns,p_RC)
plot(n/Ns,p_SRC)
legend((r'Raised Cosine',r'Square-root RC'),loc='best')
title(r'RC and SRC Pulse Shapes (functions are discrete)')
ylabel(r'Amplitude')
xlabel(r'Bit Period ($n/N_s$)')
grid()

"""
Explanation: Complete a Few Other Examples from the $3\times 2$ Plot Figure Above (time permitting)
Raised Cosine and Square-root Raised Cosine
The module digitalcom utilizes this pulse shape in many of its functions. More later in Part3
End of explanation
"""


import sk_dsp_comm.iir_design_helper as iir_d

iir_d.freqz_resp_list([p_RC,p_SRC],[1,1],'dB',8.0)
title(r'Spectral Comparison Between RC and SRC Pulse Shapes')
ylabel(r'Spectral Density (dB)')
xlabel(r'Bit Rate Normalized Frequency $f/R_b$')
ylim([-80,20])
legend((r'RC',r'SRC'),loc='best')
grid();

# Repeat the above pulse shape modeling for alpha = 0.5 and 0.25
# Overlay plots of just the SRC spectrum for alpha = 0.25, 0.35, 0.5


"""
Explanation: Plot the spectrum magnitude (Fourier transform) by repurposing the function freqz_resp_list(b,a=np.array([1]),mode = 'dB',fs=1.0,Npts = 1024) found in the module iir_design_helper
End of explanation
"""


# Some examples
(ss.prin_alias(25,100),ss.prin_alias(300,200),ss.prin_alias(18000,44100))

f = arange(64,72,.01)
f_prin = ss.prin_alias(f,8) # Sampling frequency is 8 MHz
plot(f,f_prin)
title(r'Under Sampling a Signal Over 64-72 MHz with $f_s = 8$ MHz')
ylabel(r'Output Frequency (MHz)')
xlabel(r'Input Frequency (MHz)')
grid();

"""
Explanation: Aliasing
In sigsys the function f_prin = prin_alias(f,fs) finds the principal alias of a sinusoidal signal relative to the sampling rate $f_s$:
End of explanation
"""


