public unsafe void process(Complex *input, float *output, int length)
 {
     for (int i = 0; i < length; i++)
     {
         Complex complex = input[i] * _lastIq.Conjugate();
         float   mod     = complex.Modulus();
         if (mod > 0f)
         {
             complex /= mod;
         }
         float argument = complex.Argument();
         output[i] = argument * _gain;
         _lastIq   = input[i];
     }
 }
Example #2
0
        public static Complex[][] Magnitude(this Complex[][] transform, string path)
        {
            var size = transform.Length;

            float[,] floatTransform = new float[size, size];

            for (var i = 0; i < size; i++)
            {
                for (var j = 0; j < size; j++)
                {
                    floatTransform[i, j] = Complex.Modulus(transform[i][j]);
                }
            }

            var max = 0.0;

            for (var i = 0; i < size; i++)
            {
                for (var j = 0; j < size; j++)
                {
                    if (max < floatTransform[i, j])
                    {
                        max = floatTransform[i, j];
                    }
                }
            }

            var image = new Bitmap(size, size);

            for (var l = 0; l < size; l++)
            {
                for (var k = 0; k < size; k++)
                {
                    var pixelValue = Math.Log10(1 + floatTransform[l, k]) * 255 / Math.Log10(1 + max);

                    if (!double.IsNaN(pixelValue))
                    {
                        var color = Color.FromArgb((int)pixelValue, (int)pixelValue, (int)pixelValue);

                        image.SetPixel(l, k, color);
                    }
                }
            }
            image.Save(path);
            return(transform);
        }
        public void TestPolar()
        {
            var z1     = new Complex(1, 1);
            var minus4 = new Complex(-4, 0);
            var z      = Complex.FromPolarCoordinates(16, Math.PI / 4);

            Assert.IsTrue(AreApproximatelyEqual(Math.Pow(z1.Modulus(), 2), 2));
            Assert.IsTrue(AreApproximatelyEqual(Math.PI / 4, z1.Argument()));

            var z2 = Complex.FromPolarCoordinates(Math.Sqrt(2), Math.PI / 4);

            Assert.IsTrue(AreApproximatelyEqual(z1, z2));
            Assert.IsTrue(AreApproximatelyEqual(minus4.Argument(), Math.PI));
            Assert.IsTrue(AreApproximatelyEqual(z.Modulus(), 16.0));
            Assert.IsTrue(AreApproximatelyEqual(z.Argument(), Math.PI / 4));

            var z4 = new Complex(-1, 1);

            Assert.IsTrue(AreApproximatelyEqual(z4.Argument(), 3 * Math.PI / 4.0));

            var z5 = new Complex(-1, -1);

            Assert.IsTrue(AreApproximatelyEqual(z5.Argument(), -3 * Math.PI / 4.0));
        }
Example #4
0
        public float[,] Inverse(Complex[][] inputComplex)
        {
            var p = new Complex[Size][];
            var f = new Complex[Size][];
            var t = new Complex[Size][];

            var floatImage = new float[Size, Size];

            //CALCULATE P
            for (var l = 0; l < Size; l++)
            {
                p[l] = Inverse(inputComplex[l]);
            }

            //TRANSPOSE AND COMPUTE
            for (var l = 0; l < Size; l++)
            {
                t[l] = new Complex[Size];

                for (var k = 0; k < Size; k++)
                {
                    t[l][k] = p[k][l] / (Size * Size);
                }

                f[l] = Inverse(t[l]);
            }

            for (var k = 0; k < Size; k++)
            {
                for (var l = 0; l < Size; l++)
                {
                    floatImage[k, l] = Complex.Modulus(f[k][l]); //Math.Abs(f[k][l].Real);
                }
            }
            return(floatImage);
        }
