- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一些数据想要使用二阶、三阶或四阶傅里叶级数来拟合。
同时 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
并产生以下情节:
就这么简单!我把剩下的留给你想象。有关更多信息,您可以找到 documentation here .
关于python - 适合 Python 的傅里叶级数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52524919/
我正在尝试从底层叶进程写入命名管道并从顶层进程的管道读取。 为此,我首先在顶级进程中创建 FIFO,然后使用 for 循环派生更多进程。在 for 循环中,我正在检查叶子进程,如果它是叶子,我正在写入
我需要一个XPath表达式来验证XML列表元素是否只有某种类型的节点。 例: 我需要知道列表中是否只有图像。
我想从层次结构表中获取所有叶子及其根。叶子是最低层节点,根是最顶层节点。 给定一棵树: A --B --C --D E --F --G --H 叶子是节点:C, D根是:A、E 该表如下所
尝试从嵌套数组中删除相同的 sibling (叶子)/相同的数组。 例如 $data = [ 'test' => [ 'a' => [
我可以遍历目录并只打印文件夹/目录名称,但我想排除包含其他目录的目录的文件夹名称。出于某种原因,我称其为树结构中的“最终节点”,但我很可能是在自欺欺人,这不是第一次了。 =) 在审查其他答案列表时,也
我现在经常使用 Folium,在 Python 中拥有如此易于使用的东西真的很棒。但是他们的文档严重落后,我理解。所以我有2个问题。 我在尝试获得更多标记颜色时做错了什么?这是我尝试过的:map.si
我正在尝试使用 ape 包中的 plot.phylo 命令为 R 中系统发育类型图的边缘(线)添加颜色。这个例子是一个“粉丝”类型的图,虽然我希望这个方法与“系统图类型”或其他任何东西相同。 libr
我正在尝试呈现一个 JTree 叶以具有包含可编辑 JTextArea 的 JPanel,以便用户可以输入文本并能够在添加文本时向下滚动, 并且还使用包装器来限制文本的宽度,使其向下延伸以留出更多空间
这是要修改的正确 Linux 内核代码吗?我如何进行更改以模拟 CPUID 代码以及我需要更改哪个函数。谢谢 #include #include #include #include #incl
我是一名优秀的程序员,十分优秀!