<jupyter_start><jupyter_text>SYS 611: Flip Flop (Discrete Time Simulation)<NAME> This example shows how to construct a discrete time simulation of a simple flip flop (a digital component with one bit of memory). The flip flop system has one binary state variable (q), one binary input variable (x), and one binary output variable (y). The output function is defined by the logical operation y(t)=q(t). The state transition function stores the current input value in the state variable q(t+1)=x(t). The expected behavior is to output the input variables but offset by one time step. DependenciesThis example is compatible with Python 2 environments through use of the `__future__` library function. Additionally, this example uses the `matplotlib.pyplot` library for plotting.<jupyter_code># import the python3 behavior for importing, division, and printing in python2
from __future__ import absolute_import, division, print_function

# import the matplotlib pyplot package and refer to it as `plt`
# see http://matplotlib.org/api/pyplot_api.html for documentation
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Transition and Output FunctionsDefine functions for the state transition function (delta) and output function (lambda). Note that `lambda` is a reserved word in Python (for lambda functions), so both Greek letters are prefixed by an underscore.<jupyter_code># define the state transition function
def _delta(q, x):
    # note that xor is equivalent to != in Python
    return x
    
# define the output function (a Moore machine only allows the output to be a function of the state)
def _lambda(q): 
    return q<jupyter_output><empty_output><jupyter_text>Input, State, and Output TrajectoriesDefine lists to store the input trajectory and store the computed output and state trajectories (initialize with zero value).<jupyter_code># define the input trajectory
x = [1,1,0,0,1,0,0,0,1]
    
# define the output and state trajectories (zero initial value)
y = [0,0,0,0,0,0,0,0,0]
q = [0,0,0,0,0,0,0,0,0,0]<jupyter_output><empty_output><jupyter_text>Discrete Time Simulation LogicThe discrete event simulation should start by setting the initial time and state. Then, enter a loop to:1. Compute the current output2. Compute the next state3. Update the time<jupyter_code># initialize the simulation
t = 0
q[0] = 0

# execute the simulation
while t <= 8:
    # record output value
    y[t] = _lambda(q[t])
    # record state update
    q[t+1] = _delta(q[t], x[t])
    # advance time
    t += 1<jupyter_output><empty_output><jupyter_text>Visualize OutcomesUse bar plots in `matplotlib` to plot the input, state, and output trajectories.<jupyter_code>plt.figure()
# create three subplots that all share a common x-axis
f, (ax1, ax2, ax3) = plt.subplots(3, sharex=True)
# plot the input trajectory on the first subplot
ax1.bar(range(9), x, color='k')
ax1.set_ylabel('Input ($x$)')
# plot the state trajectory on the second subplot (up to second-to-last value)
ax2.bar(range(9), q[:-1], color='k')
ax2.set_ylabel('State ($q$)')
# plot the output trajectory on the third subplot
ax3.bar(range(9), y, color='k')
ax3.set_ylabel('Output ($y$)')
# add labels and display
plt.xlabel('Time (ticks)')
plt.suptitle('Flip Flop Model')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09375, 'fraction_numerical': 0.017597087378640776, 'mean_word_length': 5.364864864864865, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '496517', 'license': 'MIT', 'n_tokens_mistral': 1022, 'n_tokens_neox': 987, 'n_words': 406, 'path': 'week7/FlipFlop.ipynb', 'repo_name': 'code-lab-org/sys611', 'size': '3304'}