www.gusucode.com > fixedpoint 案例源码程序 matlab代码 > fixedpoint/CompareResultsOfCordicsinAndSinFunctionsExample.m

    %% Compare Results of cordicsin and sin Functions
% Compare the results produced by various iterations of the |cordicsin|
% algorithm to the results of the double-precision |sin| function.
%%

% Copyright 2015 The MathWorks, Inc.


% Create 1024 points between [0, 2*pi)
stepSize = pi/512;
thRadDbl = 0:stepSize:(2*pi - stepSize);
thRadFxp = sfi(thRadDbl, 12);		% signed, 12-bit fixed point
sinThRef = sin(double(thRadFxp));	% reference results

% Use 12-bit quantized inputs and vary the number of iterations
% from 2 to 10.
% Compare the fixed-point cordicsin function results to the
% results of the double-precision sin function.

for niters = 2:2:10
    cdcSinTh  = cordicsin(thRadFxp,  niters);
    errCdcRef = sinThRef - double(cdcSinTh); 
end

figure
hold on
axis([0 2*pi -1.25 1.25])
plot(thRadFxp, sinThRef,  'b');
plot(thRadFxp, cdcSinTh,  'g');
plot(thRadFxp, errCdcRef, 'r');
ylabel('sin(\Theta)');
gca.XTick = 0:pi/2:2*pi;
gca.XTickLabel = {'0','pi/2','pi','3*pi/2','2*pi'};
gca.YTick = -1:0.5:1;
gca.YTickLabel = {'-1.0','-0.5','0','0.5','1.0'};
ref_str = 'Reference: sin(double(\Theta))';
cdc_str = sprintf('12-bit CORDIC sine; N = %d', niters);
err_str = sprintf('Error (max = %f)', max(abs(errCdcRef)));
legend(ref_str, cdc_str, err_str);
%%
% After 10 iterations, the CORDIC algorithm has approximated the sine of |_theta_| to within 0.005492 of the double-precision sine result.
%%