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

Subversion Repositories yac

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

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

powered by: WebSVN 2.1.0

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