<jupyter_start><jupyter_text>Exercises 5.1 Draw the three basic signals $\delta[n]$, $u[n]$, $r[t]$ in the range $n \in [-5,5]$<jupyter_code># Imports
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

# Ask jupyter to display plots inline
%matplotlib inline

# OSX specific (for a nicer display on "retina" screen)
%config InlineBackend.figure_format = 'retina'

# make a plotting function
def sigplot(x,sig,ylabel=''):
    _, ax = plt.subplots(figsize=(10,5))
    ax.stem(x,sig)
    ax.set_xlim((-5,5)) # range n in [-5,5]
    ax.set_xlabel('n')
    ax.set_ylabel(ylabel)
    ax.grid()
    return ax
# First, make our x-vector
x = np.arange(-16,17)

# delta signal
delta = np.zeros(x.shape)
delta[x==0] = 1 # delta is zero everywhere except at point n=0

ax1 = sigplot(x,delta,ylabel='$\delta$[n]')
plt.show(ax1)
# square u[n] signal
u = np.zeros(x.shape)
u[x>=0] = 1 # u[n] is 1 for x>=0

ax2 = sigplot(x,u,'u[n]')
plt.show(ax2)
# ramp signal r[n]
r = np.zeros(x.shape)
r[x>0] = x[x>0] # r[n] is equal to n for x>=0

ax3 = sigplot(x,r,'r[n]')
ax3.set_ylim((0,5))
plt.show(ax3)<jupyter_output><empty_output><jupyter_text>5.2 Draw the signal $y[n] = u[n] - u[n-3]$<jupyter_code># We already have the signal u[n], let's make the delayed signal u[n-3]
u3 = np.zeros(x.shape)
u3[x-3>=0] = 1
y = u-u3

ax4 = sigplot(x,y,'y[n]')
plt.show(ax4)<jupyter_output><empty_output><jupyter_text>5.3 Draw the signal $y[n] = 3(u[n] - u[n-3])$<jupyter_code># This signal is basically a scaling of our previous y[n] by a factor of 3
y2 = 3*y

# For this plot, we will plot both signals y and y2 at the same time
plt.stem(x,y2,linefmt='r-',markerfmt='r*',label='y\'=3y')
plt.stem(x,y,linefmt='b-',markerfmt='b*',label='y')
plt.xlim((-5,5)) # range n in [-5,5]
plt.xlabel('n')
plt.grid()
#plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>5.4 Draw the signal $y[n] = \frac{2}{3}r[n-3])$<jupyter_code># We already have the signal r[n], let's make the scaled and delayed signal
r2 = np.concatenate((np.zeros((1,3)),r[True,:-3]),axis=1)
y3 = 2/3*r2

ax5 = sigplot(x,y3[0,:],'y3[n]')
ax5.set_ylim((0,5))
ax5.set_xlim((0,10))
plt.show(ax5)<jupyter_output><empty_output><jupyter_text>5.5 Draw the signal $y[n] = − \delta[n]+ \delta[n−1]− \frac{1}{2} u[n−3]+ \frac{1}{2} u[n−6]$<jupyter_code># We already have all of the basic signals, let's make the others
delta1 = np.concatenate((np.zeros((1,1)),delta[True,:-1]),axis=1)
u6 = np.concatenate((np.zeros((1,3)),u3[True,:-3]),axis=1)
y = -delta + delta1 - .5*u3 + 0.5*u6

ax5 = sigplot(x,y[0,:],'y[n]')
ax5.set_ylim((-2,2))
ax5.set_xlim((-1,6))
plt.show(ax5)<jupyter_output><empty_output><jupyter_text>5.6 Even vs odd signalsFor the final exercise, we will write two functions that check if the input signal is even or odd. You can use the solutions above to draw the signals, as most of them have been already provided.<jupyter_code># Function iseven():
def iseven(sig):
    sig_rev = sig[::-1]
    return np.all(sig == sig_rev)

# Function isodd()
def isodd(sig):
    sig_rev = sig[::-1]
    return np.all(sig == -sig_rev)

print("delta[n]: ", 'even' if iseven(delta) else ('odd' if isodd(delta) else 'neither'))
print("u[n]: ", 'even' if iseven(u) else ('odd' if isodd(u) else 'neither'))
print("r[n]: ", 'even' if iseven(r) else ('odd' if isodd(r) else 'neither'))

x5 = np.arange(-5,6)
u3p = np.zeros(x5.shape)
u3p[x5+3>=0] = 1
u3n = np.zeros(x5.shape)
u3n[x5-3>=0] = 1
print("u[n+3] − u[n−3]: ", 'even' if iseven(u3p-u3n) else ('odd' if isodd(u3p-u3n) else 'neither'))

sigsin=np.sin(np.pi*x5/5)
print("sin(pi n / 5): ", 'even' if iseven(u3p-u3n) else ('odd' if isodd(u3p-u3n) else 'neither'))

#plt.plot(x5,sigsin)<jupyter_output>delta[n]:  even
u[n]:  neither
r[n]:  neither
u[n+3] − u[n−3]:  neither
sin(pi n / 5):  neither
{'fraction_non_alphanumeric': 0.2018421052631579, 'fraction_numerical': 0.041842105263157896, 'mean_word_length': 4.9576802507836994, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '517652', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1717, 'n_words': 401, 'path': 'lessons/Jupyter Notebooks/Solutions/Signal Processing Exercises Solutions.ipynb', 'repo_name': 'bordeaux-neurocampus/CNCC-2021-beginner', 'size': '3800'}