diff --git a/src/sdr/_modulation/_cpm.py b/src/sdr/_modulation/_cpm.py index e7c61d448..4d6e5683b 100644 --- a/src/sdr/_modulation/_cpm.py +++ b/src/sdr/_modulation/_cpm.py @@ -87,10 +87,10 @@ def __init__( if isinstance(symbol_labels, str): verify_literal(symbol_labels, ["bin", "gray"]) if symbol_labels == "bin": - self._symbol_labels = binary_code(self.bps) + self._symbol_labels = binary_code(self.order) self._symbol_labels_str = "bin" elif symbol_labels == "gray": - self._symbol_labels = gray_code(self.bps) + self._symbol_labels = gray_code(self.order) self._symbol_labels_str = "gray" else: symbol_labels = verify_arraylike(symbol_labels, int=True, ndim=1, size=self.order) diff --git a/src/sdr/_modulation/_psk.py b/src/sdr/_modulation/_psk.py index e8070a957..6e77d1e01 100644 --- a/src/sdr/_modulation/_psk.py +++ b/src/sdr/_modulation/_psk.py @@ -199,10 +199,10 @@ def __init__( if isinstance(symbol_labels, str): verify_literal(symbol_labels, ["bin", "gray"]) if symbol_labels == "bin": - self._symbol_labels = binary_code(self.bps) + self._symbol_labels = binary_code(self.order) self._symbol_labels_str = "bin" elif symbol_labels == "gray": - self._symbol_labels = gray_code(self.bps) + self._symbol_labels = gray_code(self.order) self._symbol_labels_str = "gray" else: symbol_labels = verify_arraylike(symbol_labels, int=True, ndim=1, size=self.order) @@ -295,7 +295,7 @@ def ber( if M == 2: # Equation 4.3-13 from Proakis Pbe = Q(np.sqrt(2 * ebn0_linear)) - elif M == 4 and np.array_equal(self._symbol_labels, gray_code(k)): + elif M == 4 and np.array_equal(self._symbol_labels, gray_code(M)): # Equation 4.3-13 from Proakis Pbe = Q(np.sqrt(2 * ebn0_linear)) else: @@ -313,7 +313,7 @@ def ber( if M == 2: # Equation 8.37 from Simon and Alouini Pbe = 2 * Q(np.sqrt(2 * ebn0_linear)) - 2 * Q(np.sqrt(2 * ebn0_linear)) ** 2 - elif M == 4 and np.array_equal(self._symbol_labels, gray_code(k)): + elif M == 4 and np.array_equal(self._symbol_labels, gray_code(M)): # Equation 8.37 from Simon and Alouini Pbe = 2 * Q(np.sqrt(2 * ebn0_linear)) - 2 * Q(np.sqrt(2 * ebn0_linear)) ** 2 else: diff --git a/src/sdr/_sequence/_symbol_mapping.py b/src/sdr/_sequence/_symbol_mapping.py index 9df6ea3d7..e417edefe 100644 --- a/src/sdr/_sequence/_symbol_mapping.py +++ b/src/sdr/_sequence/_symbol_mapping.py @@ -11,12 +11,12 @@ @export -def binary_code(degree: int) -> npt.NDArray[np.int_]: +def binary_code(length: int) -> npt.NDArray[np.int_]: """ Generates a binary code of length $n = 2^m$. Arguments: - degree: The degree $m$ of the binary code. + length: The length $n = 2^m$ of the binary code. Returns: A binary code of length $n = 2^m$. @@ -24,26 +24,26 @@ def binary_code(degree: int) -> npt.NDArray[np.int_]: Examples: .. ipython:: python - sdr.binary_code(1) sdr.binary_code(2) - sdr.binary_code(3) sdr.binary_code(4) + sdr.binary_code(8) + sdr.binary_code(16) Group: sequences-symbol-mapping """ - verify_scalar(degree, int=True, positive=True) + verify_scalar(length, int=True, positive=True, power_of_two=True) - return np.arange(2**degree) + return np.arange(length) @export -def gray_code(degree: int) -> npt.NDArray[np.int_]: +def gray_code(length: int) -> npt.NDArray[np.int_]: """ Generates a Gray code of length $n = 2^m$. Arguments: - degree: The degree $m$ of the Gray code. + length: The length $n = 2^m$ of the Gray code. Returns: A Gray code of length $n = 2^m$. @@ -51,23 +51,23 @@ def gray_code(degree: int) -> npt.NDArray[np.int_]: Examples: .. ipython:: python - sdr.gray_code(1) sdr.gray_code(2) - sdr.gray_code(3) sdr.gray_code(4) + sdr.gray_code(8) + sdr.gray_code(16) Group: sequences-symbol-mapping """ - verify_scalar(degree, int=True, positive=True) + verify_scalar(length, int=True, positive=True, power_of_two=True) - if degree == 1: + if length == 2: return np.array([0, 1]) # Generate the Gray code with degree m - 1 - code_1 = gray_code(degree - 1) + code_1 = gray_code(length // 2) # Generate the Gray code for degree m by concatenating the Gray code for degree m - 1 # with itself reversed. Also, the most significant bit of the second half # is set to 1. - return np.concatenate((code_1, code_1[::-1] + 2 ** (degree - 1))) + return np.concatenate((code_1, code_1[::-1] + length // 2)) diff --git a/tests/modulation/test_binary_code.py b/tests/modulation/test_binary_code.py index d677fdf8e..096a42013 100644 --- a/tests/modulation/test_binary_code.py +++ b/tests/modulation/test_binary_code.py @@ -4,28 +4,28 @@ def test_1(): - code = sdr.binary_code(1) + code = sdr.binary_code(2) code_truth = [0, 1] assert isinstance(code, np.ndarray) assert np.array_equal(code, code_truth) def test_2(): - code = sdr.binary_code(2) + code = sdr.binary_code(4) code_truth = [0, 1, 2, 3] assert isinstance(code, np.ndarray) assert np.array_equal(code, code_truth) def test_3(): - code = sdr.binary_code(3) + code = sdr.binary_code(8) code_truth = [0, 1, 2, 3, 4, 5, 6, 7] assert isinstance(code, np.ndarray) assert np.array_equal(code, code_truth) def test_4(): - code = sdr.binary_code(4) + code = sdr.binary_code(16) code_truth = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] assert isinstance(code, np.ndarray) assert np.array_equal(code, code_truth) diff --git a/tests/modulation/test_gray_code.py b/tests/modulation/test_gray_code.py index 6a8d00851..28590a658 100644 --- a/tests/modulation/test_gray_code.py +++ b/tests/modulation/test_gray_code.py @@ -4,28 +4,28 @@ def test_1(): - code = sdr.gray_code(1) + code = sdr.gray_code(2) code_truth = [0, 1] assert isinstance(code, np.ndarray) assert np.array_equal(code, code_truth) def test_2(): - code = sdr.gray_code(2) + code = sdr.gray_code(4) code_truth = [0, 1, 3, 2] assert isinstance(code, np.ndarray) assert np.array_equal(code, code_truth) def test_3(): - code = sdr.gray_code(3) + code = sdr.gray_code(8) code_truth = [0, 1, 3, 2, 6, 7, 5, 4] assert isinstance(code, np.ndarray) assert np.array_equal(code, code_truth) def test_4(): - code = sdr.gray_code(4) + code = sdr.gray_code(16) code_truth = [0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8] assert isinstance(code, np.ndarray) assert np.array_equal(code, code_truth)