public void TestExecuteRelationalOperationWithStaticValue()
        {
            var array1 = new[] { 1.0, 3.1, 2.4, 2.0 };
            var result = WaveMath.ExecuteRelationalOperation(WaveMath.RelationalOperatorEnum.LessOrEqualsThan, array1, 2);

            Assert.IsTrue(TestUtils.SequenceEquals(result, new[] { 1.0, 0.0, 0.0, 1.0 }));
        }
Ejemplo n.º 2
0
 public void TestSwitch3()
 {
     Assert.IsTrue(new double[] { 1, 2, 20 }.SequenceEqual(WaveMath.Switch(new double[] { 1, 2, 3 }, new double[] { 10, 12, 20 }, new double[] { 12, 20, 8 }, WaveMath.SwitchCriteriaEnum.BIsGreaterThanThreshold)));
     Assert.IsTrue(new double[] { 1, 2, 20, 4 }.SequenceEqual(WaveMath.Switch(new double[] { 1, 2, 3, 4 }, new double[] { 10, 12, 20 }, new double[] { 12, 20, 8 }, WaveMath.SwitchCriteriaEnum.BIsGreaterThanThreshold)));
     Assert.IsTrue(new double[] { 1, 2, 20, 8 }.SequenceEqual(WaveMath.Switch(new double[] { 1, 2, 3 }, new double[] { 10, 12, 20, 8 }, new double[] { 12, 20, 8 }, WaveMath.SwitchCriteriaEnum.BIsGreaterThanThreshold)));
     Assert.IsTrue(new double[] { 10, 2, 3, 8 }.SequenceEqual(WaveMath.Switch(new double[] { 1, 2, 3 }, new double[] { 10, 12, 20, 8 }, new double[] { 8 }, WaveMath.SwitchCriteriaEnum.BIsGreaterThanThreshold)));
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Multilevel 1-D Discreete Wavelet Transform
        /// </summary>
        /// <param name="signal">The signal. Example: new Signal(5, 6, 7, 8, 1, 2, 3, 4)</param>
        /// <param name="motherWavelet">The mother wavelet to be used. Example: CommonMotherWavelets.GetWaveletFromName("DB4")</param>
        /// <param name="level">The depth-level to perform the DWT</param>
        /// <param name="extensionMode">Signal extension mode</param>
        /// <param name="convolutionMode">Defines what convolution function should be used</param>
        /// <returns></returns>
        public static List <DecompositionLevel> ExecuteDWT(Signal signal, MotherWavelet motherWavelet, int level, SignalExtension.ExtensionMode extensionMode = SignalExtension.ExtensionMode.SymmetricHalfPoint, ConvolutionModeEnum convolutionMode = ConvolutionModeEnum.ManagedFFT)
        {
            var levels = new List <DecompositionLevel>();

            var approximation = (double[])signal.Samples.Clone();
            var details       = (double[])signal.Samples.Clone();

            var realLength = signal.Samples.Length;

            for (var i = 1; i <= level; i++)
            {
                var extensionSize = motherWavelet.Filters.DecompositionLowPassFilter.Length - 1;

                approximation = SignalExtension.Extend(approximation, extensionMode, extensionSize);
                details       = SignalExtension.Extend(details, extensionMode, extensionSize);

                approximation = WaveMath.Convolve(convolutionMode, approximation, motherWavelet.Filters.DecompositionLowPassFilter);
                approximation = WaveMath.DownSample(approximation);

                details = WaveMath.Convolve(convolutionMode, details, motherWavelet.Filters.DecompositionHighPassFilter);
                details = WaveMath.DownSample(details);

                realLength = realLength / 2;

                levels.Add(new DecompositionLevel
                {
                    Signal        = signal,
                    Index         = i - 1,
                    Approximation = approximation,
                    Details       = details
                });
                details = (double[])approximation.Clone();
            }
            return(levels);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Executes the block
        /// </summary>
        public override void Execute()
        {
            var inputNode1 = InputNodes[0].ConnectingNode as BlockOutputNode;
            var inputNode2 = InputNodes[1].ConnectingNode as BlockOutputNode;

            if (inputNode1 == null || inputNode1.Object == null || inputNode2 == null || inputNode2.Object == null)
            {
                return;
            }

            var outputs = new List <Signal>();
            var signals = inputNode1.Object;
            var filters = inputNode2.Object;

            for (var i = 0; i < signals.Count; i++)
            {
                var signal = signals[i];
                if (filters.Count == 0)
                {
                    outputs.Add(signal.Clone());
                    continue;
                }
                var output      = signal.Copy();
                var filterIndex = i < filters.Count ? i : 0;
                output.Samples = WaveMath.Convolve(ConvolutionMode, signal.Samples, filters[filterIndex].Samples, ReturnOnlyValid, 0, FFTMode);
                outputs.Add(output);
            }
            OutputNodes[0].Object = outputs;
            if (Cascade && OutputNodes[0].ConnectingNode != null)
            {
                OutputNodes[0].ConnectingNode.Root.Execute();
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Executes the block
        /// </summary>
        public override void Execute()
        {
            var inputNode = InputNodes[0].ConnectingNode as BlockOutputNode;

            if (inputNode == null || inputNode.Object == null)
            {
                return;
            }

            OutputNodes[0].Object.Clear();
            foreach (var inputSignal in inputNode.Object)
            {
                var ifft = (double[])inputSignal.Samples.Clone();
                ManagedFFT.Instance.FFT(ref ifft, false, Mode);
                ifft = WaveMath.DownSample(ifft, 2, true);

                var signal = new Signal(ifft)
                {
                    Start            = 0,
                    Finish           = ifft.Length - 1,
                    SamplingInterval = 1
                };
                OutputNodes[0].Object.Add(signal);
            }

            if (Cascade && OutputNodes[0].ConnectingNode != null)
            {
                OutputNodes[0].ConnectingNode.Root.Execute();
            }
        }
        public void TestExecuteRelationalOperationWithPreviousSample()
        {
            var array1 = new[] { 1.0, 3.1, 2.4, 2.1 };
            var result = WaveMath.ExecuteRelationalOperationWithPreviousSample(WaveMath.RelationalOperatorEnum.LessThan, array1);

            Assert.IsTrue(TestUtils.SequenceEquals(result, new[] { 0.0, 0.0, 1.0, 1.0 }));
        }
Ejemplo n.º 7
0
        private void SetOperationDescription()
        {
            var resourceManager = new ResourceManager(typeof(Resources));

            _name        = resourceManager.GetString(Enum.GetName(typeof(WaveMath.OperationEnum), Operation));
            _description = "y(x) = y(x) " + WaveMath.GetOperationSymbol(Operation) + " " + string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:0.####}", Value);
        }
Ejemplo n.º 8
0
        private static Signal RescaleSignal(Signal decomposition, Signal signal, DecompositionLevel level)
        {
            var rescaled = WaveMath.InterpolateCubic(decomposition, (uint)Math.Pow(2, level.Index + 1));

            if (rescaled.SamplesCount < signal.SamplesCount)
            {
                var extensionSize = signal.SamplesCount - rescaled.SamplesCount;
                int right;
                var left = right = extensionSize / 2;
                if (left + right != extensionSize)
                {
                    left++;
                }

                SignalExtension.Extend(ref rescaled, SignalExtension.ExtensionMode.SmoothPadding0, left, right);
            }
            else
            {
                rescaled.Samples = SignalExtension.Deextend(rescaled.Samples, signal.SamplesCount);
            }
            rescaled.CustomPlot       = null;
            rescaled.Finish           = signal.Finish;
            rescaled.SamplingRate     = signal.SamplingRate;
            rescaled.SamplingInterval = signal.SamplingInterval;
            return(rescaled);
        }
Ejemplo n.º 9
0
        public void CompareConvolveMethods()
        {
            const int signalLength = 32000;
            const int filterLength = 128;

            //Creates signal and filter by sine wave
            var signalBlock = new GenerateSignalBlock {
                TemplateName = "Sine", Start = 0, Finish = 1, SamplingRate = signalLength, IgnoreLastSample = true
            };

            signalBlock.Execute();
            var signal = signalBlock.OutputNodes[0].Object[0].Samples;
            var filter = new double[filterLength];

            Array.Copy(signal, filter, filterLength);

            //Run Normal convolution and show time
            var stopWatch = new System.Diagnostics.Stopwatch();

            stopWatch.Start();
            var convolvedNormal = WaveMath.ConvolveNormal(signal, filter);

            stopWatch.Stop();
            Console.WriteLine("Normal Convolve {0} samples - {1} ms", convolvedNormal.Length, stopWatch.ElapsedMilliseconds);

            //Run ManagedFFT convolution and show time
            WaveMath.ConvolveManagedFFT(signal, filter);
            stopWatch.Reset();
            stopWatch.Start();
            var convolvedFFT = WaveMath.ConvolveManagedFFT(signal, filter);

            stopWatch.Stop();
            Console.WriteLine("FFT Convolve {0} samples - {1} ms", convolvedFFT.Length, stopWatch.ElapsedMilliseconds);
        }
Ejemplo n.º 10
0
        public void TestMultiplyScalar()
        {
            var          array1   = new[] { 1.2, 2.3, 3.4, 4.5 };
            const double scalar   = 1.1;
            var          expected = new[] { 1.32, 2.53, 3.74, 4.95 };

            Assert.IsTrue(TestUtils.SequenceEquals(expected, WaveMath.Multiply(array1, scalar)));
        }
Ejemplo n.º 11
0
        public void TestAddScalar()
        {
            var          array1   = new[] { 1.2, 2.3, 3.4, 4.5 };
            const double scalar   = 1.1;
            var          expected = new[] { 2.3, 3.4, 4.5, 5.6 };

            Assert.IsTrue(TestUtils.SequenceEquals(expected, WaveMath.Add(array1, scalar)));
        }
Ejemplo n.º 12
0
        public void TestSubtractScalar()
        {
            var          array1   = new[] { 1.2, 2.3, 3.4, 4.5 };
            const double scalar   = 1.1;
            var          expected = new[] { 0.1, 1.2, 2.3, 3.4 };

            Assert.IsTrue(TestUtils.SequenceEquals(expected, WaveMath.Subtract(array1, scalar)));
        }
Ejemplo n.º 13
0
 public void TestLimitRange()
 {
     Assert.AreEqual(10, WaveMath.LimitRange(5, 10, 20));
     Assert.AreEqual(200, WaveMath.LimitRange(500, 100, 200));
     Assert.AreEqual(3000, WaveMath.LimitRange(3000, 1000, 3500));
     Assert.AreEqual(4000, WaveMath.LimitRange(4000, 4000, 4001));
     Assert.AreEqual(5000, WaveMath.LimitRange(5000, 4000, 5000));
     Assert.AreEqual(6000, WaveMath.LimitRange(6000, 6000, 6000));
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Executes the block
        /// </summary>
        public override void Execute()
        {
            SetOperationDescription();

            var inputNode1 = InputNodes[0].ConnectingNode as BlockOutputNode;
            var inputNode2 = InputNodes[1].ConnectingNode as BlockOutputNode;

            if (inputNode1 == null || inputNode1.Object.Count == 0)
            {
                return;
            }

            OutputNodes[0].Object.Clear();
            for (var i = 0; i < inputNode1.Object.Count; i++)
            {
                var signal1 = inputNode1.Object[i];

                if (Operand == OperandEnum.StaticValue)
                {
                    OutputNodes[0].Object.Add(WaveMath.ExecuteRelationalOperation(Operation, signal1, ScalarValue));
                }
                else if (Operand == OperandEnum.Signal)
                {
                    if (inputNode2 == null)
                    {
                        return;
                    }
                    Signal signal2;
                    if (i < inputNode2.Object.Count)
                    {
                        signal2 = inputNode2.Object[i];
                    }
                    else if (inputNode2.Object.Count > 0)
                    {
                        signal2 = inputNode2.Object[0];
                    }
                    else
                    {
                        OutputNodes[0].Object.Add(WaveMath.ExecuteRelationalOperation(Operation, signal1, 0));
                        continue;
                    }
                    OutputNodes[0].Object.Add(WaveMath.ExecuteRelationalOperation(Operation, signal1, signal2));
                }
                else if (Operand == OperandEnum.NextSample)
                {
                    OutputNodes[0].Object.Add(WaveMath.ExecuteRelationalOperationWithNextSample(Operation, signal1));
                }
                else if (Operand == OperandEnum.PreviousSample)
                {
                    OutputNodes[0].Object.Add(WaveMath.ExecuteRelationalOperationWithPreviousSample(Operation, signal1));
                }
            }
            if (Cascade && OutputNodes[0].ConnectingNode != null)
            {
                OutputNodes[0].ConnectingNode.Root.Execute();
            }
        }
Ejemplo n.º 15
0
        public void TestMultiplyArrays()
        {
            var array1   = new[] { 1.2, 2.3, 3.4, 4.5 };
            var array2   = new[] { 1.1, 2.2, 3.3, 1 };
            var expected = new[] { 1.32, 5.06, 11.22, 4.5 };

            Assert.IsTrue(TestUtils.SequenceEquals(expected, WaveMath.GetOperationFunction(WaveMath.OperationEnum.Multiply)(array1, array2)));
            Assert.IsTrue(TestUtils.SequenceEquals(expected, WaveMath.Multiply(array2, array1)));
            Assert.IsTrue(TestUtils.SequenceEquals(expected, WaveMath.Multiply(array1, array2.SubArray(3))));
        }
Ejemplo n.º 16
0
        public void TestDivideScalar()
        {
            var          array1   = new[] { 1.2, 2.3, 3.4, 4.5 };
            const double scalar   = 1.1;
            var          expected = new[] { 1.09090909, 2.09090909, 3.09090909, 4.09090909 };

            Assert.IsTrue(TestUtils.SequenceEquals(expected, WaveMath.Divide(array1, scalar)));

            expected = new[] { 0.0, 0.0, 0.0, 0.0 };
            Assert.IsTrue(TestUtils.SequenceEquals(expected, WaveMath.Divide(array1, 0)));
        }
        public void TestExecuteLogicOperationWithSignals()
        {
            var signal1 = new Signal(new[] { 0.0, 0.0, 45.1, 10.2 });
            var signal2 = new Signal(new[] { 0.0, 20.0, 0.1, 15.1 });
            var signal3 = new Signal(new double[] { });
            var signal4 = new Signal(null);

            Assert.IsTrue(TestUtils.SequenceEquals(new[] { 0.0, 1.0, 1.0, 1.0 }, WaveMath.ExecuteLogicOperation(WaveMath.LogicalOperationEnum.Or, signal1, signal2).Samples));
            Assert.IsTrue(TestUtils.SequenceEquals(new[] { 0.0, 1.0, 1.0, 1.0 }, WaveMath.ExecuteLogicOperation(WaveMath.LogicalOperationEnum.Or, signal1, signal2, signal3, signal4).Samples));
            Assert.IsNull(WaveMath.ExecuteLogicOperation(WaveMath.LogicalOperationEnum.Or, null, signal3, signal4));
        }
Ejemplo n.º 18
0
        public void TestShift()
        {
            var signal = new Signal {
                Start = 3, Finish = 10
            };

            WaveMath.Shift(ref signal, -2.1);

            Assert.AreEqual(0.9d, signal.Start);
            Assert.AreEqual(7.9d, signal.Finish);
        }
        public void TestExecuteRelationalOperationWithSignals()
        {
            var signal1 = new Signal(new[] { 0.0, 0.0, 45.1, 10.2 });
            var signal2 = new Signal(new[] { 0.0, 20.0, 0.1, 15.1 });
            var signal3 = new Signal(new double[] { });
            var signal4 = new Signal(null);

            Assert.IsTrue(TestUtils.SequenceEquals(new[] { 1.0, 1.0, 0.0, 1.0 }, WaveMath.ExecuteRelationalOperation(WaveMath.RelationalOperatorEnum.LessOrEqualsThan, signal1, signal2).Samples));
            Assert.IsTrue(TestUtils.SequenceEquals(new[] { 1.0, 1.0, 0.0, 1.0 }, WaveMath.ExecuteRelationalOperation(WaveMath.RelationalOperatorEnum.LessOrEqualsThan, signal1, signal2, signal3, signal4).Samples));
            Assert.IsNull(WaveMath.ExecuteRelationalOperation(WaveMath.RelationalOperatorEnum.GreaterThan, null, signal3, signal4));
        }
Ejemplo n.º 20
0
        public void TestGetAccumulatedEnergy()
        {
            var input    = new double[] { 4, 2, 1, 5, 1, 5, 8, 9, 3 };
            var expected = 226d;

            Assert.AreEqual(expected, WaveMath.GetAccumulatedEnergy(input));

            input    = new double[] { };
            expected = 0d;
            Assert.AreEqual(expected, WaveMath.GetAccumulatedEnergy(input));
        }
        public void TestGetRelationalOperationFunctionEqualsTo()
        {
            var func = WaveMath.GetRelationalOperationFunction(WaveMath.RelationalOperatorEnum.EqualsTo);

            Assert.AreEqual(1, func(0, 0));
            Assert.AreEqual(0, func(0, 1));
            Assert.AreEqual(0, func(1, 0));
            Assert.AreEqual(1, func(1, 1));
            Assert.AreEqual(0, func(0, -1));
            Assert.AreEqual(0, func(-1, 0));
            Assert.AreEqual(1, func(-1, -1));
        }
Ejemplo n.º 22
0
        public void TestAddArrays()
        {
            var array1    = new[] { 1.2, 2.3, 3.4, 4.5 };
            var array2    = new[] { 1.1, 2.2, 3.3, 0 };
            var expected  = new[] { 2.3, 4.5, 6.7, 4.5 };
            var expected2 = new[] { 2.3, 4.5, 6.7, 0 };

            Assert.IsTrue(TestUtils.SequenceEquals(expected, WaveMath.GetOperationFunction(WaveMath.OperationEnum.Sum)(array1, array2)));
            Assert.IsTrue(TestUtils.SequenceEquals(expected, WaveMath.Add(array2, array1)));
            Assert.IsTrue(TestUtils.SequenceEquals(expected, WaveMath.Add(array1, array2.SubArray(3))));
            Assert.IsTrue(TestUtils.SequenceEquals(expected2, WaveMath.Add(array1.SubArray(3), array2)));
        }
        public void TestGetRelationalOperationFunctionGreaterOrEqualsThan()
        {
            var func = WaveMath.GetRelationalOperationFunction(WaveMath.RelationalOperatorEnum.GreaterOrEqualsThan);

            Assert.AreEqual(1, func(0, 0));
            Assert.AreEqual(0, func(0, 1));
            Assert.AreEqual(1, func(1, 0));
            Assert.AreEqual(1, func(1, 1));
            Assert.AreEqual(1, func(0, -1));
            Assert.AreEqual(0, func(-1, 0));
            Assert.AreEqual(1, func(-1, -1));
        }
        public void TestGetLogicalOperationFunctionNot()
        {
            var logicalNot = WaveMath.GetLogicalOperationFunction(WaveMath.LogicalOperationEnum.Not);

            Assert.AreEqual(1, logicalNot(0, 0)); //The second parameter is not used...
            Assert.AreEqual(1, logicalNot(0, 1));
            Assert.AreEqual(0, logicalNot(1, 0));
            Assert.AreEqual(0, logicalNot(1, 1));
            Assert.AreEqual(1, logicalNot(0, -1));
            Assert.AreEqual(0, logicalNot(-1, 0));
            Assert.AreEqual(0, logicalNot(-1, -1));
        }
        public void TestGetLogicalOperationFunctionAnd()
        {
            var logicalAnd = WaveMath.GetLogicalOperationFunction(WaveMath.LogicalOperationEnum.And);

            Assert.AreEqual(0, logicalAnd(0, 0));
            Assert.AreEqual(0, logicalAnd(0, 1));
            Assert.AreEqual(0, logicalAnd(1, 0));
            Assert.AreEqual(1, logicalAnd(1, 1));
            Assert.AreEqual(0, logicalAnd(0, -1));
            Assert.AreEqual(0, logicalAnd(-1, 0));
            Assert.AreEqual(1, logicalAnd(-1, -1));
        }
        public void TestGetLogicalOperationFunctionNotXor()
        {
            var logicalNotXor = WaveMath.GetLogicalOperationFunction(WaveMath.LogicalOperationEnum.NotXor);

            Assert.AreEqual(1, logicalNotXor(0, 0));
            Assert.AreEqual(0, logicalNotXor(0, 1));
            Assert.AreEqual(0, logicalNotXor(1, 0));
            Assert.AreEqual(1, logicalNotXor(1, 1));
            Assert.AreEqual(0, logicalNotXor(0, -1));
            Assert.AreEqual(0, logicalNotXor(-1, 0));
            Assert.AreEqual(1, logicalNotXor(-1, -1));
        }
        public void TestExecuteLogicOperation()
        {
            var x1 = new[] { 0.0, 0.0, 45.1, 10.2 };
            var x2 = new[] { 0.0, 20.0, 0.1, 15.1 };

            Assert.IsTrue(TestUtils.SequenceEquals(new[] { 0.0, 1.0, 1.0, 1.0 }, WaveMath.ExecuteLogicOperation(WaveMath.LogicalOperationEnum.Or, x1, x2)));

            x1 = new[] { 0.0, 0.0, 45.1, 10.2, 1.0 };
            x2 = new[] { 0.0, 20.0, 0.1, 15.1 };
            Assert.IsTrue(TestUtils.SequenceEquals(new[] { 0.0, 1.0, 1.0, 1.0, 0.0 }, WaveMath.ExecuteLogicOperation(WaveMath.LogicalOperationEnum.Or, x1, x2)));
            Assert.IsTrue(TestUtils.SequenceEquals(new[] { 0.0, 1.0, 1.0, 1.0, 0.0 }, WaveMath.ExecuteLogicOperation(WaveMath.LogicalOperationEnum.Or, x2, x1)));
        }
Ejemplo n.º 28
0
        public void TestSubtractArrays()
        {
            var array1    = new[] { 1.2, 2.2, 3.4, 4.5 };
            var array2    = new[] { 1.1, 2.3, 3.3, 0 };
            var expected1 = new[] { 0.1, -0.1, 0.1, 4.5 };
            var expected2 = new[] { -0.1, 0.1, -0.1, -4.5 };
            var expected3 = new[] { -0.1, 0.1, -0.1, 0 };

            Assert.IsTrue(TestUtils.SequenceEquals(expected1, WaveMath.GetOperationFunction(WaveMath.OperationEnum.Subtract)(array1, array2)));
            Assert.IsTrue(TestUtils.SequenceEquals(expected2, WaveMath.Subtract(array2, array1)));
            Assert.IsTrue(TestUtils.SequenceEquals(expected1, WaveMath.Subtract(array1, array2.SubArray(3))));
            Assert.IsTrue(TestUtils.SequenceEquals(expected3, WaveMath.Subtract(array1.SubArray(3), array2)));
        }
Ejemplo n.º 29
0
            internal Disturbance(int start, int finish, int detailsLength, int signalLength)
            {
                Start  = start;
                Finish = finish;

                SignalStart  = (int)WaveMath.Scale(start, 0, detailsLength, 0, signalLength) + 1;
                SignalFinish = (int)WaveMath.Scale(finish, 0, detailsLength, 0, signalLength) + 1;

                if (start % 2 != 0 && finish % 2 != 0)
                {
                    SignalFinish--;
                }
            }
Ejemplo n.º 30
0
        public void TestInvert()
        {
            var samples = new double[] { -1, 2, 3, -4 };
            var output  = WaveMath.Invert(samples);

            Assert.IsTrue(output.SequenceEqual(new double[] { -4, 3, 2, -1 }));

            samples = new double[] { };
            output  = WaveMath.Invert(samples);
            Assert.IsTrue(output.SequenceEqual(new double[] { }));

            output = WaveMath.Invert(null);
            Assert.IsNull(output);
        }