简体   繁体   English

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

[英]How to calculate frequency of a give wave and time

I have data for Velocity vs time.我有速度与时间的数据。 The time steps are not uniform, but the Velocity data is a wave.时间步长不均匀,但 Velocity 数据是一个波。 How do I calculate the principal frequency of the velocity using FFT of Python?如何使用 Python 的 FFT 计算速度的主频率? Most of the examples I am seeing online are for uniform time stepping.我在网上看到的大多数示例都是针对统一时间步进的。

My data is like我的数据就像

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 lines like that. 10000 行这样。

Seeing some online responses, I wrote a code like the following, but it is not working:看到网上的一些回复,写了如下代码,但是不行:

#!/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()

---------------------- edit ------------------------ - - - - - - - - - - - 编辑 - - - - - - - - - - - -

with this code I am getting an output like the following figure.使用此代码,我得到如下图所示的输出。 I am not sure what this figure shows.我不确定这个数字显示了什么。 I was expecting just to see one peak in the FFT diagram我期待在 FFT 图中看到一个峰值python程序的输出

---------------------- edit ------------------------ - - - - - - - - - - - 编辑 - - - - - - - - - - - -

My results with the mock data with the sin functions suggested in the comments below are here:我在下面的评论中建议的带有 sin 函数的模拟数据的结果在这里:

模拟数据的结果

From what I can see, your code is basically fine, but missing a few details.据我所知,您的代码基本没问题,但缺少一些细节。 I think your issues are mostly about interpretation.我认为您的问题主要与解释有关。 Because of this, the mock data is the best to look at now, and here's an example with the mock data I suggested in the comments (and I've added comments about the important lines, and ## for changes):因此,现在最好查看模拟数据,下面是我在评论中建议的模拟数据示例(我已经添加了关于重要行的评论,并添加了##以进行更改):

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()

在此处输入图片说明

As you can see, there are two peaks, at + and - the input frequency (.01 Hz), as expected.如您所见,正如预期的那样,在 + 和 - 输入频率 (.01 Hz) 处有两个峰值。

Edit :编辑
Puzzled why this approach didn't work for the OP's data, I took a look at that too.不明白为什么这种方法对 OP 的数据不起作用,我也看了看。 The problem is that the sample times aren't evenly spaced.问题是采样时间间隔不均匀。 Here's a histogram of the times (code below).这是时间的直方图(下面的代码)。

在此处输入图片说明

So the time between samples is roughly evenly split between a short time and a long time.因此,样本之间的时间大致平均分配在短时间和长时间之间。 I took a quick look for a pattern here and nothing was obvious.我在这里快速查找了一个模式,没有什么是明显的。

To do an FFT, one needs evenly spaced time samples, so I interpolated to get the following:要进行 FFT,需要均匀间隔的时间样本,因此我进行了插值以获得以下结果:

在此处输入图片说明

which is reasonable (a DC offset, a primary peak and a small harmonic).这是合理的(直流偏移、初级峰值和小谐波)。 Here's the code:这是代码:

data = np.loadtxt("data.dat", usecols=(0,1))
t = data[:,0]
u = data[:,1]

tdiff = t[1:]-t[:-1]
plt.hist(tdiff)

new_times = np.linspace(min(t), max(t), len(t))
new_data = np.interp(new_times, t, u)

t, u = new_times, new_data

FFT = sy.fft(u)
freqs = syfp.fftfreq(len(u), dt)

# then plot as above

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM