blob: 0bc35e44d5874e02de190894a9a4bb302b8fad55 (
plain)
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
|
/*
* Copyright (c) 2021, Cesar Torres <shortanemoia@protonmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Complex.h>
#include <AK/Math.h>
#include <AK/Span.h>
namespace DSP {
constexpr void fft(Span<Complex<float>> sample_data, bool invert = false)
{
int n = sample_data.size();
for (int i = 1, j = 0; i < n; i++) {
int bit = n >> 1;
for (; j & bit; bit >>= 1)
j ^= bit;
j ^= bit;
if (i < j)
swap(sample_data[i], sample_data[j]);
}
for (int len = 2; len <= n; len <<= 1) {
float ang = 2 * AK::Pi<float> / static_cast<float>(len * (invert ? -1 : 1));
Complex<float> wlen = Complex<float>::from_polar(1.f, ang);
for (int i = 0; i < n; i += len) {
Complex<float> w = { 1., 0. };
for (int j = 0; j < len / 2; j++) {
Complex<float> u = sample_data[i + j];
Complex<float> v = sample_data[i + j + len / 2] * w;
sample_data[i + j] = u + v;
sample_data[i + j + len / 2] = u - v;
w *= wlen;
}
}
}
if (invert) {
for (int i = 0; i < n; i++)
sample_data[i] /= n;
}
}
}
|