Skip to content

Commit 3227558

Browse files
committed
Implement dft
1 parent b9acf8c commit 3227558

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

contents/cooley_tukey/code/c++/fft.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ using std::size_t;
2020
using complex = std::complex<double>;
2121
static const double pi = 3.14159265358979323846264338327950288419716;
2222

23+
template <typename Iter>
24+
void dft(Iter X, Iter last) {
25+
const auto N = last - X;
26+
std::vector<complex> tmp(N);
27+
for (auto i = 0; i < N; ++i) {
28+
for (auto j = 0; j < N; ++j) {
29+
using namespace std::literals::complex_literals;
30+
tmp[i] += X[j] * exp(-2.0 * M_PI * i * j / N * 1i);
31+
}
32+
}
33+
std::copy(std::begin(tmp), std::end(tmp), X);
34+
}
35+
2336
// `cooley_tukey` does the cooley-tukey algorithm, recursively
2437
template <typename Iter>
2538
void cooley_tukey(Iter first, Iter last) {

contents/cooley_tukey/cooley_tukey.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ In the end, the code looks like:
119119
{% sample lang="c" %}
120120
[import:20-39, lang:"c_cpp"](code/c/fft.c)
121121
{% sample lang="cpp" %}
122-
[import:27-57, lang:"c_cpp"](code/c++/fft.cpp)
122+
[import:36-67, lang:"c_cpp"](code/c++/fft.cpp)
123123
{% sample lang="hs" %}
124124
[import:6-19, lang:"haskell"](code/haskell/fft.hs)
125125
{% sample lang="py" %}

0 commit comments

Comments
 (0)