% lcl_iv12.m                                                   28-2-15
% Return i1, i2, v1 and v2 at the angle specified by x*pi (rads).
% v1 leads v2 by thi*pi. Amplde Modulation = m1,m2*pi rads. R1,2 = 0.
% The relative reactances of L1,L2,C1,L3 at fs are k1, k2, k3 and k4.

function [iv12] = lcl_iv12(vdc1,vdc2,phi,m1,m2,xd,k1,k2,k3,k4,tr,x)

n = (1:2:1123)';                                    % odd series, 562 rows
x = pi*x;

% Calculate the voltage phasors of each harmonic
v1 = 4*vdc1/pi./n.*sin(n*m1*pi/2);                          % REFERENCE                           
v2 = 4*tr*vdc2/pi./n.*sin(n*m2*pi/2).*exp(-1i*n*phi*pi);

% The solutions from lcl_netsolv_gen.m for r1 = r2 = 0 are :
d = xd*n.*(k1*k3 + k2*k3 - k1*k2*n.^2);             % denom
i1 = 1i*(k2*v1.*n.^2 - k3*v1 + k3*v2)./d;           % into LCL network
i2 = -1i*(k1*v2.*n.^2 + k3*v1 - k3*v2)./d;          % out of LCL network
i4 = i2 + 1i*v2./(n*k4*xd);                         % into trfmr primary

i1m = abs(i1); i1p = angle(i1);           
i1x = sum(i1m.*cos(i1p + n*x));     
i4m = abs(i4); i4p = angle(i4);           
i4x = sum(i4m.*cos(i4p + n*x));     

v1m = abs(v1); v1p = angle(v1);           
v1x = sum(v1m.*cos(v1p + n*x));     
v2m = abs(v2); v2p = angle(v2);           
v2x = sum(v2m.*cos(v2p + n*x));     

iv12 = [i1x i4x v1x v2x];
end

% Tested.  Results agree with lcl_dab.m (7201/12000 => 1.2 pi)
% r = lcl_iv12(765,432,0.6,0.7,0.8,56,0.8,0.9,1.1,5,2,1.2) =>
%   r =  -17.6430  -19.2686 -764.9692 -432.3365
