简体   繁体   English

Java中的正弦波曲线拟合

[英]Sine Wave Curve Fitting in Java

I'm using the Apache Commons Math package and I've got the following Sine Wave... 我正在使用Apache Commons Math软件包,并且获得了以下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

from the above data you can see that the wave has the following attributes... 从以上数据中您可以看到wave具有以下属性...

  • Amplitude = .05 幅度= .05
  • Phase = 0 相位= 0
  • Frequency = 5 频率= 5

However, when I add my sine wave to a HarmonicFitter like so... 但是,当我像这样将正弦波添加到HarmonicFitter ...

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;

The values returned are more like... 返回的值更像是...

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

Why has the curve fitting resulted in such drastically different attributes for a sinewave with 4 identical frequencies? 为什么曲线拟合对具有4个相同频率的正弦波产生如此大的不同属性?

You seem to be mixing up order of the output and not mapping it correctly to labels ( fit returns an array). 您似乎混淆了输出的顺序,并且没有将其正确映射到标签( fit返回一个数组)。

The values that you obtained really reflect your inputs: 您获得的值实际上反映了您的输入:

Try to draw your values on paper and put a sinewave on them -- your assertions of 0.5 for amplitude and 5 for frequency are incorrect. 尝试在纸上绘制您的值并在它们上放一个正弦波-您对振幅的0.5和对频率的5的断言是不正确的。 The phase is Ok, which is confirmed by 4.69e-5. 阶段确定,已由4.69e-5确认。 Your frequency is well above 5. The amplitude of 0.5 is what you want, not what the data shows, 1.4: because there are no points on the downslope of the sine the optimiser actuyally thinks that points 0.8, 0.83 and 0.89 all belong to the upslope of the sinewave with a much larger amplitude -- this reduces the error. 您的频率远高于5。0.5的振幅是您想要的,而不是数据显示的1.4,因为:正弦波的下坡上没有点,优化器实际上认为点0.8、0.83和0.89都属于点。正弦波的上坡幅度要大得多-这样可以减少误差。

All in all, you are overfitting by trying to fit 3 values with essentially 5 points. 总而言之,您试图通过将3个值实际拟合为5个点来进行拟合。

@Marko Topolnik has the problem. @Marko Topolnik有问题。 The Fitter is expecting a simple harmonic (ie, a single cosine or sine), which has mean zero. Fitter期望一个简单的谐波(即单个余弦或正弦),平均值为零。 So subtract 0.854 (the mean) from everything, and add that constant back to your resulting sine wave. 因此,从所有数据中减去0.854 (均值),然后将该常数加回到生成的正弦波中。

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). 尝试绘制所有内容(包括结果函数)。


Edit: 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: 第一个包含您的观点以及三个函数:所需的y=.05*cos(2πx/5) ,相同的函数加.0854 (看来您的相位不会为零),以及包装的最佳拟合函数: 最合适的图片 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] : 但是,除非您缩小到[-1e5,1e5]x[-8,8]窗口, [-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. 点的微小变化将导致输出的巨大变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM