Exemplo n.º 1
0
        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        private bool CompareFFT(double[] x)
        {
            int N  = x.Length;
            var xC = WWComplex.FromRealArray(x);

            var Xexpected = WWDftCpu.Dft1d(xC);

            var fft     = new WWRadix2Fft(N);
            var Xactual = fft.ForwardFft(xC, 1.0 / N);

            return(WWComplex.AverageDistance(Xactual, Xexpected) < 1e-8);
        }
        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        public bool TestSDFT(double[] x)
        {
            WWComplex[] Xexpected = null;
            WWDftCpu.Dft1d(WWComplex.FromRealArray(x), out Xexpected);

            var sdft = new WWSlidingDFT(x.Length);

            WWComplex[] Xactual = null;
            for (int i = 0; i < x.Length; ++i)
            {
                Xactual = sdft.Filter(x[i]);
            }

            return(WWComplex.AverageDistance(Xexpected, Xactual) < 1e-8);
        }
Exemplo n.º 3
0
        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        private bool CompareDft(double[] x)
        {
            int N = 4;

            var Xexpected = WWDftCpu.Dft1d(WWComplex.FromRealArray(x), 1.0);
            var Xactual   = new WWComplex[N];

            for (int m = 0; m < N; ++m)
            {
                var g = new WWGoertzel(m, N);
                for (int i = 0; i < N; ++i)
                {
                    Xactual[m] = g.Filter(x[i]);
                }
            }

            return(WWComplex.AverageDistance(Xexpected, Xactual) < 1e-8);
        }
        public bool TestSDFT_LastN(double[] x, int N)
        {
            System.Diagnostics.Debug.Assert(N <= x.Length);

            var xLastN = new double[N];

            Array.Copy(x, x.Length - N, xLastN, 0, N);

            WWComplex[] Xexpected = null;
            WWDftCpu.Dft1d(WWComplex.FromRealArray(xLastN), out Xexpected);

            var sdft = new WWSlidingDFT(N);

            WWComplex[] Xactual = null;
            for (int i = 0; i < x.Length; ++i)
            {
                Xactual = sdft.Filter(x[i]);
            }

            return(WWComplex.AverageDistance(Xexpected, Xactual) < 1e-8);
        }
Exemplo n.º 5
0
        public WWComplex[] Run(string path)
        {
            System.Drawing.Bitmap bm = new System.Drawing.Bitmap(path);

            int width  = bm.Width;
            int height = bm.Height;

            // 0を表す中央の線の位置とグラフの線の位置を調べる。
            var positions = new GraphPositions[width];
            var histogram = new int[height];

            for (int x = 0; x < width; ++x)
            {
                // 上から調べる
                for (int y = 0; y < height; ++y)
                {
                    var   c = bm.GetPixel(x, y);
                    float b = c.GetBrightness();
                    if (b < 0.5f)
                    {
                        positions[x].upper = y;
                        ++histogram[y];
                        break;
                    }
                }

                // 下から調べる
                for (int y = height - 1; 0 <= y; --y)
                {
                    var   c = bm.GetPixel(x, y);
                    float b = c.GetBrightness();
                    if (b < 0.5f)
                    {
                        positions[x].lower = y;
                        ++histogram[y];
                        break;
                    }
                }
            }

            int minValue = -1;

            for (int y = 0; y < height; ++y)
            {
                if (0 < histogram[y])
                {
                    minValue = y;
                    break;
                }
            }

            int maxValue = height;

            for (int y = height - 1; 0 <= y; --y)
            {
                if (0 < histogram[y])
                {
                    maxValue = y;
                    break;
                }
            }

            int zeroPosition = 0;
            int histogramMax = 0;

            for (int y = 0; y < height; ++y)
            {
                if (histogramMax < histogram[y])
                {
                    zeroPosition = y;
                    histogramMax = histogram[y];
                }
            }

            // 中央の線の位置 = zeroPosition
            // 最大値 maxValue
            // 最小値 minValue

            double center    = (maxValue + minValue) / 2.0;
            double magnitude = (maxValue - minValue) / 2.0;

            var waveForm = new WWComplex[width];

            for (int x = 0; x < width; ++x)
            {
                int iv = positions[x].upper;
                if (zeroPosition == iv)
                {
                    iv = positions[x].lower;
                }

                double v = (center - iv) / magnitude;

                waveForm[x] = new WWComplex(v, 0);
                Console.WriteLine("{0} {1}", x, v);
            }

            WWComplex [] freqResponse;
            WWDftCpu.Dft1d(waveForm, out freqResponse);

            return(freqResponse);
        }