More PRBS turtle graphics

In this plot, four cycles of PRBS-15 are shown. Like all PRBS sequences generated using a linear feedback shift register (LFSR), this binary sequence is periodic with a period of 32767 bits. The colour of each point on the path represents the position of the corresponding bit within the that cycle of the PRBS sequence.

#
# More PRBS turtle graphics - Written by Ted Burke 11-Feb-2022
#
 
import matplotlib.pyplot as plt
import numpy as np
 
# Generate 32767-bit PRBS using 15-bit linear feedback shift register (LFSR)
prbs = []
lfsr = np.ones(15, dtype=np.int8)
for i in range(2**15 - 1):
    prbs = prbs + [lfsr[0]]
    b = (lfsr[14] + lfsr[13]) % 2
    lfsr = np.concatenate((np.array([b], dtype=np.int8), lfsr[:-1]))

# Create matplotlib figure
plt.figure(figsize=(4, 4), dpi=600)

# The generated path turns this angle at every step
# One step per bit in the PRBS - left turn for 1, right turn for 0
theta = np.pi/15

# Generate series of complex points for this frame
z = 0 + 0j;
x_min = 0; x_max = 0; y_min=0; y_max = 0;
step = 0 + 1j;
points = np.array([0 + 0j])
for i in range(4):
    for bit in prbs:
        # calculate next complex point and add it to the series for this frame
        step = step * np.exp(1j*(2*bit-1)*theta) # steer left for 1 or right for 0
        z += step
        points = np.append(points, z)
        # Keep track of real and imaginary max and min for plot axes limits
        x_min = min(x_min, np.real(z))
        x_max = max(x_max, np.real(z))
        y_min = min(y_min, np.imag(z))
        y_max = max(y_max, np.imag(z))
 
# Clear the axes, then plot a line through the complex points in array a
plt.cla()
cx, cy = 0.5*(x_max+x_min), 0.5*(y_max+y_min)
width = max(x_max-x_min, y_max-y_min)
plt.xlim(cx-width/2-100, cx+width/2+100)
plt.ylim(cy-width/2-100, cy+width/2+100)
 
#plt.plot(points.real, points.imag, linewidth=0.25)
plt.scatter(points.real, points.imag, \
            c=1+np.cos(4*2*np.pi*np.arange(len(points))/len(points)), \
            marker='.', s=0.01)
 
# Save plot to a PNG file
plt.savefig('turtle.png')

Advertisement
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s