public GPSINSFilter(TimeSpan startTime, Vector3 startPos, Vector3 startVelocity, Quaternion orientation,
                            SensorSpecifications sensorSpecifications)
        {
            _startTime            = startTime;
            _orientation          = orientation;
            _sensorSpecifications = sensorSpecifications;

            X0 = Matrix.Create(new double[n, 1]
            {
                { startPos.X }, { startPos.Y }, { startPos.Z },                                 // Position
                { startVelocity.X }, { startVelocity.Y }, { startVelocity.Z },                  // Velocity
                { _orientation.X }, { _orientation.Y }, { _orientation.Z }, { _orientation.W }, // Quaternion
            });

            // Make sure we don't reference the same object, but rather copy its values.
            // It is possible to set PostX0 to a different state than X0, so that the initial guess
            // of state is wrong.
            PostX0 = X0.Clone();


            // We use a very low initial estimate for error covariance, meaning the filter will
            // initially trust the model more and the sensors/observations less and gradually adjust on the way.
            // Note setting this to zero matrix will cause the filter to infinitely distrust all observations,
            // so always use a close-to-zero value instead.
            // Setting it to a diagnoal matrix of high values will cause the filter to trust the observations more in the beginning,
            // since we say that we think the current PostX0 estimate is unreliable.
            PostP0 = 0.001 * Matrix.Identity(n, n);


            // Determine standard deviation of estimated process and observation noise variance
            // Process noise (acceleromters, gyros, etc..)
            _stdDevW = new Vector(new double[n]
            {
                _sensorSpecifications.AccelerometerStdDev.Forward,
                _sensorSpecifications.AccelerometerStdDev.Right,
                _sensorSpecifications.AccelerometerStdDev.Up,
                0, 0, 0, 0, 0, 0, 0
            });

            // Observation noise (GPS inaccuarcy etc..)
            _stdDevV = new Vector(new double[m]
            {
                _sensorSpecifications.GPSPositionStdDev.X,
                _sensorSpecifications.GPSPositionStdDev.Y,
                _sensorSpecifications.GPSPositionStdDev.Z,
//                                          0.001000, 0.001000, 0.001000,
//                                          1000, 1000, 1000,
                _sensorSpecifications.GPSVelocityStdDev.X,
                _sensorSpecifications.GPSVelocityStdDev.Y,
                _sensorSpecifications.GPSVelocityStdDev.Z,
            });


            I = Matrix.Identity(n, n);

            _zeroMM = Matrix.Zeros(m);

            _rand         = new GaussianRandom();
            _prevEstimate = GetInitialEstimate(X0, PostX0, PostP0);
        }
