public static Dictionary <int, double> SavGolFilterKoefficients(int nLeft, int nRight, int derivOrder = 0, int polynomesOrder = 6) { Dictionary <int, double> retDictKoefficients = new Dictionary <int, double>(); // A[ij] = i^j, i = -nL...nR; j = 0...M, M - polynomes order DenseMatrix dmDesignMatrix = DenseMatrix.Create(nLeft + nRight + 1, polynomesOrder + 1, (r, c) => { int currI = r - nLeft; return(Math.Pow((double)currI, c)); }); dmDesignMatrix = (DenseMatrix)dmDesignMatrix.TransposeThisAndMultiply(dmDesignMatrix); dmDesignMatrix = (DenseMatrix)dmDesignMatrix.Inverse(); DenseVector dvDesignMatrix0thRow = (DenseVector)dmDesignMatrix.Row(0); for (int n = -nLeft; n <= nRight; n++) { DenseVector dvCurrKoeffSummingVect = new DenseVector(dvDesignMatrix0thRow.Count); dvDesignMatrix0thRow.CopyTo(dvCurrKoeffSummingVect); dvCurrKoeffSummingVect.MapIndexedInplace((idx, dVal) => { return(dVal * Math.Pow((double)n, (double)idx)); }); retDictKoefficients.Add(n, dvCurrKoeffSummingVect.Sum()); } return(retDictKoefficients); }
public static DenseVector ExponentialMovingAverage(DenseVector dvValues, int gap = 10, double smoothingParameter = 0.4d) { DenseVector dvOutValues = (DenseVector)dvValues.Clone(); dvOutValues.MapIndexedInplace(new Func <int, double, double>((i, x) => { DenseVector dvWeights = DenseVector.Create(1 + gap * 2, new Func <int, double>(j => { int k = j - gap; if (i + k < 0) { return(0.0d); } if (i + k >= dvOutValues.Count) { return(0.0d); } return(Math.Exp(-Math.Abs(k) * smoothingParameter)); })); //dvWeights.MapIndexedInplace(new Func<int,double,double>((j, dVal) => //{ // if (double.IsNaN(dvValues[])) return 0.0d; //})) double sum = dvWeights.Sum(); dvWeights.MapInplace(new Func <double, double>(d => d / sum)); double retVal = 0.0d; for (int n = 0; n < 1 + gap * 2; n++) { int m = n - gap + i; if ((m < 0) || (m >= dvOutValues.Count)) { continue; } double weight = dvWeights[n]; retVal += weight * dvValues[m]; } return(retVal); })); return(dvOutValues); }
private void updateParametersScale(DenseVector dvActualParametersValues) { if (dvParametersScale == null) { dvParametersScale = DenseVector.Create(dvActualParametersValues.Count, i => Math.Abs(dvActualParametersValues[i])); } else { dvParametersScale.MapIndexedInplace((i, dVal) => { if (Math.Abs(dvActualParametersValues[i]) > dVal) { return(Math.Abs(dvActualParametersValues[i])); } else { return(dVal); } }); } }
/// <summary> /// Gets the local minimums distribution. /// </summary> /// <param name="dmFieldData">The dm field data.</param> /// <param name="dimensionNumber">The dimension number: /// 1 - rows (angle) /// 2 - columns (distance) /// </param> /// <returns>DenseMatrix.</returns> //public static DenseMatrix GetLocalMinimumsDistribution(DenseMatrix dmFieldData, PointD sunCenterPoint, PointD imageCenterPoint, double imageRadius, int imageHeight, double imageCircleCropFactor = 0.9d, int dimensionNumber = 1) public static List <Point3D> GetLocalMinimumsDistribution(DenseMatrix dmFieldData, RoundData sunDiskData, RoundData imageRoundData, int imageHeight, double imageCircleCropFactor = 0.9d) { // DenseMatrix dmFieldminimumsData = DenseMatrix.Create(dmFieldData.RowCount, dmFieldData.ColumnCount, 0.0d); List <Point3D> lRetPoints = new List <Point3D>(); double imageRadius = imageRoundData.DRadius; PointD imageCenterPoint = imageRoundData.pointDCircleCenter(); PointPolar imageCenterPointRelatedToSunCenter = new PointPolar(imageCenterPoint - sunDiskData.pointDCircleCenter(), true); double distanceSunCenterToImageCenter = PointD.Distance(imageCenterPoint, sunDiskData.pointDCircleCenter()); #region // obsolete //if (dimensionNumber == 1) //{ #endregion // obsolete for (int i = 0; i < dmFieldData.RowCount; i++) { bool itsTheCropCase = false; //если направлени на кроп кадра - то не берем в расмотрение double currentAngle = ((double)i / (double)(dmFieldData.RowCount - 1)) * 2.0d * Math.PI; LineDescription2D line, lineMargin; if (currentAngle < Math.PI) { //верхняя половина, смотрим направление на y=0.0d line = new LineDescription2D(sunDiskData.pointDCircleCenter(), new Vector2D(Math.Cos(currentAngle), -Math.Sin(currentAngle))); lineMargin = new LineDescription2D(new PointD(0.0d, 0.0d), new Vector2D(1.0d, 0.0d)); } else { line = new LineDescription2D(sunDiskData.pointDCircleCenter(), new Vector2D(Math.Cos(currentAngle), Math.Sin(currentAngle))); lineMargin = new LineDescription2D(new PointD(0.0d, imageHeight), new Vector2D(1.0d, 0.0d)); } PointD crossPointD = LineDescription2D.CrossPoint(line, lineMargin); if (crossPointD.Distance(imageCenterPoint) < imageRadius) { itsTheCropCase = true; } #region // obsolete //double yMargin = 0.0d; //double xMargin = sunCenterPoint.X + (yMargin - sunCenterPoint.Y) / Math.Tan(currentAngle); //double dx = xMargin - imageCenterPoint.X; //double dy = yMargin - imageCenterPoint.Y; //if (Math.Sqrt(dx * dx + dy * dy) < imageRadius) itsTheCropCase = true; #endregion // obsolete #region //obsolete //else //{ // //нижняя половина, смотрим направление на y=imageHeight // double yMargin = (double)imageHeight; // double xMargin = sunCenterPoint.X + (yMargin - sunCenterPoint.Y) / Math.Tan(currentAngle); // double dx = xMargin - imageCenterPoint.X; // double dy = yMargin - imageCenterPoint.Y; // if (Math.Sqrt(dx * dx + dy * dy) < imageRadius) itsTheCropCase = true; //} #endregion //obsolete //Если слишком близко к краю изображения - тоже исключаем. Минимум должен лежать не ближе, например, 1/15 //DenseMatrix dmSlicedDataMatrix = (DenseMatrix)dmFieldData.SubMatrix(i, 1, 0, dmFieldData.ColumnCount); DenseVector dvRowDataVector = (DenseVector)dmFieldData.EnumerateRows().ElementAt(i); #region // debug plotting //dvRowDataVector.SaveVectorDataAsImagePlot( // "D:\\_gulevlab\\SkyImagesAnalysis_appData\\patent-samples\\result.2015-03-24\\img-2014-09-20T16-03-58devID1\\dvRowDataVector-plot-image-" + // i.ToString("D03") + "-step1.png"); #endregion // debug plotting dvRowDataVector.MapIndexedInplace((idx, x) => ((x == 0.0d) || (idx < sunDiskData.DRadius * 1.5d)) ? (1.0d) : (x)); #region // debug plotting //dvRowDataVector.SaveVectorDataAsImagePlot( // "D:\\_gulevlab\\SkyImagesAnalysis_appData\\patent-samples\\result.2015-03-24\\img-2014-09-20T16-03-58devID1\\dvRowDataVector-plot-image-" + // i.ToString("D03") + "-step2.png"); #endregion // debug plotting double phiFromImageCenterToDirection = imageCenterPointRelatedToSunCenter.Phi - currentAngle; double distanceToImageMargin = distanceSunCenterToImageCenter * Math.Cos(phiFromImageCenterToDirection) + Math.Sqrt(imageRadius * imageRadius - distanceSunCenterToImageCenter * distanceSunCenterToImageCenter * Math.Sin(phiFromImageCenterToDirection) * Math.Sin(phiFromImageCenterToDirection)); dvRowDataVector.MapIndexedInplace( (idx, x) => ((double)idx / distanceToImageMargin >= imageCircleCropFactor) ? (1.0d) : (x)); #region // debug plotting //dvRowDataVector.SaveVectorDataAsImagePlot( // "D:\\_gulevlab\\SkyImagesAnalysis_appData\\patent-samples\\result.2015-03-24\\img-2014-09-20T16-03-58devID1\\dvRowDataVector-plot-image-" + // i.ToString("D03") + "-step3.png"); #endregion // debug plotting double minValue = dvRowDataVector.Minimum(); int minValueIndex = dvRowDataVector.MinimumIndex(); //if (!itsTheCropCase) dmFieldminimumsData[i, minValueIndex] = minValue; if ((!itsTheCropCase) && ((double)minValueIndex > sunDiskData.DRadius)) { lRetPoints.Add(new Point3D(currentAngle, minValueIndex, minValue)); } else { continue; } } #region // obsolete //} //else if (dimensionNumber == 2) //{ // for (int i = 0; i < dmFieldData.ColumnCount; i++) // { // DenseMatrix dmSlicedDataMatrix = (DenseMatrix)dmFieldData.SubMatrix(0, dmFieldData.RowCount, i, 1); // DenseVector dvSlicedDataVector = DenseVector.OfEnumerable(dmSlicedDataMatrix.Values); // dvSlicedDataVector.MapInplace(new Func<double, double>(x => (x == 0.0d) ? (1.0d) : (x))); // double minValue = dvSlicedDataVector.Minimum(); // int minValueIndex = dvSlicedDataVector.MinimumIndex(); // dmFieldminimumsData[minValueIndex, i] = minValue; // } //} #endregion // obsolete //return dmFieldminimumsData; return(lRetPoints); }
public static DenseVector NPolynomeApproximationLessSquareMethod(DenseVector dvDataValues, DenseVector dvSpace, List <PointD> lFixedPoints, int polynomeOrder = 3) { double[] koeffOut = new double[polynomeOrder + 1]; int pointsCount = dvSpace.Count; DenseMatrix dmFactorsA = DenseMatrix.Create(polynomeOrder + 1, polynomeOrder + 1, new Func <int, int, double>( (row, column) => { double sum = 0.0d; for (int j = 0; j < pointsCount; j++) { sum += Math.Pow(dvSpace[j], (column + row)); } return(sum); })); DenseVector dvFactorB = DenseVector.Create(polynomeOrder + 1, new Func <int, double>( (index) => { double sum = 0.0d; for (int j = 0; j < pointsCount; j++) { sum += dvDataValues[j] * Math.Pow(dvSpace[j], index); } return(sum); })); if (lFixedPoints.Count > 0) { foreach (PointD fixedPoint in lFixedPoints) { double fixedValuePosition = fixedPoint.X; double fixedValue = fixedPoint.Y; if (!double.IsNaN(fixedValue)) { PointD locPoint = fixedPoint; dmFactorsA.MapIndexedInplace(new Func <int, int, double, double>((row, column, dValue) => { if (row == lFixedPoints.IndexOf(locPoint)) { return(Math.Pow(fixedValuePosition, column)); } else { return(dValue); } })); dvFactorB.MapIndexedInplace(new Func <int, double, double>((index, dValue) => { if (index == lFixedPoints.IndexOf(locPoint)) { return(fixedValue); } else { return(dValue); } })); } } } DenseVector dvResult = (DenseVector)dmFactorsA.LU().Solve(dvFactorB); return(dvResult); }
public static DenseVector NPolynomeApproximationLessSquareMethod(DenseVector dvDataValues, DenseVector dvSpace, DenseVector dvFixedValues = null, int polynomeOrder = 3) { double[] koeffOut = new double[polynomeOrder + 1]; int pointsCount = dvSpace.Count; DenseMatrix dmFactorsA = DenseMatrix.Create(polynomeOrder + 1, polynomeOrder + 1, new Func <int, int, double>( (row, column) => { double sum = 0.0d; for (int j = 0; j < pointsCount; j++) { sum += Math.Pow(dvSpace[j], (column + row)); } return(sum); })); DenseVector dvFactorB = DenseVector.Create(polynomeOrder + 1, new Func <int, double>( (index) => { double sum = 0.0d; for (int j = 0; j < pointsCount; j++) { sum += dvDataValues[j] * Math.Pow(dvSpace[j], index); } return(sum); })); if (dvFixedValues != null) { int conditionIndex = 0; foreach (Tuple <int, double> fixedValueTuple in dvFixedValues.EnumerateIndexed()) { int fixedValueIndex = fixedValueTuple.Item1; double fixedValue = fixedValueTuple.Item2; if (!double.IsNaN(fixedValue)) { conditionIndex++; //int modificationRowIndex = dmFactorsA.RowCount - conditionIndex; int modificationRowIndex = conditionIndex - 1; dmFactorsA.MapIndexedInplace(new Func <int, int, double, double>((row, column, dValue) => { if (row == modificationRowIndex) { return(Math.Pow(dvSpace[fixedValueIndex], column)); } else { return(dValue); } })); dvFactorB.MapIndexedInplace(new Func <int, double, double>((index, dValue) => { if (index == modificationRowIndex) { return(fixedValue); } else { return(dValue); } })); } } } DenseVector dvResult = (DenseVector)dmFactorsA.LU().Solve(dvFactorB); return(dvResult); }