OpenCores
URL https://opencores.org/ocsvn/yac/yac/trunk

Subversion Repositories yac

[/] [yac/] [trunk/] [c_octave/] [cordic_iterative_test.m] - Blame information for rev 13

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 feddischso
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
%%%%                                                                    %%%%
3
%%%%  File           : cordic_iterative_test.m                          %%%%
4
%%%%  Project        : YAC (Yet Another CORDIC Core)                    %%%%
5
%%%%  Creation       : Feb. 2014                                        %%%%
6
%%%%  Limitations    :                                                  %%%%
7
%%%%  Platform       : Linux, Mac, Windows                              %%%%
8
%%%%  Target         : Octave, Matlab                                   %%%%
9
%%%%                                                                    %%%%
10
%%%%  Author(s):     : Christian Haettich                               %%%%
11
%%%%  Email          : feddischson@opencores.org                        %%%%
12
%%%%                                                                    %%%%
13
%%%%                                                                    %%%%
14
%%%%%                                                                  %%%%%
15
%%%%                                                                    %%%%
16
%%%%  Description                                                       %%%%
17
%%%%        Script to test/analyze the cordic C implementation          %%%%
18
%%%%        and to generate stimulus data for RTL simulation.           %%%%
19
%%%%        This created data is used to ensure, that the C             %%%%
20
%%%%        implementation behaves the same than the VHDL               %%%%
21
%%%%        implementation.                                             %%%%
22
%%%%                                                                    %%%%
23
%%%%        Three tests are implemented:                                %%%%
24
%%%%          - Random test values                                      %%%%
25
%%%%          - Linear increasing values                                %%%%
26
%%%%          - Limit values                                            %%%%
27
%%%%                                                                    %%%%
28
%%%%                                                                    %%%%
29
%%%%        Please do  'mex cordic_iterative.c' to create               %%%%
30
%%%%        the cordic_iterative.mex.                                   %%%%
31
%%%%                                                                    %%%%
32
%%%%%                                                                  %%%%%
33
%%%%                                                                    %%%%
34
%%%%  TODO                                                              %%%%
35
%%%%        The linear test is not complete                             %%%%
36
%%%%                                                                    %%%%
37
%%%%                                                                    %%%%
38
%%%%                                                                    %%%%
39
%%%%                                                                    %%%%
40
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41
%%%%                                                                    %%%%
42
%%%%                  Copyright Notice                                  %%%%
43
%%%%                                                                    %%%%
44
%%%% This file is part of YAC - Yet Another CORDIC Core                 %%%%
45
%%%% Copyright (c) 2014, Author(s), All rights reserved.                %%%%
46
%%%%                                                                    %%%%
47
%%%% YAC is free software; you can redistribute it and/or               %%%%
48
%%%% modify it under the terms of the GNU Lesser General Public         %%%%
49
%%%% License as published by the Free Software Foundation; either       %%%%
50
%%%% version 3.0 of the License, or (at your option) any later version. %%%%
51
%%%%                                                                    %%%%
52
%%%% YAC is distributed in the hope that it will be useful,             %%%%
53
%%%% but WITHOUT ANY WARRANTY; without even the implied warranty of     %%%%
54
%%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  %%%%
55
%%%% Lesser General Public License for more details.                    %%%%
56
%%%%                                                                    %%%%
57
%%%% You should have received a copy of the GNU Lesser General Public   %%%%
58
%%%% License along with this library. If not, download it from          %%%%
59
%%%% http://www.gnu.org/licenses/lgpl                                   %%%%
60
%%%%                                                                    %%%%
61
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62
function cordic_iterative_test( )
63
 
64
 
65
 
66
% global flags/values, they are static
67
% through the whole script and defined below
68
global C_FLAG_VEC_ROT C_FLAG_ATAN_3 C_MODE_CIRC C_MODE_LIN C_MODE_HYP
69
global XY_WIDTH ANGLEWIDTH GUARDBITS RM_GAIN
70
 
71
 
72 7 feddischso
cordic_iterative_setup
73 2 feddischso
 
74 7 feddischso
% open output file
75
tb_fid = fopen( TB_FILE, 'w' );
76 2 feddischso
 
77
% Number of tests, which are run
78
N_TESTS    = 10000;
79
 
80
 
81
%
82
% run test, which uses random values
83
run_random_test( N_TESTS, tb_fid );
84
%
85
% run tests, which test limits
86
run_limit_test( tb_fid );
87
%
88
% run linear value test
89
run_linear_test( 1000, tb_fid );
90
 
91
% close file
92
if tb_fid > 0
93
    fclose( tb_fid );
94
end
95
 
96
end
97
 
98
 
99
 
100
 
101
function run_limit_test( tb_fid )
102
%RUN_LIMIT_TEST Test the range limit
103
%
104
% run_limit_test( fid )
105
%
106
% This function is used to generate a test pattern
107
% with values, which are at the range limit.
108
% This values are then processed by the fixed-point YAC
109
% implementation. All input and outputs are logged into
110
% a testbench pattern file.
111
%
112
% The argument fid is the file-descriptor of the testbench pattern
113
% file.
114
%
115
 
116
 
117
data_a = [ 0 1 0 1 -1  0 -1  1 -1 ];
118
data_b = [ 0 0 1 1  0 -1 -1 -1  1 ];
119
 
120
data_c = [ 0 0 0 0  0  0  0  0  0 ...
121
           1 1 1 1  1  1  1  1  1 ...
122
           -1 -1 -1 -1 -1 -1 -1 -1 -1 ];
123
 
124
data_d = data_a * pi;
125
 
126 4 feddischso
data_a_div = [ 0.5 ,1 -0.5, -1, -0.5, -1 ];
127
data_b_div = [ 1   ,1,  1,   1,   -1, -1 ];
128
 
129
[ ~, ~, atan_err, abs_err, it_1 ]   = ccart2pol( data_a, data_b, tb_fid );
130
[ ~, ~, sin_err,  cos_err, it_2 ]   = cpol2cart( data_d, data_b, tb_fid );
131
[ ~, ~, x_err, y_err, it_3 ]        = crot( [ data_a, data_a, data_a], ...
132
                                            [ data_b, data_b, data_b], ...
133
                                              data_c, tb_fid );
134
[ ~, div_err, it_4 ]                = cdiv( data_a_div, data_b_div, tb_fid );
135
[ ~, mul_err, it_5 ]                = cmul( data_a, data_b, tb_fid  );
136 7 feddischso
 
137
print_result(    ...
138 4 feddischso
    atan_err,   it_1, ...
139
    abs_err,    it_1, ...
140
    sin_err,    it_2, ...
141
    cos_err,    it_2, ...
142
    x_err,      it_3, ...
143
    y_err,      it_3, ...
144
    div_err,    it_4, ...
145
    mul_err,    it_5, ...
146
    0,  0, ...
147
    0,  0, ...
148
    0,  0, ...
149
    0,  0, ...
150
    'Limit Value Test' );
151
 
152 2 feddischso
end
153
 
154
 
155
 
156 4 feddischso
function run_linear_test( N_TESTS, tb_fid )
157
%RUN_LINEAR_TEST Generates a linear test pattern
158 2 feddischso
%
159 4 feddischso
% run_linear_test( N, fid )
160 2 feddischso
%
161 4 feddischso
% This function is used to generate linear increasing test
162
% values.
163
% These values are then processed by the fixed-point YAC
164
% implementation. All input and outputs are logged into
165
% a testbench pattern file. In addition, the result is plotted.
166
%
167
% NOTE: only the hyperbolic functions are processed at the moment.
168
% This function needs to be extended in future.
169
%
170
%
171 2 feddischso
% The argument fid is the file-descriptor of the testbench pattern
172
% file. The argument N defines the number of values, which are processed.
173
%
174
%
175 3 feddischso
 
176 4 feddischso
data_a_h = ones( 1, N_TESTS );
177
data_b_h = linspace( -1, 1, N_TESTS ) * 0.78;
178
data_c_h = linspace( -1, 1, N_TESTS );
179
[ atanh_res, sqrt_res, atanh_err, sqrt_err, it_6 ] = catanh( data_a_h, data_b_h, tb_fid );
180
[ sinh_res, cosh_res, sinh_err, cosh_err, it_7 ]   = csinhcosh( data_c_h, tb_fid );
181
 
182
 
183
figure; plot( data_b_h, atanh_res ); title( 'atanh' );
184
figure; plot( data_b_h, atanh_err ); title( 'atanh-error' );
185
figure; plot( data_c_h, sinh_res, data_c_h, cosh_res ); title( 'sinh and cosh' );
186
figure; plot( data_c_h, sinh_err, data_c_h, cosh_err ); title( 'sinh and cosh errors' );
187 7 feddischso
 
188
 
189 4 feddischso
end
190
 
191
 
192
 
193
function run_random_test( N_TESTS, tb_fid )
194
%RUN_RANDOM_TEST Generates a random test pattern
195
%
196
% run_random_test( N, fid )
197
%
198
% This function is used to generate random test
199
% values (uniform distributed).
200
% These values are then processed by the fixed-point YAC
201
% implementation. All input and outputs are logged into
202
% a testbench pattern file.
203
%
204
%
205
% The argument fid is the file-descriptor of the testbench pattern
206
% file. The argument N defines the number of values, which are processed.
207
%
208
%
209 3 feddischso
data_a = -1 + 2 .* rand( 1, N_TESTS );
210
data_b = -1 + 2 .* rand( 1, N_TESTS );
211 4 feddischso
data_c = -1 + 2 .* rand( 1, N_TESTS );
212
data_d = -pi + 2*pi .* rand( 1, N_TESTS );
213 2 feddischso
% adapat data for division
214
data_a_div = data_a;
215
data_b_div = data_b;
216
swap_div   = ( data_b ./ data_a ) >= 2 | ( data_b ./ data_a ) < -2 ;
217
data_a_div( swap_div ) = data_b( swap_div );
218
data_b_div( swap_div ) = data_a( swap_div );
219
 
220
data_a_h   = ones( size( data_a ) );
221
data_b_h   = data_b .* 0.80694; %0.78;
222
 
223
 
224
 
225
[ ~, ~, atan_err, abs_err, it_1 ]   = ccart2pol( data_a, data_b, tb_fid );
226
[ ~, ~, sin_err,  cos_err, it_2 ]   = cpol2cart( data_d, data_b, tb_fid );
227
[ ~, ~, x_err, y_err, it_3 ]        = crot( data_a, data_b, data_c, tb_fid );
228
[ ~, div_err, it_4 ]                = cdiv( data_a_div, data_b_div, tb_fid );
229
[ ~, mul_err, it_5 ]                = cmul( data_a, data_b, tb_fid  );
230
[ ~, ~, atanh_err, sqrt_err, it_6 ] = catanh( data_a_h, data_b_h, tb_fid );
231
[ ~, ~, sinh_err, cosh_err, it_7 ]  = csinhcosh( data_a, tb_fid );
232
 
233
 
234 7 feddischso
print_result_header( 'Random Test Result' );
235
print_result2( 'atan', it_1, atan_err, data_a, data_b );
236
print_result2( 'abs ', it_1, abs_err,  data_a, data_b );
237
print_result2( 'sin ', it_2, sin_err,  data_a, data_b );
238
print_result2( 'cos ', it_2, cos_err,  data_a, data_b );
239
 
240
 
241
 
242
print_result(  atan_err,   it_1, ...
243
               abs_err,    it_1, ...
244
               sin_err,    it_2, ...
245
               cos_err,    it_2, ...
246
               x_err,      it_3, ...
247
               y_err,      it_3, ...
248
               div_err,    it_4, ...
249
               mul_err,    it_5, ...
250
               atanh_err,  it_6, ...
251
               sqrt_err,   it_6, ...
252
               sinh_err,   it_7, ...
253
               cosh_err,   it_7, ...
254
               'Random Value Test' );
255
 
256
 
257 2 feddischso
end
258
 
259 7 feddischso
function print_result_header( title )
260
 printf( ' ___________________________________________________________________\n' );
261
 fprintf( '                  %s\n', title);
262
 fprintf( ' -----+-------------------+--------------------+-------------------\n'   );
263
end
264 2 feddischso
 
265 7 feddischso
function print_result2( msg, it, err, a, b )
266
 
267
[ verr, ierr ] = max( err );
268
fprintf( '%s |  %.14f  | (%.14f %.14f) | %.14f  | %.5f \n', msg, verr, a( ierr ), b( ierr ), mean( err ), max( it ) )
269
 
270
end
271
 
272
 
273
function print_result( ...
274 2 feddischso
    atan_err,   atan_it,    ...
275
    abs_err,    abs_it,     ...
276
    sin_err,    sin_it,     ...
277
    cos_err,    cos_it,     ...
278
    x_err,      x_it,       ...
279
    y_err,      y_it,       ...
280
    div_err,    div_it,     ...
281
    mul_err,    mul_it,     ...
282
    atanh_err,  atanh_it,   ...
283
    sqrt_err,   sqrt_it,    ...
284
    sinh_err,   sinh_it,    ...
285
    cosh_err,   cosh_it,    ...
286
    title )
287
 
288
fprintf( ' ___________________________________________________________________\n' );
289
fprintf( '                  %s\n', title);
290
fprintf( ' -----+-------------------+--------------------+-------------------\n'   );
291
fprintf( '      |     max error     |   mean error       |  max iterations  \n'   );
292
fprintf( ' atan | % .14f | % .14f  | %.5f \n', max( atan_err  ), mean( atan_err  ), max( atan_it   ) );
293
fprintf( ' abs  | % .14f | % .14f  | %.5f \n', max( abs_err   ), mean( abs_err   ), max( abs_it    ) );
294
fprintf( ' sin  | % .14f | % .14f  | %.5f \n', max( sin_err   ), mean( sin_err   ), max( sin_it    ) );
295
fprintf( ' cos  | % .14f | % .14f  | %.5f \n', max( cos_err   ), mean( cos_err   ), max( cos_it    ) );
296
fprintf( ' x    | % .14f | % .14f  | %.5f \n', max( x_err     ), mean( x_err     ), max( x_it      ) );
297
fprintf( ' y    | % .14f | % .14f  | %.5f \n', max( y_err     ), mean( y_err     ), max( y_it      ) );
298
fprintf( ' div  | % .14f | % .14f  | %.5f \n', max( div_err   ), mean( div_err   ), max( div_it    ) );
299
fprintf( ' mul  | % .14f | % .14f  | %.5f \n', max( mul_err   ), mean( mul_err   ), max( mul_it    ) );
300
fprintf( ' atanh| % .14f | % .14f  | %.5f \n', max( atanh_err ), mean( atanh_err ), max( atanh_it  ) );
301
fprintf( ' sqrt | % .14f | % .14f  | %.5f \n', max( sqrt_err  ), mean( sqrt_err  ), max( sqrt_it   ) );
302
fprintf( ' sinh | % .14f | % .14f  | %.5f \n', max( sinh_err  ), mean( sinh_err  ), max( sinh_it   ) );
303
fprintf( ' cosh | % .14f | % .14f  | %.5f \n', max( cosh_err  ), mean( cosh_err  ), max( cosh_it   ) );
304
 
305
 
306
 
307 3 feddischso
end
308 2 feddischso
 
309 3 feddischso
 
310
 
311 2 feddischso
function [sinh_res, cosh_res, sinh_err, cosh_err, it ]= csinhcosh( th, fid )
312
global C_FLAG_VEC_ROT C_FLAG_ATAN_3 C_MODE_CIRC C_MODE_LIN C_MODE_HYP
313
global XY_WIDTH ANGLEWIDTH GUARDBITS RM_GAIN
314
 
315
xi = repmat( (2^(XY_WIDTH-1)-1), size( th ) );
316
yi = zeros( 1, length( th ) );
317
ai = round( th .* (2^(ANGLEWIDTH-1)-1) );
318
 
319
 
320
 
321
mode = C_MODE_HYP;
322
 
323
 
324
% cordic version
325
[ rcosh rsinh ra, it ] = cordic_iterative( ...
326
                                           xi,          ...
327
                                           yi,          ...
328
                                           ai,          ...
329
                                           mode,        ...
330
                                           XY_WIDTH,    ...
331
                                           ANGLEWIDTH,  ...
332
                                           GUARDBITS,   ...
333
                                           RM_GAIN );
334 7 feddischso
 
335
 
336
 
337 2 feddischso
cosh_res = rcosh  ./ (   2^(XY_WIDTH-1)-1 );
338
sinh_res = rsinh  ./ (   2^(XY_WIDTH-1)-1 );
339
cosh_m = cosh( th );
340
sinh_m = sinh( th );
341
sinh_err = abs(sinh_res - sinh_m );
342
cosh_err = abs(cosh_res - cosh_m );
343
 
344 7 feddischso
 
345 2 feddischso
% write TB data
346
write_tb( fid, xi, yi, ai, rcosh, rsinh, ra, mode );
347
 
348
 
349
end
350
 
351
 
352
 
353
 
354
 
355
function [atan_res, abs_res, atan_err, abs_err, it ]  = catanh( x, y, fid )
356
global C_FLAG_VEC_ROT C_FLAG_ATAN_3 C_MODE_CIRC C_MODE_LIN C_MODE_HYP
357
global XY_WIDTH ANGLEWIDTH GUARDBITS RM_GAIN
358
 
359
if( size( x ) ~= size( y ) )
360
    error( 'size error' )
361
end
362
ai = zeros( size( x ) );
363
xi = round( x * (2^(XY_WIDTH-1)-1) );
364
yi = round( y * (2^(XY_WIDTH-1)-1) );
365
 
366
 
367
mode = C_FLAG_VEC_ROT + C_MODE_HYP;
368
 
369
 
370
% cordic version
371
[ rx, ry, ra, it ] = cordic_iterative( xi,          ...
372
                                  yi,          ...
373
                                  ai,          ...
374
                                  mode,        ...
375
                                  XY_WIDTH,    ...
376
                                  ANGLEWIDTH,  ...
377
                                  GUARDBITS,   ...
378
                                  RM_GAIN );
379
% matlab version
380
m_th = atanh( y ./ x );
381
m_r  = sqrt( x.^2 - y.^2 );
382
 
383
% comparison
384
atan_res = ra ./ 2^( (ANGLEWIDTH)-1);
385
abs_res  = rx ./ ( 2^(XY_WIDTH-1) -1 );
386
atan_err = abs( m_th - atan_res );
387
abs_err  = abs( m_r  -  abs_res );
388 7 feddischso
 
389 2 feddischso
% write TB data
390
write_tb( fid, xi, yi, ai, rx, ry, ra, mode );
391
 
392
 
393
end
394
 
395
 
396
 
397
 
398
 
399
function [mul_res, mul_err, it ] = cmul( x, y, fid )
400
global C_FLAG_VEC_ROT C_FLAG_ATAN_3 C_MODE_CIRC C_MODE_LIN C_MODE_HYP
401
global XY_WIDTH ANGLEWIDTH GUARDBITS RM_GAIN
402
 
403
if( size( x ) ~= size( y ) )
404
    error( 'size error' )
405
end
406
xi = round( x * ( 2^(XY_WIDTH-1) -1 ) );
407
ai = round( y * ( 2^(XY_WIDTH-1) -1 ) );
408
yi = zeros( size( x ) );
409
 
410
 
411
mode = C_MODE_LIN;
412
 
413
% cordic version
414
[ rx, rmul, ra, it ] = cordic_iterative( xi,          ...
415
                                        yi,          ...
416
                                        ai,          ...
417
                                        mode,        ...
418
                                        XY_WIDTH,    ...
419
                                        ANGLEWIDTH,  ...
420
                                        GUARDBITS,   ...
421
                                        RM_GAIN );
422 7 feddischso
 
423
 
424 2 feddischso
mul_res  = rmul ./ (2^(ANGLEWIDTH-1)-1);
425
mul_err  = abs( y.*x -  mul_res );
426
 
427
% write TB data
428
write_tb( fid, xi, yi, ai, rx, rmul, ra, mode )
429
 
430
 
431
end
432
 
433
 
434
 
435
 
436
 
437
function [div_res, div_err, it ] = cdiv( x, y, fid )
438
global C_FLAG_VEC_ROT C_FLAG_ATAN_3 C_MODE_CIRC C_MODE_LIN C_MODE_HYP
439
global XY_WIDTH ANGLEWIDTH GUARDBITS RM_GAIN
440
 
441
if( size( x ) ~= size( y ) )
442
    error( 'size error' )
443
end
444
xi = round( x * ( 2^(XY_WIDTH-1) -1 ) );
445
yi = round( y * ( 2^(XY_WIDTH-1) -1 ) );
446
ai = zeros( size( x ) );
447
 
448
 
449
mode = C_FLAG_VEC_ROT + C_MODE_LIN;
450
 
451
% cordic version
452
[ rx, ry, rdiv, it ] = cordic_iterative( xi,          ...
453
                                  yi,          ...
454
                                  ai,          ...
455
                                  mode,        ...
456
                                  XY_WIDTH,    ...
457
                                  ANGLEWIDTH,  ...
458
                                  GUARDBITS,   ...
459
                                  RM_GAIN );
460 7 feddischso
 
461
 
462 2 feddischso
div_res  = rdiv ./ (2^(ANGLEWIDTH-1)-1);
463
div_err  = abs( y./x -  div_res );
464
 
465
% write TB data
466
write_tb( fid, xi, yi, ai, rx, ry, rdiv, mode )
467
 
468
end
469
 
470
 
471
function [x_res, y_res, x_err, y_err, it ] = crot( x, y, th, fid )
472
%
473
% does a multiplication with exp( th * i )
474
% and therefore, a rotation of the complex input value x + yi where th
475
% defines the rotation angle
476
%
477
global C_FLAG_VEC_ROT C_FLAG_ATAN_3 C_MODE_CIRC C_MODE_LIN C_MODE_HYP
478
global XY_WIDTH ANGLEWIDTH GUARDBITS RM_GAIN
479
 
480
xi = round( x * ( 2^(XY_WIDTH-1) -1 ) );
481
yi = round( y * ( 2^(XY_WIDTH-1) -1 ) );
482
ai = round( th .* (2^(ANGLEWIDTH-1)-1) );
483
 
484
mode = C_MODE_CIRC;
485
 
486
[ rx ry ra, it ] = cordic_iterative( ...
487
                                  xi,          ...
488
                                  yi,          ...
489
                                  ai,          ...
490
                                  mode,        ...
491
                                  XY_WIDTH,    ...
492
                                  ANGLEWIDTH,  ...
493
                                  GUARDBITS,   ...
494
                                  RM_GAIN );
495 7 feddischso
 
496 2 feddischso
tmp = ( x + 1i * y ) .* exp( i * th );
497 7 feddischso
 
