2015-11-01 80 views
0

你好我運行FFT功能在iOS平臺的模擬器FFT iOS的值增加了兩倍?

像這樣

-(Matrix*) spectralDensityWithVec:(Matrix*) vector { 

    NSLog(@"Vector:%@",vector); 
    int sampleSize = [vector rows]; 

    double *r = [vector array]; // arc will handle the memory 

    float * f = (float*) malloc(sampleSize * sizeof(float)); 

    for (int i = 0;i< sampleSize;i++) { 
     f[i] = r[i]; 
    } 

    // Working with 128 samples which is 2^7 
    // 7 
    vDSP_Length log2n = log2([self nextPowerOf2WithNumber:sampleSize]); 

    FFTSetup fftSetup = vDSP_create_fftsetup(log2n,FFT_RADIX2); 

    int nOver2 = sampleSize/2; 

    COMPLEX_SPLIT A; 

    A.realp = (float *) malloc(nOver2*sizeof(float)); 
    A.imagp = (float *) malloc(nOver2*sizeof(float)); 

    vDSP_ctoz((COMPLEX*)f, 2, &A, 1, nOver2); 

    vDSP_fft_zrip(fftSetup, &A,1 ,log2n, FFT_FORWARD); 

    Matrix* PSD = [Matrix matrixOfRows:sampleSize/2 Columns:1]; 

    Matrix* fftResult = [Matrix matrixOfRows:sampleSize Columns:1]; 

    // obtain the imaginary/real parts 
    for (int i = 0;i < sampleSize/2;i++) { 
     float realp = A.realp[i]; 
     float imagp = A.imagp[i]; 

     NSLog(@"real:%g img:%g",realp,imagp); 

     float val = sqrtf((realp * realp) + (imagp * imagp)) * 2.0f /(float)sampleSize ; 

     [PSD setValue:val Row:i Column:0]; 
     [fftResult setValue:realp Row:i Column:0]; 
     [fftResult setValue:imagp Row:i*2+1 Column:0]; 
    } 

    NSLog(@"FFT Result KW Energy: %@",fftResult); 

    vDSP_destroy_fftsetup(fftSetup); 

    // release memory 
    if (f) { 
     free(f); 
     f = NULL; 
    } 
    if (A.realp) { 
     free(A.realp); 
     A.realp = NULL; 
    } 
    if (A.imagp) { 
     free(A.imagp); 
     A.imagp = NULL; 
    } 

    return PSD; 
} 

用這個載體,我得到的

[1.012817, 
    1.022833, 
    1.022028, 
    1.017877, 
    1.023680, 
    1.016974, 
    1.017746, 
    1.019263, 
    1.016417, 
    1.020745, 
    1.018643, 
    1.019521, 
    1.020260, 
    1.018041, 
    1.020829, 
    1.018644, 
    1.019398, 
    1.020399, 
    1.019222, 
    1.022093, 
    1.020433, 
    1.020534, 
    1.021503, 
    1.019931, 
    1.020480, 
    1.018945, 
    1.019238, 
    1.019989, 
    1.018917, 
    1.019762, 
    1.019021, 
    1.017887, 
    1.018136, 
    1.019543, 
    1.020242, 
    1.018757, 
    1.019534, 
    1.019862, 
    1.019060, 
    1.020717, 
    1.021055, 
    1.020178, 
    1.018108, 
    1.014776, 
    1.015374, 
    1.018429, 
    1.019895, 
    1.018647, 
    1.016387, 
    1.017053, 
    1.019572, 
    1.021097, 
    1.019488, 
    1.017218, 
    1.019876, 
    1.022022, 
    1.020574, 
    1.021588, 
    1.022298, 
    1.019369, 
    1.016980, 
    1.016774, 
    1.016079, 
    1.015292] 

上面的矢量結果的結果:

130.456100 
-0.017152 
-0.013957 
-0.020526 
-0.043666 
-0.003648 
-0.018351 
-0.006409 
-0.000965 
-0.015068 
-0.001338 
0.011731 
-0.042658 
-0.012478 
0.018228 
-0.006718 
-0.026859 
-0.013061 
-0.019885 
-0.003351 
-0.005669 
-0.002971 
-0.023977 
-0.019806 
-0.036644 
-0.055367 
-0.015106 
0.000406 
-0.002309 
-0.002857 
-0.002926 
-0.002933 
0.000000 
-0.005997 
0.000000 
-0.013211 
0.000000 
-0.030161 
0.000000 
-0.021531 
0.000000 
-0.028181 
0.000000 
-0.011275 
0.000000 
-0.009881 
0.000000 
-0.009062 
0.000000 
-0.010197 
0.000000 
0.003035 
0.000000 
0.038075 
0.000000 
0.016340 
0.000000 
0.008624 
0.000000 
0.005861 
0.000000 
0.003673 
0.000000 
0.001765 

如何與numpy fft我得到這個結果:

[65.22805000000001, 
0.0, 
-0.0085759487914117867, 
-0.015603049865923341, 
-0.0069788925854868157, 
0.015041138788651551, 
-0.010263059930525575, 
0., 
-0.021832981634872219, 
-0.026161796455543486, 
-0.0018237156004342254, 
-0.013884928458450145, 
-0.0091755911990841002, 
-0.016388522893239089, 
-0.003204667797210365, 
-0.033305086561749964, 
-0.00048255506211134121, 
-0.015783138731002323, 
-0.0075337096722261337, 
-0.0071538868820418761, 
-0.00066829905037151319, 
-0.017467879005406146, 
0.0058655519004814438, 
0.011289870623693908, 
-0.021329700649694319, 
0.0018507094402767038, 
-0.0062391410740094827, 
-0.013248826390988894, 
0.0091139373764349274, 
-0.0076327732856707516, 
-0.003359005157638993, 
0.0064289012241005097, 
-0.013430000000003162, 
-0.0029959999999995546, 
-0.0065307135571379222, 
-0.006605572447413598, 
-0.0099422652522889073, 
-0.015080331241097611, 
-0.0016757131155144375, 
-0.010765855482435674, 
-0.0028346121922537357, 
-0.014091061276246888, 
-0.0014857985251928623, 
-0.0056375351611077946, 
-0.011988911430201157, 
-0.0049400354875454924, 
-0.0099028010321836699, 
-0.0045315332341146703, 
-0.018321444937884485, 
-0.0050991387310010752, 
-0.027683215530298805, 
0.0015174037330767526, 
-0.0075530851759402738, 
0.019037454812601148, 
0.00020298122212324523, 
0.0081699113196296563, 
-0.0011547055231802498, 
0.0043124328279300489, 
-0.0014283591672329676, 
0.0029302575544578394, 
-0.001462892683058185, 
0.0018357150212062624, 
-0.0014666841715850814, 
0.00088237880086386444, 
-0.0014699999999976399, 
0.0] 

我試過多種東西,而且我沒有任何想法任何人都知道我可能做錯了什麼?我從來沒有在ios上使用過FFT函數,在檢查文檔之前,它似乎對這種情況沒有幫助。

回答

1

斑點。

比例因子2 is a property of Apple's real FFT implementation。不知道爲什麼這樣,也許它更有效率。

+0

是的,我剛剛閱讀文檔,如果我將虛構和真實部分除以2,它應該給我相同的結果。然而,有2 0和蘋果表示,他們做出了暗示。第一對和最後一對的兩個虛部爲0。當我在計算中使用矢量時,這會有所幫助嗎? –

+0

蘋果包真正傅立葉變換:https://developer.apple.com/library/ios/documentation/Performance/Conceptual/vDSP_Programming_Guide/UsingFourierTransforms/UsingFourierTransforms.html所以是的,你必須考慮到這一點時,執行計算。 –