From 01d2a9ce623726557949aebb83e497ae2afa21e4 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Fri, 28 Oct 2016 15:09:49 +0200 Subject: [PATCH] FFT: tests to verify real against complex, and real forward to inverse --- .../InverseTransformTest.cs | 19 ++++++++++ .../MatchingNaiveTransformTest.cs | 36 +++++++++++++++---- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/UnitTests/IntegralTransformsTests/InverseTransformTest.cs b/src/UnitTests/IntegralTransformsTests/InverseTransformTest.cs index 19211a14..7db77ffc 100644 --- a/src/UnitTests/IntegralTransformsTests/InverseTransformTest.cs +++ b/src/UnitTests/IntegralTransformsTests/InverseTransformTest.cs @@ -109,6 +109,25 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests AssertHelpers.AlmostEqual(samples, work, 10); } + /// + /// Fourier bluestein is reversible. + /// + /// Fourier options. + [TestCase(FourierOptions.Default)] + [TestCase(FourierOptions.Matlab)] + public void FourierRealIsReversible(FourierOptions options) + { + var samples = Generate.Random(0x7FFF, GetUniform(1)); + var work = new double[samples.Length+2]; + samples.CopyTo(work, 0); + + Fourier.ForwardReal(work, samples.Length, options); + Assert.IsFalse(work.ListAlmostEqual(samples, 6)); + + Fourier.InverseReal(work, samples.Length, options); + AssertHelpers.AlmostEqual(samples, work, 10); + } + /// /// Hartley naive is reversible. /// diff --git a/src/UnitTests/IntegralTransformsTests/MatchingNaiveTransformTest.cs b/src/UnitTests/IntegralTransformsTests/MatchingNaiveTransformTest.cs index e40185d5..2fc25462 100644 --- a/src/UnitTests/IntegralTransformsTests/MatchingNaiveTransformTest.cs +++ b/src/UnitTests/IntegralTransformsTests/MatchingNaiveTransformTest.cs @@ -181,19 +181,41 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests VerifyInplace(samples, 10, options, Fourier.Inverse, Fourier.BluesteinInverse); } + [TestCase(FourierOptions.Default, 128)] + [TestCase(FourierOptions.Default, 129)] + [TestCase(FourierOptions.NoScaling, 128)] + [TestCase(FourierOptions.NoScaling, 129)] + [TestCase(FourierOptions.AsymmetricScaling, 128)] + [TestCase(FourierOptions.AsymmetricScaling, 129)] + public void RealMatchesComplex(FourierOptions options, int n) + { + var real = Generate.Random(n.IsEven() ? n + 2 : n + 1, GetUniform(1)); + real[n] = 0d; + if (n.IsEven()) real[n+1] = 0d; + var complex = new Complex[n]; + for (int i = 0; i < complex.Length; i++) + { + complex[i] = new Complex(real[i], 0d); + } + + Fourier.Forward(complex, options); + Fourier.ForwardReal(real, n, options); + + int m = (n + 1)/2; + for (int i = 0, j = 0; i < m; i++) + { + AssertHelpers.AlmostEqual(complex[i], new Complex(real[j++], real[j++]), 10); + } + } + [Test] public void AlgorithmsMatchProvider_PowerOfTwo_Large() { // 65536 = 2^16 - const FourierOptions options = FourierOptions.NoScaling; var samples = Generate.RandomComplex(65536, GetUniform(1)); - var provider = new Complex[samples.Length]; - samples.Copy(provider); - Control.FourierTransformProvider.Forward(provider, FourierTransformScaling.NoScaling); - - Verify(samples, 10, options, (a, b) => provider, Fourier.Radix2Forward); - Verify(samples, 10, options, (a, b) => provider, Fourier.BluesteinForward); + VerifyInplace(samples, 10, FourierOptions.NoScaling, (s,o) => Control.FourierTransformProvider.Forward(s, FourierTransformScaling.NoScaling), Fourier.Radix2Forward); + VerifyInplace(samples, 10, FourierOptions.NoScaling, (s, o) => Control.FourierTransformProvider.Forward(s, FourierTransformScaling.NoScaling), Fourier.BluesteinForward); } [Test]