matplotlib作圖 歸零編碼、曼切斯特編碼、非歸零編碼、差分曼切斯特編碼


效果圖




代碼

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")


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM