|
| 1 | +#include <Robocode/QDFT.h> |
| 2 | + |
| 3 | +auto bw1 = std::pow(2, (double(021) - 69) / 12) * 440; // A0 ~ 27.5 Hz |
| 4 | +auto bw2 = std::pow(2, (double(108) - 69) / 12) * 440; // C8 ~ 4186 Hz |
| 5 | +auto bw = std::make_pair(bw1, bw2); |
| 6 | + |
| 7 | +QDFT::QDFT(const double samplerate) : |
| 8 | + qdft::QDFT<float, double>(samplerate, bw), |
| 9 | + input(size()), |
| 10 | + output(size()) |
| 11 | +{ |
| 12 | + assert_true(size() > 0, "Invalid DFT size!"); |
| 13 | + |
| 14 | + const auto resolution = qdft::QDFT<float, double>::resolution(); |
| 15 | + |
| 16 | + factors.resize(size()); |
| 17 | + |
| 18 | + for (size_t i = 0; i < factors.size(); ++i) |
| 19 | + { |
| 20 | + factors[i] = std::pow(2.0, i / resolution); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +size_t QDFT::size() const |
| 25 | +{ |
| 26 | + return qdft::QDFT<float, double>::size(); |
| 27 | +} |
| 28 | + |
| 29 | +double QDFT::samplerate() const |
| 30 | +{ |
| 31 | + return qdft::QDFT<float, double>::samplerate(); |
| 32 | +} |
| 33 | + |
| 34 | +std::span<const double> QDFT::frequencies(const bool normalize) const |
| 35 | +{ |
| 36 | + if (normalize) |
| 37 | + { |
| 38 | + return factors; |
| 39 | + } |
| 40 | + |
| 41 | + return qdft::QDFT<float, double>::frequencies(); |
| 42 | +} |
| 43 | + |
| 44 | +float QDFT::transform(const float x, const std::function<void( |
| 45 | + const std::span<const std::complex<double>> dftanal, |
| 46 | + const std::span<std::complex<double>> dftsynth, |
| 47 | + const std::span<const double> dftfreqs)> callback) |
| 48 | +{ |
| 49 | + qdft(x, input.data()); |
| 50 | + |
| 51 | + std::fill(output.begin(), output.end(), std::complex<double>(0)); |
| 52 | + callback(input, output, frequencies()); |
| 53 | + output[0] = output[output.size() - 1] = 0; |
| 54 | + |
| 55 | + float y = iqdft(output.data()); |
| 56 | + y = std::clamp<float>(y, -1, +1); |
| 57 | + |
| 58 | + return y; |
| 59 | +} |
0 commit comments