simplify/optimize

This commit is contained in:
Sebastien Bourdeauducq 2020-08-10 14:29:55 +08:00
parent 630100eb7d
commit 2851be4709
2 changed files with 11 additions and 22 deletions

12
dmi.py
View File

@ -9,14 +9,14 @@ def main():
# For this PoC code, a small memory leak is less harmful than random overflows. # For this PoC code, a small memory leak is less harmful than random overflows.
gc.disable() gc.disable()
freq_sample = 5e6 freq_sample = 1e6
freq_base = 1086e6 freq_base = 1088230e3
bufsize = 4096 bufsize = 4096
induction = InductionHeater("/dev/ttyUSB0", 350e3, 445e3) induction = InductionHeater("/dev/ttyUSB0", 350e3, 445e3)
induction.start() induction.start()
try: try:
stabilizer = Stabilizer(freq_sample, 0.4, 1088.1e6 - freq_base, 50e-6, induction) stabilizer = Stabilizer(bufsize, 80.0, 1088.1e6 - freq_base, 10e-6, induction)
position_tracker = PositionTracker(int(0.1*freq_sample/bufsize)) position_tracker = PositionTracker(int(0.1*freq_sample/bufsize))
sdr = SoapySDR.Device() sdr = SoapySDR.Device()
@ -35,12 +35,12 @@ def main():
# We can't update faster than the MHS5200A serial interface # We can't update faster than the MHS5200A serial interface
stabilizer_throttle += 1 stabilizer_throttle += 1
if stabilizer_throttle == 40: if stabilizer_throttle == 8:
stabilizer.input(samples_ref) stabilizer.input(samples_ref)
stabilizer_throttle = 0 stabilizer_throttle = 0
position, leakage = position_tracker.input(samples_ref, samples_meas) #position, leakage = position_tracker.input(samples_ref, samples_meas)
print(np.sum(position)/len(position), leakage) #print(np.sum(position)/len(position), leakage)
finally: finally:
buf_sdr.dispose(buffers) buf_sdr.dispose(buffers)
finally: finally:

View File

@ -104,30 +104,19 @@ class InductionHeater:
self.thread.join() self.thread.join()
self.serial.close() self.serial.close()
# https://gist.github.com/endolith/255291
def parabolic(f, x):
xv = 1/2. * (f[x-1] - f[x+1]) / (f[x-1] - 2 * f[x] + f[x+1]) + x
yv = f[x] - 1/4. * (f[x-1] - f[x+1]) * (xv - x)
return (xv, yv)
class Stabilizer: class Stabilizer:
def __init__(self, freq_sample, amp_threshold, freq_target, k, tuner): def __init__(self, fft_size, amp_threshold, freq_target, k, tuner):
self.freq_sample = freq_sample self.freqs = np.fft.fftfreq(fft_size)
self.amp_threshold = amp_threshold self.amp_threshold = amp_threshold
self.freq_target = freq_target self.freq_target = freq_target
self.k = k self.k = k
self.tuner = tuner self.tuner = tuner
def input(self, samples): def input(self, samples):
spectrum = np.abs(np.fft.fft(samples*blackmanharris(len(samples)))[0:len(samples)//2]) spectrum = np.abs(np.fft.fft(samples*blackmanharris(len(samples))))
for i in range(len(spectrum)//100):
spectrum[i] = 0
spectrum[-i] = 0
i = np.argmax(spectrum) i = np.argmax(spectrum)
true_i, amplitude = parabolic(spectrum, i) freq = self.freqs[i]
freq = 0.5 * self.freq_sample * true_i / len(spectrum) amplitude = spectrum[i]
if amplitude > self.amp_threshold: if amplitude > self.amp_threshold:
tuning = (freq - self.freq_target)*self.k tuning = (freq - self.freq_target)*self.k