marytts.util.math

## Class FFT

• ```public class FFT
extends Object```
Author:
Marc Schröder
• ### Constructor Summary

Constructors
Constructor and Description
`FFT()`
• ### Method Summary

Methods
Modifier and Type Method and Description
`static double[]` `autoCorrelate(double[] signal)`
Compute the autocorrelation of a signal, by inverse transformation of its power spectrum.
`static double[]` `autoCorrelateWithZeroPadding(double[] signal)`
Compute the autocorrelation of a signal, by inverse transformation of its power spectrum.
`static double[]` `computeAmplitudeSpectrum_FD(double[] fft)`
From the result of the FFT (in the frequency domain), compute the absolute value for each positive frequency, i.e.
`static double[]` `computeAmplitudeSpectrum(double[] signal)`
Convenience method for computing the absolute amplitude spectrum of a real signal.
`static double[]` `computeLogAmplitudeSpectrum_FD(double[] fft)`
From the result of the FFT (in the frequency domain), compute the log amplitude for each positive frequency.
`static double[]` `computeLogAmplitudeSpectrum(double[] signal)`
Convenience method for computing the log amplitude spectrum of a real signal.
`static double[]` `computeLogPowerSpectrum_FD(double[] fft)`
From the result of the FFT, compute the log (dB) power for each positive frequency.
`static double[]` `computeLogPowerSpectrum(double[] signal)`
Convenience method for computing the log (dB) power spectrum of a real signal.
`static double[]` `computePhaseSpectrum_FD(double[] fft)`
From the result of the FFT (in the frequency domain), compute the phase spectrum for each positive frequency.
`static double[]` `computePowerSpectrum_FD(double[] fft)`
From the result of the FFT (in the frequency domain), compute the power for each positive frequency.
`static double[]` `computePowerSpectrum(double[] signal)`
Convenience method for computing the absolute power spectrum of a real signal.
`static double[]` ```convolve_FD(double[] signal1, double[] fft2)```
Compute the convolution of two signals, by multiplying them in the frequency domain.
`static double[]` ```convolve_FD(double[] signal1, double[] fft2, double deltaT)```
Compute the convolution of two signals, by multiplying them in the frequency domain.
`static double[]` ```convolve(double[] signal1, double[] signal2)```
Compute the convolution of two signals, by multiplying them in the frequency domain.
`static double[]` ```convolve(double[] signal1, double[] signal2, double deltaT)```
Compute the convolution of two signals, by multiplying them in the frequency domain.
`static double[]` ```convolveWithZeroPadding(double[] signal1, double[] signal2)```
Compute the convolution of two signals, by multipying them in the frequency domain.
`static double[]` ```convolveWithZeroPadding(double[] signal1, double[] signal2, double deltaT)```
Compute the convolution of two signals, by multipying them in the frequency domain.
`static double[]` ```correlate(double[] signal1, double[] signal2)```
Compute the correlation of two signals, by multiplying the transform of signal2 with the conjugate complex of the transform of signal1, in the frequency domain.
`static double[]` ```correlateWithZeroPadding(double[] signal1, double[] signal2)```
Compute the correlation of two signals, by multipying them in the frequency domain.
`static void` `main(String[] args)`
`static void` ```realTransform(double[] data, boolean inverse)```
Calculates the Fourier transform of a set of n real-valued data points.
`static void` ```transform(double[] realAndImag, boolean inverse)```
Carry out the FFT or inverse FFT, and return the result in the same arrays given as parameters.
`static void` ```transform(double[] real, double[] imag, boolean inverse)```
Carry out the FFT or inverse FFT, and return the result in the same arrays given as parameters.
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Constructor Detail

• #### FFT

`public FFT()`
• ### Method Detail

• #### computeLogPowerSpectrum

`public static double[] computeLogPowerSpectrum(double[] signal)`
Convenience method for computing the log (dB) power spectrum of a real signal. The signal can be of any length; internally, zeroes will be added if signal length is not a power of two.
Parameters:
`signal` - the real signal for which to compute the power spectrum.
Returns:
the power spectrum, as an array of length N/2 (where N is the power of two greater than or equal to signal.length): the log of the squared absolute values of the lower half of the complex fourier transform array.
• #### computeLogPowerSpectrum_FD

`public static double[] computeLogPowerSpectrum_FD(double[] fft)`
From the result of the FFT, compute the log (dB) power for each positive frequency.
Parameters:
`fft` - the array of real and imag parts of the complex number array, fft[0] = real[0], fft[1] = real[N/2], fft[2*i] = real[i], fft[2*i+1] = imag[i] for 1≤i<N/2
Returns:
an array of length real.length/2 containing numbers representing the log of the square of the absolute value of each complex number, p[i] = real[i]*real[i] + imag[i]*imag[i]
• #### computePowerSpectrum

`public static double[] computePowerSpectrum(double[] signal)`
Convenience method for computing the absolute power spectrum of a real signal. The signal can be of any length; internally, zeroes will be added if signal length is not a power of two.
Parameters:
`signal` - the real signal for which to compute the power spectrum.
Returns:
the power spectrum, as an array of length N/2 (where N is the power of two greater than or equal to signal.length): the squared absolute values of the lower half of the complex fourier transform array.
• #### computePowerSpectrum_FD

`public static double[] computePowerSpectrum_FD(double[] fft)`
From the result of the FFT (in the frequency domain), compute the power for each positive frequency.
Parameters:
`fft` - the array of real and imag parts of the complex number array, fft[0] = real[0], fft[1] = real[N/2], fft[2*i] = real[i], fft[2*i+1] = imag[i] for 1≤i<N/2
Returns:
an array of length real.length/2 containing numbers representing the square of the absolute value of each complex number, p[i] = real[i]*real[i] + imag[i]*imag[i]
• #### computeLogAmplitudeSpectrum

`public static double[] computeLogAmplitudeSpectrum(double[] signal)`
Convenience method for computing the log amplitude spectrum of a real signal. The signal can be of any length; internally, zeroes will be added if signal length is not a power of two.
Parameters:
`signal` - the real signal for which to compute the power spectrum.
Returns:
the log amplitude spectrum, as an array of length N/2 (where N is the power of two greater than or equal to signal.length): the log of the absolute values of the lower half of the complex fourier transform array.
• #### computeLogAmplitudeSpectrum_FD

`public static double[] computeLogAmplitudeSpectrum_FD(double[] fft)`
From the result of the FFT (in the frequency domain), compute the log amplitude for each positive frequency.
Parameters:
`fft` - the array of real and imag parts of the complex number array, fft[0] = real[0], fft[1] = real[N/2], fft[2*i] = real[i], fft[2*i+1] = imag[i] for 1≤i<N/2
Returns:
an array of length real.length/2 containing numbers representing the log of the square of the absolute value of each complex number, p[i] = real[i]*real[i] + imag[i]*imag[i]
• #### computeAmplitudeSpectrum

`public static double[] computeAmplitudeSpectrum(double[] signal)`
Convenience method for computing the absolute amplitude spectrum of a real signal. The signal can be of any length; internally, zeroes will be added if signal length is not a power of two.
Parameters:
`signal` - the real signal for which to compute the power spectrum.
Returns:
the power spectrum, as an array of length N/2 (where N is the power of two greater than or equal to signal.length): the absolute values of the lower half of the complex fourier transform array.
• #### computeAmplitudeSpectrum_FD

`public static double[] computeAmplitudeSpectrum_FD(double[] fft)`
From the result of the FFT (in the frequency domain), compute the absolute value for each positive frequency, i.e. the norm of each complex number in the lower half of the array
Parameters:
`fft` - the array of real and imag parts of the complex number array, fft[0] = real[0], fft[1] = real[N/2], fft[2*i] = real[i], fft[2*i+1] = imag[i] for 1≤i<N/2
Returns:
an array of length real.length/2 containing numbers representing the absolute value of each complex number, r[i] = sqrt(real[i]*real[i] + imag[i]*imag[i])
• #### computePhaseSpectrum_FD

