numpy - FFT iOS double the values? -
hi running fft function in simulator on ios platform
like
-(matrix*) spectraldensitywithvec:(matrix*) vector { nslog(@"vector:%@",vector); int samplesize = [vector rows]; double *r = [vector array]; // arc handle memory float * f = (float*) malloc(samplesize * sizeof(float)); (int = 0;i< samplesize;i++) { f[i] = r[i]; } // working 128 samples 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 imaginary / real parts (int = 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; }
with vector result of
[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]
the result on vector above:
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
how ever numpy fft result:
[65.22805000000001, 0.0, -0.0085759487914117867, -0.015603049865923341, -0.0069788925854868157, 0.015041138788651551, -0.010263059930525575, 0.012321275391622862, -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]
i have tried multiple things , out of ideas know doing wrong? have never worked fft function on ios before checked documentation appears not helpful in case.
well spotted.
the scaling factor of 2 is property of apple's real fft implementation. not sure why it's way, maybe it's more efficient.
Comments
Post a Comment