FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
xtensor/test/test_xfft.cpp at master · xtensor-stack/xtensor · GitHub
xtensor-stack
xtensor
Repository navigation
Code
Issues
381
(381)
Pull requests
43
(43)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
xtensor
/
test
/
test_xfft.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
95 lines (81 loc) · 3.01 KB
Breadcrumbs
xtensor
/
test
/
test_xfft.cpp
Copy path
File metadata and controls
95 lines (81 loc) · 3.01 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
xtensor/containers/xarray.hpp
"
#
include
"
xtensor/misc/xfft.hpp
"
#
include
"
test_common_macros.hpp
"
namespace
xt
{
TEST
(xfft, fft_power_2)
{
size_t
k =
2
;
size_t
n =
8192
;
size_t
A =
10
;
auto
x = xt::linspace<
float
>(
0
,
static_cast
<
float
>(n -
1
), n);
xt::xarray<
float
> y = A *
xt::sin
(
2
* xt::numeric_constants<
float
>::
PI
* x * k / n);
auto
res =
xt::fft::fft
(y) / (n /
2
);
REQUIRE
(A ==
doctest::Approx
(
std::abs
(
res
(k))).
epsilon
(.
0001
));
}
TEST
(xfft, ifft_power_2)
{
size_t
k =
2
;
size_t
n =
8
;
size_t
A =
10
;
auto
x = xt::linspace<
float
>(
0
,
static_cast
<
float
>(n -
1
), n);
xt::xarray<
float
> y = A *
xt::sin
(
2
* xt::numeric_constants<
float
>::
PI
* x * k / n);
auto
res =
xt::fft::ifft
(y) / (n /
2
);
REQUIRE
(A ==
doctest::Approx
(
std::abs
(
res
(k))).
epsilon
(.
0001
));
}
TEST
(xfft, scalar_input_throws)
{
auto
scalar = xt::xarray<
float
>::
from_shape
({});
scalar
() =
1
.
0f
;
XT_EXPECT_THROW
(
xt::fft::fft
(scalar), std::runtime_error);
XT_EXPECT_THROW
(
xt::fft::ifft
(scalar), std::runtime_error);
}
TEST
(xfft, convolve_power_2)
{
xt::xarray<
float
> x = {
1.0
,
1.0
,
1.0
,
5.0
};
xt::xarray<
float
> y = {
5.0
,
1.0
,
1.0
,
1.0
};
xt::xarray<
float
> expected = {
12
,
12
,
12
,
28
};
auto
result =
xt::fft::convolve
(x, y);
for
(
size_t
i =
0
; i < x.
size
(); i++)
{
REQUIRE
(
expected
(i) ==
doctest::Approx
(
std::abs
(
result
(i))).
epsilon
(.
0001
));
}
}
TEST
(xfft, fft_n_0_axis)
{
size_t
k =
2
;
size_t
n =
10
;
size_t
A =
1
;
size_t
dim =
10
;
auto
x = xt::linspace<
float
>(
0
, n -
1
, n) * xt::ones<
float
>({dim, n});
xt::xarray<
float
> y = A *
xt::sin
(
2
* xt::numeric_constants<
float
>::
PI
* x * k / n);
y =
xt::transpose
(y);
auto
res =
xt::fft::fft
(y,
0
) / (n /
2.0
);
REQUIRE
(A ==
doctest::Approx
(
std::abs
(
res
(k,
0
))).
epsilon
(.
0001
));
REQUIRE
(A ==
doctest::Approx
(
std::abs
(
res
(k,
1
))).
epsilon
(.
0001
));
}
TEST
(xfft, fft_n_1_axis)
{
size_t
k =
2
;
size_t
n =
15
;
size_t
A =
1
;
size_t
dim =
2
;
auto
x = xt::linspace<
float
>(
0
, n -
1
, n) * xt::ones<
float
>({dim, n});
xt::xarray<
float
> y = A *
xt::sin
(
2
* xt::numeric_constants<
float
>::
PI
* x * k / n);
auto
res =
xt::fft::fft
(y) / (n /
2.0
);
REQUIRE
(A ==
doctest::Approx
(
std::abs
(
res
(
0
, k))).
epsilon
(.
0001
));
REQUIRE
(A ==
doctest::Approx
(
std::abs
(
res
(
1
, k))).
epsilon
(.
0001
));
}
TEST
(xfft, convolve_n)
{
xt::xarray<
float
> x = {
1.0
,
1.0
,
1.0
,
5.0
,
1.0
};
xt::xarray<
float
> y = {
5.0
,
1.0
,
1.0
,
1.0
,
1.0
};
xt::xarray<
size_t
> expected = {
13
,
13
,
13
,
29
,
13
};
auto
result =
xt::fft::convolve
(x, y);
xt::xarray<
float
> abs =
xt::abs
(result);
for
(
size_t
i =
0
; i < abs.
size
(); i++)
{
REQUIRE
(
expected
(i) ==
doctest::Approx
(
abs
(i)).
epsilon
(.
0001
));
}
}
}
Back
|
FazBrowse Home
|
New Git URL