`public static double[] computePhaseSpectrum_FD(double[] fft)`
From the result of the FFT (in the frequency domain), compute the phase spectrum for each positive frequency.
Parameters:
`fft` - the array of real and imag parts of the complex number array, fft[0] = real[0], fft[1] = real[N/2], fft[2*i] = real[i], fft[2*i+1] = imag[i] for 1≤i<N/2
Returns:
an array of length real.length/2 containing numbers representing the phases of each complex number, phase[i] = atan(imag[i], real[i])
• #### transform

```public static void transform(double[] real,
double[] imag,
boolean inverse)```
Carry out the FFT or inverse FFT, and return the result in the same arrays given as parameters. In the case of the "forward" FFT, real is the signal to transform, and imag is an empty array. After the call, real will hold the real part of the complex frequency array, and imag will hold the imaginary part. They are ordered such that first come positive frequencies from 0 to fmax, then the negative frequencies from -fmax to 0 (which are the mirror image of the positive frequencies). In the case of the inverse FFT, real and imag are in input the real and imaginary part of the complex frequencies, and in output, real is the signal. The method already computes the division by array length required for the inverse transform.
Parameters:
`real` - in "forward" FFT: as input=the time-domain signal to transform, as output=the real part of the complex frequencies; in inverse FFT: as input=the real part of the complex frequencies, as output= the time-domain signal.
`imag` - in "forward" FFT: as input=an empty array, as output=the imaginary part of the complex frequencies; in inverse FFT: as input=the imaginary part of the complex frequencies, as output= not used.
`inverse` - whether to calculate the FFT or the inverse FFT.
• #### transform

```public static void transform(double[] realAndImag,
boolean inverse)```
Carry out the FFT or inverse FFT, and return the result in the same arrays given as parameters. This works exactly like #transform(real, imag, boolean), but data is represented differently: the even indices of the input array hold the real part, the odd indices the imag part of each complex number.
Parameters:
`realAndImag` - the array of complex numbers to transform
`inverse` - whether to calculate the FFT or the inverse FFT.
• #### realTransform

```public static void realTransform(double[] data,
boolean inverse)```
Calculates the Fourier transform of a set of n real-valued data points. Replaces this data (which is stored in array data[1..n]) by the positive frequency half of its complex Fourier transform. The real-valued first and last components of the complex transform are returned as elements data[1] and data[2], respectively. n must be a power of 2. This routine also calculates the inverse transform of a complex data array if it is the transform of real data. (Result in this case must be multiplied by 2/n.)
Parameters:
`data` - data
`inverse` - inverse

```public static double[] convolveWithZeroPadding(double[] signal1,
double[] signal2,
double deltaT)```
Compute the convolution of two signals, by multipying them in the frequency domain. Normalise the result with respect to deltaT (the inverse of the sampling rate). This method applies zero padding where necessary to ensure that the result is not polluted because of assumed periodicity. The two signals need not be of equal length.
Parameters:
`signal1` - signal 1
`signal2` - signal 2
`deltaT` - , the time difference between two samples (= 1/samplingrate)
Returns:
the convolved signal, with length signal1.length+signal2.length

```public static double[] convolveWithZeroPadding(double[] signal1,
double[] signal2)```
Compute the convolution of two signals, by multipying them in the frequency domain. This method applies zero padding where necessary to ensure that the result is not polluted because of assumed periodicity. The two signals need not be of equal length.
Parameters:
`signal1` - signal 1
`signal2` - signal 2
Returns:
the convolved signal, with length signal1.length+signal2.length
• #### convolve

```public static double[] convolve(double[] signal1,
double[] signal2,
double deltaT)```
Compute the convolution of two signals, by multiplying them in the frequency domain. Normalise the result with respect to deltaT (the inverse of the sampling rate). This is the core method, requiring two signals of equal length, which must be a power of two, and not checking for pollution arising from the assumed periodicity of both signals.
Parameters:
`signal1` - signal 1
`signal2` - signal 2
`deltaT` - , the time difference between two samples (= 1/samplingrate)
Returns:
the convolved signal, of the same length as the two input signals
Throws:
`IllegalArgumentException` - if the two input signals do not have the same length.
• #### convolve

