# An FM broadcast signal has  a bandwidth of 200 kHz
f_bw = 200000
n_taps = 64

# Use Remez algorithm to design filter coefficients used for downscaling in frequency.
lpf = signal.remez(n_taps, [0, f_bw, f_bw + (Fs / 2 - f_bw) / 4, Fs / 2],
                   [1, 0],
                   Hz=Fs)

# Read samples
before = time.time()
samples = sdr.read_bytes(2 * N)
print time.time() - before

# Convert samples to a numpy array
x1 = sdr.packed_bytes_to_iq(samples)

# To mix the data down, generate a digital complex exponential
# (with the same length as x1) with phase -F_offset/Fs
fc1 = np.exp(-1.0j * 2.0 * np.pi * F_offset / Fs * np.arange(len(x1)))

# Now, just multiply x1 and the digital complex exponential
x2 = x1 * fc1

# Downsample the signal
x3 = signal.lfilter(lpf, 1.0, x2)

# Decimate the signal
dec_rate = int(Fs / f_bw)
x4 = x3[0::dec_rate]