Damien Rice
Damien Rice

Reputation: 101

Waveform Encoding using Matplotlib in Python

Is there any possible way to plot a encoding graph using matplotlib in python? Suppose I do have a Data String of "01001100011". I want to plot a encoding plot like the picture using matplotlib in python.enter image description here

Upvotes: 0

Views: 288

Answers (1)

Ben Grossmann
Ben Grossmann

Reputation: 4772

You could do something like this.

import numpy as np
import matplotlib.pyplot as plt


s = "01001100011" # string to be encoded

x_max = len(s)+1

# generate waveform encoding scheme
zero_form = np.array([[0,1],[0.5,1],[0.5,0],[1,0]])
one_form = zero_form.copy()
one_form[:,1] = 1 - zero_form[:,1]
encoding = [zero_form,one_form]

# encode the string s
waveform = np.vstack([x + np.array([i,0]) for i,c in enumerate(s) 
                                          for x in encoding[int(c)]])

plt.figure(figsize = (15,3))

# plot grid
for y in [0,.5,1]:
    plt.plot([0,x_max],[y,y],'k--')
for x in range(1,x_max):
    plt.plot([x,x],[0,1.5],'k--')

# plot 0,1 labels
for i,c in enumerate(s):
    plt.text(i+.3,1.2,c, fontsize = 30)

plt.plot(*waveform.T,'r', lw = 10, alpha = .5) # plot waveform
plt.axis("off")                               # turn of axes/labels
plt.show()

The resulting figure:

enter image description here

Upvotes: 2

Related Questions