Created
May 22, 2024 14:27
-
-
Save EhWhoAmI/843b3a347c3cee3f88f6ae0fb824b9de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
sample_time = 20 | |
samples = 2**16 * sample_time | |
symbol_count = sample_time * 200 | |
symbol_length = samples // symbol_count | |
frequency = 10000 | |
time = np.linspace(0, sample_time, samples) | |
# 1 is 1 and -1 is 0 | |
data = symbols = np.random.choice([-1,1], size=symbol_count) | |
# Now generate a sin wave | |
carrier_wave = np.sin(2 * np.pi * frequency * time) | |
# Spread the signal | |
symbols = np.repeat(symbols, symbol_length) | |
symbols = np.pad(symbols, (0, max(0, len(carrier_wave) - len(symbols))), 'constant') | |
# Now multiply | |
signal = carrier_wave * symbols | |
fig, (ax1, ax2) = plt.subplots(2) | |
ax1.plot(time, signal) | |
ax1.plot(time, symbols) | |
signal += np.random.normal(0, 5, len(signal)) | |
# Decode | |
demodded = signal * np.sin(2 * np.pi * frequency * time) | |
linear = np.split(demodded[:symbol_length * symbol_count], symbol_count) | |
decoded = np.sum(linear, axis=1) / symbol_length * 2 | |
decoded[decoded > 0] = 1 | |
decoded[decoded < 0] = -1 | |
decoded_extended = np.repeat(decoded, symbol_length) | |
correct = np.sum(np.equal(decoded, data)) | |
error_rate = (symbol_count - correct) / symbol_count * 100 | |
print("Error rate: " + str(error_rate) + "%") | |
# Integrate among the symbol lenght | |
ax2.plot(time, symbols) | |
ax2.plot(time[:symbol_length * symbol_count], decoded_extended) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment