Ejemplo n.º 1
0
        public void CombineTest2()
        {
            Accord.Math.Random.Generator.Seed = 0;

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },
            };

            double[,] X = Matrix.Multiply(S, A);

            var ica = new IndependentComponentAnalysis(X);

            ica.Compute(2);

            double[,] result = ica.Result;


            float[][] expected = ica.Combine(result).ToSingle().ToJagged(true);
            float[][] actual   = ica.Combine(result.ToSingle().ToJagged(true));

            Assert.IsTrue(expected.IsEqual(actual, atol: 1e-4f));
        }
Ejemplo n.º 2
0
        public void CombineTest()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },
            };

            double[,] X = S.Multiply(A);

            IndependentComponentAnalysis ica = new IndependentComponentAnalysis(X);


            ica.Compute(2);

            var result = ica.Result;

            var expected = Accord.Statistics.Tools.ZScores(X);
            var actual   = Accord.Statistics.Tools.ZScores(ica.Combine(result));

            Assert.IsTrue(expected.IsEqual(actual, 1e-4));
        }
Ejemplo n.º 3
0
        public void CombineTest2()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },
            };

            double[,] X = S.Multiply(A);

            IndependentComponentAnalysis ica = new IndependentComponentAnalysis(X);

            ica.Compute(2);

            double[,] result = ica.Result;


            float[][] expected = ica.Combine(result).ToSingle().ToArray(true);
            float[][] actual   = ica.Combine(result.ToSingle().ToArray(true));

            Assert.IsTrue(expected.IsEqual(actual, 1e-4f));
        }
        public void ComputeTest()
        {
            Accord.Math.Tools.SetupGenerator(0);

            // Let's create a random dataset containing
            // 5000 samples of two dimensional samples.
            //
            double[,] source = Matrix.Random(5000, 2);

            // Now, we will mix the samples the dimensions of the samples.
            // A small amount of the second column will be applied to the
            // first, and vice-versa. 
            //
            double[,] mix =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },    
            };

            // mix the source data
            double[,] input = source.Multiply(mix);

            // Now, we can use ICA to identify any linear mixing between the variables, such
            // as the matrix multiplication we did above. After it has identified it, we will
            // be able to revert the process, retrieving our original samples again
            
            // Create a new Independent Component Analysis
            var ica = new IndependentComponentAnalysis(input);

            Assert.AreEqual(IndependentComponentAlgorithm.Parallel, ica.Algorithm);

            // Compute it 
            ica.Compute();

            // Now, we can retrieve the mixing and demixing matrices that were 
            // used to alter the data. Note that the analysis was able to detect
            // this information automatically:

            double[,] mixingMatrix = ica.MixingMatrix; // same as the 'mix' matrix
            double[,] revertMatrix = ica.DemixingMatrix; // inverse of the 'mix' matrix

            double[,] result = ica.Result;

            // Verify mixing matrix
            mixingMatrix = mixingMatrix.Divide(mixingMatrix.Sum().Sum());
            Assert.IsTrue(mix.IsEqual(mixingMatrix, 0.008));


            // Verify demixing matrix
            double[,] expected =
            {
                { 0.75, -0.25 },        
                { 0.25,  0.25 },
            };

            revertMatrix = revertMatrix.Divide(revertMatrix.Sum().Sum());
            Assert.IsTrue(expected.IsEqual(revertMatrix, 0.008));
        }
Ejemplo n.º 5
0
        public void ComputeTest()
        {
            Accord.Math.Tools.SetupGenerator(0);

            // Let's create a random dataset containing
            // 5000 samples of two dimensional samples.
            //
            double[,] source = Matrix.Random(5000, 2);

            // Now, we will mix the samples the dimensions of the samples.
            // A small amount of the second column will be applied to the
            // first, and vice-versa.
            //
            double[,] mix =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },
            };

            // mix the source data
            double[,] input = source.Multiply(mix);

            // Now, we can use ICA to identify any linear mixing between the variables, such
            // as the matrix multiplication we did above. After it has identified it, we will
            // be able to revert the process, retrieving our original samples again

            // Create a new Independent Component Analysis
            var ica = new IndependentComponentAnalysis(input);

            Assert.AreEqual(IndependentComponentAlgorithm.Parallel, ica.Algorithm);

            // Compute it
            ica.Compute();

            // Now, we can retrieve the mixing and demixing matrices that were
            // used to alter the data. Note that the analysis was able to detect
            // this information automatically:

            double[,] mixingMatrix = ica.MixingMatrix;   // same as the 'mix' matrix
            double[,] revertMatrix = ica.DemixingMatrix; // inverse of the 'mix' matrix

            double[,] result = ica.Result;

            // Verify mixing matrix
            mixingMatrix = mixingMatrix.Divide(mixingMatrix.Sum().Sum());
            Assert.IsTrue(mix.IsEqual(mixingMatrix, 0.008));


            // Verify demixing matrix
            double[,] expected =
            {
                { 0.75, -0.25 },
                { 0.25,  0.25 },
            };

            revertMatrix = revertMatrix.Divide(revertMatrix.Sum().Sum());
            Assert.IsTrue(expected.IsEqual(revertMatrix, 0.008));
        }
Ejemplo n.º 6
0
        public void SerializeTest()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] source = Matrix.Random(5000, 2);

            double[,] mix =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },
            };

            double[,] input = source.Dot(mix);

            var ica = new IndependentComponentAnalysis(input);

            ica.Compute();

            MemoryStream stream = new MemoryStream();

            {
                BinaryFormatter b = new BinaryFormatter();
                b.Serialize(stream, ica);
            }

            stream.Seek(0, SeekOrigin.Begin);

            {
                BinaryFormatter b = new BinaryFormatter();
                ica = (IndependentComponentAnalysis)b.Deserialize(stream);
            }


            Assert.AreEqual(IndependentComponentAlgorithm.Parallel, ica.Algorithm);

            double[][] mixingMatrix = ica.MixingMatrix;   // same as the 'mix' matrix
            double[][] revertMatrix = ica.DemixingMatrix; // inverse of the 'mix' matrix

            double[,] result = ica.Result;

            mixingMatrix = mixingMatrix.Divide(mixingMatrix.Sum());
            Assert.IsTrue(mix.IsEqual(mixingMatrix, atol: 0.008));

            double[,] expected =
            {
                { 0.75, -0.25 },
                { 0.25,  0.25 },
            };

            revertMatrix = revertMatrix.Divide(revertMatrix.Sum());
            Assert.IsTrue(expected.IsEqual(revertMatrix, atol: 0.008));
        }
Ejemplo n.º 7
0
        public void ComputeTest2()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  1, 1 },
                { -1, 3 },
            };

            A = A.Divide(Norm.Norm1(A));

            double[,] X = S.Multiply(A);

            IndependentComponentAnalysis ica = new IndependentComponentAnalysis(X, IndependentComponentAlgorithm.Deflation);

            Assert.AreEqual(IndependentComponentAlgorithm.Deflation, ica.Algorithm);

            ica.Compute(2);

            var result       = ica.Result;
            var mixingMatrix = ica.MixingMatrix;
            var revertMatrix = ica.DemixingMatrix;

            // Verify mixing matrix
            mixingMatrix = mixingMatrix.Divide(Norm.Norm1(mixingMatrix));
            Assert.IsTrue(A.IsEqual(mixingMatrix, 0.05));


            // Verify demixing matrix
            double[,] expected =
            {
                { 3, -1 },
                { 1,  1 },
            };

            expected = expected.Divide(Norm.Norm1(expected));

            revertMatrix = revertMatrix.Divide(Norm.Norm1(revertMatrix));
            Assert.IsTrue(expected.IsEqual(revertMatrix, 0.05));



            var reverted = Accord.Statistics.Tools.ZScores(result).Abs();
            var original = Accord.Statistics.Tools.ZScores(S).Abs();

            Assert.IsTrue(reverted.IsEqual(original, 0.1));
        }
Ejemplo n.º 8
0
        public void ConvergenceTest()
        {
            IndependentComponentAnalysis ica;

            // https://github.com/accord-net/framework/issues/225
            var mixedData = LoadData();

            ica            = new IndependentComponentAnalysis(mixedData, AnalysisMethod.Standardize);
            ica.Overwrite  = false;
            ica.Iterations = 1000;
            ica.Algorithm  = IndependentComponentAlgorithm.Parallel;
            ica.Contrast   = new Kurtosis();

            ica.Compute();

            Assert.AreEqual(3.2178976535060348, ica.WhiteningMatrix.Sum());
            Assert.AreEqual(1, ica.MixingMatrix.Sum(), 1e-7);
        }
        public void ComputeTest()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },
            };

            double[,] X = S.Multiply(A);

            IndependentComponentAnalysis ica = new IndependentComponentAnalysis(X, IndependentComponentAlgorithm.Parallel);

            Assert.AreEqual(IndependentComponentAlgorithm.Parallel, ica.Algorithm);

            ica.Compute(2);

            var result       = ica.Result;
            var mixingMatrix = ica.MixingMatrix;
            var revertMatrix = ica.DemixingMatrix;

            // Verify mixing matrix
            mixingMatrix = mixingMatrix.Divide(mixingMatrix.Sum().Sum());
            Assert.IsTrue(A.IsEqual(mixingMatrix, 0.008));


            // Verify demixing matrix
            double[,] expected =
            {
                { 0.75, -0.25 },
                { 0.25,  0.25 },
            };

            revertMatrix = revertMatrix.Divide(revertMatrix.Sum().Sum());
            Assert.IsTrue(expected.IsEqual(revertMatrix, 0.008));
        }
        public void ComputeTest()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },    
            };

            double[,] X = S.Multiply(A);

            IndependentComponentAnalysis ica = new IndependentComponentAnalysis(X, IndependentComponentAlgorithm.Parallel);

            Assert.AreEqual(IndependentComponentAlgorithm.Parallel, ica.Algorithm);

            ica.Compute(2);

            var result = ica.Result;
            var mixingMatrix = ica.MixingMatrix;
            var revertMatrix = ica.DemixingMatrix;

            // Verify mixing matrix
            mixingMatrix = mixingMatrix.Divide(mixingMatrix.Sum().Sum());
            Assert.IsTrue(A.IsEqual(mixingMatrix, 0.008));


            // Verify demixing matrix
            double[,] expected =
            {
                { 0.75, -0.25 },        
                { 0.25,  0.25 },
            };

            revertMatrix = revertMatrix.Divide(revertMatrix.Sum().Sum());
            Assert.IsTrue(expected.IsEqual(revertMatrix, 0.008));
        }
Ejemplo n.º 11
0
        private void btnRunAnalysis_Click(object sender, EventArgs e)
        {
            // Retrieve the input data as a double[,] matrix
            double[][] data = input.Transpose().ToDouble();


            // Create a new Independent Component Analysis
            ica = new IndependentComponentAnalysis(AnalysisMethod.Standardize)
            {
                Overwrite = true
            };

            // Compute the analysis
            var demixer = ica.Learn(data);


            // Separate the input signals
            demix = demixer.Transform(data).Transpose().ToSingle();


            btnSource1.Enabled = true;
            btnSource2.Enabled = true;
        }
Ejemplo n.º 12
0
        private void btnRunAnalysis_Click(object sender, EventArgs e)
        {
            // Retrieve the input data as a double[,] matrix
            double[,] data = input.ToMatrix(true).ToDouble();


            // Create a new Independent Component Analysis
            ica = new IndependentComponentAnalysis(data,
                                                   AnalysisMethod.Standardize)
            {
                Overwrite = true
            };

            // Compute the analysis
            ica.Compute();


            // Separate the input signals
            demix = ica.Separate(input);


            btnSource1.Enabled = true;
            btnSource2.Enabled = true;
        }
