UNPKG

ts-scikit

Version:

A scientific toolkit written in Typescript

368 lines (367 loc) 14.9 kB
import { Sampling } from './sampling'; /** * An easy-to-use fast Fourier transform. * <p> * <em>WARNING: NOT YET TESTED!</em> * This class is less flexible than {@link FftComplex} and {@link FftReal}. * For example, the user has less control over the sampling of frequency. * However, for many applications this class may be simpler to use. * <p> * For example, the following program shows how to use this class to * filter a real-valued sequence in the frequency domain. * <pre> * const fft = new Fft(nx); // nx = number of samples of f(x) * const sk = fft.getFrequencySampling1(); * const nk = sk.count; // number of frequencies sampled. * const f: number[] = ... // nx real samples of input f(x) * const g: number[] = fft.applyForward(f); * for (let kk=0, kr=0, ki=kr + 1; kk &lt; nk; ++kk, kr+=2, ki+=2 ) { * let k = sk.getValue(kk); // frequency k in cycles/sample. * // modify g[kr], the real part of g(k) * // modify g[ki], the imag part of g(k) * } * const h[] = fft.applyInverse(g); // nk real samples of output h(x) * </pre> * <p> * This example is almost as simple for multi-dimensional transforms. * <p> * A forward transform computes an output array of complex values g(x) * from an input array of real or complex values f(x). An inverse * transform computes the corresponding real of complex values f(x) * from g(k). For definiteness, in this documentation, the variable x * represents spatial coordinates and the variable k represents spatial * frequencies (or wavenumbers). For functions of time, simply replace * the word "space" with "time" in this documentation. * <p> * This class enables transforms of 1D, 2D, and 3D arrays. For example, * a 2D array f[nx2][nx1] represents nx2*nx1 samples of a function * f(x1, x2) of two spatial coordinates x1 and x2. In addition to * numbers of samples nx1 and nx2, sampling intervals dx1 and dx2 * and first sampled coordinates fx1 and fx2 may also be specified. * (The default sampling interval is 1.0 and the default first sample * coordinate is 0.0.) These sampling parameters may be specified with * samplings sx1 and sx2. * <p> * For each specified spatial sampling sx, this class defines a * corresponding frequency sampling sk, in which units of frequency * are cycles per unit distance. The number of frequencies sampled * is computed so that the Fourier transform is fast, but the number * of frequency samples is never less than the number of space samples. * Arrays to be transformed may be padded with zeros to obtain the * required frequency sampling. Optional additional padding may be * specified to sample frequency more finely. * <p> * A frequency sampling sk may be centered. Such a centered frequency * sampling always has an odd number of samples, and zero frequency * corresponds to the middle sample in the array of complex transformed * values. The default is not centered, so that zero frequency corresponds * to the first sample, the one with index 0. * <p> * Arrays input to forward transforms may contain either real or * complex values. If complex, values are packed sequentially as * (real, imag) pairs of consecutive numbers. The default input * type is real. * <p> * Signs of the exponents in the complex exponentials used in forward * transforms may be specified. The opposite signs are used for inverse * transforms. The default signs are -1 for forward transforms and 1 for * inverse transforms. */ export declare class Fft { private _fft1r; private _fft1c; private _fft2; private _fft3; private _sx1; private _sx2; private _sx3; private _sk1; private _sk2; private _sk3; private _sign1; private _sign2; private _sign3; private _nfft1; private _nfft2; private _nfft3; private _padding1; private _padding2; private _padding3; private _center1; private _center2; private _center3; private _complex; private _overwrite; static FromData(f: number[] | number[][] | number[][][], complex?: boolean): Fft; /** * Constructs an FFT for the specified 1D array of real values. * <p> * Spatial dimensions are determined from the dimensions of the * specified array. Spatial sampling intervals are 1.0, and first * sample coordinates are 0.0. * @param f an array with dimensions like those to be transformed. * @param complex true, for complex values; false, for real values. */ constructor(f: number[], complex?: boolean); /** * Constructs an FFT for the specified 2D array of real values. * <p> * Spatial dimensions are determined from the dimensions of the * specified array. Spatial sampling intervals are 1.0, and first * sample coordinates are 0.0. * @param f an array with dimensions like those to be transformed. * @param complex true, for complex values; false, for real values. */ constructor(f: number[][], complex?: boolean); /** * Constructs an FFT for the specified 3D array of real values. * <p> * Spatial dimensions are determined from the dimensions of the * specified array. Spatial sampling intervals are 1.0, and first * sample coordinates are 0.0. * @param f an array with dimensions like those to be transformed. * @param complex true, for complex values; false, for real values. */ constructor(f: number[][][], complex?: boolean); /** * Constructs an FFT with specified space sampling. * @param sx1 space sampling for the 1st dimension. */ constructor(sx1: Sampling); /** * Constructs an FFT with specified space sampling. * @param sx1 space sampling for the 1st dimension. * @param sx2 space sampling for the 2nd dimension. */ constructor(sx1: Sampling, sx2: Sampling); /** * Constructs an FFT with specified space sampling. * @param sx1 space sampling for the 1st dimension. * @param sx2 space sampling for the 2nd dimension. * @param sx3 space sampling for the 3rd dimension. */ constructor(sx1: Sampling, sx2: Sampling, sx3: Sampling); /** * Constructs a 1D FFT with specified number of space samples. * <p> * The sampling interval is 1.0 and the first sample coordinate is 0.0. * @param n1 number of samples in the first dimension. * @param complex true, for complex values; false, for real values. */ constructor(n1: number, complex?: boolean); /** * Constructs a 2D FFT with specified number of space samples. * <p> * Sampling intervals are 1.0 and first sample coordinates are 0.0. * @param n1 number of samples in the first dimension. * @param n2 number of samples in the second dimension. * @param complex true, for complex values; false, for real values. */ constructor(n1: number, n2: number, complex?: boolean); /** * Constructs a 3D FFT with specified number of space samples. * <p> * Sampling intervals are 1.0 and first sample coordinates are 0.0. * @param n1 number of samples in the first dimension. * @param n2 number of samples in the second dimension. * @param n3 number of samples in the third dimension. * @param complex true, for complex values; false, for real values. */ constructor(n1: number, n2: number, n3: number, complex?: boolean); /** * Sets the type of input (output) values for forward (inverse) transforms. * <p> * The default type is real. * @param complex true, for complex values; false, for real values. */ set complex(complex: boolean); /** * Sets the ability of this transform to overwrite specified arrays. * <p> * The array specified in an inverse transform is either copied or * overwritten internally by the inverse transform. Copying preserves * the values in the specified array, but wastes memory in the case * when those values are no longer needed. If overwrite is true, then * the inverse transform will be performed in place, so that no copy * is necessary. The default is false. * @param overwrite true, to overwrite; false, to copy. */ set overwrite(overwrite: boolean); /** * Sets the centering of frequency samplings for all dimensions. * <p> * If centered, the number of frequency samples is always odd, * and zero frequency corresponds to the middle sample. The * default center is false, so that zero frequency corresponds to * the sample with index zero in the output transformed array. * @param center true, for centering; false, otherwise. */ set center(center: boolean); /** * Sets the centering of frequency sampling for the 1st dimension. * <p> * If centered, the number of frequency samples is always odd, * and zero frequency corresponds to the middle sample. The * default center is false, so that zero frequency corresponds to * the sample with index zero in the output transformed array. * @param center true, for centering; false, otherwise. */ set center1(center: boolean); /** * Sets the centering of frequency sampling for the 2nd dimension. * <p> * If centered, the number of frequency samples is always odd, * and zero frequency corresponds to the middle sample. The * default center is false, so that zero frequency corresponds to * the sample with index zero in the output transformed array. * @param center true, for centering; false, otherwise. */ set center2(center: boolean); /** * Sets the centering of frequency sampling for the 3rd dimension. * <p> * If centered, the number of frequency samples is always odd, * and zero frequency corresponds to the middle sample. The * default center is false, so that zero frequency corresponds to * the sample with index zero in the output transformed array. * @param center true, for centering; false, otherwise. */ set center3(center: boolean); /** * Sets the minimum padding with zeros for all array dimensions. * <p> * The default minimum is zero. However, some amount of padding * may be required by the FFT. * @param padding the minimum padding. */ set padding(padding: number); /** * Sets the minimum padding with zeros for the 1st array dimension. * <p> * The default minimum is zero. However, some amount of padding may * be required by the FFT. * @param padding the minimum padding. */ set padding1(padding: number); /** * Sets the minimum padding with zeros for the 2nd array dimension. * <p> * The default minimum is zero. However, some amount of padding may * be required by the FFT. * @param padding the minimum padding. */ set padding2(padding: number); /** * Sets the minimum padding with zeros for the 3rd array dimension. * <p> * The default minimum is zero. However, some amount of padding may * be required by the FFT. * @param padding the minimum padding. */ set padding3(padding: number); /** * Gets the frequency sampling for the 1st dimension. * @returns the frequency sampling. */ get frequencySampling1(): Sampling; /** * Gets the frequency sampling for the 2nd dimension. * @returns the frequency sampling. */ get frequencySampling2(): Sampling; /** * Gets the frequency sampling for the 3rd dimension. * @returns the frequency sampling. */ get frequencySampling3(): Sampling; /** * Sets the sign used for forward transforms in all dimensions. * <p> * The opposite sign is used for inverse transforms. * The default sign is -1. * @param sign the sign, -1 or 1. */ set sign(sign: number); /** * Sets the sign used for forward transforms in the 1st dimension. * <p> * The opposite sign is used for inverse transforms. * The default sign is -1. * @param sign the sign, -1 or 1. */ set sign1(sign: number); /** * Sets the sign used for forward transforms in the 2nd dimension. * <p> * The opposite sign is used for inverse transforms. * The default sign is -1. * @param sign the sign, -1 or 1. */ set sign2(sign: number); /** * Sets the sign used for forward transforms in the 3rd dimension. * <p> * The opposite sign is used for inverse transforms. * The default sign is -1. * @param sign the sign, -1 or 1. */ set sign3(sign: number); /** * Applies a forward space-to-frequency transform of a 1D array. * @param f the array to be transformed, a sampled function of space. * @returns the transformed array, a sample function fo frequency. */ applyForward(f: number[]): number[]; /** * Applies a forward space-to-frequency transform of a 2D array. * @param f the array to be transformed, a sampled function of space. * @returns the transformed array, a sample function fo frequency. */ applyForward(f: number[][]): number[][]; /** * Applies a forward space-to-frequency transform of a 3D array. * @param f the array to be transformed, a sampled function of space. * @returns the transformed array, a sample function fo frequency. */ applyForward(f: number[][][]): number[][][]; /** * Applies an inverse frequency-to-space transform of a 1D array. * @param g the array to be transformed, a sampled function of frequency. * @returns the transformed array, a sampled function of space. */ applyInverse(g: number[]): number[]; /** * Applies an inverse frequency-to-space transform of a 2D array. * @param g the array to be transformed, a sampled function of frequency. * @returns the transformed array, a sampled function of space. */ applyInverse(g: number[][]): number[][]; /** * Applies an inverse frequency-to-space transform of a 3D array. * @param g the array to be transformed, a sampled function of frequency. * @returns the transformed array, a sampled function of space. */ applyInverse(g: number[][][]): number[][][]; /** @interal */ private _applyForward2; private _updateSampling1; private _updateSampling2; private _updateSampling3; private _pad1; private _pad2; private _pad3; private _ensureSamplingX1; private _ensureSamplingX2; private _ensureSamplingX3; private _ensureSamplingK1; private _ensureSamplingK2; private _ensureSamplingK3; /** @ignore */ private _center; /** @ignore */ private _doCenter2; /** @ignore */ private _doCenter3; /** @ignore */ private _center3d; }