Example #5
0
        private Complex postFixedOperatorEvaluator(List <Complex> values, string tokenString)
        {
            //TODO: Solve these problems in cases that cannot be evaluated numerically.
            //Possibly extend the Value type for non-numerical evaluation.
            Factors    factors;
            Complex    returnVal     = values.Last();
            List <int> listOfFactors = new List <int>();

            if (tokenString == "!")
            {
                if (values.Count() > 1)
                {
                    throw new Exception("suffix notation can only have one child");
                }
                for (int i = 2; i < returnVal.Real + 1; i++)
                {
                    listOfFactors.Add(i);
                }
                factors = new Factors(listOfFactors);
                return(returnVal.Factorial());
            }
            for (int i = values.Count() - 2; i >= 0; i--)
            {
                switch (tokenString)
                {
                case "+":
                    returnVal += values[i];
                    break;

                case "-":
                    returnVal -= values[i];
                    break;

                case "*":
                    returnVal *= values[i];
                    //TODO: Combine factors into a new factors list so the result doesn't need to be factored
                    break;

                case "/":
                    returnVal /= values[i];
                    break;

                case "%":
                    returnVal = returnVal.Modulus(values[i]);
                    break;

                case "^":
                    if (values.Count() > 2)
                    {
                        throw new Exception("Can't have more than two parameters for the power method.");
                    }
                    returnVal = MathNet.Numerics.ComplexExtensions.Power(returnVal, values[i]);
                    break;

                case "Sum":
                    returnVal += values[i];
                    break;

                default:
                    throw new Exception("unknown operator");
                }
            }
            if (returnVal == int.MinValue)
            {
                throw new Exception("No evaluation happened");
            }
            return(returnVal);
        }
        public void TestPolar()
        {
            var z1 = new Complex(1, 1);
            var minus4 = new Complex(-4, 0);
            var z = Complex.FromPolarCoordinates(16, Math.PI / 4);

            Assert.IsTrue(AreApproximatelyEqual(Math.Pow(z1.Modulus(), 2), 2));
            Assert.IsTrue(AreApproximatelyEqual(Math.PI / 4, z1.Argument()));

            var z2 = Complex.FromPolarCoordinates(Math.Sqrt(2), Math.PI / 4);

            Assert.IsTrue(AreApproximatelyEqual(z1, z2));
            Assert.IsTrue(AreApproximatelyEqual(minus4.Argument(), Math.PI));
            Assert.IsTrue(AreApproximatelyEqual(z.Modulus(), 16.0));
            Assert.IsTrue(AreApproximatelyEqual(z.Argument(), Math.PI / 4));

            var z4 = new Complex(-1,1);

            Assert.IsTrue(AreApproximatelyEqual(z4.Argument(), 3 * Math.PI / 4.0));

            var z5 = new Complex(-1, -1);

            Assert.IsTrue(AreApproximatelyEqual(z5.Argument(), -3 * Math.PI / 4.0));
        }
Example #7
0
        private static (Complex Zero, bool Converge, Complex[] qp) FxShift(int iterations, Complex s, Complex[] p, Complex[] h)
        {
            var z = new Complex(0, 0);

            var(pv, qp) = Deflate(p, s);
            var(t, qh)  = Calct(s, pv, h);
            var test = 1;
            var pasd = 0;

            for (var j = 1; j <= iterations; j++)
            {
                h = qh.NextH(t != new Complex(0, 0), t, qp);
                var ot = t.Clone();
                (t, qh) = Calct(s, pv, h);
                z       = s + t;
                var pumasa = !(t == new Complex(0, 0) || test != 1 || j == 12) && (t - ot).Modulus() < 0.5 * z.Modulus();
                if (pumasa && pasd == 1)
                {
                    var(zero, converge, qp2) = Vrshft(z, p, h);
                    if (converge)
                    {
                        return(zero, true, qp2);
                    }
                    test     = 0;
                    (pv, qp) = Deflate(p, s);
                    (t, qh)  = Calct(s, pv, h);
                    continue;
                }
                pasd = pumasa ? 1 : 0;
            }
            return(Vrshft(z, p, h));
        }
Example #8
0
 private static bool TestKungHindiZeroComplexNumber(Complex p1, Complex p2) => p1.Modulus() > DblEpsilon * 10 * p2.Modulus();