Ejemplo n.º 13
0
        public void SeparateTest()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },
            };

            double[,] X = Matrix.Multiply(S, A);

            var ica = new IndependentComponentAnalysis(X);


            ica.Compute(2);

            var expected = ica.Result;
            var actual   = ica.Separate(X);

            Assert.IsTrue(expected.IsEqual(actual, 1e-4));
        }
Ejemplo n.º 14
0
        public void SeparateTest2()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },
            };

            double[,] X = S.Dot(A);

            var ica = new IndependentComponentAnalysis(X);


            ica.Compute(2);

            var expected = ica.Result.ToSingle().ToJagged(true);
            var actual   = ica.Separate(X.ToSingle().ToJagged(true));

            Assert.IsTrue(expected.IsEqual(actual, 1e-4f));
        }
        public void learn_test()
        {
            Accord.Math.Random.Generator.Seed = 0;
            #region doc_learn
            // Let's create a random dataset containing
            // 5000 samples of two dimensional samples.
            //
            double[][] source = Jagged.Random(5000, 2);

            // Now, we will mix the samples the dimensions of the samples.
            // A small amount of the second column will be applied to the
            // first, and vice-versa. 
            //
            double[][] mix =
            {
                new double[] {  0.25, 0.25 },
                new double[] { -0.25, 0.75 },    
            };

            // mix the source data
            double[][] input = source.Multiply(mix);

            // Now, we can use ICA to identify any linear mixing between the variables, such
            // as the matrix multiplication we did above. After it has identified it, we will
            // be able to revert the process, retrieving our original samples again

            // Create a new Independent Component Analysis
            var ica = new IndependentComponentAnalysis()
            {
                Algorithm = IndependentComponentAlgorithm.Parallel,
                Contrast = new Logcosh()
            };

            // Learn the demixing transformation from the data
            MultivariateLinearRegression demix = ica.Learn(input);

            // Now, we can retrieve the mixing and demixing matrices that were 
            // used to alter the data. Note that the analysis was able to detect
            // this information automatically:

            double[][] mixingMatrix = ica.MixingMatrix; // same as the 'mix' matrix
            double[][] revertMatrix = ica.DemixingMatrix; // inverse of the 'mix' matrix

            // We can use the regression to recover the separate sources
            double[][] result = demix.Transform(input);
            #endregion


            // Verify mixing matrix
            mixingMatrix = mixingMatrix.Divide(mixingMatrix.Sum());
            Assert.IsTrue(mix.IsEqual(mixingMatrix, atol: 0.008));

            Assert.IsTrue(revertMatrix.IsEqual(demix.Weights, atol: 0.008));
            var dm = demix.Inverse().Weights;
            dm = dm.Divide(dm.Sum());
            Assert.IsTrue(mixingMatrix.IsEqual(dm, atol: 0.008));


            // Verify demixing matrix
            double[,] expected =
            {
                { 0.75, -0.25 },        
                { 0.25,  0.25 },
            };

            Assert.AreEqual(IndependentComponentAlgorithm.Parallel, ica.Algorithm);
            Assert.AreEqual(ica.Contrast.GetType(), typeof(Logcosh));

            revertMatrix = revertMatrix.Divide(revertMatrix.Sum());
            Assert.IsTrue(expected.IsEqual(revertMatrix, atol: 0.008));
        }
        public void ConvergenceTest()
        {
            IndependentComponentAnalysis ica;

            // https://github.com/accord-net/framework/issues/225
            var mixedData = LoadData();

            ica = new IndependentComponentAnalysis(mixedData, AnalysisMethod.Standardize);
            ica.Overwrite = false;
            ica.Iterations = 1000;
            ica.Algorithm = IndependentComponentAlgorithm.Parallel;
            ica.Contrast = new Kurtosis();

            ica.Compute();

            Assert.AreEqual(3.2178976535060348, ica.WhiteningMatrix.Sum());
            Assert.AreEqual(1, ica.MixingMatrix.Sum(), 1e-7);
        }
