예제 #1
0
        public void GetMatrixTest()
        {
            var matrix = Walsh.GetMatrix(0);

            Assert.AreEqual(1, matrix.GetLength(0));
            Assert.AreEqual(1, matrix.GetLength(1));
            Assert.AreEqual(1, matrix[0, 0]);

            matrix = Walsh.GetMatrix(1);
            Assert.AreEqual(2, matrix.GetLength(0));
            Assert.AreEqual(2, matrix.GetLength(1));
            Assert.AreEqual(new sbyte[, ] {
                { 1, 1 }, { 1, -1 }
            }, matrix);

            matrix = Walsh.GetMatrix(2);
            Assert.AreEqual(4, matrix.GetLength(0));
            Assert.AreEqual(4, matrix.GetLength(1));
            Assert.AreEqual(new sbyte[, ] {
                { 1, 1, 1, 1 }, { 1, 1, -1, -1 }, { 1, -1, 1, -1 }, { 1, -1, -1, 1 }
            }, matrix);

            matrix = Walsh.GetMatrix(3);
            Assert.AreEqual(8, matrix.GetLength(0));
            Assert.AreEqual(8, matrix.GetLength(1));
            Assert.AreEqual(-1, matrix[5, 4]);
        }
예제 #2
0
        public void Adamar()
        {
            int N = 8;

            sbyte[,] Ad = Walsh.AdamarMatrix(N);

            sbyte[,] AdTest = new sbyte[8, 8] {
                { 1, 1, 1, 1, 1, 1, 1, 1 },
                { 1, 1, 1, 1, -1, -1, -1, -1 },
                { 1, 1, -1, -1, -1, -1, 1, 1 },
                { 1, 1, -1, -1, 1, 1, -1, -1 },
                { 1, -1, -1, 1, 1, -1, -1, 1 },
                { 1, -1, -1, 1, -1, 1, 1, -1 },
                { 1, -1, 1, -1, -1, 1, -1, 1 },
                { 1, -1, 1, -1, 1, -1, 1, -1 },
            };


            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    if (Ad[i, j] != AdTest[i, j])
                    {
                        Assert.Fail();
                    }
                }
            }
        }
예제 #3
0
        public void Transform()
        {
            double[] s     = { 1, 2, 0, 3 };
            double[] XTest = { 1.5, 0, 0.5, -1 };

            double[] X = Walsh.Transform(s);


            for (int i = 0; i < s.Length; i++)
            {
                if (Math.Abs(X[i] - XTest[i]) > 0.0001)
                {
                    Assert.Fail();
                }
            }
        }
예제 #4
0
        public void PhaseSpectrum()
        {
            double[] s             = { 1, 2, 0, 3 };
            double[] phaseSpecTest = { 0, 0, Math.PI / 2 };

            double[] X         = Walsh.Transform(s);
            double[] phaseSpec = Walsh.PhaseSpectrum(X);


            for (int i = 0; i < phaseSpec.Length; i++)
            {
                if (Math.Abs(phaseSpec[i] - phaseSpecTest[i]) > 0.0001)
                {
                    Assert.Fail();
                }
            }
        }
예제 #5
0
        public void AmplitudeSpectrum()
        {
            double[] s           = { 1, 2, 0, 3 };
            double[] ampSpecTest = { 1.5, 0.5, 1 };

            double[] X       = Walsh.Transform(s);
            double[] ampSpec = Walsh.AmplitudeSpectrum(X);


            for (int i = 0; i < ampSpec.Length; i++)
            {
                if (Math.Abs(ampSpec[i] - ampSpecTest[i]) > 0.0001)
                {
                    Assert.Fail();
                }
            }
        }
예제 #6
0
        /// <summary>
        /// coeff(n)=c_{1,n}(f), n > 0
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        private double coeff(Func <double, double> f, int n)
        {
            // we use formula c_{1,n}(f) = c_n(f')
            --n;
            if (n == 0)
            {
                return(f(1) - f(0));
            }

            var k     = (int)Log(n, 2) + 1;
            var w     = Walsh.GetMatrix(k);
            var pow2k = Pow(2, k);
            var s     = 0d;

            for (int j = 0; j <= pow2k - 1; j++)
            {
                s += w[n, j] * (f((j + 1) / pow2k) - f(j / pow2k));
            }

            return(s);
        }
예제 #7
0
        public void GetTest()
        {
            var w0 = Walsh.Get(0);

            Assert.That(Enumerable.Range(0, 100).Select(i => w0(i / 100.0)).All(x => x == 1));

            var w1 = Walsh.Get(1);

            for (int i = 0; i < 100; i++)
            {
                var x = i / 100.0;
                if (x < 0.5)
                {
                    Assert.AreEqual(1.0, w1(x));
                }
                if (x > 0.5)
                {
                    Assert.AreEqual(-1.0, w1(x));
                }
            }

            var w3 = Walsh.Get(3);

            for (int i = 0; i < 100; i++)
            {
                var x = i / 100.0;
                if (x < 0.25)
                {
                    Assert.AreEqual(1.0, w3(x));
                }
                if (x > 0.75)
                {
                    Assert.AreEqual(1.0, w3(x));
                }
                if (x > 0.25 && x < 0.75)
                {
                    Assert.AreEqual(-1.0, w3(x));
                }
            }
        }
예제 #8
0
    private static void test01()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST01 tests FWT.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    16 March 2011
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int       j;
        const int n = 16;

        Console.WriteLine("");
        Console.WriteLine("TEST01");
        Console.WriteLine("  FWT computes a fast Walsh transform.");

        for (j = 1; j <= 2; j++)
        {
            double[] w;
            int      i;
            switch (j)
            {
            case 1:
                int seed = 123456789;
                w = UniformRNG.r8vec_uniform_01_new(n, ref seed);
                break;

            default:
            {
                w = new double[n];
                for (i = 0; i < n; i++)
                {
                    w[i] = i + 1;
                }

                break;
            }
            }

            double[] x = typeMethods.r8vec_copy_new(n, w);
            Walsh.fwt(n, ref w);
            double[] y = typeMethods.r8vec_copy_new(n, w);
            for (i = 0; i < n; i++)
            {
                y[i] /= n;
            }

            Walsh.fwt(n, ref w);
            double[] z = typeMethods.r8vec_copy_new(n, w);
            for (i = 0; i < n; i++)
            {
                z[i] /= n;
            }

            Console.WriteLine("");
            Console.WriteLine("     I        X(I)    Y=FWT(X)/N   Z=FWT(Y)/N");
            Console.WriteLine("");
            for (i = 0; i < n; i++)
            {
                Console.WriteLine("  " + i.ToString(CultureInfo.InvariantCulture).PadLeft(2)
                                  + "  " + x[i].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                                  + "  " + y[i].ToString(CultureInfo.InvariantCulture).PadLeft(10)
                                  + "  " + z[i].ToString(CultureInfo.InvariantCulture).PadLeft(10) + "");
            }
        }
    }
예제 #9
0
        private double coeffByDef(int n)
        {
            var w = Walsh.Get(n - 1);

            return(Integrals.Trapezoid(x => df(x) * w(x), 0, 1, 10000));
        }