PandA-2024.02
cordic_test.cpp
Go to the documentation of this file.
1 /************************************************
2 Copyright (c) 2016, Xilinx, Inc.
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 
11 2. Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14 
15 3. Neither the name of the copyright holder nor the names of its contributors
16 may be used to endorse or promote products derived from this software
17 without specific prior written permission.
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ************************************************/
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include "read_file.h"
32 
33 #include "cordic.h"
34 
35 #define MAX_TEST_COUNT 110465
36 #ifndef TEST_COUNT
37 #define TEST_COUNT MAX_TEST_COUNT
38 #else
39 #if TEST_COUNT > MAX_TEST_COUNT
40 #error TEST_COUNT is above maximum
41 #endif
42 #endif
43 
44 coord_t input_data[TEST_COUNT][2];
45 phase_t ref_results[TEST_COUNT][1];
46 
47 int main(int argc, char** argv)
48 {
49  printf("argc: %d\n", argc);
50  coord_t inp_x, inp_y, out_x, out_y;
51  phase_t inp_rad, out_rad;
52  float res, ref_res;
53 
54  float diff, total_error = 0;
55 
56  int i;
57  FILE *fp, *fm;
58 
59  //const float angle = M_PI_2/N;
60 
61  fp = fopen("./vector.dat", "w");
62  if (fp == NULL) {
63  fprintf(stderr, "unable to open output file\n");
64  }
65  fm = fopen("./check_res.m", "w");
66  if (fm == NULL) {
67  fprintf(stderr, "unable to open MATLAB ile\n");
68  }
69  fprintf(fm, "clear all; close all; clc;\n");
70  fprintf(fm, "%%%%\n");
71  fprintf(fm, "dat = load('vector.dat');\n");
72  fprintf(fm, "diff = abs(dat(:,3) - dat(:,4));\n");
73  fprintf(fm, "diff2= abs(dat(:,4) - dat(:,5));\n");
74  fprintf(fm, "figure; plot(diff); title 'error CORDIC vs. ATAN2'; grid;\n");
75  fprintf(fm, "figure; plot(diff2); title 'error2'; grid;\n");
76  fprintf(fm, "max_diff = max(diff)\n");
77  fprintf(fm, "sum_abs_err = sum(diff)\n");
78  fprintf(fm, "max_diff2 = max(diff2)\n");
79  fprintf(fm, "sum_abs_err2 = sum(diff2)\n");
80 
81  //read files
82  read_file<TEST_COUNT, 2, coord_t>("./input_data.txt", input_data);
83  read_file<TEST_COUNT, 1, phase_t>("./ref_results.txt", ref_results);
84 
85  // vectoring mode
86  inp_rad = 0;
87  for (i = 0; i < TEST_COUNT; i++) {
88  inp_y = (coord_t) input_data[i][0];
89  inp_x = (coord_t) input_data[i][1];
90  ref_res = (phase_t) ref_results[i][0];
91 
92  //void cordic ( bool mode, coord_t x0, coord_t y0, phase_t z0, coord_t *xn, coord_t *yn, phase_t *zn);
93  //if ( (inp_y>0) & (inp_x>0) )
94  {
95  // REF DESIGN
96  float c_atan2 = atan2f((float) inp_y, (float) inp_x);
97  // DUT
98  top_atan2(inp_y, inp_x, &out_rad);
99 
100  //check results
101  diff = out_rad - ref_results[i][0];
102  if (diff < 0)
103  diff = 0 - diff; // take absolute value
104  total_error += diff;
105  fprintf(fp, "%20.10f %20.10f %20.10f %20.10f %20.10f\n",
106  (float) inp_y, (float) inp_x, (float) out_rad,
107  (float) ref_results[i][0], (float) c_atan2);
108  //fprintf(stderr, "%20.10f %20.10f %20.10f %20.10f\n", (float) inp_y, (float) inp_x, (float) out_rad, (float) ref_results[i][0]);
109  }
110  }
111 
112  fprintf(stderr, "total error = %f\n", total_error);
113  fprintf(stderr, "relative error = %f\n", total_error / TEST_COUNT);
114 
115  fclose(fp);
116  fclose(fm);
117 
118  return 0;
119 
120 }
#define NULL
int main(int argc, char **argv)
Definition: cordic_test.cpp:47
#define TEST_COUNT
Definition: cordic_test.cpp:37
coord_t input_data[TEST_COUNT][2]
Definition: cordic_test.cpp:44
phase_t ref_results[TEST_COUNT][1]
Definition: cordic_test.cpp:45
void top_atan2(coord_t y0, coord_t x0, phase_t *zn)

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13