"""Utility functions relating to noise and SNR calculations.""" import numpy as np def get_noise_variance_from_SNR(SNR: float, n: int, k: int) -> float: """Calculate the variance of the noise from an SNR and the signal amplitude. :param SNR: Signal-to-Noise-Ratio in dB (E_b/N_0) :param n: Length of a codeword of the used code :param k: Length of a dataword of the used code :return: Variance of the noise """ SNR_linear = 10 ** (SNR / 10) variance = 1 / (2 * (k/n) * SNR_linear) return variance def add_awgn(c: np.array, SNR: float, n: int, k: int) -> np.array: """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. :param c: Binary vector representing the data to be transmitted :param SNR: Signal-to-Noise-Ratio in dB :param n: Length of a codeword of the used code :param k: Length of a dataword of the used code :return: Data vector with added noise """ noise_var = get_noise_variance_from_SNR(SNR, n, k) y = c + np.sqrt(noise_var) * np.random.normal(size=c.size) return y