forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiir.cpp
More file actions
160 lines (126 loc) · 4.44 KB
/
iir.cpp
File metadata and controls
160 lines (126 loc) · 4.44 KB
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
/*******************************************************
* 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 <arrayfire.h>
#include <gtest/gtest.h>
#include <testHelpers.hpp>
#include <af/dim4.hpp>
#include <af/traits.hpp>
#include <string>
#include <vector>
using af::array;
using af::cdouble;
using af::cfloat;
using af::convolve1;
using af::dim4;
using af::dtype;
using af::dtype_traits;
using af::exception;
using af::fir;
using af::iir;
using af::randu;
using std::string;
using std::vector;
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) {
SUPPORTED_TYPE_CHECK(T);
try {
dtype ty = (dtype)dtype_traits<T>::af_type;
array x = randu(xrows, xcols, ty);
array b = randu(brows, bcols, ty);
array y = fir(b, x);
array c = 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 (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) {
SUPPORTED_TYPE_CHECK(T);
try {
dtype ty = (dtype)dtype_traits<T>::af_type;
array x = randu(xrows, xcols, ty);
array b = randu(brows, bcols, ty);
array a = randu(1, bcols, ty);
array bNorm = b / tile(a, brows);
array y = iir(b, a, x);
array c = 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 (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) {
SUPPORTED_TYPE_CHECK(T);
vector<dim4> inDims;
vector<vector<T> > inputs;
vector<vector<T> > outputs;
readTests<T, T, float>(testFile, inDims, inputs, outputs);
try {
array a = array(inDims[0], &inputs[0][0]);
array b = array(inDims[1], &inputs[1][0]);
array x = array(inDims[2], &inputs[2][0]);
array y = iir(b, a, x);
vector<T> gold = outputs[0];
ASSERT_EQ(gold.size(), (size_t)y.elements());
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 (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");
}