如何处理np.fft的输出?

时间:2015-05-23 22:27:07

标签: python numpy fft

我对如何使用FFD的输出感到困惑,这里是一个由

生成的示例输出
   result = np.abs(fftpack.fft(targetArray))[0:sample_size/2]

print result会给出一个ndarray:

 [  4.21477326e+05   3.03444591e+04   1.80682694e+04   1.05924224e+04
       1.98799134e+04   2.82620258e+04   1.39538936e+04   2.40331051e+04
       4.57465472e+04   6.41241587e+04   1.88203479e+04   1.88670469e+04
       5.42137198e+03   5.97675834e+03   1.23986101e+04   9.70841580e+02
       2.07380817e+04   4.52632516e+04   4.49493295e+04   1.03481720e+04
       2.09126074e+04   2.07691720e+04   1.22091133e+04   1.85574554e+04
       1.70652450e+04   2.53329892e+04   3.33015423e+04   2.80037092e+04
       2.22665347e+04   2.08644916e+04   1.76186194e+04   1.14038308e+04
       3.10918574e+04   2.97875178e+04   5.85659852e+04   3.77721316e+04
       1.78952068e+04   1.74037254e+04   1.80938450e+04   3.05330693e+04
       4.24407000e+04   2.74864012e+04   4.41637099e+04   3.31359768e+04
       1.74327061e+04   1.06382371e+04   2.49780963e+04   1.66725706e+04
       8.29635786e+03   2.58186162e+04   1.16012033e+04   2.63763332e+04
       1.57059736e+04   1.70297649e+04   1.03524047e+04   3.25882139e+03
       5.26665772e+03   1.15851816e+04   1.28794168e+04   1.01768783e+04
       8.62533363e+03   4.42905399e+03   4.23319771e+03   9.19616531e+03
       4.25165576e+03   4.96332367e+03   6.89522891e+03   4.04350194e+03
       1.44337315e+04   7.78488536e+03   5.85210341e+03   3.56468734e+03
       8.35331002e+03   5.41229478e+03   4.55374300e+03   1.01986739e+04
       7.04839030e+03   3.78646612e+03   7.07872186e+03   4.06017111e+03
       4.63905900e+03   9.84305110e+03   6.24621254e+03   2.76105428e+03
       3.23469868e+03   2.31035716e+03   2.15636401e+03   2.60730520e+03
       3.77829898e+03   3.40698553e+03   8.72582345e+02   1.72800115e+03
       1.83439871e+03   7.95636998e+03   4.69696233e+03   2.50245035e+03
       6.50413681e+03   4.75657064e+03   3.18621272e+03   1.38365470e+03
       6.25551674e+02   3.51399477e+03   2.96447293e+03   1.88665733e+03
       1.63347429e+03   1.92721372e+03   5.34091697e+03   2.86529694e+03
       1.80361382e+03   3.65567684e+03   2.08391205e+03   3.77302704e+03
       2.61882954e+03   1.17689735e+03   1.10303601e+03   1.46603669e+03
       1.67959657e+03   1.90800388e+03   2.35782546e+03   1.61309844e+03
       1.36326484e+03   4.06967773e+03   1.40142207e+03   1.32657523e+03
       3.17829657e+03   2.48240862e+03   1.84764188e+03   2.46198195e+03
       2.44352793e+03   1.29546145e+03   9.34633855e+02   1.42411185e+02
       1.11686208e+03   1.61629862e+03   1.82113405e+03   1.26350347e+03
       3.63268437e+03   9.33373272e+02   8.45292645e+02   1.03929325e+03
       1.65583031e+03   9.54310546e+02   1.95195173e+03   1.91535953e+03
       5.61485427e+02   1.98666296e+03   9.88850958e+02   7.80781362e+02
       1.16064386e+03   1.08425676e+03   3.95616137e+02   1.25423006e+03
       2.12467757e+03   7.12337370e+02   1.44060716e+03   7.73146781e+02
       1.05641593e+03   1.19763314e+03   1.59583780e+03   1.23434921e+03
       3.33146158e+02   1.75650022e+03   8.81978933e+02   1.28186954e+03
       1.47573928e+03   8.07757403e+02   8.84292001e+02   1.64624690e+03
       1.29680496e+03   4.76763593e+02   1.14002526e+03   1.88558087e+02
       6.21497355e+02   5.30485958e+02   1.14902281e+03   4.16705689e+02
       1.46212548e+03   1.32165278e+03   7.72060051e+02   9.39714410e+02
       1.09011170e+03   8.90859235e+02   7.67129975e+02   2.72632265e+02
       2.71574309e+02   5.28939138e+02   5.04479312e+02   4.53129779e+02
       7.42214724e+02   2.61798368e+02   4.98990728e+02   6.02745861e+02
       9.87830434e+02   2.97161466e+02   1.08718023e+03   5.87366849e+02
       3.00425885e+02   8.33291219e+02   1.31052224e+02   2.31099067e+02
       6.64652156e+02   1.32180021e+02   2.92862313e+00   2.39475444e+02
       7.71773465e+02   8.34334863e+02   7.92791780e+02   6.70180885e+02
       5.73451905e+02   4.66006885e+02   9.48437277e+02   7.04566875e+02
       2.54136392e+02   4.29167074e+02   2.69560662e+02   6.08905902e+02
       1.04487371e+03   5.70108773e+02   5.03504459e+02   7.67808997e+02
       4.38126513e+02   7.56769864e+02   7.36892665e+02   5.61631429e+02
       8.44062274e+02   8.30259267e+02   3.41959075e+02   4.06049010e+02
       1.68799150e+02   7.98590743e+02   5.24271279e+02   4.96069745e+02
       5.49647172e+02   7.41309283e+02   9.07897622e+02   1.04985345e+03
       1.00181109e+03   5.42974899e+02   7.35959741e+02   4.04694642e+02
       5.81271022e+02   2.01778038e+02   6.00141101e+02   3.80334242e+02
       6.44350585e+02   8.54890120e+02   7.12173695e+02   8.64161173e+02
       7.57346370e+02   7.92985369e+02   7.39425694e+02   4.64160309e+02
       7.04501040e+02   4.39166237e+02   1.01374899e+03   7.39703012e+02
       8.22200001e+02   4.71396567e+02   8.06529692e+02   7.18184947e+02
       7.04886010e+02   6.71256922e+02   5.19651471e+02   9.20043821e+02
       7.69576193e+02   8.78863865e+02   1.09071085e+03   9.10790235e+02
       6.99356743e+02   9.75210348e+02   7.42159855e+02   2.94034843e+02
       6.98690944e+02   7.64206208e+02   6.88827262e+02   5.81514517e+02
       1.00230881e+03   7.13219427e+02   8.59968358e+02   8.52206990e+02
       4.52436732e+02   6.05729013e+02   8.60630471e+02   7.77693596e+02
       6.06655413e+02   7.24578627e+02   6.57839491e+02   6.72231281e+02
       7.01971817e+02   4.12298654e+02   6.04044947e+02   6.71707719e+02
       6.30927816e+02   7.82746860e+02   7.94808478e+02   5.94066021e+02
       6.51161261e+02   7.95649076e+02   2.92195286e+02   4.08585488e+02
       6.10540227e+02   5.15197819e+02   5.67327416e+02   5.21334315e+02
       4.52410192e+02   7.44553730e+02   6.98824805e+02   7.93759345e+02
       5.97743322e+02   5.74907952e+02   3.85973511e+02   3.58967385e+02
       5.79438559e+02   4.50199311e+02   4.60028768e+02   4.84243380e+02
       7.86184753e+02   4.04682342e+02   5.55837013e+02   6.36922370e+02
       3.40645318e+02   5.57139578e+02   3.69777174e+02   3.78496601e+02
       5.39000001e+02   8.90982470e+02   3.24737044e+02   2.77411040e+02
       4.87813362e+02   1.67412278e+02   7.61243559e+02   3.58371802e+02
       5.23608891e+02   2.89915508e+02   5.71091257e+02   6.46835815e+02
       4.49435858e+02]

我想用这个数组来计算FFT能量,但这些数字看起来并不像一个复数。 我想写一个这样的函数:

def get_energy(input):
  energy = 0
  for e in input:
    energy = energy + sqrt(pow(e.real,2)+pow(e.imag,2))
  return energy

(我对python语言不太熟悉,抱歉这种非pythonic编码)

非常感谢..

1 个答案:

答案 0 :(得分:1)

首先,您的0x8E看起来不像复数FFT输出,因为您计算了FFT的绝对值。这从来都不复杂......

因此:

result

result = fftpack.fft(targetArray)[0:sample_size//2] 确保切片的上限是整数。)

您的sample_size//2 - 函数应该看起来像这样:

get_energy

信号的能量是平方幅度的总和......或者你可以把它写成

def get_energy(input):
    return np.sum(input*input.conj())

请注意,在def get_energy(input): return np.sum(np.abs(input)**2) 中,数学运算是按元素执行的。因此,您不必将每个元素放在for循环中。