Example #2
0
 public static double[] cameraTransform(double PointX, double PointY, double PointZ)
 {
     // From http://en.wikipedia.org/wiki/3D_projection#Perspective_projection :
     //
     // Point(X|Y|X)			a{x,y,z}	The point in 3D space that is to be projected.
     // Cube.Camera(X|Y|X)	c{x,y,z}	The location of the camera.
     // Cube.Theta(X|Y|X)	θ{x,y,z}	The rotation of the camera. When c{x,y,z}=<0,0,0>, and 0{x,y,z}=<0,0,0>, the 3D vector <1,2,0> is projected to the 2D vector <1,2>.
     // Cube.Viewer(X|Y|X)	e{x,y,z}	The viewer's position relative to the display surface.
     // Bsub(X|Y)			b{x,y}		The 2D projection of a.
     //
     // "First, we define a point DsubXYZ as a translation of point a{x,y,z} into a coordinate system defined by
     // c{x,y,z}. This is achieved by subtracting c{x,y,z} from a{x,y,z} and then applying a vector rotation matrix
     // using -θ{x,y,z} to the result. This transformation is often called a camera transform (note that these
     // calculations assume a left-handed system of axes)."
     MathNet.Numerics.LinearAlgebra.Matrix convMat1, convMat2, convMat3, convMat4, convMat41, convMat42, DsubXYZ;
     double CosThetaX = Math.Cos(Camera.RotationX); double SinThetaX = Math.Sin(Camera.RotationX);
     double CosThetaY = Math.Cos(Camera.RotationY); double SinThetaY = Math.Sin(Camera.RotationY);
     double CosThetaZ = Math.Cos(Camera.RotationZ); double SinThetaZ = Math.Sin(Camera.RotationZ);
     convMat1 = new MathNet.Numerics.LinearAlgebra.Matrix(new double[][] {
         new double[] {	1,	0,			0					},
         new double[] {	0,	CosThetaX,	((-1)*(SinThetaX))	},
         new double[] {	0,	SinThetaX,	CosThetaX			}
     });
     convMat2 = new MathNet.Numerics.LinearAlgebra.Matrix(new double[][] {
         new double[] {	CosThetaY,				0,	SinThetaY	},
         new double[] {	0,						1,	0			},
         new double[] {	((-1)*(SinThetaY)),		0,	CosThetaY	}
     });
     convMat3 = new MathNet.Numerics.LinearAlgebra.Matrix(new double[][] {
         new double[] {	CosThetaZ,	((-1)*(SinThetaZ)),		0	},
         new double[] {	SinThetaZ,	CosThetaZ,				0	},
         new double[] {	0,			0,						1	}
     });
     convMat41 = new MathNet.Numerics.LinearAlgebra.Matrix(new double[][] {
         new double[] {	PointX	},
         new double[] {	PointY	},
         new double[] {	PointZ	}
     });
     convMat42 = new MathNet.Numerics.LinearAlgebra.Matrix(new double[][] {
         new double[] {	Camera.X	},
         new double[] {	Camera.Y	},
         new double[] {	Camera.Z	}
     });
     convMat4 = convMat41.Clone();
     convMat4.Subtract(convMat42);
     DsubXYZ = ((convMat1.Multiply(convMat2)).Multiply(convMat3)).Multiply(convMat4);
     double[] returnVals = new double[3];
     returnVals[0] = DsubXYZ[0, 0]; returnVals[1] = DsubXYZ[1, 0]; returnVals[2] = DsubXYZ[2, 0];
     return returnVals;
 }
        public GPSINSFilter(TimeSpan startTime, Vector3 startPos, Vector3 startVelocity, Quaternion orientation,
                            SensorSpecifications sensorSpecifications)
        {
            _startTime = startTime;
            _orientation = orientation;
            _sensorSpecifications = sensorSpecifications;

            X0 = Matrix.Create(new double[n,1]
                                   {
                                       {startPos.X}, {startPos.Y}, {startPos.Z}, // Position
                                       {startVelocity.X}, {startVelocity.Y}, {startVelocity.Z}, // Velocity
                                       {_orientation.X}, {_orientation.Y}, {_orientation.Z}, {_orientation.W}, // Quaternion  
                                   });

            // Make sure we don't reference the same object, but rather copy its values.
            // It is possible to set PostX0 to a different state than X0, so that the initial guess
            // of state is wrong. 
            PostX0 = X0.Clone();


            // We use a very low initial estimate for error covariance, meaning the filter will
            // initially trust the model more and the sensors/observations less and gradually adjust on the way.
            // Note setting this to zero matrix will cause the filter to infinitely distrust all observations,
            // so always use a close-to-zero value instead.
            // Setting it to a diagnoal matrix of high values will cause the filter to trust the observations more in the beginning,
            // since we say that we think the current PostX0 estimate is unreliable.
            PostP0 = 0.001*Matrix.Identity(n, n);


            // Determine standard deviation of estimated process and observation noise variance
            // Process noise (acceleromters, gyros, etc..)
            _stdDevW = new Vector(new double[n]
                                      {
                                          _sensorSpecifications.AccelerometerStdDev.Forward,   
                                          _sensorSpecifications.AccelerometerStdDev.Right,
                                          _sensorSpecifications.AccelerometerStdDev.Up,
                                          0, 0, 0, 0, 0, 0, 0
                                      });

            // Observation noise (GPS inaccuarcy etc..)
            _stdDevV = new Vector(new double[m]
                                      {
                                          _sensorSpecifications.GPSPositionStdDev.X,
                                          _sensorSpecifications.GPSPositionStdDev.Y,
                                          _sensorSpecifications.GPSPositionStdDev.Z,
//                                          0.001000, 0.001000, 0.001000,
//                                          1000, 1000, 1000,
                                          _sensorSpecifications.GPSVelocityStdDev.X,
                                          _sensorSpecifications.GPSVelocityStdDev.Y,
                                          _sensorSpecifications.GPSVelocityStdDev.Z,
                                      });


            I = Matrix.Identity(n, n);
            
            _zeroMM = Matrix.Zeros(m);

            _rand = new GaussianRandom();
            _prevEstimate = GetInitialEstimate(X0, PostX0, PostP0);
        }
