DFT的AS2实现

来源:互联网 发布:淘宝网查看投诉的卖家 编辑:程序博客网 时间:2024/04/29 05:54

 

/**

* DFT.as   傅立叶变换(AS描述)

 * @Author     sshc625

 * @version 1.0

 */

// DFT输出结构: 模长, 相位, 实部, 虚部.

var dftObject:Object = {radii:0, radian:0, real:0, imag:0};

 

// --------------------------------------------------------------------------

var N:Number = 10;                     // 采样点数

var singalArray:Array = new Array();   // 输入信号(离散信号源)

var dftArray:Array = new Array();      // DFT输出

for(var i:Number=0; i<N; i++) {

    singalArray.push( Math.random()*10 );// 输入信号,信号源一般是A/D转换后提供的,而不是这样随机的

    dftArray.push( dftObject );        // 初始化输出

}

 

// --------------------------------------------------------------------------

// DFT过程, 时间复杂度为Math.pow(N, 2)

for(var i:Number=0; i<N; i++) {

    for(var j:Number=0; j<N; j++) {

       dftArray[i].real += singalArray[j]*Math.cos( 2*Math.PI*j*i/N );    // 计算实部

       dftArray[i].imag -= singalArray[j]*Math.sin( 2*Math.PI*j*i/N );    // 计算虚部

    }

    dftArray[i].radii = convr( dftArray[i].real, dftArray[i].imag );      // 计算模长

    dftArray[i].radian = conth( dftArray[i].real, dftArray[i].imag );     // 计算相位

   

    for(var property in dftArray[i])

       trace(property+" ----- "+dftArray[i][property]);

    trace("****************************************");

}

/*

for--i, for--j嵌套循环内, 注意到j0:

Math.cos( 2*Math.PI*j*i/N ) ---- Math.cos( 0 ) ------ 1

Math.sin( 2*Math.PI*j*i/N ) ---- Math.sin( 0 ) ------ 0

上面的嵌套循环可改写为

for(var i:Number=0; i<N; i++) {

    for(var j:Number=1; j<N; j++) {

       dftArray[i].real += singalArray[j]*Math.cos( 2*Math.PI*j*i/N );    // 计算实部

       dftArray[i].imag -= singalArray[j]*Math.sin( 2*Math.PI*j*i/N );    // 计算虚部

    }

    dftArray[i].real += singalArray[0];       // 这里必须补起来, 因为cos(0)1

   

    dftArray[i].radii = convr( dftArray[i].real, dftArray[i].imag );      // 计算模长

    dftArray[i].radii = conth( dftArray[i].real, dftArray[i].imag );      // 计算相位

}

这样就减少了一层循环

*/

 

 

// --------------------------------------------------------------------------

/**

 * 求模长

 */

function convr(reft:Number, imft:Number):Number {

    return Math.sqrt(Math.pow(reft, 2)+Math.pow(imft, 2));

}

/**

 * reft为实部, imft为虚部

 * 求复数的夹角

 */

function conth(reft:Number, imft:Number):Number {

    if (reft == 0 && imft == 0) {

       return 0;

    }

    if (reft>0 && imft>0) {

       return (Math.atan(Math.abs(imft/reft)));

    }

    if (reft<0 && imft>0) {

       return (Math.PI-Math.atan(Math.abs(imft/reft)));

    }

    if (reft<0 && imft<0) {

       return (Math.PI+Math.atan(Math.abs(imft/reft)));

    }

    if (reft>0 && imft<0) {

       return (2*Math.PI+Math.atan(Math.abs(imft/reft)));

    }

    return 0;

}

 

原创粉丝点击