File: iir.cpp

package info (click to toggle)
arrayfire 3.3.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 109,016 kB
  • sloc: cpp: 127,909; lisp: 6,878; python: 3,923; ansic: 1,051; sh: 347; makefile: 338; xml: 175
file content (188 lines) | stat: -rw-r--r-- 4,594 bytes parent folder | download
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*******************************************************
 * Copyright (c) 2014, ArrayFire
 * All rights reserved.
 *
 * This file is distributed under 3-clause BSD license.
 * The complete license agreement can be obtained at:
 * http://arrayfire.com/licenses/BSD-3-Clause
 ********************************************************/

#include <gtest/gtest.h>
#include <arrayfire.h>
#include <af/dim4.hpp>
#include <af/traits.hpp>
#include <string>
#include <vector>
#include <testHelpers.hpp>

using std::vector;
using std::string;
using af::cfloat;
using af::cdouble;
using af::dim4;

template<typename T>
class filter : public ::testing::Test
{
public:
    virtual void SetUp() {}
};

// create a list of types to be tested
typedef ::testing::Types<float, double, cfloat, cdouble> TestTypes;
TYPED_TEST_CASE(filter, TestTypes);


template<typename T>
void firTest(const int xrows, const int xcols, const int brows, const int bcols)
{
    if (noDoubleTests<T>()) return;
    try {
        af::dtype ty = (af::dtype)af::dtype_traits<T>::af_type;
        af::array x = af::randu(xrows, xcols, ty);
        af::array b = af::randu(brows, bcols, ty);

        af::array y = af::fir(b, x);
        af::array c = af::convolve1(x, b, AF_CONV_EXPAND);

        const int ycols = xcols * bcols;
        const int crows = xrows + brows - 1;
        const int yrows = xrows;

        vector<T> hy(yrows * ycols);
        vector<T> hc(crows * ycols);

        y.host(&hy[0]);
        c.host(&hc[0]);

        for (int j = 0; j < ycols; j++) {
            for (int i = 0; i < yrows; i++) {
                ASSERT_NEAR(real(hy[j * yrows + i]),
                            real(hc[j * crows + i]), 0.01);
            }
        }
    } catch (af::exception &ex) {
        FAIL() << ex.what();
    }
}

TYPED_TEST(filter, firVecVec)
{
    firTest<TypeParam>(10000, 1, 1000, 1);
}

TYPED_TEST(filter, firVecMat)
{
    firTest<TypeParam>(10000, 1, 50, 10);
}

TYPED_TEST(filter, firMatVec)
{
    firTest<TypeParam>(5000, 10, 100, 1);
}

TYPED_TEST(filter, firMatMat)
{
    firTest<TypeParam>(5000, 10, 50, 10);
}

template<typename T>
void iirA0Test(const int xrows, const int xcols, const int brows, const int bcols)
{
    if (noDoubleTests<T>()) return;
    try {
        af::dtype ty = (af::dtype)af::dtype_traits<T>::af_type;
        af::array x = af::randu(xrows, xcols, ty);
        af::array b = af::randu(brows, bcols, ty);
        af::array a = af::randu(    1, bcols, ty);
        af::array bNorm = b / tile(a, brows);

        af::array y = af::iir(b, a, x);
        af::array c = af::convolve1(x, bNorm, AF_CONV_EXPAND);

        const int ycols = xcols * bcols;
        const int crows = xrows + brows - 1;
        const int yrows = xrows;

        vector<T> hy(yrows * ycols);
        vector<T> hc(crows * ycols);

        y.host(&hy[0]);
        c.host(&hc[0]);

        for (int j = 0; j < ycols; j++) {
            for (int i = 0; i < yrows; i++) {
                ASSERT_NEAR(real(hy[j * yrows + i]),
                            real(hc[j * crows + i]), 0.01);
            }
        }
    } catch (af::exception &ex) {
        FAIL() << ex.what();
    }
}

TYPED_TEST(filter, iirA0VecVec)
{
    iirA0Test<TypeParam>(10000, 1, 1000, 1);
}

TYPED_TEST(filter, iirA0VecMat)
{
    iirA0Test<TypeParam>(10000, 1, 50, 10);
}

TYPED_TEST(filter, iirA0MatVec)
{
    iirA0Test<TypeParam>(5000, 10, 100, 1);
}

TYPED_TEST(filter, iirA0MatMat)
{
    iirA0Test<TypeParam>(5000, 10, 50, 10);
}

template<typename T>
void iirTest(const char *testFile)
{
    if (noDoubleTests<T>()) return;
    vector<af::dim4> inDims;

    vector<vector<T> > inputs;
    vector<vector<T> > outputs;
    readTests<T, T, float> (testFile, inDims, inputs, outputs);

    try {
        af::array a = af::array(inDims[0], &inputs[0][0]);
        af::array b = af::array(inDims[1], &inputs[1][0]);
        af::array x = af::array(inDims[2], &inputs[2][0]);

        af::array y = af::iir(b, a, x);
        std::vector<T> gold = outputs[0];
        ASSERT_EQ(gold.size(), (size_t)y.elements());

        std::vector<T> out(y.elements());
        y.host(&out[0]);

        for(size_t i = 0; i < gold.size(); i++) {
            ASSERT_NEAR(real(out[i]), real(gold[i]), 0.01) << "at: " << i;
        }

    } catch (af::exception &ex) {
        FAIL() << ex.what();
    }
}

TYPED_TEST(filter, iirVecVec)
{
    iirTest<TypeParam>(TEST_DIR"/iir/iir_vv.test");
}

TYPED_TEST(filter, iirVecMat)
{
    iirTest<TypeParam>(TEST_DIR"/iir/iir_vm.test");
}

TYPED_TEST(filter, iirMatMat)
{
    iirTest<TypeParam>(TEST_DIR"/iir/iir_mm.test");
}