import numpy as np import matplotlib.pyplot as plot from scipy import signal, constants import argparse import os def rp_raw_to_numpy(rp_raw): # Convert raw buffer strings to numpy arrays buff_string = rp_raw.split(',') return np.array(list(map(float, buff_string))) RP_IP_ADDRS = { "creotech-1": "192.168.1.104", "creotech-2": "192.168.1.105", "mlabs": "rp-f05cc9", } def main(): parser = argparse.ArgumentParser(description="Data plotting tool for Sayma DAC/TTL") parser.add_argument("dir", help="output directory", type=str) parser.add_argument("rps", metavar="NAME:CHANNEL", help="input of the RedPitaya at where data is collected; " "CHANNEL must be 1 or 2; " "NAME must be any of: " + " ".join(list(RP_IP_ADDRS.keys())), type=str, nargs=2) args = parser.parse_args() # Must only compare 2 data name, channel = args.rps[0].split(':') y1_filename = 'rp_{}_y{}_raw.bin'.format(name, channel) name, channel = args.rps[1].split(':') y2_filename = 'rp_{}_y{}_raw.bin'.format(name, channel) if y1_filename == y2_filename: raise ValueError("Both files are the same.") with open(os.path.join(args.dir, y1_filename), 'rb') as f: y1_raw = f.read().decode('utf-8') with open(os.path.join(args.dir, y2_filename), 'rb') as f: y2_raw = f.read().decode('utf-8') if None in [y1_raw, y2_raw]: raise IOError("Raw RP string files cannot be opened.") y1 = rp_raw_to_numpy(y1_raw) y2 = rp_raw_to_numpy(y2_raw) # Define t as an array of timestamps (in seconds) for each sample # (Note that RedPitaya's oscilloscope has a sampling rate @ 125MHz) t = np.arange(y1.shape[0])/125e6 # Generate matrix Y by having arrays y1 and y2 as 2 rows y = np.c_[y1, y2].T # Element-wise multiply an array of cos(2pi*9e6*t) with each row in Y; # Then, downsample the array by 10 as Z z = signal.decimate(y*np.exp(1j*2*np.pi*9e6*t), q=10, ftype="fir", zero_phase=True)[:, 10:] # Downsample Z by 10 again. z = signal.decimate(z, q=10, ftype="fir", zero_phase=True)[:, 10:] # Element-wise multiply Z[0] with the conjugate of Z[1] to get the phase difference (i.e. angle(z0) - angle(z1)), and use the mean value. angle = np.angle(np.mean(z[0]*z[1].conj())) print(angle) # Normalize y1 and y2 for plotting y1 /= abs(y1).max() y2 /= abs(y2).max() plot.plot(y1) plot.plot(y2) plot.show() if __name__ == "__main__": main()