Loading...

pfft.pfft


class Fft(T);
A class for calculating discrete fourier transform. The methods of this class use split format for complex data. This means that a complex data set is represented as two arrays - one for the real part and one for the imaginary part. An instance of this class can only be used for transforms of one particular size. The template parameter is the floating point type that the methods of the class will operate on.

Example:
import std.stdio, std.conv, std.exception;
import pfft.pfft;

void main(string[] args)
{
    auto n = to!int(args[1]);
    enforce((n & (n-1)) == 0, "N must be a power of two.");

    alias Fft!float F;

    auto f = new F(n);

    auto re = F.allocate(n);
    auto im = F.allocate(n);

    foreach(i, _; re)
        readf("%s %s\n", &re[i], &im[i]);

    f.fft(re, im);

    foreach(i, _; re)
        writefln("%s\t%s", re[i], im[i]);
}


this(size_t n);
The Fft constructor. The parameter is the size of data sets that fft and ifft will operate on. I will refer to this number as n in the rest of the documentation for this class.Tables used in fft and ifft are calculated in the constructor.

void fft(T[] re, T[] im);
Calculates discrete fourier transform. re should contain the real part of the data and im the imaginary part of the data. The method operates in place - the result is saved back to re and im. Both arrays must be properly aligned - to obtain a properly aligned array you can use allocate.

void ifft(T[] re, T[] im);
Calculates inverse discrete fourier transform scaled by n. The arguments have the same role as they do in fft.

T[] allocate(size_t n);
Allocates an array that is aligned properly for use with fft, ifft and scale methods.

void scale(T[] data, T k);
Scales an array data by factor k. The array must be properly aligned. To obtain a properly aligned array, use allocate.

class Rfft(T);
A class for calculating real discrete fourier transform. The methods of this class use split format for complex data. This means that complex data set is represented as two arrays - one for the real part and one for the imaginary part. An instance of this class can only be used for transforms of one particular size. The template parameter is the floating point type that the methods of the class will operate on.

Example:
import std.stdio, std.conv, std.exception;
import pfft.pfft;

void main(string[] args)
{
    auto n = to!int(args[1]);
    enforce((n & (n-1)) == 0, "N must be a power of two.");

    alias Rfft!float F;

    auto f = new F(n);

    auto data = F.allocate(n);

    foreach(ref e; data)
        readf("%s\n", &e);

    f.rfft(data);

    foreach(i; 0 .. n / 2 + 1)
        writefln("%s\t%s", data[i], (i == 0 || i == n / 2) ? 0 : data[i]);
}


this(size_t n);
The Rfft constructor. The parameter is the size of data sets that rfft will operate on. I will refer to this number as n in the rest of the documentation for this class. All tables used in rfft are calculated in the constructor.

void rfft(T[] data);
Calculates discrete fourier transform of the real valued sequence in data. The method operates in place. When the method completes, data contains the result. First n / 2 + 1 elements contain the real part of the result and the rest contains the imaginary part. Imaginary parts at position 0 and n / 2 are known to be equal to 0 and are not stored, so the content of data looks like this:

r(0), r(1), ... r(n / 2), i(1), i(2), ... i(n / 2 - 1)

The elements of the result at position greater than n / 2 can be trivially calculated from the relation DFT(f)[i] = DFT(f)[n - i]* that holds because the input sequence is real.

The length of the array must be equal to n and the array must be properly aligned. To obtain a properly aligned array you can use allocate.

void irfft(T[] data);
Calculates the inverse of rfft, scaled by n (You can use scale to normalize the result). Before the method is called, data should contain a complex sequence in the same format as the result of rfft. It is assumed that the input sequence is a discrete fourier transform of a real valued sequence, so the elements of the input sequence not stored in data can be calculated from DFT(f)[i] = DFT(f)[n - i]*. When the method completes, the array contains the real part of the inverse discrete fourier transform. The imaginary part is known to be equal to 0.

The length of the array must be equal to n and the array must be properly aligned. To obtain a properly aligned array you can use allocate.

alias allocate;
An alias for Fft!T. allocate

alias scale;
An alias for Fft!T. scale