%**************************************************************************
%This function calculates the Stokes parameters from the electric field.
%
%Input:
%ex_fund, spontaneous complex Ex(omega_fund) for a single electron (V/m)
%ey_fund, spontaneous complex Ey(omega_fund) for a single electron (V/m)
%
%Output:
%s0 to s3, Stokes parameters (V/m)^2
%p1 to p3, polarization rates
%
%Zachary Wolf
%8/4/10

function [s0, s1, s2, s3, p1, p2, p3] = rad_calc_stokes(ex_fund, ey_fund)

%Method
%See J. Clarke, The Science and Technology of Undulators and Wigglers, p. 92.
%S0 = Ix + Iy = I45 + I135 = Ir + Il
%S1 = Ix - Iy
%S2 = I45 - I135
%S3 = Ir - Il
%S0 = Ex Ex* + Ey Ey*
%S1 = Ex Ex* - Ey Ey*
%S2 = Ex Ey* + Ex* Ey
%S3 = i(Ex* Ey - Ex Ey*)
%P1 = S1 / S0
%P2 = S2 / S0
%P3 = S3 / S0

%Correction
%Sign error above
%S3 = i(Ex Ey* - Ex* Ey)

%Simplify notation
ex = ex_fund;
ey = ey_fund;

%Calculations
s0 = ex * conj(ex) + ey * conj(ey);
s1 = ex * conj(ex) - ey * conj(ey);
s2 = ex * conj(ey) + ey * conj(ex);
s3 = 1i * (ex * conj(ey) - ey * conj(ex));
p1 = s1 / s0;
p2 = s2 / s0;
p3 = s3 / s0;

%Done
end

