Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / general / qa_gr_fxpt_nco.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 #include <qa_gr_fxpt_nco.h>
27 #include <gr_fxpt_nco.h>
28 #include <gr_nco.h>
29 #include <cppunit/TestAssert.h>
30 #include <iostream>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <math.h>
34
35 static const float SIN_COS_TOLERANCE = 1e-5;
36
37 //static const float SIN_COS_FREQ = 5003;
38 static const float SIN_COS_FREQ = 4096;
39
40 static const int SIN_COS_BLOCK_SIZE = 100000;
41
42 static double max_d(double a, double b)
43 {
44   return fabs(a) > fabs(b) ? a : b;
45 }
46
47 void
48 qa_gr_fxpt_nco::t0 ()
49 {
50   gr_nco<float,float>   ref_nco;
51   gr_fxpt_nco           new_nco;
52   double max_error = 0, max_phase_error = 0;
53
54   ref_nco.set_freq ((float)(2 * M_PI / SIN_COS_FREQ));
55   new_nco.set_freq ((float)(2 * M_PI / SIN_COS_FREQ));
56
57   CPPUNIT_ASSERT_DOUBLES_EQUAL (ref_nco.get_freq(), new_nco.get_freq(), SIN_COS_TOLERANCE);
58
59   for (int i = 0; i < SIN_COS_BLOCK_SIZE; i++){
60     float ref_sin = ref_nco.sin ();
61     float new_sin = new_nco.sin ();
62     //printf ("i = %6d\n", i);
63     CPPUNIT_ASSERT_DOUBLES_EQUAL (ref_sin, new_sin, SIN_COS_TOLERANCE);
64
65     max_error = max_d (max_error, ref_sin-new_sin);
66
67     float ref_cos = ref_nco.cos ();
68     float new_cos = new_nco.cos ();
69     CPPUNIT_ASSERT_DOUBLES_EQUAL (ref_cos, new_cos, SIN_COS_TOLERANCE);
70
71     max_error = max_d (max_error, ref_cos-new_cos);
72
73     ref_nco.step ();
74     new_nco.step ();
75
76     CPPUNIT_ASSERT_DOUBLES_EQUAL (ref_nco.get_phase(), new_nco.get_phase(), SIN_COS_TOLERANCE);
77
78     max_phase_error = max_d (max_phase_error, ref_nco.get_phase()-new_nco.get_phase());
79   }
80   // printf ("Fxpt  max error %.9f, max phase error %.9f\n", max_error, max_phase_error);
81 }
82
83 void
84 qa_gr_fxpt_nco::t1 ()
85 {
86   gr_nco<float,float>   ref_nco;
87   gr_fxpt_nco           new_nco;
88   gr_complex            ref_block[SIN_COS_BLOCK_SIZE];
89   gr_complex            new_block[SIN_COS_BLOCK_SIZE];
90   double max_error = 0;
91
92   ref_nco.set_freq ((float)(2 * M_PI / SIN_COS_FREQ));
93   new_nco.set_freq ((float)(2 * M_PI / SIN_COS_FREQ));
94
95   CPPUNIT_ASSERT_DOUBLES_EQUAL (ref_nco.get_freq(), new_nco.get_freq(), SIN_COS_TOLERANCE);
96
97   ref_nco.sincos ((gr_complex*)ref_block, SIN_COS_BLOCK_SIZE);
98   new_nco.sincos ((gr_complex*)new_block, SIN_COS_BLOCK_SIZE);
99
100   for (int i = 0; i < SIN_COS_BLOCK_SIZE; i++){
101     CPPUNIT_ASSERT_DOUBLES_EQUAL (ref_block[i].real(), new_block[i].real(), SIN_COS_TOLERANCE);
102     max_error = max_d (max_error, ref_block[i].real()-new_block[i].real());
103
104     CPPUNIT_ASSERT_DOUBLES_EQUAL (ref_block[i].imag(), new_block[i].imag(), SIN_COS_TOLERANCE);
105     max_error = max_d (max_error, ref_block[i].imag()-new_block[i].imag());
106   }
107   CPPUNIT_ASSERT_DOUBLES_EQUAL (ref_nco.get_phase(), new_nco.get_phase(), SIN_COS_TOLERANCE);
108   // printf ("Fxpt  max error %.9f, max phase error %.9f\n", max_error, max_phase_error);
109 }
110
111 void
112 qa_gr_fxpt_nco::t2 ()
113 {
114 }
115
116 void
117 qa_gr_fxpt_nco::t3 ()
118 {
119 }