forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfft.cpp
More file actions
381 lines (302 loc) · 15.3 KB
/
fft.cpp
File metadata and controls
381 lines (302 loc) · 15.3 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*******************************************************
* 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 <stdexcept>
#include <testHelpers.hpp>
using std::string;
using std::vector;
using af::cfloat;
using af::cdouble;
TEST(fft, Invalid_Array)
{
if (noDoubleTests<float>()) return;
vector<float> in(10000,1);
af_array inArray = 0;
af_array outArray = 0;
af::dim4 dims(10,10,10,1);
ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &(in.front()),
dims.ndims(), dims.get(), (af_dtype) af::dtype_traits<float>::af_type));
ASSERT_EQ(AF_ERR_SIZE, af_fft(&outArray, inArray, 1.0, 0));
ASSERT_EQ(AF_SUCCESS, af_destroy_array(inArray));
}
TEST(fft2, Invalid_Array)
{
if (noDoubleTests<float>()) return;
vector<float> in(100,1);
af_array inArray = 0;
af_array outArray = 0;
af::dim4 dims(5,5,2,2);
ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &(in.front()),
dims.ndims(), dims.get(), (af_dtype) af::dtype_traits<float>::af_type));
ASSERT_EQ(AF_ERR_SIZE, af_fft2(&outArray, inArray, 1.0, 0, 0));
ASSERT_EQ(AF_SUCCESS, af_destroy_array(inArray));
}
TEST(fft3, Invalid_Array)
{
if (noDoubleTests<float>()) return;
vector<float> in(100,1);
af_array inArray = 0;
af_array outArray = 0;
af::dim4 dims(10,10,1,1);
ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &(in.front()),
dims.ndims(), dims.get(), (af_dtype) af::dtype_traits<float>::af_type));
ASSERT_EQ(AF_ERR_SIZE, af_fft3(&outArray, inArray, 1.0, 0, 0, 0));
ASSERT_EQ(AF_SUCCESS, af_destroy_array(inArray));
}
TEST(ifft1, Invalid_Array)
{
if (noDoubleTests<float>()) return;
vector<float> in(100,1);
af_array inArray = 0;
af_array outArray = 0;
af::dim4 dims(5,5,4,1);
ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &(in.front()),
dims.ndims(), dims.get(), (af_dtype) af::dtype_traits<cfloat>::af_type));
ASSERT_EQ(AF_ERR_SIZE, af_ifft(&outArray, inArray, 0.01, 0));
ASSERT_EQ(AF_SUCCESS, af_destroy_array(inArray));
}
TEST(ifft2, Invalid_Array)
{
if (noDoubleTests<float>()) return;
vector<float> in(100,1);
af_array inArray = 0;
af_array outArray = 0;
af::dim4 dims(100,1,1,1);
ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &(in.front()),
dims.ndims(), dims.get(), (af_dtype) af::dtype_traits<cfloat>::af_type));
ASSERT_EQ(AF_ERR_SIZE, af_ifft2(&outArray, inArray, 0.01, 0, 0));
ASSERT_EQ(AF_SUCCESS, af_destroy_array(inArray));
}
TEST(ifft3, Invalid_Array)
{
if (noDoubleTests<float>()) return;
vector<float> in(100,1);
af_array inArray = 0;
af_array outArray = 0;
af::dim4 dims(10,10,1,1);
ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &(in.front()),
dims.ndims(), dims.get(), (af_dtype) af::dtype_traits<cfloat>::af_type));
ASSERT_EQ(AF_ERR_SIZE, af_ifft3(&outArray, inArray, 0.01, 0, 0, 0));
ASSERT_EQ(AF_SUCCESS, af_destroy_array(inArray));
}
template<typename inType, typename outType, bool isInverse>
void fftTest(string pTestFile, dim_type pad0=0, dim_type pad1=0, dim_type pad2=0)
{
if (noDoubleTests<inType>()) return;
if (noDoubleTests<outType>()) return;
vector<af::dim4> numDims;
vector<vector<inType> > in;
vector<vector<outType> > tests;
readTestsFromFile<inType, outType>(pTestFile, numDims, in, tests);
af::dim4 dims = numDims[0];
af_array outArray = 0;
af_array inArray = 0;
ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &(in[0].front()),
dims.ndims(), dims.get(), (af_dtype)af::dtype_traits<inType>::af_type));
if (isInverse){
switch (dims.ndims()) {
case 1 : ASSERT_EQ(AF_SUCCESS, af_ifft (&outArray, inArray, 1.0, pad0)); break;
case 2 : ASSERT_EQ(AF_SUCCESS, af_ifft2(&outArray, inArray, 1.0, pad0, pad1)); break;
case 3 : ASSERT_EQ(AF_SUCCESS, af_ifft3(&outArray, inArray, 1.0, pad0, pad1, pad2)); break;
default: throw std::runtime_error("This error shouldn't happen, pls check");
}
} else {
switch(dims.ndims()) {
case 1 : ASSERT_EQ(AF_SUCCESS, af_fft (&outArray, inArray, 1.0, pad0)); break;
case 2 : ASSERT_EQ(AF_SUCCESS, af_fft2(&outArray, inArray, 1.0, pad0, pad1)); break;
case 3 : ASSERT_EQ(AF_SUCCESS, af_fft3(&outArray, inArray, 1.0, pad0, pad1, pad2)); break;
default: throw std::runtime_error("This error shouldn't happen, pls check");
}
}
size_t out_size = tests[0].size();
outType *outData= new outType[out_size];
ASSERT_EQ(AF_SUCCESS, af_get_data_ptr((void*)outData, outArray));
vector<outType> goldBar(tests[0].begin(), tests[0].end());
size_t test_size = 0;
switch(dims.ndims()) {
case 1 : test_size = dims[0]/2+1; break;
case 2 : test_size = dims[1] * (dims[0]/2+1); break;
case 3 : test_size = dims[2] * dims[1] * (dims[0]/2+1); break;
default : test_size = dims[0]/2+1; break;
}
for (size_t elIter=0; elIter<test_size; ++elIter) {
bool isUnderTolerance = std::abs(goldBar[elIter]-outData[elIter])<0.001;
ASSERT_EQ(true, isUnderTolerance)<<
"Expected value="<<goldBar[elIter] <<"\t Actual Value="<<
outData[elIter] << " at: " << elIter<< std::endl;
}
// cleanup
delete[] outData;
ASSERT_EQ(AF_SUCCESS, af_destroy_array(inArray));
ASSERT_EQ(AF_SUCCESS, af_destroy_array(outArray));
}
#define INSTANTIATE_TEST(func, name, is_inverse, in_t, out_t, ...) \
TEST(func, name) \
{ \
fftTest<in_t, out_t, is_inverse>(__VA_ARGS__); \
}
// Real to complex transforms
INSTANTIATE_TEST(fft , R2C_Float, false, float, cfloat, string(TEST_DIR"/signal/fft_r2c.test") );
INSTANTIATE_TEST(fft , R2C_Double, false, double, cdouble, string(TEST_DIR"/signal/fft_r2c.test") );
INSTANTIATE_TEST(fft2, R2C_Float, false, float, cfloat, string(TEST_DIR"/signal/fft2_r2c.test"));
INSTANTIATE_TEST(fft2, R2C_Double, false, double, cdouble, string(TEST_DIR"/signal/fft2_r2c.test"));
INSTANTIATE_TEST(fft3, R2C_Float, false, float, cfloat, string(TEST_DIR"/signal/fft3_r2c.test"));
INSTANTIATE_TEST(fft3, R2C_Double, false, double, cdouble, string(TEST_DIR"/signal/fft3_r2c.test"));
// complex to complex transforms
INSTANTIATE_TEST(fft , C2C_Float, false, cfloat, cfloat, string(TEST_DIR"/signal/fft_c2c.test") );
INSTANTIATE_TEST(fft , C2C_Double, false, cdouble, cdouble, string(TEST_DIR"/signal/fft_c2c.test") );
INSTANTIATE_TEST(fft2, C2C_Float, false, cfloat, cfloat, string(TEST_DIR"/signal/fft2_c2c.test"));
INSTANTIATE_TEST(fft2, C2C_Double, false, cdouble, cdouble, string(TEST_DIR"/signal/fft2_c2c.test"));
INSTANTIATE_TEST(fft3, C2C_Float, false, cfloat, cfloat, string(TEST_DIR"/signal/fft3_c2c.test"));
INSTANTIATE_TEST(fft3, C2C_Double, false, cdouble, cdouble, string(TEST_DIR"/signal/fft3_c2c.test"));
// transforms on padded and truncated arrays
INSTANTIATE_TEST(fft2, R2C_Float_Trunc, false, float, cfloat, string(TEST_DIR"/signal/fft2_r2c_trunc.test"), 16, 16);
INSTANTIATE_TEST(fft2, R2C_Double_Trunc, false, double, cdouble, string(TEST_DIR"/signal/fft2_r2c_trunc.test"), 16, 16);
INSTANTIATE_TEST(fft2, C2C_Float_Pad, false, cfloat, cfloat, string(TEST_DIR"/signal/fft2_c2c_pad.test"), 16, 16);
INSTANTIATE_TEST(fft2, C2C_Double_Pad, false, cdouble, cdouble, string(TEST_DIR"/signal/fft2_c2c_pad.test"), 16, 16);
// inverse transforms
// complex to complex transforms
INSTANTIATE_TEST(ifft , C2C_Float, true, cfloat, cfloat, string(TEST_DIR"/signal/ifft_c2c.test") );
INSTANTIATE_TEST(ifft , C2C_Double, true, cdouble, cdouble, string(TEST_DIR"/signal/ifft_c2c.test") );
INSTANTIATE_TEST(ifft2, C2C_Float, true, cfloat, cfloat, string(TEST_DIR"/signal/ifft2_c2c.test"));
INSTANTIATE_TEST(ifft2, C2C_Double, true, cdouble, cdouble, string(TEST_DIR"/signal/ifft2_c2c.test"));
INSTANTIATE_TEST(ifft3, C2C_Float, true, cfloat, cfloat, string(TEST_DIR"/signal/ifft3_c2c.test"));
INSTANTIATE_TEST(ifft3, C2C_Double, true, cdouble, cdouble, string(TEST_DIR"/signal/ifft3_c2c.test"));
template<typename inType, typename outType, int rank, bool isInverse>
void fftBatchTest(string pTestFile, dim_type pad0=0, dim_type pad1=0, dim_type pad2=0)
{
if (noDoubleTests<inType>()) return;
if (noDoubleTests<outType>()) return;
vector<af::dim4> numDims;
vector<vector<inType> > in;
vector<vector<outType> > tests;
readTestsFromFile<inType, outType>(pTestFile, numDims, in, tests);
af::dim4 dims = numDims[0];
af_array outArray = 0;
af_array inArray = 0;
ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &(in[0].front()),
dims.ndims(), dims.get(), (af_dtype)af::dtype_traits<inType>::af_type));
if(isInverse) {
switch(rank) {
case 1 : ASSERT_EQ(AF_SUCCESS, af_ifft (&outArray, inArray, 1.0, pad0)); break;
case 2 : ASSERT_EQ(AF_SUCCESS, af_ifft2(&outArray, inArray, 1.0, pad0, pad1)); break;
case 3 : ASSERT_EQ(AF_SUCCESS, af_ifft3(&outArray, inArray, 1.0, pad0, pad1, pad2)); break;
default: throw std::runtime_error("This error shouldn't happen, pls check");
}
} else {
switch(rank) {
case 1 : ASSERT_EQ(AF_SUCCESS, af_fft (&outArray, inArray, 1.0, pad0)); break;
case 2 : ASSERT_EQ(AF_SUCCESS, af_fft2(&outArray, inArray, 1.0, pad0, pad1)); break;
case 3 : ASSERT_EQ(AF_SUCCESS, af_fft3(&outArray, inArray, 1.0, pad0, pad1, pad2)); break;
default: throw std::runtime_error("This error shouldn't happen, pls check");
}
}
size_t out_size = tests[0].size();
outType *outData= new outType[out_size];
ASSERT_EQ(AF_SUCCESS, af_get_data_ptr((void*)outData, outArray));
vector<outType> goldBar(tests[0].begin(), tests[0].end());
size_t test_size = 0;
size_t batch_count = dims[rank];
switch(rank) {
case 1 : test_size = dims[0]/2+1; break;
case 2 : test_size = dims[1] * (dims[0]/2+1); break;
case 3 : test_size = dims[2] * dims[1] * (dims[0]/2+1); break;
default : test_size = dims[0]/2+1; break;
}
size_t batch_stride = 1;
for(int i=0; i<rank; ++i) batch_stride *= dims[i];
for(size_t batchId=0; batchId<batch_count; ++batchId) {
size_t off = batchId*batch_stride;
for (size_t elIter=0; elIter<test_size; ++elIter) {
bool isUnderTolerance = std::abs(goldBar[elIter+off]-outData[elIter+off])<0.001;
ASSERT_EQ(true, isUnderTolerance)<<"Batch id = "<<batchId<<
"; Expected value="<<goldBar[elIter+off] <<"\t Actual Value="<<
outData[elIter+off] << " at: " << elIter<< std::endl;
}
}
// cleanup
delete[] outData;
ASSERT_EQ(AF_SUCCESS, af_destroy_array(inArray));
ASSERT_EQ(AF_SUCCESS, af_destroy_array(outArray));
}
#define INSTANTIATE_BATCH_TEST(func, name, rank, is_inverse, in_t, out_t, ...) \
TEST(func, name##_Batch) \
{ \
fftBatchTest<in_t, out_t, rank, is_inverse>(__VA_ARGS__); \
}
// real to complex transforms
INSTANTIATE_BATCH_TEST(fft , R2C_Float, 1, false, float, cfloat, string(TEST_DIR"/signal/fft_r2c_batch.test") );
INSTANTIATE_BATCH_TEST(fft2, R2C_Float, 2, false, float, cfloat, string(TEST_DIR"/signal/fft2_r2c_batch.test"));
INSTANTIATE_BATCH_TEST(fft3, R2C_Float, 3, false, float, cfloat, string(TEST_DIR"/signal/fft3_r2c_batch.test"));
// complex to complex transforms
INSTANTIATE_BATCH_TEST(fft , C2C_Float, 1, false, cfloat, cfloat, string(TEST_DIR"/signal/fft_c2c_batch.test") );
INSTANTIATE_BATCH_TEST(fft2, C2C_Float, 2, false, cfloat, cfloat, string(TEST_DIR"/signal/fft2_c2c_batch.test"));
INSTANTIATE_BATCH_TEST(fft3, C2C_Float, 3, false, cfloat, cfloat, string(TEST_DIR"/signal/fft3_c2c_batch.test"));
// inverse transforms
// complex to complex transforms
INSTANTIATE_BATCH_TEST(ifft , C2C_Float, 1, true, cfloat, cfloat, string(TEST_DIR"/signal/ifft_c2c_batch.test") );
INSTANTIATE_BATCH_TEST(ifft2, C2C_Float, 2, true, cfloat, cfloat, string(TEST_DIR"/signal/ifft2_c2c_batch.test"));
INSTANTIATE_BATCH_TEST(ifft3, C2C_Float, 3, true, cfloat, cfloat, string(TEST_DIR"/signal/ifft3_c2c_batch.test"));
// transforms on padded and truncated arrays
INSTANTIATE_BATCH_TEST(fft2, R2C_Float_Trunc, 2, false, float, cfloat, string(TEST_DIR"/signal/fft2_r2c_trunc_batch.test"), 16, 16);
INSTANTIATE_BATCH_TEST(fft2, R2C_Double_Trunc, 2, false, double, cdouble, string(TEST_DIR"/signal/fft2_r2c_trunc_batch.test"), 16, 16);
INSTANTIATE_BATCH_TEST(fft2, C2C_Float_Pad, 2, false, cfloat, cfloat, string(TEST_DIR"/signal/fft2_c2c_pad_batch.test"), 16, 16);
INSTANTIATE_BATCH_TEST(fft2, C2C_Double_Pad, 2, false, cdouble, cdouble, string(TEST_DIR"/signal/fft2_c2c_pad_batch.test"), 16, 16);
/////////////////////////////////////// CPP ////////////////////////////////////
//
template<typename inType, typename outType, bool isInverse>
void cppFFTTest(string pTestFile, dim_type pad0=0, dim_type pad1=0, dim_type pad2=0)
{
if (noDoubleTests<inType>()) return;
if (noDoubleTests<outType>()) return;
vector<af::dim4> numDims;
vector<vector<inType> > in;
vector<vector<outType> > tests;
readTestsFromFile<inType, outType>(pTestFile, numDims, in, tests);
af::dim4 dims = numDims[0];
af::array signal(dims, &(in[0].front()));
af::array output;
if (isInverse){
output = ifft3(signal, 1.0);
} else {
output = fft3(signal, 1.0);
}
size_t out_size = tests[0].size();
cfloat *outData= new cfloat[out_size];
output.host((void*)outData);
vector<cfloat> goldBar(tests[0].begin(), tests[0].end());
size_t test_size = 0;
switch(dims.ndims()) {
case 1 : test_size = dims[0]/2+1; break;
case 2 : test_size = dims[1] * (dims[0]/2+1); break;
case 3 : test_size = dims[2] * dims[1] * (dims[0]/2+1); break;
default : test_size = dims[0]/2+1; break;
}
for (size_t elIter=0; elIter<test_size; ++elIter) {
bool isUnderTolerance = std::abs(goldBar[elIter]-outData[elIter])<0.001;
ASSERT_EQ(true, isUnderTolerance)<<
"Expected value="<<goldBar[elIter] <<"\t Actual Value="<<
outData[elIter] << " at: " << elIter<< std::endl;
}
// cleanup
delete[] outData;
}
TEST(fft3, CPP)
{
cppFFTTest<cfloat, cfloat, false>(string(TEST_DIR"/signal/fft3_c2c.test"));
}
TEST(ifft3, CPP)
{
cppFFTTest<cfloat, cfloat, true>(string(TEST_DIR"/signal/ifft3_c2c.test"));
}