Example #4
0
        /// <summary>LU Decomposition</summary>
        /// <param name="A">  Rectangular matrix
        /// </param>
        /// <returns>     Structure to access L, U and piv.
        /// </returns>

        public LUDecomposition(Matrix A)
        {
            // Use a "left-looking", dot-product, Crout/Doolittle algorithm.

            LU  = (Matrix)A.Clone();
            piv = new int[m];
            for (int i = 0; i < m; i++)
            {
                piv[i] = i;
            }
            pivsign = 1;
            //double[] LUrowi;
            double[] LUcolj = new double[m];

            // Outer loop.

            for (int j = 0; j < n; j++)
            {
                // Make a copy of the j-th column to localize references.

                for (int i = 0; i < m; i++)
                {
                    LUcolj[i] = LU[i, j];
                }

                // Apply previous transformations.

                for (int i = 0; i < m; i++)
                {
                    //LUrowi = LU[i];

                    // Most of the time is spent in the following dot product.

                    int    kmax = System.Math.Min(i, j);
                    double s    = 0.0;
                    for (int k = 0; k < kmax; k++)
                    {
                        s += LU[i, k] * LUcolj[k];
                    }

                    LU[i, j] = LUcolj[i] -= s;
                }

                // Find pivot and exchange if necessary.

                int p = j;
                for (int i = j + 1; i < m; i++)
                {
                    if (System.Math.Abs(LUcolj[i]) > System.Math.Abs(LUcolj[p]))
                    {
                        p = i;
                    }
                }
                if (p != j)
                {
                    for (int k = 0; k < n; k++)
                    {
                        double t = LU[p, k]; LU[p, k] = LU[j, k]; LU[j, k] = t;
                    }
                    int k2 = piv[p]; piv[p] = piv[j]; piv[j] = k2;
                    pivsign = -pivsign;
                }

                // Compute multipliers.

                if (j < m & LU[j, j] != 0.0)
                {
                    for (int i = j + 1; i < m; i++)
                    {
                        LU[i, j] /= LU[j, j];
                    }
                }
            }
        }
        public GPSFilter2D(GPSObservation startState)
        {
            X0 = Matrix.Create(new double[n, 1]
            {
                { startState.Position.X }, { startState.Position.Y }, { startState.Position.Z },
                // Position
                { 0 }, { 0 }, { 0 },                  // Velocity
                { 0 }, { 0 }, { 0 },                  // Acceleration
            });

            PostX0 = X0.Clone();

            /* Matrix.Create(new double[n, 1]
             *                     {
             *                         {startState.Position.X}, {startState.Position.Y}, {startState.Position.Z}, // Position
             *                         {1}, {0}, {0}, // Velocity
             *                         {0}, {1}, {0}, // Acceleration
             *                     });*/

            // Start by assuming no covariance between states, meaning position, velocity, acceleration and their three XYZ components
            // have no correlation and behave independently. This not entirely true.
            PostP0 = Matrix.Identity(n, n);


            // Refs:
            // http://www.romdas.com/technical/gps/gps-acc.htm
            // http://www.sparkfun.com/datasheets/GPS/FV-M8_Spec.pdf
            // http://onlinestatbook.com/java/normalshade.html

            // Assuming GPS Sensor: FV-M8
            // Cold start: 41s
            // Hot start: 1s
            // Position precision: 3.3m CEP (horizontal circle, half the points within this radius centred on truth)
            // Position precision (DGPS): 2.6m CEP


            //            const float coVarQ = ;
            //            _r = covarianceR;

            // Determine standard deviation of estimated process and observation noise variance
            // Position process noise
            _stdDevW = Vector.Zeros(n); //(float)Math.Sqrt(_q);

            _rand = new GaussianRandom();

            // Circle Error Probable (50% of the values are within this radius)
            //            const float cep = 3.3f;

            // GPS position observation noise by standard deviation [meters]
            // Assume gaussian distribution, 2.45 x CEP is approx. 2dRMS (95%)
            // ref: http://www.gmat.unsw.edu.au/snap/gps/gps_survey/chap2/243.htm

            // Found empirically by http://onlinestatbook.com/java/normalshade.html
            // using area=0.5 and limits +- 3.3 meters
            _stdDevV = new Vector(new double[m]
            {
                0,
                ObservationNoiseStdDevY,
                0,
//                                             0,
//                                             0,
//                                             0,
//                                             0,
//                                             0,
//                                             0,
//                                             0,
            });
            //Vector.Zeros(Observations);
            //2, 2, 4.8926f);


            H = Matrix.Identity(m, n);
            I = Matrix.Identity(n, n);
            Q = new Matrix(n, n);
            R = Matrix.Identity(m, m);


            _prevEstimate = GetInitialEstimate(X0, PostX0, PostP0);
        }
        /// <summary>Construct the singular value decomposition.</summary>
        /// <remarks>Provides access to U, S and V.</remarks>
        /// <param name="Arg">Rectangular matrix</param>
        public SingularValueDecomposition(Matrix Arg)
        {
            transpose = (Arg.RowCount < Arg.ColumnCount);

            // Derived from LINPACK code.
            // Initialize.
            Matrix A = (Matrix)Arg.Clone();

            if (transpose)
            {
                A.Transpose();
            }

            m = A.RowCount;
            n = A.ColumnCount;
            int nu = System.Math.Min(m, n);

            s = new double[System.Math.Min(m + 1, n)];
            U = new Matrix(m, nu);
            V = new Matrix(n, n);

            double[] e     = new double[n];
            double[] work  = new double[m];
            bool     wantu = true;
            bool     wantv = true;

            // Reduce A to bidiagonal form, storing the diagonal elements
            // in s and the super-diagonal elements in e.

            int nct = System.Math.Min(m - 1, n);
            int nrt = System.Math.Max(0, System.Math.Min(n - 2, m));

            for (int k = 0; k < System.Math.Max(nct, nrt); k++)
            {
                if (k < nct)
                {
                    // Compute the transformation for the k-th column and
                    // place the k-th diagonal in s[k].
                    // Compute 2-norm of k-th column without under/overflow.
                    s[k] = 0;
                    for (int i = k; i < m; i++)
                    {
                        s[k] = Fn.Hypot(s[k], A[i, k]);
                    }
                    if (s[k] != 0.0)
                    {
                        if (A[k, k] < 0.0)
                        {
                            s[k] = -s[k];
                        }
                        for (int i = k; i < m; i++)
                        {
                            A[i, k] /= s[k];
                        }
                        A[k, k] += 1.0;
                    }
                    s[k] = -s[k];
                }
                for (int j = k + 1; j < n; j++)
                {
                    if ((k < nct) & (s[k] != 0.0))
                    {
                        // Apply the transformation.

                        double t = 0;
                        for (int i = k; i < m; i++)
                        {
                            t += A[i, k] * A[i, j];
                        }
                        t = (-t) / A[k, k];
                        for (int i = k; i < m; i++)
                        {
                            A[i, j] += t * A[i, k];
                        }
                    }

                    // Place the k-th row of A into e for the
                    // subsequent calculation of the row transformation.

                    e[j] = A[k, j];
                }
                if (wantu & (k < nct))
                {
                    // Place the transformation in U for subsequent back
                    // multiplication.

                    for (int i = k; i < m; i++)
                    {
                        U[i, k] = A[i, k];
                    }
                }
                if (k < nrt)
                {
                    // Compute the k-th row transformation and place the
                    // k-th super-diagonal in e[k].
                    // Compute 2-norm without under/overflow.
                    e[k] = 0;
                    for (int i = k + 1; i < n; i++)
                    {
                        e[k] = Fn.Hypot(e[k], e[i]);
                    }
                    if (e[k] != 0.0)
                    {
                        if (e[k + 1] < 0.0)
                        {
                            e[k] = -e[k];
                        }
                        for (int i = k + 1; i < n; i++)
                        {
                            e[i] /= e[k];
                        }
                        e[k + 1] += 1.0;
                    }
                    e[k] = -e[k];
                    if ((k + 1 < m) & (e[k] != 0.0))
                    {
                        // Apply the transformation.

                        for (int i = k + 1; i < m; i++)
                        {
                            work[i] = 0.0;
                        }
                        for (int j = k + 1; j < n; j++)
                        {
                            for (int i = k + 1; i < m; i++)
                            {
                                work[i] += e[j] * A[i, j];
                            }
                        }
                        for (int j = k + 1; j < n; j++)
                        {
                            double t = (-e[j]) / e[k + 1];
                            for (int i = k + 1; i < m; i++)
                            {
                                A[i, j] += t * work[i];
                            }
                        }
                    }
                    if (wantv)
                    {
                        // Place the transformation in V for subsequent
                        // back multiplication.

                        for (int i = k + 1; i < n; i++)
                        {
                            V[i, k] = e[i];
                        }
                    }
                }
            }

            // Set up the final bidiagonal matrix or order p.

            int p = System.Math.Min(n, m + 1);

            if (nct < n)
            {
                s[nct] = A[nct, nct];
            }
            if (m < p)
            {
                s[p - 1] = 0.0;
            }
            if (nrt + 1 < p)
            {
                e[nrt] = A[nrt, p - 1];
            }
            e[p - 1] = 0.0;

            // If required, generate U.

            if (wantu)
            {
                for (int j = nct; j < nu; j++)
                {
                    for (int i = 0; i < m; i++)
                    {
                        U[i, j] = 0.0;
                    }
                    U[j, j] = 1.0;
                }
                for (int k = nct - 1; k >= 0; k--)
                {
                    if (s[k] != 0.0)
                    {
                        for (int j = k + 1; j < nu; j++)
                        {
                            double t = 0;
                            for (int i = k; i < m; i++)
                            {
                                t += U[i, k] * U[i, j];
                            }
                            t = (-t) / U[k, k];
                            for (int i = k; i < m; i++)
                            {
                                U[i, j] += t * U[i, k];
                            }
                        }
                        for (int i = k; i < m; i++)
                        {
                            U[i, k] = -U[i, k];
                        }
                        U[k, k] = 1.0 + U[k, k];
                        for (int i = 0; i < k - 1; i++)
                        {
                            U[i, k] = 0.0;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < m; i++)
                        {
                            U[i, k] = 0.0;
                        }
                        U[k, k] = 1.0;
                    }
                }
            }

            // If required, generate V.

            if (wantv)
            {
                for (int k = n - 1; k >= 0; k--)
                {
                    if ((k < nrt) & (e[k] != 0.0))
                    {
                        for (int j = k + 1; j < nu; j++)
                        {
                            double t = 0;
                            for (int i = k + 1; i < n; i++)
                            {
                                t += V[i, k] * V[i, j];
                            }
                            t = (-t) / V[k + 1, k];
                            for (int i = k + 1; i < n; i++)
                            {
                                V[i, j] += t * V[i, k];
                            }
                        }
                    }
                    for (int i = 0; i < n; i++)
                    {
                        V[i, k] = 0.0;
                    }
                    V[k, k] = 1.0;
                }
            }

            // Main iteration loop for the singular values.

            int    pp   = p - 1;
            int    iter = 0;
            double eps  = System.Math.Pow(2.0, -52.0);

            while (p > 0)
            {
                int k, kase;

                // Here is where a test for too many iterations would go.

                // This section of the program inspects for
                // negligible elements in the s and e arrays.  On
                // completion the variables kase and k are set as follows.

                // kase = 1     if s(p) and e[k-1] are negligible and k<p
                // kase = 2     if s(k) is negligible and k<p
                // kase = 3     if e[k-1] is negligible, k<p, and
                //              s(k), ..., s(p) are not negligible (qr step).
                // kase = 4     if e(p-1) is negligible (convergence).

                for (k = p - 2; k >= -1; k--)
                {
                    if (k == -1)
                    {
                        break;
                    }
                    if (System.Math.Abs(e[k]) <= eps * (System.Math.Abs(s[k]) + System.Math.Abs(s[k + 1])))
                    {
                        e[k] = 0.0;
                        break;
                    }
                }
                if (k == p - 2)
                {
                    kase = 4;
                }
                else
                {
                    int ks;
                    for (ks = p - 1; ks >= k; ks--)
                    {
                        if (ks == k)
                        {
                            break;
                        }
                        double t = (ks != p?System.Math.Abs(e[ks]):0.0) + (ks != k + 1?System.Math.Abs(e[ks - 1]):0.0);
                        if (System.Math.Abs(s[ks]) <= eps * t)
                        {
                            s[ks] = 0.0;
                            break;
                        }
                    }
                    if (ks == k)
                    {
                        kase = 3;
                    }
                    else if (ks == p - 1)
                    {
                        kase = 1;
                    }
                    else
                    {
                        kase = 2;
                        k    = ks;
                    }
                }
                k++;

                // Perform the task indicated by kase.

                switch (kase)
                {
                // Deflate negligible s(p).
                case 1:
                {
                    double f = e[p - 2];
                    e[p - 2] = 0.0;
                    for (int j = p - 2; j >= k; j--)
                    {
                        double t  = Fn.Hypot(s[j], f);
                        double cs = s[j] / t;
                        double sn = f / t;
                        s[j] = t;
                        if (j != k)
                        {
                            f        = (-sn) * e[j - 1];
                            e[j - 1] = cs * e[j - 1];
                        }
                        if (wantv)
                        {
                            for (int i = 0; i < n; i++)
                            {
                                t           = cs * V[i, j] + sn * V[i, p - 1];
                                V[i, p - 1] = (-sn) * V[i, j] + cs * V[i, p - 1];
                                V[i, j]     = t;
                            }
                        }
                    }
                }
                break;

                // Split at negligible s(k).


                case 2:
                {
                    double f = e[k - 1];
                    e[k - 1] = 0.0;
                    for (int j = k; j < p; j++)
                    {
                        double t  = Fn.Hypot(s[j], f);
                        double cs = s[j] / t;
                        double sn = f / t;
                        s[j] = t;
                        f    = (-sn) * e[j];
                        e[j] = cs * e[j];
                        if (wantu)
                        {
                            for (int i = 0; i < m; i++)
                            {
                                t           = cs * U[i, j] + sn * U[i, k - 1];
                                U[i, k - 1] = (-sn) * U[i, j] + cs * U[i, k - 1];
                                U[i, j]     = t;
                            }
                        }
                    }
                }
                break;

                // Perform one qr step.


                case 3:
                {
                    // Calculate the shift.

                    double scale = System.Math.Max(System.Math.Max(System.Math.Max(System.Math.Max(System.Math.Abs(s[p - 1]), System.Math.Abs(s[p - 2])), System.Math.Abs(e[p - 2])), System.Math.Abs(s[k])), System.Math.Abs(e[k]));
                    double sp    = s[p - 1] / scale;
                    double spm1  = s[p - 2] / scale;
                    double epm1  = e[p - 2] / scale;
                    double sk    = s[k] / scale;
                    double ek    = e[k] / scale;
                    double b     = ((spm1 + sp) * (spm1 - sp) + epm1 * epm1) / 2.0;
                    double c     = (sp * epm1) * (sp * epm1);
                    double shift = 0.0;
                    if ((b != 0.0) | (c != 0.0))
                    {
                        shift = System.Math.Sqrt(b * b + c);
                        if (b < 0.0)
                        {
                            shift = -shift;
                        }
                        shift = c / (b + shift);
                    }
                    double f = (sk + sp) * (sk - sp) + shift;
                    double g = sk * ek;

                    // Chase zeros.

                    for (int j = k; j < p - 1; j++)
                    {
                        double t  = Fn.Hypot(f, g);
                        double cs = f / t;
                        double sn = g / t;
                        if (j != k)
                        {
                            e[j - 1] = t;
                        }
                        f        = cs * s[j] + sn * e[j];
                        e[j]     = cs * e[j] - sn * s[j];
                        g        = sn * s[j + 1];
                        s[j + 1] = cs * s[j + 1];
                        if (wantv)
                        {
                            for (int i = 0; i < n; i++)
                            {
                                t           = cs * V[i, j] + sn * V[i, j + 1];
                                V[i, j + 1] = (-sn) * V[i, j] + cs * V[i, j + 1];
                                V[i, j]     = t;
                            }
                        }
                        t        = Fn.Hypot(f, g);
                        cs       = f / t;
                        sn       = g / t;
                        s[j]     = t;
                        f        = cs * e[j] + sn * s[j + 1];
                        s[j + 1] = (-sn) * e[j] + cs * s[j + 1];
                        g        = sn * e[j + 1];
                        e[j + 1] = cs * e[j + 1];
                        if (wantu && (j < m - 1))
                        {
                            for (int i = 0; i < m; i++)
                            {
                                t           = cs * U[i, j] + sn * U[i, j + 1];
                                U[i, j + 1] = (-sn) * U[i, j] + cs * U[i, j + 1];
                                U[i, j]     = t;
                            }
                        }
                    }
                    e[p - 2] = f;
                    iter     = iter + 1;
                }
                break;

                // Convergence.


                case 4:
                {
                    // Make the singular values positive.

                    if (s[k] <= 0.0)
                    {
                        s[k] = (s[k] < 0.0?-s[k]:0.0);
                        if (wantv)
                        {
                            for (int i = 0; i <= pp; i++)
                            {
                                V[i, k] = -V[i, k];
                            }
                        }
                    }

                    // Order the singular values.

                    while (k < pp)
                    {
                        if (s[k] >= s[k + 1])
                        {
                            break;
                        }
                        double t = s[k];
                        s[k]     = s[k + 1];
                        s[k + 1] = t;
                        if (wantv && (k < n - 1))
                        {
                            for (int i = 0; i < n; i++)
                            {
                                t = V[i, k + 1]; V[i, k + 1] = V[i, k]; V[i, k] = t;
                            }
                        }
                        if (wantu && (k < m - 1))
                        {
                            for (int i = 0; i < m; i++)
                            {
                                t = U[i, k + 1]; U[i, k + 1] = U[i, k]; U[i, k] = t;
                            }
                        }
                        k++;
                    }
                    iter = 0;
                    p--;
                }
                break;
                }
            }

            // (vermorel) transposing the results if needed
            if (transpose)
            {
                // swaping U and V
                Matrix T = V;
                V = U;
                U = T;
            }
        }
