簡體   English   中英

如何計算給定波的頻率和時間

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

我有速度與時間的數據。 時間步長不均勻,但 Velocity 數據是一個波。 如何使用 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 函數的模擬數據的結果在這里:

模擬數據的結果

據我所知,您的代碼基本沒問題,但缺少一些細節。 我認為您的問題主要與解釋有關。 因此,現在最好查看模擬數據,下面是我在評論中建議的模擬數據示例(我已經添加了關於重要行的評論,並添加了##以進行更改):

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

在此處輸入圖片說明

如您所見,正如預期的那樣,在 + 和 - 輸入頻率 (.01 Hz) 處有兩個峰值。

編輯
不明白為什么這種方法對 OP 的數據不起作用,我也看了看。 問題是采樣時間間隔不均勻。 這是時間的直方圖(下面的代碼)。

在此處輸入圖片說明

因此,樣本之間的時間大致平均分配在短時間和長時間之間。 我在這里快速查找了一個模式,沒有什么是明顯的。

要進行 FFT,需要均勻間隔的時間樣本,因此我進行了插值以獲得以下結果:

在此處輸入圖片說明

這是合理的(直流偏移、初級峰值和小諧波)。 這是代碼:

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