package com.test.test2;

public class FFT {
    public static final int FFT_N_LOG = 10; // FFT_N_LOG <= 13
    public static final int FFT_N = 1 << FFT_N_LOG;
    private static final float MINY = (float) ((FFT_N << 2) * Math.sqrt(2)); // (*)
    private final float[] real, imag, sintable, costable;
    private final int[] bitReverse;

public FFT() {
        real = new float[FFT_N];
        imag = new float[FFT_N];
        sintable = new float[FFT_N >> 1];
        costable = new float[FFT_N >> 1];
        bitReverse = new int[FFT_N];

int i, j, k, reve;
        for (i = 0; i < FFT_N; i++) {
            k = i;
            for (j = 0, reve = 0; j != FFT_N_LOG; j++) {
                reve <<= 1;
                reve |= (k & 1);
                k >>>= 1;
            }
            bitReverse[i] = reve;
        }

double theta, dt = 2 * 3.14159265358979323846 / FFT_N;
        for (i = 0; i < (FFT_N >> 1); i++) {
            theta = i * dt;
            costable[i] = (float) Math.cos(theta);
            sintable[i] = (float) Math.sin(theta);
        }
    }

/**
     * 用于频谱显示的快速傅里叶变换
     *
     * @param realIO
     *            输入FFT_N个实数,也用它暂存fft后的FFT_N/2个输出值(复数模的平方)。
     */
    public void calculate(float[] realIO) {
        int i, j, k, ir, exchanges = 1, idx = FFT_N_LOG - 1;
        float cosv, sinv, tmpr, tmpi;
        for (i = 0; i != FFT_N; i++) {
            real[i] = realIO[bitReverse[i]];
            imag[i] = 0;
        }

for (i = FFT_N_LOG; i != 0; i--) {
            for (j = 0; j != exchanges; j++) {
                cosv = costable[j << idx];
                sinv = sintable[j << idx];
                for (k = j; k < FFT_N; k += exchanges << 1) {
                    ir = k + exchanges;
                    tmpr = cosv * real[ir] - sinv * imag[ir];
                    tmpi = cosv * imag[ir] + sinv * real[ir];
                    real[ir] = real[k] - tmpr;
                    imag[ir] = imag[k] - tmpi;
                    real[k] += tmpr;
                    imag[k] += tmpi;
                }
            }
            exchanges <<= 1;
            idx--;
        }

j = FFT_N >> 1;
        /*
         * 输出模的平方(的FFT_N倍):
         * for(i = 1; i <= j; i++)
         * realIO[i-1] = real[i] * real[i] + imag[i] * imag[i];
         *
         * 如果FFT只用于频谱显示,可以"淘汰"幅值较小的而减少浮点乘法运算. MINY的值
         * 和Spectrum.Y0,Spectrum.logY0对应.
         */
        sinv = MINY;
        cosv = -MINY;
        for (i = j; i != 0; i--) {
            tmpr = real[i];
            tmpi = imag[i];
            if (tmpr > cosv && tmpr < sinv && tmpi > cosv && tmpi < sinv)
                realIO[i - 1] = 0;
            else
                realIO[i - 1] = tmpr * tmpr + tmpi * tmpi;
        }
    }

public static void main(String[] args) {
        FFT fft2 = new FFT();
        float[] realIo = { 1, 2 };
        fft2.calculate(realIo);
    }
}

