跳到正文

科技·工程

FFT

litjohn约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年2月22日
收录
2026年2月22日
更新
2026年11月8日
篇幅
约 7 分钟

版本

  1. v1当前

AI 作品。笑点自寻。

不过其实挺深刻的。

C++
#include <iostream>
#include <complex>
#include <vector>
#include <cmath>
#include <iomanip>

using Complex = std::complex<double>;
constexpr double PI = 3.14159265358979323846;

// 强制编译器内联,坚决不留任何函数调用栈
#if defined(__GNUC__) || defined(__clang__)
    #define FORCE_INLINE __attribute__((always_inline)) inline
#elif defined(_MSC_VER)
    #define FORCE_INLINE __forceinline
#else
    #define FORCE_INLINE inline
#endif

// ==========================================================
// 1. 蝴蝶操作(Butterfly)的模板完全展开
// ==========================================================
template <int N, int I>
struct Butterfly {
    FORCE_INLINE static void apply(Complex* out) {
        // 编译期常量计算旋转因子 W_N^I (编译器在 -O3 下会直接将其折叠为常数)
        constexpr double angle = -2.0 * PI * I / N;
        const Complex w(std::cos(angle), std::sin(angle));

        // 核心蝴蝶计算
        Complex t = w * out[I + N / 2];
        Complex u = out[I];

        out[I]         = u + t;
        out[I + N / 2] = u - t;

        // 递归实例化下一个蝴蝶操作
        Butterfly<N, I + 1>::apply(out);
    }
};

// 蝴蝶操作递归终点
template <int N>
struct Butterfly<N, N / 2> {
    FORCE_INLINE static void apply(Complex* /*out*/) {
        // 递归终止,什么都不做
    }
};

// ==========================================================
// 2. FFT 递归深度的模板完全展开 (分治阶段)
// ==========================================================
template <int N, int Stride = 1>
struct FFT_Unrolled {
    FORCE_INLINE static void apply(Complex* out, const Complex* in) {
        // 1. 编译期静态分治:偶数部分和奇数部分
        FFT_Unrolled<N / 2, Stride * 2>::apply(out, in);
        FFT_Unrolled<N / 2, Stride * 2>::apply(out + N / 2, in + Stride);

        // 2. 对当前层执行完全展开的蝴蝶操作
        Butterfly<N, 0>::apply(out);
    }
};

// FFT 分治递归终点 (N = 1)
template <int Stride>
struct FFT_Unrolled<1, Stride> {
    FORCE_INLINE static void apply(Complex* out, const Complex* in) {
        out[0] = in[0]; // 直接赋值,底层汇编就是一条 MOV 指令
    }
};

// ==========================================================
// 3. 动态分派器 (运行时根据 k 调用对应的实例化代码)
// ==========================================================
void fft_dispatch(int k, Complex* out, const Complex* in) {
    switch (k) {
        case 0: FFT_Unrolled<1>::apply(out, in); break;
        case 1: FFT_Unrolled<2>::apply(out, in); break;
        case 2: FFT_Unrolled<4>::apply(out, in); break;
        case 3: FFT_Unrolled<8>::apply(out, in); break;
        case 4: FFT_Unrolled<16>::apply(out, in); break;
        case 5: FFT_Unrolled<32>::apply(out, in); break;
        case 6: FFT_Unrolled<64>::apply(out, in); break;
        // 你可以继续写到 7, 8... 但建议不要超过 10,否则编译器会崩溃
        default:
            std::cerr << "K out of bounds for unrolled FFT!" << std::endl;
    }
}

// ==========================================================
// 4. 测试代码
// ==========================================================
int main() {
    constexpr int K = 3;
    constexpr int N = 1 << K; // N = 8

    std::vector<Complex> in(N);
    std::vector<Complex> out(N);

    // 构造一个简单的测试信号
    for (int i = 0; i < N; ++i) {
        in[i] = Complex(i, 0);
    }

    // 调用 K=3 的展开 FFT
    fft_dispatch(K, out.data(), in.data());

    // 打印结果
    std::cout << std::fixed << std::setprecision(2);
    for (int i = 0; i < N; ++i) {
        std::cout << "out[" << i << "] = " << out[i].real()
                  << " + " << out[i].imag() << "i\n";
    }

    return 0;
}
原文