Ejemplo n.º 17
0
        // THE MAIN CARDIO COMPUTATION:
        private double HeartComputation(Bitmap image)
        {
            // ComputeAndStorAverageRGB(image);
            ComputeAndStorAverageRGB(image);

            if (isDataReady)
            {
                ////////////////////////////////
                // NORMALIZE THE RGB SIGNALS BY SUBTRACTING THE MEAN:
                //build color matrix
                double[][] colors =
                {
                    red,
                    green,
                    blue
                };


                ///////////////////////////////////
                // ADD A HIGH PASS FILTERING STEP:

                var   testFilter = new Accord.Audio.Filters.HighPassFilter(0.1f);
                float RC         = 1 / (2 * (float)(3.142) * (float)(0.1));
                float dt         = 1 / (float)(30);
                float ALPHA      = dt / (dt + RC);
                testFilter.Alpha = ALPHA;

                var floatMtx = new float[colors.Length][];
                for (int i = 0; i < colors.Length; i++)
                {
                    floatMtx[i] = new float[colors[i].Length];
                    for (int j = 0; j < colors[i].Length; j++)
                    {
                        floatMtx[i][j] = (float)colors[i][j];
                    }
                }

                Accord.Audio.Signal target     = Accord.Audio.Signal.FromArray(floatMtx[0], 1, Accord.Audio.SampleFormat.Format32BitIeeeFloat);
                Accord.Audio.Signal target_out = testFilter.Apply(target);
                target_out.CopyTo(floatMtx[0]);

                target     = Accord.Audio.Signal.FromArray(floatMtx[1], 1, Accord.Audio.SampleFormat.Format32BitIeeeFloat);
                target_out = testFilter.Apply(target);
                target_out.CopyTo(floatMtx[1]);

                target     = Accord.Audio.Signal.FromArray(floatMtx[2], 1, Accord.Audio.SampleFormat.Format32BitIeeeFloat);
                target_out = testFilter.Apply(target);
                target_out.CopyTo(floatMtx[2]);


                for (int i = 0; i < floatMtx.Length; i++)
                {
                    for (int j = 0; j < floatMtx[i].Length; j++)
                    {
                        colors[i][j] = (double)floatMtx[i][j];
                    }
                }
                ///////////////////////////////////

                ///////////////////////////////////
                // Z-SCORE:
                double[][] norm_colorsT;
                double[][] norm_colors;
                double[][] colorsT = colors.Transpose();
                norm_colorsT = Accord.Statistics.Tools.ZScores(colorsT);
                norm_colors  = norm_colorsT.Transpose();
                ///////////////////////////////////

                ///////////////////////////////////
                // PERFORM ICA:
                var ica = new IndependentComponentAnalysis()
                {
                    Algorithm       = IndependentComponentAlgorithm.Parallel,
                    Contrast        = new Logcosh(),
                    Iterations      = 1000,
                    NumberOfInputs  = 3,
                    NumberOfOutputs = 3
                };
                ica.Iterations = 1000000;

                MultivariateLinearRegression demix = ica.Learn(norm_colorsT);
                double[][] resultT = demix.Transform(norm_colorsT);
                double[][] result  = resultT.Transpose();

                ///////////////////////////////////

                ///////////////////////////////////
                // ADD A Low PASS FILTERING STEP:

                var LPFilter = new Accord.Audio.Filters.LowPassFilter(0.1f);
                RC               = 1 / (2 * (float)(3.142) * (float)(0.1));
                dt               = 1 / (float)(30);
                ALPHA            = dt / (dt + RC);
                testFilter.Alpha = ALPHA;


                var LPMtx = new float[result.Length][];
                for (int i = 0; i < result.Length; i++)
                {
                    LPMtx[i] = new float[result[i].Length];
                    for (int j = 0; j < result[i].Length; j++)
                    {
                        LPMtx[i][j] = (float)result[i][j];
                    }
                }

                target     = Accord.Audio.Signal.FromArray(LPMtx[0], 30, Accord.Audio.SampleFormat.Format32BitIeeeFloat);
                target_out = testFilter.Apply(target);
                target_out.CopyTo(LPMtx[0]);

                target     = Accord.Audio.Signal.FromArray(LPMtx[1], 30, Accord.Audio.SampleFormat.Format32BitIeeeFloat);
                target_out = testFilter.Apply(target);
                target_out.CopyTo(LPMtx[1]);

                target     = Accord.Audio.Signal.FromArray(LPMtx[2], 30, Accord.Audio.SampleFormat.Format32BitIeeeFloat);
                target_out = testFilter.Apply(target);
                target_out.CopyTo(LPMtx[2]);

                /*
                 * for (int i = 0; i < LPMtx.Length; i++)
                 * {
                 *  for (int j = 0; j < LPMtx[i].Length; j++)
                 *      result[i][j] = (double)LPMtx[i][j];
                 * }
                 */
                ///////////////////////////////////


                ///////////////////////////////////
                // CALCULATE THE FFT OF ECH CHANNEL:
                int      length = red.Length;
                double[] imagR  = new double[length];
                double[] imagG  = new double[length];
                double[] imagB  = new double[length];

                red_norm   = norm_colors[0];
                green_norm = norm_colors[1];
                blue_norm  = norm_colors[2];

                ica_source_1 = result[0];
                ica_source_2 = result[1];
                ica_source_3 = result[2];

                Accord.Math.Transforms.FourierTransform2.FFT(result[0], imagR, FourierTransform.Direction.Forward);
                Accord.Math.Transforms.FourierTransform2.FFT(result[1], imagG, FourierTransform.Direction.Forward);
                Accord.Math.Transforms.FourierTransform2.FFT(result[2], imagB, FourierTransform.Direction.Forward);


                double[] magR = GetMag(result[0], imagR);
                double[] magG = GetMag(result[1], imagG);
                double[] magB = GetMag(result[2], imagB);

                for (int i = 0; i < timeWindows / 2; i++)
                {
                    red_norm_FFT[i]   = magR[i];
                    green_norm_FFT[i] = magG[i];
                    blue_norm_FFT[i]  = magB[i];
                }
                ///////////////////////////////////


                ///////////////////////////////////
                // FIND THE MAX FREQUENCY AND POWER FROM THE FFTs:
                double[] freq = Vector.Interval((double)0, (double)(timeWindows - 1));
                for (int i = 0; i < freq.Length; i++)
                {
                    freq[i] = freq[i] / (double)timeWindows * FrameRate;
                }

                int    MaxRedIndex;
                double MaxRedValue;
                GetMax(out MaxRedIndex, out MaxRedValue, freq, magR, 0.45, 3);

                int    MaxGreenIndex;
                double MaxGreenValue;
                GetMax(out MaxGreenIndex, out MaxGreenValue, freq, magG, 0.45, 3);

                int    MaxBlueIndex;
                double MaxBlueValue;
                GetMax(out MaxBlueIndex, out MaxBlueValue, freq, magB, 0.45, 3);


                //Get the max power
                if ((MaxRedValue > MaxGreenValue) && (MaxRedValue > MaxBlueValue))
                {
                    //Red is the Max
                    HeartBeatFrequency = freq[MaxRedIndex];
                }

                if ((MaxGreenValue > MaxRedValue) && (MaxGreenValue > MaxBlueValue))
                {
                    //Green is the Max
                    HeartBeatFrequency = freq[MaxGreenIndex];
                }

                if ((MaxBlueValue > MaxRedValue) && (MaxBlueValue > MaxGreenValue))
                {
                    //Blue is the Max
                    HeartBeatFrequency = freq[MaxBlueIndex];
                }
                ///////////////////////////////////
                HeartBeatFrequency = HeartBeatFrequency * 60;
                isDataReady        = false;
                return(HeartBeatFrequency);
            }
            return(HeartBeatFrequency);
        }
