public class Dft extends Object
This class contains a collection of DFT-related methods. The current version is not yet a complete implementation of all the classic DFT functions and focuses on the conversion between a real signal and a complex spectrum.
See this Stackexchange entry for a discussion of magnitude normalization.
| Constructor and Description |
|---|
Dft() |
| Modifier and Type | Method and Description |
|---|---|
static Complex[] |
directDft(double[] x)
Computes the DFT on an array of real numbers and returns the complex result.
|
static Complex |
directDftSingle(double[] x,
int pos,
int len,
int relativeFrequency,
boolean normalize)
Computes the DFT on real numbers for a single frequency.
|
static Complex[] |
directDftSpectrum(double[] x)
Computes the DFT on an array of real numbers and returns the complex spectrum.
|
static Complex[] |
goertzel(double[] x)
Computes the DFT on an array of real numbers and returns the complex result.
|
static Complex |
goertzelSingle(double[] x,
int pos,
int len,
int relativeFrequency,
boolean normalize)
Computes the DFT on real numbers for a single frequency.
|
static Complex[] |
goertzelSpectrum(double[] x)
Computes the DFT on an array of real numbers and returns the complex spectrum.
|
static double[] |
synthesizeFromSpectrum(Complex[] x,
boolean odd)
Computes a kind of inverse DFT on an array of complex numbers that represent a spectrum,
and returns the result as an array of real numbers.
|
public static Complex directDftSingle(double[] x, int pos, int len, int relativeFrequency, boolean normalize)
This is a reference implementation without any optimization. It's simple to understand, but slow.
x - The input values (samples).pos - The index of the first value in x to be processed.len - The number of values in x to be processed, starting at pos.relativeFrequency - A frequency relative to len.
It represents the number of sinusoidal oscillations within len
and is normally within the range 0 (for DC) to len / 2.
The absolute frequency is relativeFrequency * samplingRate / len.normalize - true to normalize the magnitude of the result,
so that it represents the amplitude of the sinusoidal frequency component.public static Complex goertzelSingle(double[] x, int pos, int len, int relativeFrequency, boolean normalize)
This version uses the Goertzel algorithm
to compute the DFT.
It produces the same results as directDft(double[]), but it's much faster.
x - The input values (samples).pos - The index of the first value in x to be processed.len - The number of values in x to be processed, starting at pos.relativeFrequency - A frequency relative to len.
It represents the number of sinusoidal oscillations within len
and is normally within the range 0 (for DC) to len / 2.
The absolute frequency is relativeFrequency * samplingRate / len.normalize - true to normalize the magnitude of the result,
so that it represents the amplitude of the sinusoidal frequency component.public static Complex[] directDft(double[] x)
This method calls directDftSingle(double[], int, int, int, boolean) with normalize = false.
x - The input values (samples).public static Complex[] goertzel(double[] x)
This method calls goertzelSingle(double[], int, int, int, boolean) with normalize = false.
x - The input values (samples).public static Complex[] directDftSpectrum(double[] x)
This method calls directDftSingle(double[], int, int, int, boolean) with normalize = true
for the frequencies from 0 to x.length / 2.
See synthesizeFromSpectrum(biz.source_code.dsp.math.Complex[], boolean) for the inverse function.
x - The input values (samples).public static Complex[] goertzelSpectrum(double[] x)
This method calls goertzelSingle(double[], int, int, int, boolean) with normalize = true
for the frequencies from 0 to x.length / 2.
See synthesizeFromSpectrum(biz.source_code.dsp.math.Complex[], boolean) for the inverse function.
x - The input values (samples).public static double[] synthesizeFromSpectrum(Complex[] x, boolean odd)
This is a reference implementation without any optimization. It's simple to understand, but slow.
This is the inverse function for directDftSpectrum(double[]) and goertzelSpectrum(double[]).
x - Complex numbers that define the amplitudes and phases of the sinusoidal frequency components.odd - If odd is true, 2 * x.length - 1 output values are generated.
Otherwise 2 * x.length - 2 output values are generated.