Exemplo n.º 1
0
        public WalshTransform(int n)
        {
            if (Math.Log(n, 2) % 1 != 0)
            {
                throw new ApplicationException("Wrong N!");
            }

            _n = n;

            RademacherFunctions    = new int[4][];
            RademacherFunctions[0] = new[] { 1, 1, 1, 1, 1, 1, 1, 1 };
            RademacherFunctions[1] = new[] { 1, 1, 1, 1, -1, -1, -1, -1 };
            RademacherFunctions[2] = new[] { 1, 1, -1, -1, 1, 1, -1, -1 };
            RademacherFunctions[3] = new[] { 1, -1, 1, -1, 1, -1, 1, -1 };

            WalshFunctions    = new int[n][];
            WalshFunctions[0] = new[] { 1, 1, 1, 1, 1, 1, 1, 1 };
            var nn = new[] { 0, 0, 0, 0 };
            var rr = new int[3];

            for (int i = 1; i < n; i++)
            {
                string bin = NumericHelper.IntToBinaryString(i, 3);
                nn[3] = Convert.ToInt32(bin[0].ToString(), 10);
                nn[2] = Convert.ToInt32(bin[1].ToString(), 10);
                nn[1] = Convert.ToInt32(bin[2].ToString(), 10);

                for (int j = nn.Length - 1; j > 0; j--)
                {
                    rr[nn.Length - 1 - j] = XOR(nn[j], nn[j - 1]);
                }

                WalshFunctions[i] = new[] { 1, 1, 1, 1, 1, 1, 1, 1 };
                for (int j = 0; j < rr.Length; j++)
                {
                    if (rr[j] == 1)
                    {
                        WalshFunctions[i] = NumericHelper.MultiplyArrays(WalshFunctions[i], RademacherFunctions[j + 1]);
                    }
                }
            }

            SetWalshFunctionsInHadamarOrder();
        }