Java 中的正弦函数拟合 - 解释 commons.apache 的输出

Sine Function Fitting in Java - interpreting output from commons.apache

我有兴趣在 Java 中创建最适合某些给定点的正弦函数。这些点在以下数组中给出:

double[] array_to_fit = {0,2,4,6,8,8,5,3,3,0};

我找到了 Java 的 HarmonicCurveFitter 函数,并决定在 Android 中使用它。为了将它们与此功能一起使用,我相信我需要通过减去每个点的平均值来 'pre-process' 数据,这给了我以下内容:

double[] array_to_fit = {-3.9,-1.9,0.1,2.1,4.1,4.1,1.1,-0.9,-0.9,-3.9};

并将数据拟合到 HarmonicCurveFitter 对象中:

        //add points to WeightedObservedPoints object
        for (int index = 0; index < array_to_fit.length; index++) {
            obs.add(index, array_to_fit[index]-average);
        }
        double[] bestFit = fitter.fit(obs.toList());
        amp = bestFit[0];
        freq = bestFit[1];
        phase = bestFit[2];

Android 函数返回的输出如下:

我在 Python 中使用 SciPy 完成了相同的练习,结果如下:

如您所见,相位值不同。当我在图表中绘制两个函数时:

Visual output graph results

如您所见,在 Python 中创建的正弦函数似乎确实没问题,而 Android 中的正弦函数似乎在水平轴上略有偏移...

有人知道这是什么原因吗?我是否错误地解释了 HarmonicCurveFitter 函数的相位输出?

已解决!错误是我期待的是正弦,但这个库中的函数是用余弦表示的。功能如前所述

f(t) = A * cos(omega*t + phi)