function [sA,sB,sC] = spacevecpwm(Va_ref,Vb_ref,Vc_ref,fs,CurrentTime,Vdc)
% Three phase space vector pulse width modulation using generalised multiphase space vector approach 
% This file contains source code written by Rohit Chandan (Copyright (c) 2013). Please read the copyright notice in file license_rohitchandan.txt

Ts=1/fs;                     % PWM sampling time period; fs - PWM frequency
alp=2*pi/3;                  % phase diff 120 degree
% Vm=sqrt(2)*Vsrms;
% Va_ref=Vm*sin(2*pi*f1*CurrentTime);
% Vb_ref=Vm*sin(2*pi*f1*CurrentTime+2*pi/3);
% Vc_ref=Vm*sin(2*pi*f1*CurrentTime+4*pi/3);
Vtri=Ts*(0.5-2*asin(sin(2*pi*CurrentTime/Ts+pi/2))/(2*pi)); % triangular wave generation

% three phase to two phase transformation (clark transformation)
Vds=(Va_ref+Vb_ref*cos(alp)+ Vc_ref*cos(2*alp));
Vqs=(Vb_ref*sin(alp)+ Vc_ref*sin(2*alp));
% sector indentification
tht=atan2(Vqs,Vds);
if tht >= 0
    theta=tht;
else
    theta=2*pi+tht;
end

if theta>=0 && theta<alp/2
    Sn=1;
elseif theta>=alp/2 && theta<alp
    Sn=2;
elseif theta>=alp && theta<3/2*alp
    Sn=3;
elseif theta>=3/2*alp && theta<2*alp
    Sn=4;
elseif theta>=2*alp && theta<5/2*alp
    Sn=5;
else Sn=6;
end

% selection of switching vector for each sector
if Sn==1
    v1=[1 ;0 ;0];  %4;
    v2=[1 ;1 ;0];  %6;
    v0=[1 ;1 ;1];
elseif Sn==2
    v1=[1; 1; 0];  %6;
    v2=[0; 1; 0];  %2;
    v0=[1; 1; 1];
elseif Sn==3
    v1=[0; 1; 0];  %2;
    v2=[0; 1; 1];  %3;
    v0=[1; 1; 1];
elseif Sn==4
    v1=[0; 1; 1];  %3;
    v2=[0; 0; 1];  %1
    v0=[1; 1; 1];
elseif Sn==5
    v1=[0; 0; 1];   %1;
    v2=[1; 0; 1];   %5;
    v0=[1; 1; 1];
else
    v1=[1; 0; 1];  %5;
    v2=[1; 0; 0];  %4;
    v0=[1; 1; 1];  %0;
end

u=Sn;
% using volt sec balance calcution of active timing vector
An_inv=(Ts/(sin(pi/3)*Vdc))*[sin(u*pi/3) -cos(u*pi/3) ; -sin((u-1)*pi/3) cos((u-1)*pi/3) ];
Vref=[Vds; Vqs];

tn=An_inv*Vref;

t0by2=(Ts-tn(1)-tn(2))/2;
t120=[tn(1); tn(2); t0by2];

V120=[v1 v2 v0];
% calculation for tga gating time period for each leg
tgx = V120*t120;

% generation of switching function SA SB SC
if tgx(1)>= Vtri
    sA=1;
else
    sA=-1;  
end
if tgx(2)>= Vtri
    sB=1;   
else
    sB=-1;   
end
if tgx(3)>= Vtri
    sC=1;
else
    sC=-1;
end