```public static double[] convolve(double[] signal1,
double[] signal2)```
Compute the convolution of two signals, by multiplying them in the frequency domain. This is the core method, requiring two signals of equal length, which must be a power of two, and not checking for pollution arising from the assumed periodicity of both signals.
Parameters:
`signal1` - signal 1
`signal2` - signal 2
Returns:
the convolved signal, of the same length as the two input signals
Throws:
`IllegalArgumentException` - if the two input signals do not have the same length.
• #### convolve_FD

```public static double[] convolve_FD(double[] signal1,
double[] fft2,
double deltaT)```
Compute the convolution of two signals, by multiplying them in the frequency domain. Normalise the result with respect to deltaT (the inverse of the sampling rate). This is a specialised version of the core method, requiring two signals of equal length, which must be a power of two, and not checking for pollution arising from the assumed periodicity of both signals. In this version, the first signal is provided in the time domain, while the second is already transformed into the frequency domain.
Parameters:
`signal1` - the first input signal, in the time domain
`fft2` - the complex transform of the second signal, in the frequency domain fft[0] = real[0], fft[1] = real[N/2], fft[2*i] = real[i], fft[2*i+1] = imag[i] for 1≤i<N/2
`deltaT` - , the time difference between two samples (= 1/samplingrate)
Returns:
the convolved signal, of the same length as the two input signals
Throws:
`IllegalArgumentException` - if the two input signals do not have the same length.
• #### convolve_FD

```public static double[] convolve_FD(double[] signal1,
double[] fft2)```
Compute the convolution of two signals, by multiplying them in the frequency domain. This is a specialised version of the core method, requiring two signals of equal length, which must be a power of two, and not checking for pollution arising from the assumed periodicity of both signals. In this version, the first signal is provided in the time domain, while the second is already transformed into the frequency domain.
Parameters:
`signal1` - the first input signal, in the time domain
`fft2` - the complex transform of the second signal, in the frequency domain fft[0] = real[0], fft[1] = real[N/2], fft[2*i] = real[i], fft[2*i+1] = imag[i] for 1≤i<N/2
Returns:
the convolved signal, of the same length as the two input signals
Throws:
`IllegalArgumentException` - if the two input signals do not have the same length.

```public static double[] correlateWithZeroPadding(double[] signal1,
double[] signal2)```
Compute the correlation of two signals, by multipying them in the frequency domain. This method applies zero padding where necessary to ensure that the result is not polluted because of assumed periodicity. The two signals need not be of equal length.
Parameters:
`signal1` - signal 1
`signal2` - signal 2
Returns:
the correlation function, with length signal1.length+signal2.length
• #### correlate

```public static double[] correlate(double[] signal1,
double[] signal2)```
Compute the correlation of two signals, by multiplying the transform of signal2 with the conjugate complex of the transform of signal1, in the frequency domain. Sign convention: If signal2 is shifted by n to the right of signal2, then the correlation function will have a peak at positive n. This is the core method, requiring two signals of equal length, which must be a power of two, and not checking for pollution arising from the assumed periodicity of both signals.
Parameters:
`signal1` - signal 1
`signal2` - signal 2
Returns:
the correlated signal, of the same length as the two input signals
Throws:
`IllegalArgumentException` - if the two input signals do not have the same length.
• #### autoCorrelate

`public static double[] autoCorrelate(double[] signal)`
Compute the autocorrelation of a signal, by inverse transformation of its power spectrum. This is the core method, requiring a signal whose length must be a power of two, and not checking for pollution arising from the assumed periodicity of the signal.
Parameters:
`signal` - signal
Returns:
the correlated signal, of the same length as the input signal

`public static double[] autoCorrelateWithZeroPadding(double[] signal)`
Compute the autocorrelation of a signal, by inverse transformation of its power spectrum. This method applies zero padding where necessary to ensure that the result is not polluted because of assumed periodicity.
Parameters:
`signal` - signal
Returns:
the correlated signal, of the same length as the input signal
• #### main

```public static void main(String[] args)
throws Exception```
Throws:
`Exception`