从x-y点列表进行离散傅立叶变换 [英] Discrete fourier transformation from a list of x-y points
问题描述
我想做的是,从具有周期性模式的x-y点列表中计算周期.凭借有限的数学知识,我知道傅立叶变换可以完成这种事情.
What I'm trying to do is, from a list of x-y points that has a periodic pattern, calculate the period. With my limited mathematics knowledge I know that Fourier Transformation can do this sort of thing.
我正在编写Python代码.
I'm writing Python code.
我在此处找到了相关答案,但它使用的是均匀的分布的x轴,即dt
是固定的,对我而言不是这种情况.由于我不太了解其背后的数学原理,因此不确定它是否可以在我的代码中正常工作.
I found a related answer here, but it uses an evenly-distributed x axis, i.e. dt
is fixed, which isn't the case for me. Since I don't really understand the math behind it, I'm not sure if it would work properly in my code.
我的问题是,这行得通吗?还是在numpy
中已经有一些方法可以完成我的工作?或者,我该怎么办?
My question is, does it work? Or, is there some method in numpy
that already does my work? Or, how can I do it?
编辑:所有值均为Pythonic float
(即双精度)
All values are Pythonic float
(i.e. double-precision)
推荐答案
对于间距不均匀的样品,可以使用腰部痕迹周期图.这是一个例子,信号 主频为2.5 rad/s.
For samples that are not evenly spaced, you can use scipy.signal.lombscargle
to compute the Lomb-Scargle periodogram. Here's an example, with a signal whose
dominant frequency is 2.5 rad/s.
from __future__ import division
import numpy as np
from scipy.signal import lombscargle
import matplotlib.pyplot as plt
np.random.seed(12345)
n = 100
x = np.sort(10*np.random.rand(n))
# Dominant periodic signal
y = np.sin(2.5*x)
# Add some smaller periodic components
y += 0.15*np.cos(0.75*x) + 0.2*np.sin(4*x+.1)
# Add some noise
y += 0.2*np.random.randn(x.size)
plt.figure(1)
plt.plot(x, y, 'b')
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
dxmin = np.diff(x).min()
duration = x.ptp()
freqs = np.linspace(1/duration, n/duration, 5*n)
periodogram = lombscargle(x, y, freqs)
kmax = periodogram.argmax()
print("%8.3f" % (freqs[kmax],))
plt.figure(2)
plt.plot(freqs, np.sqrt(4*periodogram/(5*n)))
plt.xlabel('Frequency (rad/s)')
plt.grid()
plt.axvline(freqs[kmax], color='r', alpha=0.25)
plt.show()
脚本打印2.497
并生成以下图:
The script prints 2.497
and generates the following plots:
这篇关于从x-y点列表进行离散傅立叶变换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!