1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002,2013,2018 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 3, 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 
27 #include <gnuradio/attributes.h>
28 #include <gnuradio/blocks/rotator.h>
29 #include <gnuradio/expj.h>
30 #include <gnuradio/math.h>
31 #include <boost/test/unit_test.hpp>
32 
33 #include <cmath>
34 
35 // error vector magnitude
error_vector_mag(gr_complex a,gr_complex b)36 __GR_ATTR_UNUSED static float error_vector_mag(gr_complex a, gr_complex b)
37 {
38     return abs(a - b);
39 }
40 
BOOST_AUTO_TEST_CASE(t1)41 BOOST_AUTO_TEST_CASE(t1)
42 {
43     static const unsigned int N = 100000;
44 
45     gr::blocks::rotator r;
46 
47     double phase_incr = 2 * GR_M_PI / 1003;
48     double phase = 0;
49 
50     // Old code: We increment then return the rotated value, thus we
51     // need to start one tick back r.set_phase(gr_complex(1,0) *
52     // conj(gr_expj(phase_incr)));
53 
54     r.set_phase(gr_complex(1, 0));
55     r.set_phase_incr(gr_expj(phase_incr));
56 
57     for (unsigned i = 0; i < N; i++) {
58         gr_complex expected = gr_expj(phase);
59         gr_complex actual = r.rotate(gr_complex(1, 0));
60 
61         BOOST_CHECK(std::abs(expected - actual) <= 0.0001);
62 
63         phase += phase_incr;
64         if (phase >= 2 * GR_M_PI)
65             phase -= 2 * GR_M_PI;
66     }
67 }
68 
BOOST_AUTO_TEST_CASE(t2)69 BOOST_AUTO_TEST_CASE(t2)
70 {
71     static const unsigned int N = 100000;
72 
73     gr::blocks::rotator r;
74     gr_complex* input = new gr_complex[N];
75     gr_complex* output = new gr_complex[N];
76 
77     double phase_incr = 2 * GR_M_PI / 1003;
78     double phase = 0;
79 
80     r.set_phase(gr_complex(1, 0));
81     r.set_phase_incr(gr_expj(phase_incr));
82 
83     // Generate a unity sequence
84     for (unsigned i = 0; i < N; i++)
85         input[i] = gr_complex(1.0f, 0.0f);
86 
87     // Rotate it
88     r.rotateN(output, input, N);
89 
90     // Compare with expected result
91     for (unsigned i = 0; i < N; i++) {
92         gr_complex expected = gr_expj(phase);
93         gr_complex actual = output[i];
94 
95         BOOST_CHECK(std::abs(expected - actual) <= 0.0001);
96 
97         phase += phase_incr;
98         if (phase >= 2 * GR_M_PI)
99             phase -= 2 * GR_M_PI;
100     }
101 
102     delete[] output;
103     delete[] input;
104 }
105