File: fft_real.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 (121 lines) | stat: -rw-r--r-- 2,700 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
/*******************************************************
 * Copyright (c) 2015, 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 std::abs;
using af::cfloat;
using af::cdouble;


template<typename T>
class FFT_REAL : public ::testing::Test
{
};

typedef ::testing::Types<af::cfloat, af::cdouble> TestTypes;
TYPED_TEST_CASE(FFT_REAL, TestTypes);

template<int rank>
af::array fft(const af::array &in, double norm)
{
    switch(rank) {
    case 1: return af::fftNorm(in, norm);
    case 2: return af::fft2Norm(in, norm);
    case 3: return af::fft3Norm(in, norm);
    default: return in;
    }
}

#define MY_ASSERT_NEAR(aa, bb, cc) ASSERT_NEAR(abs(aa), abs(bb), (cc))

template<typename Tc, int rank>
void fft_real(af::dim4 dims)
{
    typedef typename af::dtype_traits<Tc>::base_type Tr;
    if (noDoubleTests<Tr>()) return;

    af::dtype ty = (af::dtype)af::dtype_traits<Tr>::af_type;
    af::array a = af::randu(dims, ty);

    bool is_odd = dims[0] & 1;

    int dim0 = dims[0] / 2 + 1;

    double norm = 1;
    for (int i = 0; i < rank; i++) norm *= dims[i];
    norm = 1/norm;

    af::array as = af::fftR2C<rank>(a, norm);
    af::array af = fft<rank>(a, norm);


    std::vector<Tc> has(as.elements());
    std::vector<Tc> haf(af.elements());

    as.host(&has[0]);
    af.host(&haf[0]);

    for (int j = 0; j < a.elements() / dims[0]; j++) {
        for (int i = 0; i < dim0; i++) {
            MY_ASSERT_NEAR(haf[j * dims[0] + i], has[j * dim0 + i], 1E-2) << "at " << j * dims[0] + i;
        }
    }

    af::array b = af::fftC2R<rank>(as, is_odd, 1);

    std::vector<Tr> ha(a.elements());
    std::vector<Tr> hb(a.elements());

    a.host(&ha[0]);
    b.host(&hb[0]);

    for (int j = 0; j < a.elements(); j++) {
        ASSERT_NEAR(ha[j], hb[j], 1E-2);
    }
}

TYPED_TEST(FFT_REAL, Even1D)
{
    fft_real<TypeParam, 1>(af::dim4(1024, 256));
}

TYPED_TEST(FFT_REAL, Odd1D)
{
    fft_real<TypeParam, 1>(af::dim4(625, 256));
}

TYPED_TEST(FFT_REAL, Even2D)
{
    fft_real<TypeParam, 2>(af::dim4(1024, 256));
}

TYPED_TEST(FFT_REAL, Odd2D)
{
    fft_real<TypeParam, 2>(af::dim4(625, 256));
}

TYPED_TEST(FFT_REAL, Even3D)
{
    fft_real<TypeParam, 3>(af::dim4(32, 32, 32));
}

TYPED_TEST(FFT_REAL, Odd3D)
{
    fft_real<TypeParam, 3>(af::dim4(25, 32, 32));
}