From 570149c2ee5b4102b721f3db7ce6e54d18b10995 Mon Sep 17 00:00:00 2001 From: hackens-bot Date: Sun, 15 Dec 2024 20:46:29 +0100 Subject: [PATCH] loopback test --- xfer_tracer/script.py | 77 +++++++++++-------------------------------- xfer_tracer/shell.nix | 3 +- 2 files changed, 21 insertions(+), 59 deletions(-) diff --git a/xfer_tracer/script.py b/xfer_tracer/script.py index c0c2d24..f0e63fc 100755 --- a/xfer_tracer/script.py +++ b/xfer_tracer/script.py @@ -27,69 +27,30 @@ def audio_callback(in_data, frame_count, time_info, status): return (outbytes, pa.paContinue) paudio = pa.PyAudio() -stream = paudio.open(format=pa.paFloat32, +output_strm = paudio.open(format=pa.paFloat32, channels=1, rate=48000, output=True, stream_callback=audio_callback) +output_strm.start_stream() -def do_frequency(scaled_f, osci, hscale): - scaled_frequency = scaled_f - time.sleep(0.1) - osci.write("acquire:state run") # Restart acquisition sequence - time.sleep(10 * hscale) +input_strm = paudio.open(format=pa.paFloat32, + channels=1, + rate=48000, + input=True) +input_strm.stop_stream() - osci.write("data:source CH1") - A_samps = osci.query_binary_values("curve?",datatype='b',container=numpy.array) - A_samps = A_samps.astype(np.float64) +def do_frequency(scaled_f): + scaled_frequency = scaled_f + time.sleep(0.2) # Stall for latency/transitory behavior + input_strm.start_stream() + L = input_strm.read(int(48000 * 0.1)) # We get 1/sqrt(n) noise rejection and 1/n spurious tone rejection + input_strm.stop_stream() + return np.frombuffer(L, dtype=np.float32) - osci.write("data:source CH2") # For now, do not adjust for zeros in the resp. - B_samps = osci.query_binary_values("curve?",datatype='b',container=numpy.array) - B_samps = B_samps.astype(np.float64) +import matplotlib.pyplot as plt +L = do_frequency(440.0/48000) +print(L) +plt.plot(L) +plt.show() - A_samps = np.rfft(A_samps) # No window, because of the precise frequency - B_samps = np.rfft(B_samps) - - # We have (10 * hscale)/Tcurv periods - return B_samps[np.round((10 * hscale) * (scaled_f * 4800))] - /A_samps[np.round((10 * hscale) * (scaled_f * 4800))] - -def find_oscillo(): - rm = pyvisa.ResourceManager() - R = rm.list_resources("USB?*") - if R.empty(): - print("No device found") - exit(1) - return rm.open_resource(R[0]) - -osci = find_oscillo() -print(osci.query("*IDN?")) - -osci.write("*RST") -time.sleep(5) -osci.write("horizontal:main:scale 5.0e-4") - -osci.write("ch1:scale 1.0e-1") -osci.write("ch1:bandwidth ON") -osci.write("ch1:coupling AC") -osci.write("select:ch1 1") - -osci.write("ch2:scale 5.0e-2") -osci.write("ch2:bandwidth ON") -osci.write("ch2:coupling AC") -osci.write("select:ch2 1") - -# 8-bit data -osci.write("data:source CH1") -osci.write("data:encdg ascii") - -osci.write("trigger:main:edge:source CH1") -osci.write("trigger:main:mode NORMAL") -osci.write("trigger:main:type EDGE") - -osci.write("acquire:mode average") -osci.write("acquire:numavg 128") -osci.write("acquire:stopafter sequence") -osci.write("acquire:state RUN") - -print(osci.query("curve?")) diff --git a/xfer_tracer/shell.nix b/xfer_tracer/shell.nix index 7f41241..b504312 100644 --- a/xfer_tracer/shell.nix +++ b/xfer_tracer/shell.nix @@ -10,6 +10,7 @@ pkgs.mkShell { ps.psutil ps.pyusb ps.numpy - ps.pyaudio])) + ps.pyaudio + ps.matplotlib])) ]; }