Example #1
0
        public void NaiveTransformsRealSineCorrectly()
        {
            var samples = SignalGenerator.EquidistantPeriodic(w => new Complex(Math.Sin(w), 0), Constants.Pi2, 0, 16);

            // real-odd transforms to imaginary odd
            var dft = new DiscreteFourierTransform();
            var spectrum = dft.NaiveForward(samples, FourierOptions.Matlab);

            // all real components must be zero
            foreach (var c in spectrum)
            {
                Assert.AreEqual(0, c.Real, 1e-12, "real");
            }

            // all imaginary components except second and last musth be zero
            for (var i = 0; i < spectrum.Length; i++)
            {
                if (i == 1)
                {
                    Assert.AreEqual(-8, spectrum[i].Imaginary, 1e-12, "imag second");
                }
                else if (i == spectrum.Length - 1)
                {
                    Assert.AreEqual(8, spectrum[i].Imaginary, 1e-12, "imag last");
                }
                else
                {
                    Assert.AreEqual(0, spectrum[i].Imaginary, 1e-12, "imag");
                }
            }
        }
        public void FourierBluesteinIsReversible(FourierOptions options)
        {
            var dft = new DiscreteFourierTransform();

            var samples = Generate.RandomComplex(0x7FFF, GetUniform(1));
            var work = new Complex[samples.Length];
            samples.CopyTo(work, 0);

            dft.BluesteinForward(work, options);
            Assert.IsFalse(work.ListAlmostEqual(samples, 6));

            dft.BluesteinInverse(work, options);
            AssertHelpers.ListAlmostEqual(samples, work, 10);
        }
        public void FourierRadix2IsReversible(FourierOptions options)
        {
            var dft = new DiscreteFourierTransform();

            var samples = SignalGenerator.Random((u, v) => new Complex(u, v), GetUniform(1), 0x8000);
            var work = new Complex[samples.Length];
            samples.CopyTo(work, 0);

            dft.Radix2Forward(work, options);
            Assert.IsFalse(work.ListAlmostEqual(samples, 6));

            dft.Radix2Inverse(work, options);
            AssertHelpers.ListAlmostEqual(samples, work, 12);
        }
        public void FourierBluesteinMatchesNaiveOnRandomNonPowerOfTwo(FourierOptions options)
        {
            var dft = new DiscreteFourierTransform();
            var samples = Generate.RandomComplex(0x7F, GetUniform(1));

            VerifyMatchesNaiveComplex(
                samples,
                10,
                s => dft.NaiveForward(s, options),
                s => dft.BluesteinForward(s, options));
            VerifyMatchesNaiveComplex(
                samples,
                10,
                s => dft.NaiveInverse(s, options),
                s => dft.BluesteinInverse(s, options));
        }
        public void FourierBluesteinMatchesNaiveOnRandomNonPowerOfTwo(FourierOptions options)
        {
            var dft = new DiscreteFourierTransform();
            var samples = SignalGenerator.Random((u, v) => new Complex(u, v), GetUniform(1), 0x7F);

            VerifyMatchesNaiveComplex(
                samples,
                1e-12,
                s => dft.NaiveForward(s, options),
                s => dft.BluesteinForward(s, options));
            VerifyMatchesNaiveComplex(
                samples,
                1e-12,
                s => dft.NaiveInverse(s, options),
                s => dft.BluesteinInverse(s, options));
        }
        public void FourierBluesteinIsReversible(FourierOptions options)
        {
            var dft = new DiscreteFourierTransform();

            VerifyIsReversibleComplex(
                0x7FFF,
                1e-12,
                s =>
                    {
                        dft.BluesteinForward(s, options);
                        return s;
                    },
                s =>
                    {
                        dft.BluesteinInverse(s, options);
                        return s;
                    });
        }
        public void FourierRadix2MatchesNaiveOnRealSine(FourierOptions options)
        {
            var dft = new DiscreteFourierTransform();
            var samples = Generate.PeriodicMap(16, w => new Complex(Math.Sin(w), 0), 16, 1.0, Constants.Pi2);

            VerifyMatchesNaiveComplex(
                samples,
                12,
                s => dft.NaiveForward(s, options),
                s => dft.Radix2Forward(s, options));

            VerifyMatchesNaiveComplex(
                samples,
                12,
                s => dft.NaiveInverse(s, options),
                s => dft.Radix2Inverse(s, options));
        }
        public void FourierRadix2MatchesNaiveOnRandom(FourierOptions options)
        {
            var dft = new DiscreteFourierTransform();
            var samples = Generate.RandomComplex(0x80, GetUniform(1));

            VerifyMatchesNaiveComplex(
                samples,
                10,
                s => dft.NaiveForward(s, options),
                s => dft.Radix2Forward(s, options));

            VerifyMatchesNaiveComplex(
                samples,
                10,
                s => dft.NaiveInverse(s, options),
                s => dft.Radix2Inverse(s, options));
        }
        public void FourierBluesteinMatchesNaiveOnRealSineNonPowerOfTwo(FourierOptions options)
        {
            var dft = new DiscreteFourierTransform();
            var samples = Generate.PeriodicMap(14, w => new Complex(Math.Sin(w), 0), 14, 1.0, Constants.Pi2);

            VerifyMatchesNaiveComplex(
                samples,
                12,
                s => dft.NaiveForward(s, options),
                s => dft.BluesteinForward(s, options));

            VerifyMatchesNaiveComplex(
                samples,
                12,
                s => dft.NaiveInverse(s, options),
                s => dft.BluesteinInverse(s, options));
        }
