www.gusucode.com > fixedpoint 案例源码程序 matlab代码 > fixedpoint/CompareResultsOfCordiccosAndCosFunctionsExample.m
%% Compare Results of cordiccos and cos Functions % Compare the results produced by various iterations of the |cordiccos| % algorithm to the results of the double-precision |cos| 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 cosThRef = cos(double(thRadFxp)); % reference results % Use 12-bit quantized inputs and vary the number % of iterations from 2 to 10. % Compare the fixed-point CORDIC results to the % double-precision trig function results. for niters = 2:2:10 cdcCosTh = cordiccos(thRadFxp, niters); errCdcRef = cosThRef - double(cdcCosTh); end figure hold on axis([0 2*pi -1.25 1.25]); plot(thRadFxp, cosThRef, 'b'); plot(thRadFxp, cdcCosTh, 'g'); plot(thRadFxp, errCdcRef, 'r'); ylabel('cos(\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: cos(double(\Theta))'; cdc_str = sprintf('12-bit CORDIC cosine; 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 cosine of |_theta_| to within 0.005187 of the double-precision cosine result. %%