gpt4 book ai didi

python - 适合 Python 的傅里叶级数

转载 作者:太空宇宙 更新时间:2023-11-03 13:58:24 25 4
gpt4 key购买 nike

我有一些数据想要使用二阶、三阶或四阶傅里叶级数来拟合。

同时 this关于堆栈溢出的问题和答案接近我想用 scipy 做的事情,他们已经将他们的系数预先定义为 tau = 0.045。我希望我的拟合能找到具有 95% 置信区间的可能系数(a0、w1、w2、w3 等),就像 MATLAB curve fit 一样相当于傅里叶级数。我看到的另一个选项是使用 fourier_series from sympy然而,此函数仅适用于适合定义函数的符号参数,而不适用于原始数据。

1) 有没有办法让 sympy fourier_series 接收原始数据而不是函数或其他使用此库的变通方法?

2) 或在存在多个未知数(系数)的情况下对数据进行 scipy 曲线拟合

最佳答案

如果您愿意,您可以非常接近 sympy 数据拟合代码,使用我为此目的编写的名为 symfit 的包。它基本上使用 sympy 接口(interface)包装了 scipy。使用 symfit,您可以执行如下操作:

from symfit import parameters, variables, sin, cos, Fit
import numpy as np
import matplotlib.pyplot as plt

def fourier_series(x, f, n=0):
"""
Returns a symbolic fourier series of order `n`.

:param n: Order of the fourier series.
:param x: Independent variable
:param f: Frequency of the fourier series
"""
# Make the parameter objects for all the terms
a0, *cos_a = parameters(','.join(['a{}'.format(i) for i in range(0, n + 1)]))
sin_b = parameters(','.join(['b{}'.format(i) for i in range(1, n + 1)]))
# Construct the series
series = a0 + sum(ai * cos(i * f * x) + bi * sin(i * f * x)
for i, (ai, bi) in enumerate(zip(cos_a, sin_b), start=1))
return series

x, y = variables('x, y')
w, = parameters('w')
model_dict = {y: fourier_series(x, f=w, n=3)}
print(model_dict)

这将打印出我们想要的符号模型:

{y: a0 + a1*cos(w*x) + a2*cos(2*w*x) + a3*cos(3*w*x) + b1*sin(w*x) + b2*sin(2*w*x) + b3*sin(3*w*x)}

接下来,我将把它拟合到一个简单的阶跃函数中,向您展示它是如何工作的:

# Make step function data
xdata = np.linspace(-np.pi, np.pi)
ydata = np.zeros_like(xdata)
ydata[xdata > 0] = 1
# Define a Fit object for this model and data
fit = Fit(model_dict, x=xdata, y=ydata)
fit_result = fit.execute()
print(fit_result)

# Plot the result
plt.plot(xdata, ydata)
plt.plot(xdata, fit.model(x=xdata, **fit_result.params).y, color='green', ls=':')

这将打印:

Parameter Value        Standard Deviation
a0 5.000000e-01 2.075395e-02
a1 -4.903805e-12 3.277426e-02
a2 5.325068e-12 3.197889e-02
a3 -4.857033e-12 3.080979e-02
b1 6.267589e-01 2.546980e-02
b2 1.986491e-02 2.637273e-02
b3 1.846406e-01 2.725019e-02
w 8.671471e-01 3.132108e-02
Fitting status message: Optimization terminated successfully.
Number of iterations: 44
Regression Coefficient: 0.9401712713086535

并产生以下情节:

enter image description here

就这么简单!我把剩下的留给你想象。有关更多信息,您可以找到 documentation here .

关于python - 适合 Python 的傅里叶级数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52524919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com