for i=1:len
if (i==1)
delta_theta(i)=theta(i);
else
%求相位差值
tmp1=abs(theta(i)-theta(i-1));
tmp2=abs(pi-theta(i)-theta(i-1));
tmp3=abs(-theta(i)-theta(i-1)-pi);
%谁的差值跟上一次差值最接近就是哪一个
if (abs(tmp1-delta_theta(i-1))<abs(tmp2-delta_theta(i-1)) && abs(tmp1-delta_theta(i-1))<abs(tmp3-delta_theta(i-1)))
delta_theta(i)=tmp1;
else if (abs(tmp2-delta_theta(i-1))<abs(tmp3-delta_theta(i-1)))
delta_theta(i)=tmp2;
else
delta_theta(i)=tmp3;
end
end
end
end