Example #7
0
        LUDecomposition(
            Matrix A
            )
        {
            // TODO: it is usually considered as a poor practice to execute algorithms within a constructor.

            // Use a "left-looking", dot-product, Crout/Doolittle algorithm.

            LU           = A.Clone();
            _rowCount    = A.RowCount;
            _columnCount = A.ColumnCount;

            piv = new int[_rowCount];
            for (int i = 0; i < _rowCount; i++)
            {
                piv[i] = i;
            }
            pivsign = 1;
            //double[] LUrowi;
            double[] LUcolj = new double[_rowCount];

            // Outer loop.

            for (int j = 0; j < _columnCount; j++)
            {
                // Make a copy of the j-th column to localize references.

                for (int i = 0; i < LUcolj.Length; i++)
                {
                    LUcolj[i] = LU[i][j];
                }

                // Apply previous transformations.

                for (int i = 0; i < LUcolj.Length; i++)
                {
                    //LUrowi = LU[i];

                    // Most of the time is spent in the following dot product.

                    int    kmax = Math.Min(i, j);
                    double s    = 0.0;
                    for (int k = 0; k < kmax; k++)
                    {
                        s += LU[i][k] * LUcolj[k];
                    }

                    LU[i][j] = LUcolj[i] -= s;
                }

                // Find pivot and exchange if necessary.

                int p = j;
                for (int i = j + 1; i < LUcolj.Length; i++)
                {
                    if (Math.Abs(LUcolj[i]) > Math.Abs(LUcolj[p]))
                    {
                        p = i;
                    }
                }
                if (p != j)
                {
                    for (int k = 0; k < _columnCount; k++)
                    {
                        double t = LU[p][k]; LU[p][k] = LU[j][k]; LU[j][k] = t;
                    }
                    int k2 = piv[p]; piv[p] = piv[j]; piv[j] = k2;
                    pivsign = -pivsign;
                }

                // Compute multipliers.

                if ((j < _rowCount) && (LU[j][j] != 0.0))
                {
                    for (int i = j + 1; i < _rowCount; i++)
                    {
                        LU[i][j] /= LU[j][j];
                    }
                }
            }

            InitOnDemandComputations();
        }
        public GPSFilter2D(GPSObservation startState)
        {
            X0 = Matrix.Create(new double[n,1]
                                   {
                                       {startState.Position.X}, {startState.Position.Y}, {startState.Position.Z},
                                       // Position
                                       {0}, {0}, {0}, // Velocity
                                       {0}, {0}, {0}, // Acceleration
                                   });

            PostX0 = X0.Clone();
            /* Matrix.Create(new double[n, 1]
                                   {
                                       {startState.Position.X}, {startState.Position.Y}, {startState.Position.Z}, // Position
                                       {1}, {0}, {0}, // Velocity
                                       {0}, {1}, {0}, // Acceleration
                                   });*/

            // Start by assuming no covariance between states, meaning position, velocity, acceleration and their three XYZ components
            // have no correlation and behave independently. This not entirely true.
            PostP0 = Matrix.Identity(n, n);


            // Refs: 
            // http://www.romdas.com/technical/gps/gps-acc.htm
            // http://www.sparkfun.com/datasheets/GPS/FV-M8_Spec.pdf
            // http://onlinestatbook.com/java/normalshade.html 

            // Assuming GPS Sensor: FV-M8
            // Cold start: 41s
            // Hot start: 1s
            // Position precision: 3.3m CEP (horizontal circle, half the points within this radius centred on truth)
            // Position precision (DGPS): 2.6m CEP


            //            const float coVarQ = ;
            //            _r = covarianceR;

            // Determine standard deviation of estimated process and observation noise variance
            // Position process noise
            _stdDevW = Vector.Zeros(n); //(float)Math.Sqrt(_q);

            _rand = new GaussianRandom();

            // Circle Error Probable (50% of the values are within this radius)
            //            const float cep = 3.3f;

            // GPS position observation noise by standard deviation [meters]
            // Assume gaussian distribution, 2.45 x CEP is approx. 2dRMS (95%)
            // ref: http://www.gmat.unsw.edu.au/snap/gps/gps_survey/chap2/243.htm

            // Found empirically by http://onlinestatbook.com/java/normalshade.html
            // using area=0.5 and limits +- 3.3 meters
            _stdDevV = new Vector(new double[m]
                                      {
                                          0,
                                          ObservationNoiseStdDevY,
                                          0,
//                                             0,
//                                             0,
//                                             0, 
//                                             0, 
//                                             0, 
//                                             0, 
//                                             0,
                                      });
            //Vector.Zeros(Observations);
            //2, 2, 4.8926f);


            H = Matrix.Identity(m, n);
            I = Matrix.Identity(n, n);
            Q = new Matrix(n, n);
            R = Matrix.Identity(m, m);


            _prevEstimate = GetInitialEstimate(X0, PostX0, PostP0);
        }
