import math def bell202_fsk_modulate(bits, sample_rate=8000, baud_rate=1200): """ Modulates a list of bits into a Bell 202 FSK continuous-phase audio signal. Bell 202 Standards: - Mark (1): 1200 Hz - Space (0): 2200 Hz - Transmission speed: 1200 baud """ MARK_FREQ = 1200 # Frequency for '1' SPACE_FREQ = 2200 # Frequency for '0' # Calculate how many audio samples represent a single bit samples_per_bit = sample_rate / baud_rate signal = [] phase = 0.0 # Process each bit sequentially for bit in bits: # 1. Assign the target frequency based on the bit value freq = MARK_FREQ if bit == 1 else SPACE_FREQ # 2. Determine the exact integer sample count for this bit duration num_samples = int(round(samples_per_bit)) # 3. Generate sine wave samples for this bit's duration for _ in range(num_samples): sample = math.sin(phase) signal.append(sample) # Increment phase relative to the current frequency phase += 2 * math.pi * freq / sample_rate # Keep phase bounded between 0 and 2*pi to avoid floating-point drift phase = phase % (2 * math.pi) return signal # --- Example Usage --- if __name__ == "__main__": # Example bitstream (e.g., standard testing sequence) binary_data = [1, 0, 1, 1, 0, 0, 1] # Generate the signal using standard 8000 Hz telephone sample rate fs = 8000 fsk_signal = bell202_fsk_modulate(binary_data, sample_rate=fs) print(f"Successfully modulated {len(binary_data)} bits!") print(f"Total audio samples generated: {len(fsk_signal)}") print(f"First 10 raw sample floating-point values:") print([round(sample, 4) for sample in fsk_signal[:10]])