FFT(快速傅立叶算法 for java)的更多相关文章

  1. BZOJ 2179: FFT快速傅立叶

    2179: FFT快速傅立叶 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 2923  Solved: 1498[Submit][Status][Di ...

  2. 【bzoj2179】FFT快速傅立叶 FFT模板

    2016-06-01  09:34:54 很久很久很久以前写的了... 今天又比较了一下效率,貌似手写复数要快很多. 贴一下模板: #include<iostream> #include& ...

  3. 【BZOJ 2179】 2179: FFT快速傅立叶 (FFT)

    2179: FFT快速傅立叶 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 3308  Solved: 1720 Description 给出两个n位 ...

  4. bzoj 2179: FFT快速傅立叶 -- FFT

    2179: FFT快速傅立叶 Time Limit: 10 Sec  Memory Limit: 259 MB Description 给出两个n位10进制整数x和y,你需要计算x*y. Input ...

  5. 【BZOJ2179】FFT快速傅立叶

    [BZOJ2179]FFT快速傅立叶 Description 给出两个n位10进制整数x和y,你需要计算x*y. Input 第一行一个正整数n. 第二行描述一个位数为n的正整数x. 第三行描述一个位 ...

  6. [bzoj2179]FFT快速傅立叶_FFT

    FFT快速傅立叶 bzoj-2179 题目大意:给出两个n位10进制整数x和y,你需要计算x*y. 注释:$1\le n\le 6\times 10^4$. 想法: $FFT$入门题. $FFT$实现 ...

  7. 【CodeVS 3123】高精度练习之超大整数乘法 &【BZOJ 2197】FFT快速傅立叶

    第一次写法法塔,,,感到威力无穷啊 看了一上午算导就当我看懂了?PS:要是机房里能有个清净的看书环境就好了 FFT主要是用了巧妙的复数单位根,复数单位根在复平面上的对称性使得快速傅立叶变换的时间复杂度 ...

  8. BZOJ 2179 FFT快速傅立叶 题解

    bzoj 2179 Description 给出两个n位10进制整数x和y,你需要计算x*y. [题目分析] 高精裸题.练手. [代码] 1.手动高精 #include<cstdio> # ...

  9. FFT快速傅里叶变换算法

    1.FFT算法概要: FFT(Fast Fourier Transformation)是离散傅氏变换(DFT)的快速算法.即为快速傅氏变换.它是根据离散傅氏变换的奇.偶.虚.实等特性,对离散傅立叶变换 ...

随机推荐

  1. python mongodb MapReduce

    # -*- coding: utf-8 -*-import osimport csvimport pymongofrom pymongo import MongoClientfrom bson.cod ...

  2. js去除重复数值

    var c=[2,4,3,5,2,2,2], a = {}, i = 0; for(;i<c.length;i++){ a[c[i]] = 1 //利用对象名称不能重复的特性来去重 } c=[] ...

  3. [转载]MongoDB C# 驱动教程

    本教程基于C#驱动 v1.6.x . Api 文档见此处: http://api.mongodb.org/csharp/current/. 简介 本教程介绍由10gen支持的,用于MongoDB的C# ...

  4. 一些有用的webservice

    http://developer.51cto.com/art/200908/147125.htm 下面总结了一些常用的Web Service,是平时乱逛时收集的,希望对大家有用. ========== ...

  5. Android RelativeLayout 属性

    // 相对于给定ID控件 android:layout_above 将该控件的底部置于给定ID的控件之上; android:layout_below 将该控件的底部置于给定ID的控件之下; andro ...

  6. loadrunner_analysis技巧_average 和 90% percent

    “90% Percent Time” 表示90%的事务response time 都维持在某个值附近,不是 average response time * 90%;  “Average Time” 平 ...

  7. 找不到mysql服务或mysql服务名无效

    问题原因:mysql服务没有安装. 解决办法: 在 mysql bin目录下 以管理员的权限 执行 mysqld -install命令 出现:Service successfully installe ...

  8. comm命令——

    comm命令 :对已经有序的文件进行比较——第一列只在文件1中出现的文件,第二列只在文件2中出现的文件,第三列在文件1和文件2中同事出现的文件 请注意前提条件:             comm对文件 ...

  9. 关于 ASP.NET MVC 4 如果管理用户

    很久没上来写博客,因为自己没写博客的日子里去学了一下OBJECTIVE-C 和 ASP.NET MVC.最近在学ASP.NET MVC 4,有个问题一直在困扰着我,就是怎样管理用SIMPLE MEMB ...

  10. Android RelativeLayout

    RelativeLayout为相对布局,这种布局内的组件总是相对兄弟组件.父容器来确定的,在定义控件的位置时,需要参照其他控件的位置. 这个程序实现了一个梅花的相对布局 <?xml versio ...