Ejemplo n.º 1
0
        public void SamplesProcessorFunctions_GetFeedbackCompositeTest()
        {
            // y[n] = x[n] + x[n-1] + 0.5 * x[n-2] - y[n-1]
            Func <double, double> func   = x => x;
            Func <double, double> delay1 = SamplesProcessorFunctions.GetDelay(
                func,
                1.0);
            Func <double, double> delay2 = SamplesProcessorFunctions.GetDelay(
                func,
                1.0);
            Func <double, double> delay3 = SamplesProcessorFunctions.GetDelay(
                x => delay2.Invoke(x),
                2.0);
            Func <double, double> xMain = x => x + delay1.Invoke(x) + 0.5 * delay3.Invoke(x);

            var comp = SamplesProcessorFunctions.GetFeedbackComposite(
                (x, ix) => xMain.Invoke(x) - ix,
                1.0);

            // Comparação com o solucionador de equações às diferenças
            var diffSolv = SamplesProcessorFunctions.GetDifferenceEquationSolver(
                func,
                1.0,
                new double[] { 1.0, 0.5 },
                new double[] { -1.0 },
                new double[] { 1.0, 2.0 },
                new double[] { 1.0 });

            var t = 0.0;

            while (t < 100)
            {
                var expected = diffSolv.Invoke(t);
                var actual   = comp.Invoke(t);
                Assert.AreEqual(expected, actual);

                t += 1.0;
            }
        }
Ejemplo n.º 2
0
        public void SamplesProcessorFunctions_GetDelayTest()
        {
            var f          = SamplesGeneratorFunctions.GetSineFunc();
            var startValue = 2.0;
            var delay      = SamplesProcessorFunctions.GetDelay(
                f,
                startValue);

            // Testa o retardo simples.
            var t        = 0.0;
            var expected = startValue;

            while (t < 100)
            {
                var actual = delay.Invoke(t);
                Assert.AreEqual(expected, actual);

                expected = f.Invoke(t);
                t       += 1.0;
            }

            // Testa um retardo composto
            var startValue1 = 2.0;
            var startValue2 = 1.0;
            var startValue3 = 3.0;

            delay = SamplesProcessorFunctions.GetDelay(
                f,
                startValue);
            var delay1 = SamplesProcessorFunctions.GetDelay(
                delay,
                1.0);
            var delay2 = SamplesProcessorFunctions.GetDelay(
                delay1,
                3.0);

            t = 0.0;
            while (t < 100)
            {
                var actual = delay2.Invoke(t);
                Assert.AreEqual(startValue3, actual);

                startValue3 = startValue2;
                startValue2 = startValue1;
                startValue1 = f.Invoke(t);
                t          += 1.0;
            }

            // Testa um retardo complexo
            Func <double, double> func = x => x;

            delay1 = SamplesProcessorFunctions.GetDelay(
                func,
                1.0);
            delay2 = SamplesProcessorFunctions.GetDelay(
                func,
                1.0);
            Func <double, double> delay3 = SamplesProcessorFunctions.GetDelay(
                x => delay2.Invoke(x),
                2.0);
            Func <double, double> xMain = x => x + delay1.Invoke(x) + 0.5 * delay3.Invoke(x);

            var expectedItems = new double[] {
                2, 1.5, 3, 5.5, 8, 10.5, 13, 15.5,
                18, 20.5, 23, 25.5, 28, 30.5, 33, 35.5
            };

            t = 0.0;
            for (var i = 0; i < expectedItems.LongLength; ++i)
            {
                var innerExpected = expectedItems[i];
                var innerActual   = xMain.Invoke(t);
                Assert.AreEqual(innerExpected, innerActual);
                t += 1.0;
            }
        }