# SDR Lab -- Python Notebook

!pip install ipympl librosa
%matplotlib inline
from common import *
from IPython.display import HTML, Audio, display
from scipy import signal
import numpy as np
# Audio
import librosa
import librosa.display
# Helpers
from common import *

## Variable Definition

f_sample = 300e3
dt = 1/f_sample
f_center = 89.7e6
seconds = 1.0 # Start with this low to save some processing time, but end with it a little higher.
N_SAMPLES = int(seconds*f_sample)
BUFFER_SIZE = 256
vars = np.load("rhapsody.npz")
y = vars['y']
y_q = y.real
y_i = y.imag

## Recreating the Audio

I attempted to add a bandpass filter to the system centered on the center frequency. However, this didn't work because the center frequency was much larger than our sampling frequency. This inequality causes the error because it violates Nyquist's theorem which states that a periodic signal must be sampled at more than twice the highest frequency component of the signal. I tried applying high pass filters that we're valid within the Nyquist frequency for our sampling rate however that wasn't as effective as just using the simple derivative.

sos = signal.butter(10, f_center, 'hp', fs=f_sample, output='sos')
m_hat = (signal.sosfilt(sos, y_q) * y_i) - (signal.sosfilt(sos, y_i) * y_q)
t = np.arange(0,dt*(len(m_hat)+1),dt)
fig, axs = plt.subplots()
axs.set_title("M_hat(t)")
axs.plot(t[:-1], m_hat, color='C0')
axs.set_xlabel("Time")
axs.set_ylabel("Amplitude")
Audio(m_hat, rate=f_sample)

What worked best for me was using the derivative as shown below.

m_hat = (np.diff(y_q, prepend=y_q[0]) * y_i) - (np.diff(y_i, prepend=y_i[0]) * y_q)
t = np.arange(0,dt*(len(m_hat)+1),dt)
fig, axs = plt.subplots()
axs.set_title("M_hat(t)")
axs.plot(t[:-1], m_hat, color='C0')
axs.set_xlabel("Time")
axs.set_ylabel("Amplitude")
Audio(m_hat, rate=f_sample)