You are on page 1of 3

% Homework 2: Analysing MZI data

clear all
close all
clc

% data 3
V = 0:0.5:8;
P1 = [1.38, 1.28, 1.17, 1.02, 0.86, 0.64, 0.44, 0.26, 0.12, 0.04, 0.01, ...
0.1, 0.21, 0.32, 0.42, 0.46, 0.48];
P2 = [0.13, 0.24, 0.34, 0.48, 0.65, 0.82, 0.99, 1.14, 1.28, 1.32, 1.28, ...
1.14, 0.79, 0.58, 0.4, 0.34, 0.3];
xp = linspace(0,8);

P_in = 1.5; % determined from sum of powers

%% Sum
alpha_v = 2.*(P1+P2)./P_in - 1;

f_alpha = fit(V',alpha_v','poly2');
alpha_fit = f_alpha.p1*xp.^2 + f_alpha.p2.*xp + f_alpha.p3;

if 1==0
fig = figure('units', 'centimeters', 'pos', [0 0 14 9.26], 'tag', '');
plot(xp, alpha_fit);
title('Measured alpha(V)')
hold on
plot(V, alpha_v, 'o-')
legend('Fit', '\alpha(V)')
xlabel('Voltage [V]')
ylabel('Alpha(V) [-]')
end

%% Difference
% fig = figure('units', 'centimeters', 'pos', [0 0 14 9.26], 'tag', '');
% plot(V, P1-P2, 'o-')

cos_phi = (P1-P2) ./ (P_in .* sqrt(alpha_v));


% hold on
% plot(V, cos_phi, 'o')
% hold on

% fit to sinusoidal
y = cos_phi;
x = V;
yu = max(y);
yl = min(y);
yr = (yu-yl); % Range of ?y?
yz = y-yu+(yr/2);
zx = x(yz .* circshift(yz,[0 1]) <= 0); % Find zero-crossings
per = 2*mean(diff(zx)); % Estimate period
ym = mean(y); % Estimate offset
fitfunc = @(b,x) b(1).*(sin(2*pi*x./b(2) + 2*pi/b(3))) + b(4);
fcn = @(b) sum((fitfunc(b,x) - y).^2);
s = fminsearch(fcn, [yr; per; -1; ym]) ;
% xp = linspace(min(x),max(x));
f2 = fitfunc(s,xp);
% plot(xp,f2, 'r')
hold on
% plot(xp, s(1).*(sin(2*pi*xp./s(2) + 2*pi/s(3))) + s(4))
cos_amp = s(1);
cos_per = s(2);
cos_fre = 1/s(2);
cos_phase = 2*pi/s(3) - 0.5*pi + 2*pi;
cos_offset = s(4);

cos_fit = cos_amp.*(cos(2*pi*xp.*cos_fre + cos_phase)) + cos_offset;


cos_fit_inv = acos(cos_fit);

limit = 60;

fit_2 = fit(xp(1:limit)', cos_fit_inv(1:limit)', 'poly2');


phi_v_fit = fit_2.p1.*xp.^2 + fit_2.p2.*xp + fit_2.p3;
f_phi = fit_2;

%% construct measurement data from fits


P_out_1_fit = 0.25 * P_in * (alpha_fit + 2.*sqrt(alpha_fit) .* ...
cos(phi_v_fit) + 1);
P_out_2_fit = 0.25 * P_in * (alpha_fit - 2.*sqrt(alpha_fit) .* ...
cos(phi_v_fit) + 1);

%% Figures
fig = figure('units', 'centimeters', 'pos', [0 0 14 9.26], 'tag', '');
plot(V, P1, 'o')
hold on
plot(V, P2, 'o')
hold on
plot(xp, P_out_1_fit)
hold on
plot(xp, P_out_2_fit)
legend('P_{out,1}', 'P_{out,2}', 'calculated P_{out,1}', ...
'calculated P_{out,2}')
title('Measurement data and calulated powers')
xlabel('Voltage (V)')
ylabel('Power (mW)')
hgexport(fig, strcat(pwd, '/figures/', 'hw2_3'));

fig = figure('units', 'centimeters', 'pos', [0 0 14 9.26], 'tag', '');


plot(V,P1+P2, 'o')
hold on
plot(V,alpha_v,'o')
hold on
plot(xp, alpha_fit);

legend('P_{out1} + P_{out,2}', '\alpha(V)', 'fit on \alpha(V)')


xlabel('Voltage (V)')
ylabel('Power (mW)')
hgexport(fig, strcat(pwd, '/figures/', 'hw2_1'));
fig = figure('units', 'centimeters', 'pos', [0 0 14 9.26], 'tag', '');
plot(V,P1-P2, 'o')
hold on
plot(V, cos_phi, 'o')
hold on
plot(xp, cos_fit)
hold on
plot(xp, acos(cos_fit))
hold on
plot(xp, phi_v_fit)

legend('P_{out,1} - P_{out,2}', 'cos(\phi(V)+\Delta\phi)',...


'cos(\phi(V)+\Delta\phi) fit', '\phi(V)+\Delta\phi', ...
'\phi(V)+\Delta\phi fit', 'Location', 'northwest')
xlabel('Voltage (V)')
ylabel('Power (mW) or Phase (rad)')
hgexport(fig, strcat(pwd, '/figures/', 'hw2_2'));

You might also like