display detected beat frequency

This commit is contained in:
Sebastien Bourdeauducq 2020-08-10 20:25:01 +08:00
parent efc7ca9e32
commit 8b90d7395b
4 changed files with 43 additions and 23 deletions

4
dmi.py
View File

@ -19,8 +19,8 @@ def main():
induction = InductionHeater("/dev/ttyUSB0", 350e3, 445e3)
induction.start()
try:
def stabilizer_cb(spectrum, tuning):
gui.update_beat_spectrum(spectrum)
def stabilizer_cb(spectrum, peak_freq, tuning):
gui.update_beat_spectrum(spectrum, peak_freq)
induction.set(tuning)
stabilizer = Stabilizer(block_size, 80.0, 1088.1e6 - freq_base, 10e-6, stabilizer_cb)

4
gui.py
View File

@ -54,8 +54,8 @@ class GUI:
os.path.join(os.path.dirname(os.path.abspath(__file__)), "gui_impl.py"),
str(freq_sample), str(freq_base), str(block_size)])
def update_beat_spectrum(self, data):
obj = {"action": "update_beat_spectrum", "data": data}
def update_beat_spectrum(self, block, peak_freq):
obj = {"action": "update_beat_spectrum", "block": block, "peak_freq": peak_freq}
self.impl.write_pyon(obj)
def close(self):

View File

@ -10,30 +10,49 @@ from sipyco.pipe_ipc import AsyncioChildComm
from sipyco import pyon
class SpectrogramWidget(pg.PlotWidget):
class SpectrogramItem(pg.ImageItem):
def __init__(self, freq_sample, freq_base, block_size):
super(SpectrogramWidget, self).__init__()
pg.ImageItem.__init__(self)
depth = 100
self.img_array = np.zeros((depth, block_size))
self.img = pg.ImageItem()
self.addItem(self.img)
self.img.setImage(self.img_array, autoLevels=True)
self.setImage(self.img_array, autoLevels=True)
pos = np.array([0., 1., 0.5, 0.25, 0.75])
color = np.array([[0,255,255,255], [255,255,0,255], [0,0,0,255], (0, 0, 255, 255), (255, 0, 0, 255)], dtype=np.ubyte)
cmap = pg.ColorMap(pos, color)
lut = cmap.getLookupTable(0.0, 1.0, 256)
self.img.setLookupTable(lut)
self.setLookupTable(lut)
self.img.setRect(QtCore.QRectF(0.0, freq_base-freq_sample/2, float(depth), freq_sample))
self.setRect(QtCore.QRectF(0.0, (freq_base-freq_sample/2)/1e6, float(depth), freq_sample/1e6))
def update(self, block):
def add_block(self, block):
self.img_array = np.roll(self.img_array, -1, 0)
self.img_array[-1:] = np.fft.fftshift(block)
self.img.setImage(self.img_array, autoLevels=True)
self.setImage(self.img_array, autoLevels=True)
class MainWindow(pg.GraphicsWindow):
def __init__(self, freq_sample, freq_base, block_size):
pg.GraphicsWindow.__init__(self)
self.setWindowTitle("NOPTICA Wavemeter")
self.freq_sample = freq_sample
self.freq_base = freq_base
self.text = pg.LabelItem()
self.addItem(self.text)
p1 = self.addPlot(row=1, col=0)
self.beat_spectrum = SpectrogramItem(freq_sample, freq_base, block_size)
p1.addItem(self.beat_spectrum)
def update_params(self, peak_freq):
if peak_freq is None:
self.text.setText("REF: NO BEAT")
else:
self.text.setText("REF: {:.3f}MHz".format((self.freq_base + self.freq_sample*peak_freq)/1e6))
class IPCClient(AsyncioChildComm):
@ -44,13 +63,14 @@ class IPCClient(AsyncioChildComm):
line = await self.readline()
return pyon.decode(line.decode())
async def listen(self, spectrogram):
async def listen(self, main_window):
while True:
obj = await self.read_pyon()
try:
action = obj["action"]
if action == "update_beat_spectrum":
spectrogram.update(obj["data"])
main_window.beat_spectrum.add_block(obj["block"])
main_window.update_params(obj["peak_freq"])
if action == "terminate":
self.close_cb()
return
@ -73,11 +93,10 @@ def main():
ipc = IPCClient(os.getenv("NOPTICA2_IPC"))
loop.run_until_complete(ipc.connect())
try:
main_widget = SpectrogramWidget(freq_sample, freq_base, block_size)
main_widget.setWindowTitle("NOPTICA Wavemeter")
main_widget.show()
ipc.set_close_cb(main_widget.close)
asyncio.ensure_future(ipc.listen(main_widget))
main_window = MainWindow(freq_sample, freq_base, block_size)
main_window.show()
ipc.set_close_cb(main_window.close)
asyncio.ensure_future(ipc.listen(main_window))
loop.run_forever()
finally:
ipc.close()

View File

@ -116,14 +116,15 @@ class Stabilizer:
def input(self, samples):
spectrum = np.abs(np.fft.fft(samples*blackmanharris(len(samples))))
i = np.argmax(spectrum)
freq = self.freqs[i]
amplitude = spectrum[i]
if amplitude > self.amp_threshold:
freq = self.freqs[i]
tuning = (freq - self.freq_target)*self.k
else:
freq = None
tuning = 0.0
self.cb(spectrum, tuning)
self.cb(spectrum, freq, tuning)
def continuous_unwrap(last_phase, last_phase_unwrapped, p):