Ejemplo n.º 18
0
        private void btnRunAnalysis_Click(object sender, EventArgs e)
        {
            // Retrieve the input data as a double[,] matrix
            double[][] data = input.Transpose().ToDouble();


            // Create a new Independent Component Analysis
            ica = new IndependentComponentAnalysis(AnalysisMethod.Standardize)
            {
                Overwrite = true 
            };

            // Compute the analysis
            var demixer = ica.Learn(data);


            // Separate the input signals
            demix = demixer.Transform(data).Transpose().ToSingle();


            btnSource1.Enabled = true;
            btnSource2.Enabled = true;
        }
        public void SerializeTest()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] source = Matrix.Random(5000, 2);

            double[,] mix =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },    
            };

            double[,] input = source.Multiply(mix);

            var ica = new IndependentComponentAnalysis(input);

            ica.Compute();

            MemoryStream stream = new MemoryStream();

            {
                BinaryFormatter b = new BinaryFormatter();
                b.Serialize(stream, ica);
            }

            stream.Seek(0, SeekOrigin.Begin);

            {
                BinaryFormatter b = new BinaryFormatter();
                ica = (IndependentComponentAnalysis)b.Deserialize(stream);
            }


            Assert.AreEqual(IndependentComponentAlgorithm.Parallel, ica.Algorithm);

            double[,] mixingMatrix = ica.MixingMatrix; // same as the 'mix' matrix
            double[,] revertMatrix = ica.DemixingMatrix; // inverse of the 'mix' matrix

            double[,] result = ica.Result;

            mixingMatrix = mixingMatrix.Divide(mixingMatrix.Sum().Sum());
            Assert.IsTrue(mix.IsEqual(mixingMatrix, 0.008));

            double[,] expected =
            {
                { 0.75, -0.25 },        
                { 0.25,  0.25 },
            };

            revertMatrix = revertMatrix.Divide(revertMatrix.Sum().Sum());
            Assert.IsTrue(expected.IsEqual(revertMatrix, 0.008));


        }
        public void CombineTest()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },    
            };

            double[,] X = S.Multiply(A);

            IndependentComponentAnalysis ica = new IndependentComponentAnalysis(X);


            ica.Compute(2);

            var result = ica.Result;

            var expected = Accord.Statistics.Tools.ZScores(X);
            var actual = Accord.Statistics.Tools.ZScores(ica.Combine(result));

            Assert.IsTrue(expected.IsEqual(actual, 1e-4));
        }
        public void SeparateTest2()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },    
            };

            double[,] X = S.Multiply(A);

            IndependentComponentAnalysis ica = new IndependentComponentAnalysis(X);


            ica.Compute(2);

            var expected = ica.Result.ToSingle().ToArray(true);
            var actual = ica.Separate(X.ToSingle().ToArray(true));

            Assert.IsTrue(expected.IsEqual(actual, 1e-4f));
        }
