python - 用傅里叶分量拟合时间序列 : estimating Fourier series coefficients

标签 python scipy time-series fft model-fitting

问题:我有一组表现出周期性变化的测量值(时间、测量值、误差),我想用以下形式的傅立叶级数拟合它们

enter image description here

其中 A0 是我测量的平均值,t 是时间,t0 是(已知)引用时间,P 是(已知)周期。我想拟合系数 A_k 和 phi_k。

这是我目前得到的:

# Find Fourier components
# nfourier is the number of fourier components 

    def fourier(coeffs, time_data, epoch, period, nfourier, A0):
       import numpy as np
       omega = 2.0*np.pi/period
       fseries = np.zeros(len(time_data))
       fseries.fill(A0)
       for k in range(nfourier):
          ak = coeffs[k]
          phik = coeffs[k+1]
          time_diff = time_data - epoch
          fseries = fseries + ak * np.cos(k * omega * time_diff + phik)

       return fseries

我估计残差如下:

def residuals(coeffs, measurement_data, time_data, error_data, epoch, period, nfourier, A0):
   model = fourier(coeffs, time_data, epoch, period, nfourier, A0)
   result = measurement_data - model
   return result

然后我将它与:

def fit_it(coeffs, measurement_data, time_data, error_data, epoch, period, nfourier, A0):
   from scipy.optimize import leastsq
   opt_coeff = leastsq(residuals, coeffs, args=(measurement_data, time_data, error_data, epoch, period, nfourier, A0))
   return opt_coeff

程序成功完成,但拟合似乎失败了,如下图所示: enter image description here

我不确定我在这里做错了什么,但也许专家可以提供一些建议。如果有人愿意提供帮助,我很乐意提供测试数据集。

最佳答案

我不明白傅里叶拟合背后的原因。我想您想知道数据的调制频率分量。我建议您每次都取数据的平均值并取 fft,它会让您更深入地了解数据的频谱。

就您的代码而言,我想发表两条评论。首先第k个元素的相位是第k+1个元素的幅度。其次,error_data 没有从函数残差中获取任何东西。你可以检查这些点。

这更像是评论,但我没有足够的声誉来发表评论。只是想帮忙。

问候

关于python - 用傅里叶分量拟合时间序列 : estimating Fourier series coefficients,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34361250/

相关文章:

python - Pandas 中的自定义时间序列重采样

python - 使用 pygame 库的简单 python 游戏的问题

python - Scipy稀疏矩阵和稀疏向量之间的欧几里德距离

python - 如何在 scipy.interpolate 中设置三次样条插值的第一个和最后一个斜率?

python - 将优化输出转储到数组中

python - 使用 Pandas 计算 12 月 - 1 月 - 2 月平均值

python - 缺少项算术级数 - 清理我的代码

python - 使用python从类中返回变量

Python 随机生成器

time-series - 使用 BoxCox 转换后如何返回原始数据?