data = [222, 251,212,188 , 244, 202, 198, 244, 175, 216]
import numpy as np
print("Discrete fourier transform")
print(np.fft.fft(data))
print("Inverse discrete fourier transform")
print(np.fft.ifft(data))
以上代码是使用dft
的{{1}}代码。
问题是:
如何在DFT中找到 3个主要频率和 3个主要频率的能量,例如numpy
1-D
?
另外:
在
list
或DFT
列表中还有IDFT
和2-D
以及DFT中相应的 3个主要频率和 3个主要频率中的?
答案 0 :(得分:1)
在处理离散变换时很容易犯错误,因为实现的多种变体可以对结果的数学产生直接影响。由于您的数据由所有实数组成,没有虚数,所以np.fft.rfft函数更易于使用。我提供了一些示例python / bash代码,以说明其用法以及如何为包含的数据查找能谱。
import numpy as np
data = [222, 251,212,188 , 244, 202, 198, 244, 175, 216]
#print("One-sided discrete fourier transform coefficients")
complex_one_sided_spectrum = np.fft.rfft(data, norm='ortho')
#print(complex_one_sided_spectrum)
#print("Magnitude")
one_sided_spectrum_magnitude = np.abs(complex_one_sided_spectrum)
#print(one_sided_spectrum_magnitude)
#print("Wave Numbers")
wave_numbers = np.arange(len(complex_one_sided_spectrum))
#print(wave_numbers)
#print("Energy Spectrum")
wave_energy = one_sided_spectrum_magnitude * one_sided_spectrum_magnitude
#print(wave_energy)
#output table to command line
print("\t".join(['wave numbers', 'energy spectrum', 'dft magnitude', 'dft coefficient']))
for i in range(0,len(complex_one_sided_spectrum)):
row = [str(wave_numbers[i]),
str(wave_energy[i]),
str(one_sided_spectrum_magnitude[i]),
str(complex_one_sided_spectrum[i])]
print("\t".join(row))
wave number energy spectrum dft magnitude dft coefficient
0 463110.4000000001 680.5221524682353 (680.5221524682353+0j)
1 156.53675171891524 12.511464811080884 (8.323007319698682-9.341536323065784j)
2 374.41182935941566 19.349724270888608 (13.380080448562616-13.978028349857077j)
3 1014.9632482810841 31.85848785302096 (15.39407513156416-27.892395005177345j)
4 1240.8881706405841 35.22624264153905 (-18.43972470483202+30.01440859738189j)
5 250.0 15.811388300841896 (-15.811388300841896+0j)
我将在这里使用bash(因为我只是想这样做),但是它很容易转换为python代码。
$ python 52675886.py | sort --key=2 --reverse | column -t -s $'\t' | head -n 4
wave number energy spectrum dft magnitude dft coefficient
0 463110.4000000001 680.5221524682353 (680.5221524682353+0j)
2 374.41182935941566 19.349724270888608 (13.380080448562616-13.978028349857077j)
5 250.0 15.811388300841896 (-15.811388300841896+0j)
因此,主要的三个波分别是0、2和5。要将波数转换为频率,您需要知道数据的采样率。
更新
# convert attribute arrays into a list of wave dictionaries
waves = []
for i in wave_numbers:
wave = {'wave_number': wave_numbers[i],
'wave_energy': wave_energy[i],
'one_sided_spectrum_magnitude': one_sided_spectrum_magnitude[i],
'complex_one_sided_spectrum': complex_one_sided_spectrum[i]}
waves.append(wave)
#print(waves)
# tell sorted that we want to compare the wave_energy
def wave_energy_comparison_key(wave):
return wave['wave_energy']
sorted_waves = sorted(waves, key=wave_energy_comparison_key, reverse=True)
#print(sorted_waves)
top_3_waves = [sorted_waves[i] for i in range(0,3)]
print(top_3_waves)