import numpy as np
import matplotlib.pyplot as plt

def axesCross(ax):
    ax.spines['left'].set_position('zero')
    ax.spines['bottom'].set_position('zero')
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')



fig = plt.figure(figsize=(9,9), dpi=120)

(ax1, ax2), (ax3, ax4), (ax5, ax6) = fig.subplots(3, 2)

f = lambda x: .6*np.sin(.2*(x+1))+2+.03*x
h = lambda x: np.abs(.95**x)
mod = lambda x: np.where(x%2==0, 1, 0)
x1 = np.arange(-30, 30, 1)
x2 = np.arange(-10, 11, 1)

ax1.set_xlim(-20, 20)
ax1.set_ylim(0, 4)
#ax1.xaxis.set_ticks()
markers11, stemlines11, baseline11 = ax1.stem(x1, f(x1*2))
markers12, stemlines12, baseline12 = ax1.stem(x2, f(x2*2))
plt.setp(markers11, 'markersize', 3, 'color', 'r', 'alpha', .3)
plt.setp(stemlines11, 'linewidth', 1, 'color', 'r', 'alpha', .3)
plt.setp(markers12, 'color', 'r', 'markersize', 3)
plt.setp(stemlines12, 'color', 'r', 'linewidth', 1)
ax1.annotate(r"$x[n]$", xy=(0, 0), xytext=(-18, 3.5), color='r')

ax2.set_xlim(-20, 20)
ax2.set_ylim(0, 4)
#ax1.xaxis.set_ticks()
markers23, stemlines23, baseline23 = ax2.stem(x2, h(x2*2), markerfmt='x')
plt.setp(markers23, 'color', 'C2', 'markersize', 3)
plt.setp(stemlines23, 'color', 'C2', 'linewidth', '1', 'alpha', .5)
ax2.annotate(r"$h[n]$", xy=(0, 0), xytext=(-18, 3.5), color='C2')

ax3.set_xlim(-20, 20)
ax3.set_ylim(0, 4)
#ax1.xaxis.set_ticks()
markers33, stemlines33, baseline33 = ax3.stem(x2, f(x2*2))
plt.setp(markers33, 'markersize', 3, 'color', 'r')
plt.setp(stemlines33, 'linewidth', 1, 'color', 'r')
markers31, stemlines31, baseline31 = ax3.stem(x1, f(x1*2))
plt.setp(markers31, 'color', 'r', 'markersize', 3, 'alpha', .3)
plt.setp(stemlines31, 'color', 'r', 'linewidth', 1, 'alpha', .3)
markers32, stemlines32, baseline32 = ax3.stem(x2, h(x2*2), markerfmt='x')
plt.setp(markers32, 'color', 'C2', 'markersize', 3)
plt.setp(stemlines32, 'color', 'C2', 'linewidth', 1)
ax3.annotate(r"$x_a[n] = $", xy=(0, 0), xytext=(-18, 3.5))
ax3.annotate(r"$x[n]$", xy=(0, 0), xytext=(-11, 3.5), color='r')
ax3.annotate(r"$*$", xy=(0, 0), xytext=(-6, 3.5))
ax3.annotate(r"$h[n]$", xy=(0, 0), xytext=(-4, 3.5), color='C2')

ax4.set_xlim(-20, 20)
ax4.set_ylim(0, 4)
#ax1.xaxis.set_ticks()
markers43, stemlines43, baseline43 = ax4.stem(x1, f(x1)*mod(x1))
plt.setp(markers43, 'color', 'r', 'markersize', 3)
plt.setp(stemlines43, 'color', 'r', 'linewidth', 1)
markers41, stemlines41, baseline41 = ax4.stem(x1, h(x1)*mod(x1), markerfmt='x')
plt.setp(markers41, 'color', 'C2', 'markersize', 3)
plt.setp(stemlines41, 'color', 'C2', 'linewidth', 1)
#markers42, stemlines42, baseline42 = ax4.stem(x2, f(x2*4))
#plt.setp(markers42, 'color', 'r', 'markersize', 3)
#plt.setp(stemlines42, 'color', 'r', 'linewidth', 1)
ax4.annotate(r"$y[n] = expand\ x_a[n]\ with\ L$", xy=(0, 0), xytext=(-18, 3.5))

ax5.set_xlim(-20, 20)
ax5.set_ylim(0, 4)
#ax1.xaxis.set_ticks()
markers51, stemlines51, baseline51 = ax5.stem(x1, f(x1)*mod(x1))
plt.setp(markers51, 'markersize', 3, 'color', 'r')
plt.setp(stemlines51, 'color', 'r', 'linewidth', '1')
ax5.annotate(r"$x_b[n] = expand\ x[n]\ with\ L$", xy=(0, 0), xytext=(-18, 3.5), color='r')

ax6.set_xlim(-20, 20)
ax6.set_ylim(0, 4)
#ax1.xaxis.set_ticks()
markers63, stemlines63, baseline63 = ax6.stem(x1, f(x1)*mod(x1))
plt.setp(markers63, 'color', 'r', 'markersize', 3)
plt.setp(stemlines63, 'color', 'r', 'linewidth', 1)
markers61, stemlines61, baseline61 = ax6.stem(x1, h(x1)*mod(x1), markerfmt='x')
plt.setp(markers61, 'color', 'C2', 'markersize', 3)
plt.setp(stemlines61, 'color', 'C2', 'linewidth', 1)
ax6.annotate(r"$y[n] = $", xy=(0, 0), xytext=(-18, 3.5))
ax6.annotate(r"$x_b[n]$", xy=(0, 0), xytext=(-12, 3.5), color='r')
ax6.annotate(r"*", xy=(0, 0), xytext=(-6, 3.5))
ax6.annotate(r"$expand\ h[n]\ with\ L$", xy=(0, 0), xytext=(-4, 3.5), color='C2')

fig.savefig("Einterchange.png", format='png')
plt.show()
