Skip to content

Instantly share code, notes, and snippets.

@edy555
Created November 25, 2013 15:58
Show Gist options
  • Save edy555/7643605 to your computer and use it in GitHub Desktop.
Save edy555/7643605 to your computer and use it in GitHub Desktop.
trial to build sna with rtl2832u dongle and pll synthesizer
#!/usr/bin/env python
from adf4351usb import ADF4351
import numpy as np
import rtlsdr
import time
import matplotlib.pyplot as plt
import scipy.fftpack as sf
#start = 390e6
#end = 410e6
#step = 0.5e6
start = 400e6
end = 500e6
step = 1e6
def inclusive_range(x, y, s):
while x <= y:
yield x
x += s
SAMPLE_RATE = 2e6
GAIN = 0
READ_SAMPLES = 65536
N_SAMPLES = 2048 # 1ms
PICKUP_OFFSET = READ_SAMPLES-N_SAMPLES
FREQ_CAL = -53.0e-6
FREQ_OFFSET = 5e3
window_func = np.hamming(N_SAMPLES)
def rtl_setup():
rtl = rtlsdr.RtlSdr()
rtl.set_sample_rate(SAMPLE_RATE)
rtl.set_gain(GAIN)
rtl.set_manual_gain_enabled(True)
return rtl
pll = ADF4351()
rtl = rtl_setup()
#pll.setFrequency((start+end) / 2)
def measure(f):
pll.setFrequency(f + FREQ_OFFSET)
rtl.set_center_freq(f * (1.0 + FREQ_CAL))
time.sleep(1)
samples = rtl.read_samples(READ_SAMPLES)
samples = samples[PICKUP_OFFSET:PICKUP_OFFSET+N_SAMPLES]
#print samples
pow = np.sum(np.absolute(samples)**2)/N_SAMPLES
print "%g %g %g"%(f,pow,10 * np.log10(pow))
return pow
def measure_fft(f):
pll.setFrequency(f + FREQ_OFFSET)
rtl.set_center_freq(f * (1.0 + FREQ_CAL))
#time.sleep(0.5)
samples = rtl.read_samples(READ_SAMPLES)
samples = samples[PICKUP_OFFSET:PICKUP_OFFSET+N_SAMPLES]
fourier_val = sf.fft(samples * window_func) / N_SAMPLES
pow = max(abs(fourier_val))
print "%g %g"%(f,pow)
return pow
#np.arange(start, end, step)
freq = list(inclusive_range(start, end, step))
measured = [measure(f) for f in freq]
rtl.close()
#for f in freq:
#pow = measure(f)
#print "%g %g %g"%(f,pow,20 * np.log10(pow))
plt.xlabel("Frequency(Hz)")
plt.ylabel("Amplitude(dB)")
plt.plot(freq, 20 * np.log10(measured), "-")
plt.ylim(-60, 0)
plt.show()
@spyhound
Copy link

File "./sna-test.py", line 41, in
pll = ADF4351()
File "adf4351usb.py", line 122, in init
h = hid.device(0x0456,0xB40D)
File "hid.pyx", line 45, in hid.device.cinit (hid.c:1351)
IOError: open failed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment