Files
fmdemod/fmdemod3.py
2024-01-14 23:41:53 -08:00

36 lines
1.2 KiB
Python

import numpy as np
import sounddevice as sd
from scipy.io import wavfile
from scipy.signal import decimate, butter, filtfilt
# Load the raw IQ samples from the file
filename = "fm_capture6.bin"
iq_samples = np.fromfile(filename, dtype=np.int8)
# Define the FM demodulation parameters
sample_rate = int(2e6) # Sample rate of the IQ samples
audio_sample_rate = int(48e3) # Desired audio sample rate
fm_deviation = int(75e3) # FM deviation of the radio signal
channel_width = int(200e3) # Channel width of the low-pass filter
# Design the low-pass filter
nyquist_freq = sample_rate / 2
cutoff_freq = channel_width / nyquist_freq
b, a = butter(10, int(75e3), btype='low', fs=int(2e6))
# Apply the low-pass filter
filtered_audio = filtfilt(b, a, iq_samples)
# Demodulate the FM signal
demodulated_audio = np.diff(filtered_audio.astype(np.float32)) * np.conj(filtered_audio[:-1]).astype(np.float32)
demodulated_audio = decimate(demodulated_audio, int(100))
# Normalize the audio to the range [-1, 1]
normalized_audio = demodulated_audio / np.max(np.abs(demodulated_audio))
# Play the audio
sd.play(normalized_audio, audio_sample_rate)
sd.wait()
# output_filename = "demodulated_audio.wav"
# wavfile.write(output_filename, audio_sample_rate, normalized_audio)