Example #10
0
        public void Radix2ThrowsWhenNotPowerOfTwo()
        {
            var samples = SignalGenerator.Random((u, v) => new Complex(u, v), GetUniform(1), 0x7F);

            var dft = new DiscreteFourierTransform();

            Assert.Throws(typeof (ArgumentException), () => dft.Radix2Forward(samples, FourierOptions.Default));
            Assert.Throws(typeof (ArgumentException), () => dft.Radix2Inverse(samples, FourierOptions.Default));
            Assert.Throws(typeof (ArgumentException), () => DiscreteFourierTransform.Radix2(samples, -1));
            Assert.Throws(typeof (ArgumentException), () => DiscreteFourierTransform.Radix2Parallel(samples, -1));
        }
        public void FourierRadix2MatchesNaiveOnRealSine(FourierOptions options)
        {
            var dft = new DiscreteFourierTransform();
            var samples = SignalGenerator.EquidistantPeriodic(w => new Complex(Math.Sin(w), 0), Constants.Pi2, 0, 16);

            VerifyMatchesNaiveComplex(
                samples,
                1e-12,
                s => dft.NaiveForward(s, options),
                s => dft.Radix2Forward(s, options));

            VerifyMatchesNaiveComplex(
                samples,
                1e-12,
                s => dft.NaiveInverse(s, options),
                s => dft.Radix2Inverse(s, options));
        }
        public void FourierRadix2MatchesNaiveOnRandom(FourierOptions options)
        {
            var dft = new DiscreteFourierTransform();
            var samples = SignalGenerator.Random((u, v) => new Complex(u, v), GetUniform(1), 0x80);

            VerifyMatchesNaiveComplex(
                samples,
                1e-12,
                s => dft.NaiveForward(s, options),
                s => dft.Radix2Forward(s, options));

            VerifyMatchesNaiveComplex(
                samples,
                1e-12,
                s => dft.NaiveInverse(s, options),
                s => dft.Radix2Inverse(s, options));
        }
        public void FourierRadix2IsReversible(FourierOptions options)
        {
            var dft = new DiscreteFourierTransform();

            VerifyIsReversibleComplex(
                0x8000,
                1e-12,
                s =>
                    {
                        dft.Radix2Forward(s, options);
                        return s;
                    },
                s =>
                    {
                        dft.Radix2Inverse(s, options);
                        return s;
                    });
        }
        public void FourierNaiveIsReversible(FourierOptions options)
        {
            var dft = new DiscreteFourierTransform();

            VerifyIsReversibleComplex(
                0x80,
                1e-12,
                s => dft.NaiveForward(s, options),
                s => dft.NaiveInverse(s, options));
        }
        public void FourierBluesteinMatchesNaiveOnRealSineNonPowerOfTwo(FourierOptions options)
        {
            var dft = new DiscreteFourierTransform();
            var samples = SignalGenerator.EquidistantPeriodic(w => new Complex(Math.Sin(w), 0), Constants.Pi2, 0, 14);

            VerifyMatchesNaiveComplex(
                samples,
                12,
                s => dft.NaiveForward(s, options),
                s => dft.BluesteinForward(s, options));

            VerifyMatchesNaiveComplex(
                samples,
                12,
                s => dft.NaiveInverse(s, options),
                s => dft.BluesteinInverse(s, options));
        }