From becd9cb96d1e3a17b802796092c6f86ac4e06f98 Mon Sep 17 00:00:00 2001 From: appellet Date: Tue, 20 May 2025 19:52:36 +0200 Subject: [PATCH] fix: improve quality --- receiver.py | 59 +++++++++++++++-------------------------------------- 1 file changed, 16 insertions(+), 43 deletions(-) diff --git a/receiver.py b/receiver.py index c8e048e..fe90cce 100644 --- a/receiver.py +++ b/receiver.py @@ -1,69 +1,42 @@ import numpy as np - def bits_to_text(bits, alphabet): - """Convert binary bits to text using the alphabet.""" char_to_idx = {char: idx for idx, char in enumerate(alphabet)} idx_to_char = {idx: char for char, idx in char_to_idx.items()} text = '' for i in range(0, len(bits), 6): - # Convert 6 bits to an integer idx = int(''.join(map(str, bits[i:i + 6])), 2) - text += idx_to_char.get(idx, '?') # Default to '?' if index invalid + text += idx_to_char.get(idx, '?') return text - def receiver(Y): - """ - Decode the received signal Y to a 40-character text message. - - Parameters: - Y (np.ndarray): Received signal of length 480. - - Returns: - str: Decoded 40-character text message. - """ - # Define constants - G = 10 # Power gain - sigma2 = 10 # Noise variance + G = 10 # Match channel.py + sigma2 = 10 alphabet = ( 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' '0123456789 .' ) - - # Input validation if Y.size != 480: raise ValueError("Received signal must have length 480.") - n = 480 bits = np.zeros(240, dtype=int) - E = 4 # Energy per bit, must match transmitter + E = 4 + + # Estimate channel state + even_energy = np.mean(np.abs(Y[::2])) + odd_energy = np.mean(np.abs(Y[1::2])) + # If even indices have higher energy, likely s=1; else s=2 + s_hat = 1 if even_energy > odd_energy else 2 - # Process each bit (sent at indices 2i and 2i+1) for i in range(240): - y_odd = Y[2 * i] # Even index in Y (0-based) - y_even = Y[2 * i + 1] # Odd index in Y - - # LLR for channel 1: odd indices have gain sqrt(G), even have gain 1 - # H0: bit = 0 (sent +sqrt(E)), H1: bit = 1 (sent -sqrt(E)) - llr_ch1 = ( - (y_odd * np.sqrt(E) * np.sqrt(G) / sigma2) + # Odd index term - (y_even * np.sqrt(E) / sigma2) # Even index term - ) - - # LLR for channel 2: even indices have gain sqrt(G), odd have gain 1 - llr_ch2 = ( - (y_odd * np.sqrt(E) / sigma2) + # Odd index term - (y_even * np.sqrt(E) * np.sqrt(G) / sigma2) # Even index term - ) - - # Combine LLRs (assuming equal prior probabilities for both channels) - llr = 0.5 * (llr_ch1 + llr_ch2) - - # Decode bit: LLR > 0 implies bit = 0, LLR < 0 implies bit = 1 + y_even = Y[2 * i] + y_odd = Y[2 * i + 1] + if s_hat == 1: + llr = (y_even * np.sqrt(E) * np.sqrt(G) + y_odd * np.sqrt(E)) / sigma2 + else: + llr = (y_even * np.sqrt(E) + y_odd * np.sqrt(E) * np.sqrt(G)) / sigma2 bits[i] = 1 if llr < 0 else 0 - # Convert bits to text text = bits_to_text(bits, alphabet) return text \ No newline at end of file