Skip to content

Instantly share code, notes, and snippets.

@RJ722
Created November 10, 2018 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RJ722/0954746b206f01e0178c621cc3d390c6 to your computer and use it in GitHub Desktop.
Save RJ722/0954746b206f01e0178c621cc3d390c6 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
BIT_DURATION = 1
MODULATION_FREQ = 3 / BIT_DURATION
N_SAMPLES = 10000
def generate_random_sequence(length):
return np.random.randint(0, 2, (length, ))
def to_bipolar(arr):
"""
Binary Antipodal signaling.
Note that arr is assumed to be a numpy array
"""
return 2 * arr - 1
def main():
length = int(input("Please enter the length of the signal: "))
arr = generate_random_sequence(length)
unipolar_arr = np.array(arr)
bipolar = to_bipolar(unipolar_arr)
time = np.linspace(0, unipolar_arr.size, N_SAMPLES)
samples_per_bit = N_SAMPLES/unipolar_arr.size
dd = np.repeat(unipolar_arr, samples_per_bit)
bb = np.repeat(bipolar, samples_per_bit)
_, ax = plt.subplots(2, 1, sharex=True, sharey=True, squeeze=True)
ax[0].plot(time, dd)
ax[1].plot(time, bb)
ax[0].set_xlabel('Time')
ax[0].set_ylabel('Baseband Signal')
ax[1].set_ylabel('Antipodal Signal')
label = "Random numbers generated: " + str(arr)
plt.text(5, 4, label, ha='right')
plt.show()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment