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

Subversion Repositories yac

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

Go to most recent revision | 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
%
73
% flags: they are also define in cordic_iterative.c and
74
%        cordic_iterative_pkg.vhd
75
C_FLAG_VEC_ROT  = 2^3;
76
C_FLAG_ATAN_3   = 2^2;
77
C_MODE_CIRC     = 0;
78
C_MODE_LIN      = 1;
79
C_MODE_HYP      = 2;
80
 
81
% initialize the random-generator's seed
82
rand('seed', 1633);
83
 
84
 
85
% cordic setup:
86
% this must fit to the testbench
87
XY_WIDTH   = 25;
88
ANGLEWIDTH = 25;
89
GUARDBITS  = 2;
90
RM_GAIN    = 5;
91
 
92
 
93
% Number of tests, which are run
94
N_TESTS    = 10000;
95
 
96
% open test file
97
tb_fid = fopen( './tb_data.txt', 'w' );
98
%tb_fid = 0;
99
 
100
 
101
 
102
 
103
 
104
%
105
% run test, which uses random values
106
run_random_test( N_TESTS, tb_fid );
107
%
108
% run tests, which test limits
109
run_limit_test( tb_fid );
110
%
111
% run linear value test
112
run_linear_test( 1000, tb_fid );
113
 
114
% close file
115
if tb_fid > 0
116
    fclose( tb_fid );
117
end
118
 
119
end
120
 
121
 
122
 
123
 
124
function run_limit_test( tb_fid )
125
%RUN_LIMIT_TEST Test the range limit
126
%
127
% run_limit_test( fid )
128
%
129
% This function is used to generate a test pattern
130
% with values, which are at the range limit.
131
% This values are then processed by the fixed-point YAC
132
% implementation. All input and outputs are logged into
133
% a testbench pattern file.
134
%
135
% The argument fid is the file-descriptor of the testbench pattern
136
% file.
137
%
138
 
139
 
140
data_a = [ 0 1 0 1 -1  0 -1  1 -1 ];
141
data_b = [ 0 0 1 1  0 -1 -1 -1  1 ];
142
 
143
data_c = [ 0 0 0 0  0  0  0  0  0 ...
144
           1 1 1 1  1  1  1  1  1 ...
145
           -1 -1 -1 -1 -1 -1 -1 -1 -1 ];
146
 
147
data_d = data_a * pi;
148
 
149
data_a_div = [ 0.5 ,1 -0.5, -1, -0.5, -1 ];
150
data_b_div = [ 1   ,1,  1,   1,   -1, -1 ];
151
 
152
[ ~, ~, atan_err, abs_err, it_1 ]   = ccart2pol( data_a, data_b, tb_fid );
153
[ ~, ~, sin_err,  cos_err, it_2 ]   = cpol2cart( data_d, data_b, tb_fid );
154
[ ~, ~, x_err, y_err, it_3 ]        = crot( [ data_a, data_a, data_a], ...
155
                                            [ data_b, data_b, data_b], ...
156
                                              data_c, tb_fid );
157
[ ~, div_err, it_4 ]                = cdiv( data_a_div, data_b_div, tb_fid );
158
[ ~, mul_err, it_5 ]                = cmul( data_a, data_b, tb_fid  );
159
 
160
print_result_info(    ...
161
    atan_err,   it_1, ...
162
    abs_err,    it_1, ...
163
    sin_err,    it_2, ...
164
    cos_err,    it_2, ...
165
    x_err,      it_3, ...
166
    y_err,      it_3, ...
167
    div_err,    it_4, ...
168
    mul_err,    it_5, ...
169
    0,  0, ...
170
    0,  0, ...
171
    0,  0, ...
172
    0,  0, ...
173
    'Limit Value Test' );
174
 
175
end
176
 
177
 
178
 
179
function run_linear_test( N_TESTS, tb_fid )
180
%RUN_LINEAR_TEST Generates a linear test pattern
181
%
182
% run_linear_test( N, fid )
183
%
184
% This function is used to generate linear increasing test
185
% values.
186
% These values are then processed by the fixed-point YAC
187
% implementation. All input and outputs are logged into
188
% a testbench pattern file. In addition, the result is plotted.
189
%
190
% NOTE: only the hyperbolic functions are processed at the moment.
191
% This function needs to be extended in future.
192
%
193
%
194
% The argument fid is the file-descriptor of the testbench pattern
195
% file. The argument N defines the number of values, which are processed.
196
%
197
%
198
 
199
data_a_h = ones( 1, N_TESTS );
200
data_b_h = linspace( -1, 1, N_TESTS ) * 0.78;
201
data_c_h = linspace( -1, 1, N_TESTS );
202
[ atanh_res, sqrt_res, atanh_err, sqrt_err, it_6 ] = catanh( data_a_h, data_b_h, tb_fid );
203
[ sinh_res, cosh_res, sinh_err, cosh_err, it_7 ]   = csinhcosh( data_c_h, tb_fid );
204
 
205
 
206
figure; plot( data_b_h, atanh_res ); title( 'atanh' );
207
figure; plot( data_b_h, atanh_err ); title( 'atanh-error' );
208
figure; plot( data_c_h, sinh_res, data_c_h, cosh_res ); title( 'sinh and cosh' );
209
figure; plot( data_c_h, sinh_err, data_c_h, cosh_err ); title( 'sinh and cosh errors' );
210
end
211
 
212
 
213
 
214
function run_random_test( N_TESTS, tb_fid )
215
%RUN_RANDOM_TEST Generates a random test pattern
216
%
217
% run_random_test( N, fid )
218
%
219
% This function is used to generate random test
220
% values (uniform distributed).
221
% These values are then processed by the fixed-point YAC
222
% implementation. All input and outputs are logged into
223
% a testbench pattern file.
224
%
225
%
226
% The argument fid is the file-descriptor of the testbench pattern
227
% file. The argument N defines the number of values, which are processed.
228
%
229
%
230
data_a = -1 + 2 .* rand( 1, N_TESTS );
231
data_b = -1 + 2 .* rand( 1, N_TESTS );
232
data_c = -1 + 2 .* rand( 1, N_TESTS );
233
data_d = -pi + 2*pi .* rand( 1, N_TESTS );
234
% adapat data for division
235
data_a_div = data_a;
236
data_b_div = data_b;
237
swap_div   = ( data_b ./ data_a ) >= 2 | ( data_b ./ data_a ) < -2 ;
238
data_a_div( swap_div ) = data_b( swap_div );
239
data_b_div( swap_div ) = data_a( swap_div );
240
 
241
data_a_h   = ones( size( data_a ) );
242
data_b_h   = data_b .* 0.80694; %0.78;
243
 
244
 
245
 
246
[ ~, ~, atan_err, abs_err, it_1 ]   = ccart2pol( data_a, data_b, tb_fid );
247
[ ~, ~, sin_err,  cos_err, it_2 ]   = cpol2cart( data_d, data_b, tb_fid );
248
[ ~, ~, x_err, y_err, it_3 ]        = crot( data_a, data_b, data_c, tb_fid );
249
[ ~, div_err, it_4 ]                = cdiv( data_a_div, data_b_div, tb_fid );
250
[ ~, mul_err, it_5 ]                = cmul( data_a, data_b, tb_fid  );
251
[ ~, ~, atanh_err, sqrt_err, it_6 ] = catanh( data_a_h, data_b_h, tb_fid );
252
[ ~, ~, sinh_err, cosh_err, it_7 ]  = csinhcosh( data_a, tb_fid );
253
 
254
print_result_info(  atan_err,   it_1, ...
255
                    abs_err,    it_1, ...
256
                    sin_err,    it_2, ...
257
                    cos_err,    it_2, ...
258
                    x_err,      it_3, ...
259
                    y_err,      it_3, ...
260
                    div_err,    it_4, ...
261
                    mul_err,    it_5, ...
262
                    atanh_err,  it_6, ...
263
                    sqrt_err,   it_6, ...
264
                    sinh_err,   it_7, ...
265
                    cosh_err,   it_7, ...
266
                    'Random Value Test' );
267
 
268
 
269
end
270
 
271
 
272
function print_result_info( ...
273
    atan_err,   atan_it,    ...
274
    abs_err,    abs_it,     ...
275
    sin_err,    sin_it,     ...
276
    cos_err,    cos_it,     ...
277
    x_err,      x_it,       ...
278
    y_err,      y_it,       ...
279
    div_err,    div_it,     ...
280
    mul_err,    mul_it,     ...
281
    atanh_err,  atanh_it,   ...
282
    sqrt_err,   sqrt_it,    ...
283
    sinh_err,   sinh_it,    ...
284
    cosh_err,   cosh_it,    ...
285
    title )
286
 
287
fprintf( ' ___________________________________________________________________\n' );
288
fprintf( '                  %s\n', title);
289
fprintf( ' -----+-------------------+--------------------+-------------------\n'   );
290
fprintf( '      |     max error     |   mean error       |  max iterations  \n'   );
291
fprintf( ' atan | % .14f | % .14f  | %.5f \n', max( atan_err  ), mean( atan_err  ), max( atan_it   ) );
292
fprintf( ' abs  | % .14f | % .14f  | %.5f \n', max( abs_err   ), mean( abs_err   ), max( abs_it    ) );
293
fprintf( ' sin  | % .14f | % .14f  | %.5f \n', max( sin_err   ), mean( sin_err   ), max( sin_it    ) );
294
fprintf( ' cos  | % .14f | % .14f  | %.5f \n', max( cos_err   ), mean( cos_err   ), max( cos_it    ) );
295
fprintf( ' x    | % .14f | % .14f  | %.5f \n', max( x_err     ), mean( x_err     ), max( x_it      ) );
296
fprintf( ' y    | % .14f | % .14f  | %.5f \n', max( y_err     ), mean( y_err     ), max( y_it      ) );
297
fprintf( ' div  | % .14f | % .14f  | %.5f \n', max( div_err   ), mean( div_err   ), max( div_it    ) );
298
fprintf( ' mul  | % .14f | % .14f  | %.5f \n', max( mul_err   ), mean( mul_err   ), max( mul_it    ) );
299
fprintf( ' atanh| % .14f | % .14f  | %.5f \n', max( atanh_err ), mean( atanh_err ), max( atanh_it  ) );
300
fprintf( ' sqrt | % .14f | % .14f  | %.5f \n', max( sqrt_err  ), mean( sqrt_err  ), max( sqrt_it   ) );
301
fprintf( ' sinh | % .14f | % .14f  | %.5f \n', max( sinh_err  ), mean( sinh_err  ), max( sinh_it   ) );
302
fprintf( ' cosh | % .14f | % .14f  | %.5f \n', max( cosh_err  ), mean( cosh_err  ), max( cosh_it   ) );
303
 
304
end
305
 
306
 
307
 
308
 
309
 
310
function [sinh_res, cosh_res, sinh_err, cosh_err, it ]= csinhcosh( th, fid )
311
global C_FLAG_VEC_ROT C_FLAG_ATAN_3 C_MODE_CIRC C_MODE_LIN C_MODE_HYP
312
global XY_WIDTH ANGLEWIDTH GUARDBITS RM_GAIN
313
 
314
xi = repmat( (2^(XY_WIDTH-1)-1), size( th ) );
315
yi = zeros( 1, length( th ) );
316
ai = round( th .* (2^(ANGLEWIDTH-1)-1) );
317
 
318
 
319
 
320
mode = C_MODE_HYP;
321
 
322
 
323
% cordic version
324
[ rcosh rsinh ra, it ] = cordic_iterative( ...
325
                                           xi,          ...
326
                                           yi,          ...
327
                                           ai,          ...
328
                                           mode,        ...
329
                                           XY_WIDTH,    ...
330
                                           ANGLEWIDTH,  ...
331
                                           GUARDBITS,   ...
332
                                           RM_GAIN );
333
 
334
 
335
 
336
cosh_res = rcosh  ./ (   2^(XY_WIDTH-1)-1 );
337
sinh_res = rsinh  ./ (   2^(XY_WIDTH-1)-1 );
338
cosh_m = cosh( th );
339
sinh_m = sinh( th );
340
sinh_err = abs(sinh_res - sinh_m );
341
cosh_err = abs(cosh_res - cosh_m );
342
 
343
 
344
% write TB data
345
write_tb( fid, xi, yi, ai, rcosh, rsinh, ra, mode );
346
 
347
 
348
end
349
 
350
 
351
 
352
 
353
 
354
function [atan_res, abs_res, atan_err, abs_err, it ]  = catanh( x, y, fid )
355
global C_FLAG_VEC_ROT C_FLAG_ATAN_3 C_MODE_CIRC C_MODE_LIN C_MODE_HYP
356
global XY_WIDTH ANGLEWIDTH GUARDBITS RM_GAIN
357
 
358
if( size( x ) ~= size( y ) )
359
    error( 'size error' )
360
end
361
ai = zeros( size( x ) );
362
xi = round( x * (2^(XY_WIDTH-1)-1) );
363
yi = round( y * (2^(XY_WIDTH-1)-1) );
364
 
365
 
366
mode = C_FLAG_VEC_ROT + C_MODE_HYP;
367
 
368
 
369
% cordic version
370
[ rx, ry, ra, it ] = cordic_iterative( xi,          ...
371
                                  yi,          ...
372
                                  ai,          ...
373
                                  mode,        ...
374
                                  XY_WIDTH,    ...
375
                                  ANGLEWIDTH,  ...
376
                                  GUARDBITS,   ...
377
                                  RM_GAIN );
378
% matlab version
379
m_th = atanh( y ./ x );
380
m_r  = sqrt( x.^2 - y.^2 );
381
 
382
% comparison
383
atan_res = ra ./ 2^( (ANGLEWIDTH)-1);
384
abs_res  = rx ./ ( 2^(XY_WIDTH-1) -1 );
385
atan_err = abs( m_th - atan_res );
386
abs_err  = abs( m_r  -  abs_res );
387
 
388
% write TB data
389
write_tb( fid, xi, yi, ai, rx, ry, ra, mode );
390
 
391
 
392
end
393
 
394
 
395
 
396
 
397
 
398
function [mul_res, mul_err, it ] = cmul( x, y, fid )
399
global C_FLAG_VEC_ROT C_FLAG_ATAN_3 C_MODE_CIRC C_MODE_LIN C_MODE_HYP
400
global XY_WIDTH ANGLEWIDTH GUARDBITS RM_GAIN
401
 
402
if( size( x ) ~= size( y ) )
403
    error( 'size error' )
404
end
405
xi = round( x * ( 2^(XY_WIDTH-1) -1 ) );
406
ai = round( y * ( 2^(XY_WIDTH-1) -1 ) );
407
yi = zeros( size( x ) );
408
 
409
 
410
mode = C_MODE_LIN;
411
 
412
% cordic version
413
[ rx, rmul, ra, it ] = cordic_iterative( xi,          ...
414
                                        yi,          ...
415
                                        ai,          ...
416
                                        mode,        ...
417
                                        XY_WIDTH,    ...
418
                                        ANGLEWIDTH,  ...
419
                                        GUARDBITS,   ...
420
                                        RM_GAIN );
421
 
422
 
423
mul_res  = rmul ./ (2^(ANGLEWIDTH-1)-1);
424
mul_err  = abs( y.*x -  mul_res );
425
 
426
% write TB data
427
write_tb( fid, xi, yi, ai, rx, rmul, ra, mode )
428
 
429
 
430
end
431
 
432
 
433
 
434
 
435
 
436
function [div_res, div_err, it ] = cdiv( x, y, fid )
437
global C_FLAG_VEC_ROT C_FLAG_ATAN_3 C_MODE_CIRC C_MODE_LIN C_MODE_HYP
438
global XY_WIDTH ANGLEWIDTH GUARDBITS RM_GAIN
439
 
440
if( size( x ) ~= size( y ) )
441
    error( 'size error' )
442
end
443
xi = round( x * ( 2^(XY_WIDTH-1) -1 ) );
444
yi = round( y * ( 2^(XY_WIDTH-1) -1 ) );
445
ai = zeros( size( x ) );
446
 
447
 
448
mode = C_FLAG_VEC_ROT + C_MODE_LIN;
449
 
450
% cordic version
451
[ rx, ry, rdiv, it ] = cordic_iterative( xi,          ...
452
                                  yi,          ...
453
                                  ai,          ...
454
                                  mode,        ...
455
                                  XY_WIDTH,    ...
456
                                  ANGLEWIDTH,  ...
457
                                  GUARDBITS,   ...
458
                                  RM_GAIN );
459
 
460
 
461
div_res  = rdiv ./ (2^(ANGLEWIDTH-1)-1);
462
div_err  = abs( y./x -  div_res );
463
 
464
% write TB data
465
write_tb( fid, xi, yi, ai, rx, ry, rdiv, mode )
466
 
467
end
468
 
469
 
470
function [x_res, y_res, x_err, y_err, it ] = crot( x, y, th, fid )
471
%
472
% does a multiplication with exp( th * i )
473
% and therefore, a rotation of the complex input value x + yi where th
474
% defines the rotation angle
475
%
476
global C_FLAG_VEC_ROT C_FLAG_ATAN_3 C_MODE_CIRC C_MODE_LIN C_MODE_HYP
477
global XY_WIDTH ANGLEWIDTH GUARDBITS RM_GAIN
478
 
479
xi = round( x * ( 2^(XY_WIDTH-1) -1 ) );
480
yi = round( y * ( 2^(XY_WIDTH-1) -1 ) );
481
ai = round( th .* (2^(ANGLEWIDTH-1)-1) );
482
 
483
mode = C_MODE_CIRC;
484
 
485
[ rx ry ra, it ] = cordic_iterative( ...
486
                                  xi,          ...
487
                                  yi,          ...
488
                                  ai,          ...
489
                                  mode,        ...
490
                                  XY_WIDTH,    ...
491
                                  ANGLEWIDTH,  ...
492
                                  GUARDBITS,   ...
493
                                  RM_GAIN );
494
 
495
tmp = ( x + 1i * y ) .* exp( i * th );
496
 
497
x_res = rx  ./ (   2^(XY_WIDTH-1)-1 );
498
y_res = ry  ./ (   2^(XY_WIDTH-1)-1 );
499
 
500
y_err = abs(x_res - real(tmp) );
501
x_err = abs(y_res - imag(tmp) );
502
 
503
% write TB data
504
write_tb( fid, xi, yi, ai, rx, ry, ra, mode )
505
 
506
 
507
end
508
 
509
 
510
function [sin_res, cos_res, sin_err, cos_err, it ]= cpol2cart( th, r, fid )
511
%
512
% does the Matlab equivalent pol2cart
513
%
514
 
515
global C_FLAG_VEC_ROT C_FLAG_ATAN_3 C_MODE_CIRC C_MODE_LIN C_MODE_HYP
516
global XY_WIDTH ANGLEWIDTH GUARDBITS RM_GAIN
517
 
518
xi = r .* (2^(XY_WIDTH-1)-1);
519
yi = zeros( 1, length( th ) );
520
ai = round( th .* (2^(ANGLEWIDTH-1)-1) );
521
 
522
mode = C_MODE_CIRC;
523
 
524
[ rcos rsin ra, it ] = cordic_iterative( ...
525
                                  xi,          ...
526
                                  yi,          ...
527
                                  ai,          ...
528
                                  mode,        ...
529
                                  XY_WIDTH,    ...
530
                                  ANGLEWIDTH,  ...
531
                                  GUARDBITS,   ...
532
                                  RM_GAIN );
533
 
534
 
535
 
536
cos_res = rcos  ./ (   2^(XY_WIDTH-1)-1 );
537
sin_res = rsin  ./ (   2^(XY_WIDTH-1)-1 );
538
[ cos_m, sin_m ] = pol2cart( th, r );
539
sin_err = abs(sin_res - sin_m );
540
cos_err = abs(cos_res - cos_m );
541
 
542
% write TB data
543
write_tb( fid, xi, yi, ai, rcos, rsin, ra, mode )
544
 
545
 
546
end
547
 
548
 
549
 
550
 
551
 
552
function [atan_res, abs_res, atan_err, abs_err, it ]  = ccart2pol( x, y, fid )
553
 
554
global C_FLAG_VEC_ROT C_FLAG_ATAN_3 C_MODE_CIRC C_MODE_LIN C_MODE_HYP
555
global XY_WIDTH ANGLEWIDTH GUARDBITS RM_GAIN
556
 
557
if( size( x ) ~= size( y ) )
558
    error( 'size error' )
559
end
560
ai = zeros( size( x ) );
561
xi = round( x * (2^(XY_WIDTH-1)-1) );
562
yi = round( y * (2^(XY_WIDTH-1)-1) );
563
 
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
 
599
function write_tb( fid, x_i, y_i, a_i, x_o, y_o, a_o, mode )
600
 
601
if fid > 0
602
    for x = 1 : length( x_i )
603
        fprintf( fid, '%ld ', fix( [ x_i(x), y_i(x), a_i(x), x_o(x), y_o(x), a_o(x), mode ] ) );
604
        fprintf( fid, '\n' );
605
    end
606
end
607
 
608
end

powered by: WebSVN 2.1.0

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