-
Notifications
You must be signed in to change notification settings - Fork 6
/
SingleToneSweeper.py
110 lines (83 loc) · 2.91 KB
/
SingleToneSweeper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from __future__ import print_function
import math
import numpy as np
from pyLMS7002Soapy import pyLMS7002Soapy as pyLMSS
from scipy import signal
class SingleToneSweeper:
sampleCnt = 4096
radio = None
rxStream = None
sampleRate = None
bandWidth = None
aborted = False
events = None
def __init__(self, radio, events):
self.radio = radio
self.events = events
self.radio.tddMode = True
self.radio.testSignalDC(0x3fff, 0x3fff)
self.rxStream = self.radio.sdr.setupStream(pyLMSS.SOAPY_SDR_RX, pyLMSS.SOAPY_SDR_CF32, [0], {"bufferLength": str(self.sampleCnt*10)})
def setSampleRate(self, sampleRate):
sampleRate = float(sampleRate)
if (self.sampleRate==sampleRate):
return
if (self.sampleRate is not None):
self.radio.sdr.deactivateStream(self.rxStream)
self.radio.txNCOFreq = 0
self.radio.cgenFrequency = sampleRate * 8
self.radio.rxBandwidth = round(sampleRate * 1.25)
self.radio.txBandwidth = round(sampleRate * 1.25)
self.radio.rxSampleRate = sampleRate
self.radio.txSampleRate = sampleRate
self.sampleRate = sampleRate
self.bandWidth = sampleRate #round(sampleRate*0.5)
self.radio.sdr.activateStream(self.rxStream)
def setGain(self, rxGain, txGain):
self.radio.rxGain = rxGain #-12, 61
self.radio.txGain = txGain #-12, 64
def abortSweep(self):
self.aborted = True
def sweep(self, snaStartFreq, snaEndFreq, snaNumSteps):
self.aborted = False
numSteps = math.ceil(snaNumSteps / 2) * 2
txNCOStep = round(self.bandWidth / numSteps)
txNCOOffset = round(txNCOStep / 2)
txNCOFreqStart = txNCOStep*(numSteps/2) - txNCOOffset
txRfFreq = snaStartFreq + txNCOFreqStart
txNCOFreqStart *= -1
self.events.sweepStart(snaStartFreq, txNCOStep, math.floor((snaEndFreq-snaStartFreq) / txNCOStep) + 1)
n = 0
brk = False
while (True):
fftIndex = 0 #round(numSteps/2)
txNCOFreq = txNCOFreqStart
self.radio.txNCOFreq = txNCOFreq #pretune nco to avoid loss of samples
self.radio.txRfFreq = txRfFreq
self.radio.configureAntenna(txRfFreq)
for i in range(0, numSteps):
print(".", end="", flush=True)
self.radio.txNCOFreq = txNCOFreq
targetTime = self.radio.sdr.getHardwareTime() + 1e6
buff = self.readSamples(self.sampleCnt, targetTime)
fft = signal.welch(buff, 1.0, 'flattop', numSteps, scaling='spectrum', return_onesided=False, detrend=False)
fft = np.fft.fftshift(fft[1])
pwr = 10*np.log10(fft[fftIndex])
self.events.sweepResult(n, pwr)
if ((txRfFreq + txNCOFreq >= snaEndFreq) or (self.aborted)):
brk = True
break
txNCOFreq += txNCOStep
fftIndex += 1
n += 1
print(" ")
if (brk):
break
txRfFreq += self.bandWidth
def readSamples(self, nSamples, targetTimeNs):
buff = np.zeros(nSamples, np.complex64)
numElemsRequest = nSamples
while numElemsRequest > 0:
sr = self.radio.sdr.readStream(self.rxStream, [buff], nSamples)
if (sr.timeNs>=targetTimeNs):
numElemsRequest -= sr.ret
return buff