from __future__ import division import matplotlib.pyplot as plt import numpy as np import sys sys.path.append('/users/swinkels/deploy/PythonVirgoTools/trunk/src') from virgotools import * log_to_console(level='INFO') cm_start('tune_phase_script') nframe = 10 # concat some frames by hand, need some dedicated function for this DC = [] I = [] Q = [] # connect online and get data with ConnectOnline('FbmMainUsers', 'V1:LSC*') as connection: for i in range(nframe): frame = connection.get_next_frame() print 'Got frame with gps = ', frame.gps DC.append(frame.LSC_B2_DC) I.append(frame.LSC_B2_6MHz_I) Q.append(frame.LSC_B2_6MHz_Q) DC = np.concatenate(DC) I = np.concatenate(I) Q = np.concatenate(Q) # quick and dirty derivation with some averaging over 2*dn samples # don't care about normalization dn = 100 pad = np.zeros(dn) dI = I[2*dn:] - I[:-2*dn] dI = np.concatenate((pad, dI, pad)) dQ = Q[2*dn:] - Q[:-2*dn] dQ = np.concatenate((pad, dQ, pad)) # find best fringe ires = 2 * dn + DC[2*dn:-2*dn].argmin() # calculate phase based on derivatives. Sign is arbitrary, will be absorbed by loop gain, # so can use atan instead of atan2 phase = np.arctan(dQ[ires] / dI[ires]) print 'measured phase: %.3f rad (%.2f degrees)' % (phase, np.degrees(phase)) print 'setting demodulation phase to %.3f' % (-phase) LSC = AclServer('LSC_Acl_TEST') LSC.B2_alpha = -phase # quick plot t = np.arange(len(DC)) / 10000. plt.subplot(311) plt.plot(t, DC, t[ires], DC[ires], '.r') plt.title('DC') plt.subplot(312) plt.plot(t, I, t, Q) plt.legend(('I', 'Q')) plt.title('demodulated signals') plt.subplot(313) plt.plot(t, dI, t, dQ, t[ires], dI[ires], '.r', t[ires], dQ[ires], '.r') plt.legend(('dI', 'dQ')) plt.title('derivatives') plt.show() print 'finished!'