498 2 feddischso
x_res = rx  ./ (   2^(XY_WIDTH-1)-1 );
499
y_res = ry  ./ (   2^(XY_WIDTH-1)-1 );
500
 
501
y_err = abs(x_res - real(tmp) );
502
x_err = abs(y_res - imag(tmp) );
503
 
504
% write TB data
505
write_tb( fid, xi, yi, ai, rx, ry, ra, mode )
506
 
507
 
508
end
509
 
510
 
511
function [sin_res, cos_res, sin_err, cos_err, it ]= cpol2cart( th, r, fid )
512
%
513
% does the Matlab equivalent pol2cart
514
%
515
 
516
global C_FLAG_VEC_ROT C_FLAG_ATAN_3 C_MODE_CIRC C_MODE_LIN C_MODE_HYP
517
global XY_WIDTH ANGLEWIDTH GUARDBITS RM_GAIN
518
 
519
xi = r .* (2^(XY_WIDTH-1)-1);
520
yi = zeros( 1, length( th ) );
521
ai = round( th .* (2^(ANGLEWIDTH-1)-1) );
522
 
523
mode = C_MODE_CIRC;
524
 
525
[ rcos rsin ra, it ] = cordic_iterative( ...
526
                                  xi,          ...
527
                                  yi,          ...
528
                                  ai,          ...
529
                                  mode,        ...
530
                                  XY_WIDTH,    ...
531
                                  ANGLEWIDTH,  ...
532
                                  GUARDBITS,   ...
533
                                  RM_GAIN );
534 7 feddischso
 
535
 
536
 
537 2 feddischso
cos_res = rcos  ./ (   2^(XY_WIDTH-1)-1 );
538
sin_res = rsin  ./ (   2^(XY_WIDTH-1)-1 );
539
[ cos_m, sin_m ] = pol2cart( th, r );
540
sin_err = abs(sin_res - sin_m );
541
cos_err = abs(cos_res - cos_m );
542
 
543
% write TB data
544
write_tb( fid, xi, yi, ai, rcos, rsin, ra, mode )
545
 
546
 
547
end
548
 
549
 
550
 
551
 
552
 
553
function [atan_res, abs_res, atan_err, abs_err, it ]  = ccart2pol( x, y, fid )
554
 
555
global C_FLAG_VEC_ROT C_FLAG_ATAN_3 C_MODE_CIRC C_MODE_LIN C_MODE_HYP
556
global XY_WIDTH ANGLEWIDTH GUARDBITS RM_GAIN
557
 
558
if( size( x ) ~= size( y ) )
559
    error( 'size error' )
560
end
561
ai = zeros( size( x ) );
562
xi = round( x * (2^(XY_WIDTH-1)-1) );
563
yi = round( y * (2^(XY_WIDTH-1)-1) );
564
 
565
mode = C_FLAG_VEC_ROT + C_MODE_CIRC;
566
 
567
 
568
% cordic version
569
[ rx, ry, ra, it ] = cordic_iterative( xi,          ...
570
                                  yi,          ...
571
                                  ai,          ...
572
                                  mode,        ...
573
                                  XY_WIDTH,    ...
574
                                  ANGLEWIDTH,  ...
575
                                  GUARDBITS,   ...
576
                                  RM_GAIN );
577
% matlab version:
578
m_th = atan2( y,  x );
579
m_r  = sqrt( x.^2 + y.^2 );
580
 
581
 
582
% comparison
583
atan_res = ra ./ 2^( (ANGLEWIDTH)-1);
584
abs_res  = rx ./ ( 2^(XY_WIDTH-1) -1 );
585
atan_err = abs( m_th - atan_res );
586
abs_err  = abs( m_r  -  abs_res );
587
 
588
% TODO: ATAN oder ATAN2  atan( 0 / x ) != atan2( 0, x )!!!!
589
 
590
% write TB data
591
write_tb( fid, xi, yi, ai, rx, ry, ra, mode )
592
 
593
end
594
 
595
 
596
 
597
 
598
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.