Example #9
0
        LUDecomposition(Matrix a)
        {
            /* TODO: it is usually considered as a poor practice to execute algorithms within a constructor */

            /* Use a "left-looking", dot-product, Crout/Doolittle algorithm. */

            _lu          = a.Clone();
            _rowCount    = a.RowCount;
            _columnCount = a.ColumnCount;

            _pivot = new int[_rowCount];
            for (int i = 0; i < _rowCount; i++)
            {
                _pivot[i] = i;
            }

            _pivotSign = 1;

            ////double[] LUrowi;
            double[] LUcolj = new double[_rowCount];

            /* Outer loop */

            for (int j = 0; j < _columnCount; j++)
            {
                /* Make a copy of the j-th column to localize references */

                for (int i = 0; i < LUcolj.Length; i++)
                {
                    LUcolj[i] = _lu[i][j];
                }

                /* Apply previous transformations */

                for (int i = 0; i < LUcolj.Length; i++)
                {
                    ////LUrowi = LU[i];

                    /* Most of the time is spent in the following dot product */

                    int    kmax = Math.Min(i, j);
                    double s    = 0.0;
                    for (int k = 0; k < kmax; k++)
                    {
                        s += _lu[i][k] * LUcolj[k];
                    }

                    _lu[i][j] = LUcolj[i] -= s;
                }

                /* Find pivot and exchange if necessary */

                int p = j;

                for (int i = j + 1; i < LUcolj.Length; i++)
                {
                    if (Math.Abs(LUcolj[i]) > Math.Abs(LUcolj[p]))
                    {
                        p = i;
                    }
                }

                if (p != j)
                {
                    for (int k = 0; k < _columnCount; k++)
                    {
                        double t = _lu[p][k];
                        _lu[p][k] = _lu[j][k];
                        _lu[j][k] = t;
                    }

                    int k2 = _pivot[p];
                    _pivot[p] = _pivot[j];
                    _pivot[j] = k2;

                    _pivotSign = -_pivotSign;
                }

                /* Compute multipliers */

                if ((j < _rowCount) && (_lu[j][j] != 0.0))
                {
                    for (int i = j + 1; i < _rowCount; i++)
                    {
                        _lu[i][j] /= _lu[j][j];
                    }
                }
            }

            InitOnDemandComputations();
        }