summaryrefslogtreecommitdiff
path: root/gr-blocks/lib/qa_rotator.cc
blob: e35e4dc4cdf53f971820ca4140d00ac5fa91b879 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* -*- c++ -*- */
/*
 * Copyright 2002,2013,2018 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gnuradio/attributes.h>
#include <gnuradio/blocks/rotator.h>
#include <gnuradio/expj.h>
#include <gnuradio/math.h>
#include <boost/test/unit_test.hpp>

#include <cmath>

// error vector magnitude
__GR_ATTR_UNUSED static float error_vector_mag(gr_complex a, gr_complex b)
{
    return abs(a - b);
}

BOOST_AUTO_TEST_CASE(t1)
{
    static const unsigned int N = 100000;

    gr::blocks::rotator r;

    double phase_incr = 2 * GR_M_PI / 1003;
    double phase = 0;

    // Old code: We increment then return the rotated value, thus we
    // need to start one tick back r.set_phase(gr_complex(1,0) *
    // conj(gr_expj(phase_incr)));

    r.set_phase(gr_complex(1, 0));
    r.set_phase_incr(gr_expj(phase_incr));

    for (unsigned i = 0; i < N; i++) {
        gr_complex expected = gr_expj(phase);
        gr_complex actual = r.rotate(gr_complex(1, 0));

        BOOST_CHECK(std::abs(expected - actual) <= 0.0001);

        phase += phase_incr;
        if (phase >= 2 * GR_M_PI)
            phase -= 2 * GR_M_PI;
    }
}

BOOST_AUTO_TEST_CASE(t2)
{
    static const unsigned int N = 100000;

    gr::blocks::rotator r;
    std::vector<gr_complex> input(N);
    std::vector<gr_complex> output(N);

    double phase_incr = 2 * GR_M_PI / 1003;
    double phase = 0;

    r.set_phase(gr_complex(1, 0));
    r.set_phase_incr(gr_expj(phase_incr));

    // Generate a unity sequence
    for (unsigned i = 0; i < N; i++)
        input[i] = gr_complex(1.0f, 0.0f);

    // Rotate it
    r.rotateN(output.data(), input.data(), N);

    // Compare with expected result
    for (unsigned i = 0; i < N; i++) {
        gr_complex expected = gr_expj(phase);
        gr_complex actual = output[i];

        BOOST_CHECK(std::abs(expected - actual) <= 0.0001);

        phase += phase_incr;
        if (phase >= 2 * GR_M_PI)
            phase -= 2 * GR_M_PI;
    }
}