zoukankan      html  css  js  c++  java
  • Fast Fourier Transform in C# (CookyTurkey)

    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.

    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;
    }


  • 相关阅读:
    [Tool]使用ConfuserEx混淆代码
    Python_安装官方whl包和tar.gz包
    0017_集合的补充
    0016_练习题d2
    0015_各数据类型方法代码实现
    0014_基本数据类型及常用方法剖析
    0013_运算符
    0012_编码转换
    0011_练习题d1
    0010_while循环
  • 原文地址:https://www.cnblogs.com/Dah/p/850904.html
Copyright © 2011-2022 走看看