//
// Math.NET Numerics, part of the Math.NET Project
// http://mathnet.opensourcedotnet.info
//
// Copyright (c) 2009 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
namespace MathNet.Numerics.IntegralTransforms.Algorithms
{
using System;
using NumberTheory;
using Threading;
///
/// Complex Fast (FFT) Implementation of the Discrete Fourier Transform (DFT).
///
public partial class DiscreteFourierTransform
{
///
/// Generate the bluestein sequence for the provided problem size.
///
/// Number of samples.
/// Bluestein sequence exp(I*Pi*k^2/N)
private static Complex[] BluesteinSequence(int n)
{
double s = Constants.Pi / n;
var sequence = new Complex[n];
for (int k = 0; k < sequence.Length; k++)
{
double t = s * (k * k);
sequence[k] = Complex.WithRealImaginary(Math.Cos(t), Math.Sin(t));
}
return sequence;
}
///
/// Convolution with the bluestein sequence.
///
/// Sample Vector.
private static void BluesteinConvolution(Complex[] samples)
{
int n = samples.Length;
Complex[] sequence = BluesteinSequence(n);
// Padding to power of two >= 2N–1 so we can apply Radix-2 FFT.
int m = ((n << 1) - 1).CeilingToPowerOfTwo();
Complex[] b = new Complex[m];
Complex[] a = new Complex[m];
Parallel.Invoke(
() =>
{
// Build and transform padded sequence b_k = exp(I*Pi*k^2/N)
for (int i = 0; i < n; i++)
{
b[i] = sequence[i];
}
for (int i = m - n + 1; i < b.Length; i++)
{
b[i] = sequence[m - i];
}
Radix2(b, -1);
},
() =>
{
// Build and transform padded sequence a_k = x_k * exp(-I*Pi*k^2/N)
for (int i = 0; i < samples.Length; i++)
{
a[i] = sequence[i].Conjugate * samples[i];
}
Radix2(a, -1);
});
for (int i = 0; i < a.Length; i++)
{
a[i] *= b[i];
}
Radix2(a, 1);
var nbinv = 1.0 / m;
for (int i = 0; i < samples.Length; i++)
{
samples[i] = nbinv * sequence[i].Conjugate * a[i];
}
}
///
/// Swap the real and imaginary parts of each sample.
///
/// Sample Vector.
private static void SwapRealImaginary(Complex[] samples)
{
for (int i = 0; i < samples.Length; i++)
{
samples[i] = Complex.WithRealImaginary(samples[i].Imaginary, samples[i].Real);
}
}
///
/// Bluestein generic DFT, useful e.g. to verify faster algorithms.
///
/// Time-space sample vector.
/// Fourier series exponent sign.
internal static void Bluestein(Complex[] samples, int exponentSign)
{
int n = samples.Length;
if (n.IsPowerOfTwo())
{
Radix2(samples, exponentSign);
return;
}
if (exponentSign == 1)
{
SwapRealImaginary(samples);
}
BluesteinConvolution(samples);
if (exponentSign == 1)
{
SwapRealImaginary(samples);
}
}
///
/// Bluestein forward FFT for arbitrary sample vectors.
///
/// Sample vector, where the FFT is evaluated in place.
/// Fourier Transform Convention Options.
public void BluesteinForward(Complex[] samples, FourierOptions options)
{
Bluestein(samples, SignByOptions(options));
ForwardScaleByOptions(options, samples);
}
///
/// Bluestein inverse FFT for arbitrary sample vectors.
///
/// Sample vector, where the FFT is evaluated in place.
/// Fourier Transform Convention Options.
public void BluesteinInverse(Complex[] samples, FourierOptions options)
{
Bluestein(samples, -SignByOptions(options));
InverseScaleByOptions(options, samples);
}
}
}