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

Subversion Repositories yac

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

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

powered by: WebSVN 2.1.0

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