Fixed SNR amplitude; Fixed BER calculation

This commit is contained in:
Andreas Tsouchlos 2022-11-07 00:32:54 +01:00
parent 74ee1cc4db
commit be6ded2162
2 changed files with 9 additions and 6 deletions

View File

@ -19,15 +19,16 @@ def _get_noise_amp_from_SNR(SNR: float, signal_amp: float = 1) -> float:
return noise_amp return noise_amp
def add_awgn(c: np.array, SNR: float) -> np.array: def add_awgn(c: np.array, SNR: float, signal_amp: float = 1) -> np.array:
"""Add Additive White Gaussian Noise to a data vector. As this function adds random noise to the input, """Add Additive White Gaussian Noise to a data vector. As this function adds random noise to the input,
the output changes, even if it is called multiple times with the same input. the output changes, even if it is called multiple times with the same input.
:param c: Binary vector representing the data to be transmitted :param c: Binary vector representing the data to be transmitted
:param SNR: Signal-to-Noise-Ratio in dB :param SNR: Signal-to-Noise-Ratio in dB
:param signal_amp: Amplitude of the signal. Used for the noise amplitude calculation
:return: Data vector with added noise :return: Data vector with added noise
""" """
noise_amp = _get_noise_amp_from_SNR(SNR, signal_amp=1) noise_amp = _get_noise_amp_from_SNR(SNR, signal_amp=signal_amp)
y = c + np.random.normal(scale=noise_amp, size=c.size) y = c + np.random.normal(scale=noise_amp, size=c.size)
return y return y
@ -66,6 +67,7 @@ def test_decoder(decoder: typing.Any,
bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt}"): bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt}"):
total_bit_errors = 0 total_bit_errors = 0
total_bits = 0
for n in tqdm(range(N_max), desc=f"Simulating for SNR = {SNR} dB", for n in tqdm(range(N_max), desc=f"Simulating for SNR = {SNR} dB",
position=1, position=1,
@ -74,15 +76,15 @@ def test_decoder(decoder: typing.Any,
# TODO: Is this a valid simulation? Can we just add AWGN to the codeword, ignoring and modulation and ( # TODO: Is this a valid simulation? Can we just add AWGN to the codeword, ignoring and modulation and (
# e.g. matched) filtering? # e.g. matched) filtering?
y = add_awgn(x, SNR) y = add_awgn(x, SNR, signal_amp=(1 / np.sqrt(2)))
y_hat = decoder.decode(y) y_hat = decoder.decode(y)
total_bit_errors += count_bit_errors(c, y_hat) total_bit_errors += count_bit_errors(c, y_hat)
total_bits += c.size
if total_bit_errors >= target_bit_errors: if total_bit_errors >= target_bit_errors:
break break
total_bits = c.size * N_max
BERs.append(total_bit_errors / total_bits) BERs.append(total_bit_errors / total_bits)
return np.array(SNRs), np.array(BERs) return np.array(SNRs), np.array(BERs)

View File

@ -21,7 +21,7 @@ def main():
# Test decoder # Test decoder
d = np.array([0, 1, 0, 1]) d = np.array([0, 1, 1, 1])
c = np.dot(G.transpose(), d) % 2 c = np.dot(G.transpose(), d) % 2
print(f"Simulating with c = {c}") print(f"Simulating with c = {c}")
@ -33,6 +33,7 @@ def main():
ax = sns.lineplot(data=data, x="SNR", y="BER") ax = sns.lineplot(data=data, x="SNR", y="BER")
ax.set(yscale="log") ax.set(yscale="log")
ax.set_yticks([10e-5, 10e-4, 10e-3, 10e-2, 10e-1, 10e0])
# ax.set_ylim([10e-6, 10e0]) # ax.set_ylim([10e-6, 10e0])
plt.show() plt.show()