Ejemplo n.º 22
0
        public void learn_test()
        {
            Accord.Math.Random.Generator.Seed = 0;
            #region doc_learn
            // Let's create a random dataset containing
            // 5000 samples of two dimensional samples.
            //
            double[][] source = Jagged.Random(5000, 2);

            // Now, we will mix the samples the dimensions of the samples.
            // A small amount of the second column will be applied to the
            // first, and vice-versa.
            //
            double[][] mix =
            {
                new double[] {  0.25, 0.25 },
                new double[] { -0.25, 0.75 },
            };

            // mix the source data
            double[][] input = source.Dot(mix);

            // Now, we can use ICA to identify any linear mixing between the variables, such
            // as the matrix multiplication we did above. After it has identified it, we will
            // be able to revert the process, retrieving our original samples again

            // Create a new Independent Component Analysis
            var ica = new IndependentComponentAnalysis()
            {
                Algorithm = IndependentComponentAlgorithm.Parallel,
                Contrast  = new Logcosh()
            };

            // Learn the demixing transformation from the data
            MultivariateLinearRegression demix = ica.Learn(input);

            // Now, we can retrieve the mixing and demixing matrices that were
            // used to alter the data. Note that the analysis was able to detect
            // this information automatically:

            double[][] mixingMatrix = ica.MixingMatrix;   // same as the 'mix' matrix
            double[][] revertMatrix = ica.DemixingMatrix; // inverse of the 'mix' matrix

            // We can use the regression to recover the separate sources
            double[][] result = demix.Transform(input);
            #endregion


            // Verify mixing matrix
            mixingMatrix = mixingMatrix.Divide(mixingMatrix.Sum());
            Assert.IsTrue(mix.IsEqual(mixingMatrix, atol: 0.008));

            Assert.IsTrue(revertMatrix.IsEqual(demix.Weights, atol: 0.008));
            var dm = demix.Inverse().Weights;
            dm = dm.Divide(dm.Sum());
            Assert.IsTrue(mixingMatrix.IsEqual(dm, atol: 0.008));


            // Verify demixing matrix
            double[,] expected =
            {
                { 0.75, -0.25 },
                { 0.25,  0.25 },
            };

            Assert.AreEqual(IndependentComponentAlgorithm.Parallel, ica.Algorithm);
            Assert.AreEqual(ica.Contrast.GetType(), typeof(Logcosh));

            revertMatrix = revertMatrix.Divide(revertMatrix.Sum());
            Assert.IsTrue(expected.IsEqual(revertMatrix, atol: 0.008));
        }
