效果图
代码
import matplotlib.pyplot as plt
config = {
'color': 'black',
'lw': 5,
}
def init():
plt.figure(figsize=(13, 4))
plt.ylim(-0.5, 1.5)
plt.yticks([0, 1])
plt.gca().xaxis.set_major_locator(plt.MultipleLocator(1))
plt.grid()
def RZ(code):
init()
plt.title('归零编码方式', fontsize=20)
plt.xlim(0, len(code))
for i, bit in enumerate(code):
plt.text(i + 0.4, -0.4, bit, fontsize=25)
if bit == "1":
plt.plot([i, i, i + 0.5, i + 0.5, i + 1], [0, 1, 1, 0, 0], **config)
else:
plt.plot([i, i + 1], [0, 0], **config)
def NRZ(code):
init()
plt.title('非归零编码方式', fontsize=20)
plt.xlim(0, len(code))
prev = "0"
for i, bit in enumerate(code):
plt.text(i + 0.4, -0.4, bit, fontsize=25)
if bit == "1":
if bit != prev:
prev = "1"
plt.plot([i, i], [0, 1], **config)
plt.plot([i, i + 1], [1, 1], **config)
else:
if bit != prev:
prev = "0"
plt.plot([i, i], [1, 0], **config)
plt.plot([i, i + 1], [0, 0], **config)
def Manchester(code):
init()
plt.title('曼切斯特', fontsize=20)
plt.xlim(0, len(code))
prev = "0"
for i, bit in enumerate(code):
plt.text(i + 0.4, -0.4, bit, fontsize=25)
if bit == "1":
if bit == prev:
plt.plot([i, i], [0, 1], **config)
prev = "1"
plt.plot([i, i + 0.5, i + 0.5, i + 1], [1, 1, 0, 0], **config)
else:
if bit == prev:
plt.plot([i, i], [1, 0], **config)
prev = "0"
plt.plot([i, i + 0.5, i + 0.5, i + 1], [0, 0, 1, 1], **config)
def Diff_Manchester(code):
init()
plt.title('差分曼切斯特', fontsize=20)
plt.xlim(0, len(code))
change = False
for i, bit in enumerate(code):
plt.text(i + 0.4, -0.4, bit, fontsize=25)
if bit == "1":
change = not change
if change:
if bit == "0":
plt.plot([i, i], [0, 1], **config)
plt.plot([i, i + 0.5, i + 0.5, i + 1], [1, 1, 0, 0], **config)
else:
if bit == "0":
plt.plot([i, i], [1, 0], **config)
plt.plot([i, i + 0.5, i + 0.5, i + 1], [0, 0, 1, 1], **config)
RZ("10011010")
plt.savefig("rz.png")
NRZ("10011010")
plt.savefig("nrz.png")
Manchester("10011010")
plt.savefig("m.png")
Diff_Manchester("10011010")
plt.savefig("dm.png")