// Function to make linear least squares estimation.
        private void LeastSquares()
        {
            // Arrays for datagrid's values. Makes matrix and vector creating easier.
            double[] realtempArray   = new double[dgCalibration.Rows.Count];
            double[] sensorTempArray = new double[dgCalibration.Rows.Count];

            for (int i = 0; i < dgCalibration.Rows.Count; i++)
            {
                if (dgCalibration.Rows[i].Cells[1].Value != null)   // Includes only values that has been set by user.
                {
                    double sensorTemp = Convert.ToDouble(dgCalibration.Rows[i].Cells[0].Value);
                    double realTemp   = Convert.ToDouble(dgCalibration.Rows[i].Cells[1].Value);
                    realtempArray[i]   = realTemp;
                    sensorTempArray[i] = sensorTemp;
                }
            }

            // Fitting a straight on Data Points with LS-method.
            DoubleVector y   = new DoubleVector(realtempArray);
            DoubleMatrix m   = new DoubleMatrix(dgCalibration.Rows.Count, 1, sensorTempArray, StorageType.ColumnMajor);
            var          lsq = new DoubleLeastSquares(m, y, true);

            _intercept = lsq.X[0];
            _slope     = lsq.X[1];

            nudSensorCalA.Value = (decimal)lsq.X[1];
            nudSensorCalB.Value = (decimal)lsq.X[0];
        }
Beispiel #2
0
        private static double[] GSolve(Pixel[][] imagesSamples, double[] exposureTime, int smoothFactor, int ch)
        {
            int z_max = 256;
            int p     = imagesSamples.Length;
            int n     = imagesSamples[0].Length;

            double[,] A = new double[n * p + z_max + 1, z_max + n];
            double[] b = new double[A.GetLength(0)];

            int k = 0;

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < p; ++j)
                {
                    int    z   = imagesSamples[j][i].getChannel(ch);
                    double wij = W(z);
                    A[k, z]         = wij;
                    A[k, z_max + i] = -wij;
                    b[k]            = wij * Math.Log(exposureTime[j]);
                    k += 1;
                }
            }

            // Limit middle value
            A[k, 128] = 1;
            k        += 1;

            // Smoothing
            for (int i = 0; i < z_max - 1; ++i)
            {
                double w_k = W(i + 1);
                A[k, i]     = smoothFactor * W(w_k);
                A[k, i + 1] = -2 * smoothFactor * w_k;
                A[k, i + 2] = smoothFactor * w_k;
                k          += 1;
            }

            // Solve SVD
            var AA  = new DoubleMatrix(A);
            var bb  = new DoubleVector(b);
            var lsq = new DoubleLeastSquares(AA, bb, true);

            double[] g = new double[256];
            for (int i = 0; i < 256; ++i)
            {
                g[i] = lsq.X[i];
            }

            return(g);
        }