Ejemplo n.º 23
0
        private void btnRunAnalysis_Click(object sender, EventArgs e)
        {
            // Retrieve the input data as a double[,] matrix
            double[,] data = input.ToMatrix(true).ToDouble();


            // Create a new Independent Component Analysis
            ica = new IndependentComponentAnalysis(data,
                AnalysisMethod.Standardize) { Overwrite = true };

            // Compute the analysis
            ica.Compute();


            // Separate the input signals
            demix = ica.Separate(input);


            btnSource1.Enabled = true;
            btnSource2.Enabled = true;
        }
        public void CombineTest2()
        {
            Accord.Math.Random.Generator.Seed = 0;

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },    
            };

            double[,] X = Matrix.Multiply(S, A);

            var ica = new IndependentComponentAnalysis(X);

            ica.Compute(2);

            double[,] result = ica.Result;


            float[][] expected = ica.Combine(result).ToSingle().ToJagged(true);
            float[][] actual = ica.Combine(result.ToSingle().ToJagged(true));

            Assert.IsTrue(expected.IsEqual(actual, atol: 1e-4f));
        }
        public void ComputeTest2()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  1, 1 },
                { -1, 3 },    
            };

            A = A.Divide(Norm.Norm1(A));

            double[,] X = S.Multiply(A);

            IndependentComponentAnalysis ica = new IndependentComponentAnalysis(X, IndependentComponentAlgorithm.Deflation);

            Assert.AreEqual(IndependentComponentAlgorithm.Deflation, ica.Algorithm);

            ica.Compute(2);

            var result = ica.Result;
            var mixingMatrix = ica.MixingMatrix;
            var revertMatrix = ica.DemixingMatrix;

            // Verify mixing matrix
            mixingMatrix = mixingMatrix.Divide(Norm.Norm1(mixingMatrix));
            Assert.IsTrue(A.IsEqual(mixingMatrix, 0.05));


            // Verify demixing matrix
            double[,] expected =
            {
                { 3, -1 },        
                { 1,  1 },
            };

            expected = expected.Divide(Norm.Norm1(expected));

            revertMatrix = revertMatrix.Divide(Norm.Norm1(revertMatrix));
            Assert.IsTrue(expected.IsEqual(revertMatrix, 0.05));



            var reverted = Accord.Statistics.Tools.ZScores(result).Abs();
            var original = Accord.Statistics.Tools.ZScores(S).Abs();

            Assert.IsTrue(reverted.IsEqual(original, 0.1));
        }