7

I am trying to draw digital signal waveform graph for bits like 01010101010101 using pyplot in python, like

http://upload.wikimedia.org/wikipedia/commons/thumb/5/50/Differential_manchester_encoding.svg/600px-Differential_manchester_encoding.svg.png

Is it possible with Pyplot?

alko
  • 46,136
  • 12
  • 94
  • 102
maths
  • 1,399
  • 5
  • 23
  • 38
  • this can be done using `plot`, but it requires a little tweak of the input coordinates: whenever there is a change in the signal an additional point should be added to the sequence with the same time *x coordinate) and appropriate zero-one value for the y coordinate. I do not have Matlab open right now to provide a detailed solution, but this info should be enough to implement one. – Shai Nov 17 '13 at 21:19
  • In matlab: [stairs](http://www.mathworks.com/help/matlab/ref/stairs.html) – A. Donda Nov 17 '13 at 21:26

4 Answers4

11

As noted by tcaswell, the right function to use is step. Here an example that approximates your given plot:

import matplotlib.pyplot as plt
import numpy as np

def my_lines(ax, pos, *args, **kwargs):
    if ax == 'x':
        for p in pos:
            plt.axvline(p, *args, **kwargs)
    else:
        for p in pos:
            plt.axhline(p, *args, **kwargs)

bits = [0,1,0,1,0,0,1,1,1,0,0,1,0]
data = np.repeat(bits, 2)
clock = 1 - np.arange(len(data)) % 2
manchester = 1 - np.logical_xor(clock, data)
t = 0.5 * np.arange(len(data))

plt.hold(True)
my_lines('x', range(13), color='.5', linewidth=2)
my_lines('y', [0.5, 2, 4], color='.5', linewidth=2)
plt.step(t, clock + 4, 'r', linewidth = 2, where='post')
plt.step(t, data + 2, 'r', linewidth = 2, where='post')
plt.step(t, manchester, 'r', linewidth = 2, where='post')
plt.ylim([-1,6])

for tbit, bit in enumerate(bits):
    plt.text(tbit + 0.5, 1.5, str(bit))

plt.gca().axis('off')
plt.show()

enter image description here

Bas Swinckels
  • 18,095
  • 3
  • 45
  • 62
8

Just use plt.step

plt.step(x, y, where='pre')

See the docs and Linestyle in matplotlib step function and Step function in matplotlib for examples.

Community
  • 1
  • 1
tacaswell
  • 84,579
  • 22
  • 210
  • 199
2

Here's my attempt. The trick I've used is to double-up each x and y-coordinate with np.repeat and then offset them by 1 to generate the coordinates of each corner. Let me know if this needs better explanation. You can add grid lines with plt.grid(True).

import numpy as np
import matplotlib.pyplot as plt

data = [1, 0, 0, 1, 1, 0, 1, 0]
xs = np.repeat(range(len(data)), 2)
ys = np.repeat(data, 2)
xs = xs[1:]
ys = ys[:-1]
xs = np.append(xs,xs[-1] + 1)
ys = np.append(ys, ys[-1])
plt.plot(xs, ys)
plt.ylim(-0.5, 1.5)
plt.show()

enter image description here

rey
  • 3
  • 2
YXD
  • 31,741
  • 15
  • 75
  • 115
1

Since your title also says "in Matlab": use stairs:

y = [0 1 0 1 0 1 0 1 0 1 0 1 0 1]; % define values
stairs(0:length(y)-1,y) % do the plotting
ylim([-.5 1.5]) % adjust axis limits

enter image description here

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147