run /users/swinkels/deploy/MatlabVirgoTools/trunk/startup.m %% dt = 16; % analyze time for single step tramp = 1; tmargin = 1; fline = 44; % Hz assert(rem(dt * fline, 1) == 0, 'number of cycles should be integer') %% get data switch 3 case 1 % 1 deg steps gstart = utc2gps(2016, 10, 31, 18, 42, 35); gstop = utc2gps(2016, 10, 31, 18, 50, 0); case 2 % 0.1 deg steps, first attempt gstart = utc2gps(2016, 10, 31, 19, 13, 0); gstop = utc2gps(2016, 10, 31, 19, 27, 0); case 3 % 0.1 deg steps, second attempt gstart = utc2gps(2016, 10, 31, 19, 31, 0); gstop = utc2gps(2016, 10, 31, 19, 45, 0); end [phi, q, t] = getChannels({'LSC_B4_alpha', 'LSC_B4_6MHz_QUAD'}, gstart, gstop); fsample = 1 / (t(2) - t(1)); %% find steps, Acl makes changes at integer seconds % start of positive steps istep = find(diff(diff(phi) == 0) == -1); tstep = round(istep / fsample); assert(max(diff(tstep) - tramp - 2 * tmargin >= dt), 'dt too long') tstart = tstep - dt - tmargin; tstart = [tstart; tstep(end) + tramp + tmargin]; assert(min(tstart) > 0) assert(max(tstart) + dt < t(end)) istart = fsample * tstart; assert(all(phi(istart) == phi(istart + fsample * dt)), 'phase changed during step') %% check that steps are found correctly figure plot(t, phi, t(istart), phi(istart), '.g', ... t(istart + dt*fsample), phi(istart+dt*fsample), '.r'); %% do FFTs to get complex line amplitude f = (0:dt*fsample - 1) / dt; iline = argmin(abs(f - fline)); Z = nan(size(tstart)); Phi = nan(size(tstart)); for i = 1:length(istart) ind = istart(i) + (1:dt*fsample); qq = q(ind); Q = fft(qq); Z(i) = Q(iline); Phi(i) = mean(phi(ind)); end Phi = rad2deg(Phi); %% find demod phase of line % injected frequency is integer, so phase of injected line should be constant p_real = polyfit(Phi, real(Z), 1); p_imag = polyfit(Phi, imag(Z), 1); theta = atan2(p_imag(1), p_real(1)); Zprime = Z * exp(-1j * theta); %% check demod phase rotation figure plot(real(Z), imag(Z), '.-', real(Zprime), imag(Zprime), '.-') legend('raw', sprintf('rotated by %.1f deg', rad2deg(theta))) axis equal %% find zerocrossing p = polyfit(Phi, real(Zprime), 1); phi_zero = -p(2) / p(1); figure plot(Phi, real(Zprime), 'ob', Phi, polyval(p, Phi), '-r', phi_zero, 0, '+k') grid legend('Demodulated amplitude', 'fit', 'zero crossing', 'location', 'northwest') xlabel('LSC\_B4\_alpha (deg)') ylabel('Demodulated line amplitude (a.u.)') title (sprintf('Measurement of %s\nOptimal phase at %.3f deg', ... gps2str(gstart), phi_zero))