private void doFit() { int l = _values.Length; List <PointF> ps = new List <PointF>(l); for (int i = 0; i < l; ++i) { var v = _values[i]; if (v > _average) { ps.Add(new PointF(i, _values[i])); } } var fittedParamsTemp = CurveFunctions.FindPolynomialLeastSquaresFit(ps, 2); var errorTemp = Math.Sqrt(CurveFunctions.ErrorSquared(ps, fittedParamsTemp)); _fittedParams = fittedParamsTemp; _error = errorTemp; // -b / 2a _fittedMaxPos = (float)(-_fittedParams[1] / (2 * _fittedParams[2])); lblError.Text = string.Format("{0:0.0000}", _error); string pt = ""; foreach (var p in _fittedParams) { pt += p.ToString("0.000"); } lblParams.Text = pt; }
private void panel1_Paint(object sender, PaintEventArgs e) { bool first = true; Point oldPoint = new Point(); Graphics g = e.Graphics; var ca = (_average - _min) * _yk; g.DrawLine(Pens.Green, 0, ca, _w, ca); for (var i = 0; i < _count; ++i) { Point p = new Point((int)(i * _xk), (int)(_h + (_min - _values[i]) * _yk)); if (!first) { g.DrawLine(Pens.Black, oldPoint, p); } oldPoint = p; first = false; } if (_fittedParams != null) { first = true; for (var x = 0; x < _w; ++x) { Point p = new Point((int)(x * _xk), (int)(_h + (_min - CurveFunctions.F(_fittedParams, x)) * _yk)); if (!first) { g.DrawLine(Pens.Blue, oldPoint, p); } oldPoint = p; first = false; } var fp = _fittedMaxPos * _xk; g.DrawLine(Pens.Red, fp, 0, fp, _h); } }
float getDist(float[] values) { // this is basically trying to fit a parable // to the points which focus rank is higher than average // THIS IS A CHEAP TRICK soon to be substituted // with RANSAC or RANSAC-like technique... try { float min = 1000000; int minPos = -1; float max = 0; int maxPos = -1; int l = values.Length; float v = 0; float accu = 0; for (int i = 0; i < l; ++i) { v = values[i]; accu += v; if (v < min) { min = v; minPos = i; } if (v > max) { max = v; maxPos = i; } } float average = accu / l; List <PointF> ps = new List <PointF>(l); for (int i = 0; i < l; ++i) { v = values[i]; if (v > average) { ps.Add(new PointF(i, values[i])); } } var fittedParamsTemp = CurveFunctions.FindPolynomialLeastSquaresFit(ps, 2); if (fittedParamsTemp[2] > 0) { return(-1); } var errorTemp = Math.Sqrt(CurveFunctions.ErrorSquared(ps, fittedParamsTemp)); //if (errorTemp > 0.5f) //{ // return -1; //} if (max - average < CurveReliabilityTreshold) { return(-1); } // -b / 2a float res = (float)(-fittedParamsTemp[1] / (2 * fittedParamsTemp[2])); return((res >= l) || (res <= 0) ? -1 : res); } catch { return(-1); } }