如何计算给定波的频率和时间

问题描述 投票:4回答:1

我有速度与时间的数据。时间步长不一致,但是力度数据是波动的。如何使用Python的FFT计算速度的主频率?我在网上看到的大多数示例都是为了统一时间。

我的数据就像

7.56683038E+02  2.12072850E-01 
7.56703750E+02  2.13280844E-01
7.56724461E+02  2.14506402E-01
7.56745172E+02  2.15748934E-01
7.56765884E+02  2.17007907E-01
7.56786595E+02  2.18282753E-01

10000行。

[看到一些在线回复,我编写了如下代码,但它不起作用:

#!/usr/bin/env python

import numpy as np
import scipy as sy
import scipy.fftpack as syfp
import pylab as pyl

# Calculate the number of data points
length = 0
for line in open("data.dat"):
    length = length + 1

# Read in data from file here
t = np.zeros(shape=(length,1))
u = np.zeros(shape=(length,1))


length = 0
for line in open("data.dat"):
    columns = line.split(' ')
    t[length] = float(columns[0])
    u[length] = float(columns[1])
    length = length + 1

# Do FFT analysis of array
FFT = sy.fft(u)

# Getting the related frequencies
freqs = syfp.fftfreq(len(u))

# Create subplot windows and show plot
pyl.subplot(211)
pyl.plot(t, u)
pyl.xlabel('Time')
pyl.ylabel('Amplitude')
pyl.subplot(212)
pyl.plot(freqs, sy.log10(FFT), 'x')
pyl.show()

----------------------编辑------------------------] >

使用此代码,我得到如下图所示的输出。我不确定这个数字显示了什么。我期望在FFT图中看到一个峰值“由python程序输出”

----------------------编辑------------------------] >

我在以下注释中建议的带有sin函数的模拟数据的结果在这里:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9YY0U0NC5wbmcifQ==” alt =“模拟数据结果”>

我有速度与时间的数据。时间步长不一致,但是力度数据是波动的。如何使用Python的FFT计算速度的主频率?大多数示例...

python numpy scipy fft dft
1个回答
5
投票

据我所知,您的代码基本上不错,但是缺少一些细节。我认为您的问题主要与解释有关。因此,最好看一下模拟数据,这是我在注释中建议的模拟数据示例(并且我已经添加了有关重要行的注释,并添加了##来进行更改):

import numpy as np
import scipy as sy
import scipy.fftpack as syfp
import pylab as pyl

dt = 0.02071 
t = dt*np.arange(100000)             ## time at the same spacing of your data
u = np.sin(2*np.pi*t*.01)            ## Note freq=0.01 Hz

# Do FFT analysis of array
FFT = sy.fft(u)

# Getting the related frequencies
freqs = syfp.fftfreq(len(u), dt)     ## added dt, so x-axis is in meaningful units

# Create subplot windows and show plot
pyl.subplot(211)
pyl.plot(t, u)
pyl.xlabel('Time')
pyl.ylabel('Amplitude')
pyl.subplot(212)
pyl.plot(freqs, sy.log10(abs(FFT)), '.')  ## it's important to have the abs here
pyl.xlim(-.05, .05)                       ## zoom into see what's going on at the peak
pyl.show()
© www.soinside.com 2019 - 2024. All rights reserved.