Improper cosine wave using cordic algorithm?

z1=0:(2*pi/100):2*pi; y1=zeros(1,length(z1)); x1=ones(1,length(z1))/1.646760255;
c1=zeros(1,length(z1)); CORDIC_SIN=zeros(1,length(z1)); CORDIC_COS=zeros(1,length(z1));
a1=zeros(1,length(z1)); b1=zeros(1,length(z1));
inpLUT=zeros(1,20);
inpLUT(1)=atan(1);
for i=1:1:21
inpLUT(i+1)=atan(2^-i);
end
n=10;
for i=1:1:length(z1)
a1=x1(i);
b1=y1(i);
c1=z1(i);
[a1,b1,c1]=cordic_rotation_kernel(a1,b1,c1, inpLUT, n);
CORDIC_SIN(i)=b1;
CORDIC_COS(i)=a1;
end
theta=0:(2*pi/100):2*pi;
sin_out=sin(theta);
cos_out=cos(theta);
Abs_error_sine=abs(CORDIC_SIN-sin_out);
Abs_error_cos=abs(CORDIC_COS-cos_out);
figure(3)
plot(theta,CORDIC_SIN,theta,CORDIC_COS,theta,sin_out,theta,cos_out);
xlabel('Angle (in radians');
ylabel('Amplitude');
legend('Computed cordic sine','Computed cordic cosine','sin out','cos out');
figure(4)
plot(theta,Abs_error_sine,theta,Abs_error_cos);
xlabel('Angle (in radians');
ylabel('Error');
legend('Sine error','Cosine error');
function [x, y, z] = cordic_rotation_kernel(x, y, z,inpLUT, n)
% Perform CORDIC rotation kernel algorithm for N iterations.
xtmp = x;
ytmp = y;
if z > 0 && z <= pi/2
z=z;
end
if z > pi/2 && z <= pi
z=pi-z;
end
if z > pi && z <= (3*pi)/2
z=pi-z;
end
if z > (3*pi)/2 && z <= 2*pi
z=z-2*pi;
end
for idx = 1:n
if z < 0
z(:) = accumpos(z, inpLUT(idx));
x(:) = accumpos(x, ytmp);
y(:) = accumneg(y, xtmp);
else
z(:) = accumneg(z, inpLUT(idx));
x(:) = accumneg(x, ytmp);
y(:) = accumpos(y, xtmp);
end
xtmp = bitsra(x, idx); % bit-shift-right for multiply by 2^(-idx)
ytmp = bitsra(y, idx); % bit-shift-right for multiply by 2^(-idx)
end
end
Above is the CORDIC function that I have used for creating a sine and a cosine wave. I am trying to compare the inbuilt function with the one that I am attempting to implement. However, from pi to 3*pi , the values of cosine wave tends to be inverted. Kindly do tell me how to rectify this issue?

Réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by