C# code snippet below is an illustration of the Cooky-Turkey
algorithm, the performance may suck when processing huge datasets, but you can use arrays of double instead of arrays of complex number structure to reduce the performance impact by object initializations and method invocations(overloaded operators).
Furthermore, you can use "Butterfly" computation(http://www.cmlab.csie.ntu.edu.tw/cml/dsp/training/coding/transform/fft.html) to gain a much better performance.
Furthermore, you can use "Butterfly" computation(http://www.cmlab.csie.ntu.edu.tw/cml/dsp/training/coding/transform/fft.html) to gain a much better performance.
private Complex[] FFT(Complex[] input,bool invert)
{
if (input.Length == 1)
{
return new Complex[] { input[0] };
}
int length = input.Length;
int half = length / 2;
Complex[] result = new Complex[length];
double fac = -2.0 * Math.PI / length;
if (invert)
{
fac = -fac;
}
Complex[] evens = new Complex[half];
for (int i = 0; i < half; i++)
{
evens[i] = input[2 * i];
}
Complex[] evenResult = FFT(evens,invert);
Complex[] odds = evens;
for (int i = 0; i < half; i++)
{
odds[i] = input[2 * i + 1];
}
Complex[] oddResult = FFT(odds,invert);
for (int k = 0; k < half; k++)
{
double fack = fac * k;
Complex oddPart = oddResult[k] * new Complex(Math.Cos(fack), Math.Sin(fack));
result[k] = evenResult[k] + oddPart;
result[k + half] = evenResult[k] - oddPart;
}
return result;
}
{
if (input.Length == 1)
{
return new Complex[] { input[0] };
}
int length = input.Length;
int half = length / 2;
Complex[] result = new Complex[length];
double fac = -2.0 * Math.PI / length;
if (invert)
{
fac = -fac;
}
Complex[] evens = new Complex[half];
for (int i = 0; i < half; i++)
{
evens[i] = input[2 * i];
}
Complex[] evenResult = FFT(evens,invert);
Complex[] odds = evens;
for (int i = 0; i < half; i++)
{
odds[i] = input[2 * i + 1];
}
Complex[] oddResult = FFT(odds,invert);
for (int k = 0; k < half; k++)
{
double fack = fac * k;
Complex oddPart = oddResult[k] * new Complex(Math.Cos(fack), Math.Sin(fack));
result[k] = evenResult[k] + oddPart;
result[k + half] = evenResult[k] - oddPart;
}
return result;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 《HelloGitHub》第 106 期
· 数据库服务器 SQL Server 版本升级公告
· 深入理解Mybatis分库分表执行原理
· 使用 Dify + LLM 构建精确任务处理应用