From 43e0d4b75a55a6132b34ed56fae4dc7944b9c326 Mon Sep 17 00:00:00 2001 From: wendtalexander Date: Thu, 26 Sep 2024 14:29:47 +0200 Subject: [PATCH] adding calbi for different db --- pyrelacs/repros/calbi.py | 192 ++++++++++++++++++++++++--------------- 1 file changed, 121 insertions(+), 71 deletions(-) diff --git a/pyrelacs/repros/calbi.py b/pyrelacs/repros/calbi.py index 9f837cd..4ffef54 100644 --- a/pyrelacs/repros/calbi.py +++ b/pyrelacs/repros/calbi.py @@ -82,78 +82,128 @@ class Calibration(MccDac): t = np.arange(0, DURATION, 1 / SAMPLERATE) data = AMPLITUDE * np.sin(2 * np.pi * SINFREQ * t) # data = np.concatenate((data, data)) - - stim = self.write_analog( - data, - [0, 0], - SAMPLERATE, - ScanOption=uldaq.ScanOption.EXTTRIGGER, - ) - readout = self.read_analog( - [0, 1], - DURATION, - SAMPLERATE, - ScanOption=uldaq.ScanOption.EXTTRIGGER, - ) - self.diggital_trigger() - signal.signal(signal.SIGSEGV, self.segfault_handler) - log.info(self.ao_device) - ai_status = uldaq.ScanStatus.RUNNING - ao_status = uldaq.ScanStatus.RUNNING - - log.debug( - f"Status Analog_output {ao_status}\n, Status Analog_input {ai_status}" - ) - while (ai_status != uldaq.ScanStatus.IDLE) and ( - ao_status != uldaq.ScanStatus.IDLE - ): - log.debug("Scanning") - time.sleep(0.5) - ai_status = self.ai_device.get_scan_status()[0] - ao_status = self.ao_device.get_scan_status()[0] - - log.debug( - f"Status Analog_output {ao_status}\n, Status Analog_input {ai_status}" - ) + db_values = [0.0, -5.0, -8.5, -10.0] + colors = ["red", "blue", "black", "green"] + colors_in = ["lightcoral", "lightblue", "grey", "lightgreen"] fig, axes = plt.subplots(2, 2, sharex="col") - channel1 = np.array(readout[::2]) - channel2 = np.array(readout[1::2]) - beat = channel1 + channel2 - beat_square = beat**2 - - f, powerspec = welch(beat, fs=SAMPLERATE) - powerspec = decibel(powerspec) - - f_sq, powerspec_sq = welch(beat_square, fs=SAMPLERATE) - powerspec_sq = decibel(powerspec_sq) - peaks = find_peaks(powerspec_sq, prominence=20)[0] - - f_stim, powerspec_stim = welch(channel1, fs=SAMPLERATE) - powerspec_stim = decibel(powerspec_stim) - - f_in, powerspec_in = welch(channel2, fs=SAMPLERATE) - powerspec_in = decibel(powerspec_in) - - axes[0, 0].plot(t, channel1, label="Readout Channel0") - axes[0, 0].plot(t, channel2, label="Readout Channel1") - - axes[0, 1].plot(f_stim, powerspec_stim, label="powerspec Channel0") - axes[0, 1].plot(f_in, powerspec_in, label="powerspec Channel2") - axes[0, 1].set_xlabel("Freq [HZ]") - axes[0, 1].set_ylabel("dB") - - axes[1, 0].plot(t, beat, label="Beat") - axes[1, 0].plot(t, beat**2, label="Beat squared") - axes[1, 0].legend() - - axes[1, 1].plot(f, powerspec) - axes[1, 1].plot(f_sq, powerspec_sq) - axes[1, 1].scatter(f_sq[peaks], powerspec_sq[peaks]) - axes[1, 1].set_xlabel("Freq [HZ]") - axes[1, 1].set_ylabel("dB") - axes[0, 0].legend() - embed() - exit() + for i, db_value in enumerate(db_values): + self.set_attenuation_level(db_channel1=db_value) + stim = self.write_analog( + data, + [0, 0], + SAMPLERATE, + ScanOption=uldaq.ScanOption.EXTTRIGGER, + ) + readout = self.read_analog( + [0, 1], + DURATION, + SAMPLERATE, + ScanOption=uldaq.ScanOption.EXTTRIGGER, + ) + self.diggital_trigger() + signal.signal(signal.SIGSEGV, self.segfault_handler) + log.info(self.ao_device) + ai_status = uldaq.ScanStatus.RUNNING + ao_status = uldaq.ScanStatus.RUNNING + + log.debug( + f"Status Analog_output {ao_status}\n, Status Analog_input {ai_status}" + ) + while (ai_status != uldaq.ScanStatus.IDLE) and ( + ao_status != uldaq.ScanStatus.IDLE + ): + # log.debug("Scanning") + time.time_ns() + ai_status = self.ai_device.get_scan_status()[0] + ao_status = self.ao_device.get_scan_status()[0] + + self.write_bit(channel=0, bit=0) + log.debug( + f"Status Analog_output {ao_status}\n, Status Analog_input {ai_status}" + ) + channel1 = np.array(readout[::2]) + channel2 = np.array(readout[1::2]) + beat = channel1 + channel2 + beat_square = beat**2 + + f, powerspec = welch(beat, fs=SAMPLERATE) + powerspec = decibel(powerspec) + + f_sq, powerspec_sq = welch(beat_square, fs=SAMPLERATE) + powerspec_sq = decibel(powerspec_sq) + peaks = find_peaks(powerspec_sq, prominence=20)[0] + + f_stim, powerspec_stim = welch(channel1, fs=SAMPLERATE) + powerspec_stim = decibel(powerspec_stim) + + f_in, powerspec_in = welch(channel2, fs=SAMPLERATE) + powerspec_in = decibel(powerspec_in) + + axes[0, 0].plot( + t, + channel1, + label=f"{db_value} Readout Channel0", + color=colors[i], + ) + axes[0, 0].plot( + t, + channel2, + label=f"{db_value} Readout Channel1", + color=colors_in[i], + ) + + axes[0, 1].plot( + f_stim, + powerspec_stim, + label=f"{db_value} powerspec Channel0", + color=colors[i], + ) + axes[0, 1].plot( + f_in, + powerspec_in, + label=f"{db_value} powerspec Channel2", + color=colors_in[i], + ) + axes[0, 1].set_xlabel("Freq [HZ]") + axes[0, 1].set_ylabel("dB") + + axes[1, 0].plot( + t, + beat, + label="Beat", + color=colors[i], + ) + axes[1, 0].plot( + t, + beat**2, + label="Beat squared", + color=colors_in[i], + ) + axes[1, 0].legend() + + axes[1, 1].plot( + f, + powerspec, + color=colors[i], + ) + axes[1, 1].plot( + f_sq, + powerspec_sq, + color=colors_in[i], + label=f"dB {db_value}, first peak {np.min(f_sq[peaks])}", + ) + axes[1, 1].scatter( + f_sq[peaks], + powerspec_sq[peaks], + color="maroon", + ) + axes[1, 1].set_xlabel("Freq [HZ]") + axes[1, 1].set_ylabel("dB") + axes[0, 0].legend() + axes[1, 1].legend() + plt.show() + self.set_analog_to_zero() + self.disconnect_dac() def decibel(power, ref_power=1.0, min_power=1e-20):