Java中的正弦波曲线拟合 [英] Sine Wave Curve Fitting in Java

查看:31
本文介绍了Java中的正弦波曲线拟合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Apache Commons Math 包,我'有以下正弦波...

I'm using the Apache Commons Math package and I've got the following Sine Wave...

  0.90, 0.85, 0.80, 0.83, 0.89
  0.90, 0.85, 0.80, 0.83, 0.89
  0.90, 0.85, 0.80, 0.83, 0.89
  0.90, 0.85, 0.80, 0.83, 0.89

从上面的数据可以看出,wave有以下几个属性...

from the above data you can see that the wave has the following attributes...

  • 振幅 = .05
  • 阶段 = 0
  • 频率 = 5

但是,当我像这样将正弦波添加到 HarmonicFitter 时...

However, when I add my sine wave to a HarmonicFitter like so...

HarmonicFitter fitter = new HarmonicFitter(new LevenbergMarquardtOptimizer());

fitter.addObservedPoint(0, 0.90);
fitter.addObservedPoint(1, 0.85);
fitter.addObservedPoint(2, 0.80);
fitter.addObservedPoint(3, 0.83);
fitter.addObservedPoint(4, 0.89);

fitter.addObservedPoint(5, 0.90);
fitter.addObservedPoint(6, 0.85);
fitter.addObservedPoint(7, 0.80);
fitter.addObservedPoint(8, 0.83);
fitter.addObservedPoint(9, 0.89);

fitter.addObservedPoint(10, 0.90);
fitter.addObservedPoint(11, 0.85);
fitter.addObservedPoint(12, 0.80);
fitter.addObservedPoint(13, 0.83);
fitter.addObservedPoint(14, 0.89);

fitter.addObservedPoint(15, 0.90);
fitter.addObservedPoint(16, 0.85);
fitter.addObservedPoint(17, 0.80);
fitter.addObservedPoint(18, 0.83);
fitter.addObservedPoint(19, 0.89);

double[] vals = fitter.fit();

return vals;

返回的值更像是...

Amplitude: 5.19813329138371
Frequency: 4.69209750375546E-5
Phase: 1.405312649084833

为什么曲线拟合会导致具有 4 个相同频率的正弦波具有如此截然不同的属性?

Why has the curve fitting resulted in such drastically different attributes for a sinewave with 4 identical frequencies?

推荐答案

@Marko Topolnik 有问题.Fitter 期待一个简单的谐波(即单个余弦或正弦),它的平均值为零.因此,从所有内容中减去 0.854(平均值),然后将该常数加回到生成的正弦波中.

@Marko Topolnik has the problem. The Fitter is expecting a simple harmonic (i.e., a single cosine or sine), which has mean zero. So subtract 0.854 (the mean) from everything, and add that constant back to your resulting sine wave.

事实上,微小的频率产生平坦的正弦波,所以其他数字无关紧要.尝试绘制所有内容(包括生成的函数).

As things are, the tiny frequency is giving a flat sine wave, so the other numbers are irrelevant. Try plotting everything (including the resulting function).

这里有两个图.第一个有你的分数和三个函数:你想要的 y=.05*cos(2πx/5),相同的函数加上 .0854(看起来你的阶段赢了't 为零),以及包的最佳拟合函数:但是,除非您缩小到 [-1e5,1e5]x[-8,8] 的窗口,否则您无法区分包的最佳拟合函数:这也意味着包的最佳拟合函数非常不稳定.你的点的微小变化都会导致输出的巨大变化.

Here are two plots. The first has your points along with three functions: your desired y=.05*cos(2πx/5), the same function plus .0854 (it appears your phase won't be zero), and the package's best fit function: But you can't distinguish the package's best fit function until you zoom way out to a window of [-1e5,1e5]x[-8,8]: This also means that the package's best fit function is wildly unstable. A small change in your points will cause a large change in the output.

这篇关于Java中的正弦波曲线拟合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