Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fixes #20] Implement Inverse Short Time Fourier Transform #28

Merged
merged 11 commits into from
Nov 7, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public double[] computeCoefficients(double[] cutoff, double[] gain) {
// Perform the Inverse DFT
InverseDiscreteFourier transform = new InverseDiscreteFourier(fx2D, true);
transform.idft();
double[] outFull = transform.getRealSignal();
double[] outFull = transform.getReal();

Hamming w = new Hamming(this.numTaps);
double[] window = w.getWindow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private void resample_fft() {

DiscreteFourier df = new DiscreteFourier(this.signal);
df.dft();
double[][] X = df.getFull(true);
double[][] X = df.getComplex2D(true);
double[][] Y = new double[this.num/2+1][2];

int N = Math.min(this.num, Nx);
Expand All @@ -195,7 +195,7 @@ private void resample_fft() {

InverseDiscreteFourier idf = new InverseDiscreteFourier(Y, true);
idf.idft();
double[] y = idf.getRealSignal();
double[] y = idf.getReal();
this.output = MathArrays.scale((float)this.num/(float)Nx, y);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ public double[][] getMagPhaseDeg(boolean onlyPositive) throws ExceptionInInitial
}

/**
* Returns the complex value of the discrete fourier transformed sequence
* Returns the complex value of the discrete fourier transformed sequence as a 2D matrix
* @param onlyPositive Set to True if non-mirrored output is required
* @throws java.lang.ExceptionInInitializerError if called before executing dft() method
* @return double[][] The complex DFT output; first array column = real part; second array column = imaginary part
*/
public double[][] getFull(boolean onlyPositive) throws ExceptionInInitializerError {
public double[][] getComplex2D(boolean onlyPositive) throws ExceptionInInitializerError {
Complex[] dftout = getComplex(onlyPositive);
return UtilMethods.complexTo2D(dftout);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private void fillH() {
public void hilbertTransform() {
DiscreteFourier dft = new DiscreteFourier(this.signal);
dft.dft();
double[][] dftOut = dft.getFull(false);
double[][] dftOut = dft.getComplex2D(false);

double[][] modOut = new double[dftOut.length][dftOut[0].length];

Expand All @@ -76,7 +76,7 @@ public void hilbertTransform() {

InverseDiscreteFourier idft = new InverseDiscreteFourier(modOut, false);
idft.idft();
this.output = idft.getAsComplex();
this.output = idft.getComplex();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@

import org.apache.commons.math3.complex.Complex;

import java.util.Arrays;

/**
* <h1>Inverse Discrete Fourier Transform</h1>
* The InverseDiscreteFourier class applies the inverse discrete fourier transform on the input sequence (real/complex) and
* provides different representations of the reconstructed signal to be returned (real signal, complex signal, absolute signal)
* provides different representations of the reconstructed signal to be returned (real signal, complex signal, ...)
* <p>
*
* @author Sambit Paul
Expand Down Expand Up @@ -76,15 +78,38 @@ public void idft() {
}
}

/**
* This method returns the complex value of the generated signal as a Complex array.
* @throws java.lang.ExceptionInInitializerError if called before executing idft() method
* @return Complex[] The signal (complex)
*/
protected Complex[] getComplex() throws ExceptionInInitializerError {
checkOutput();
return this.signal;
}

/**
* This method returns the complex value of the generated signal as a 2D matrix.
* @throws java.lang.ExceptionInInitializerError if called before executing idft() method
* @return double[][] The signal (complex)
*/
public double[][] getComplex2D() throws ExceptionInInitializerError {
checkOutput();
double[][] ret = new double[this.signal.length][2];
for (int i=0; i<ret.length; i++) {
ret[i][0] = this.signal[i].getReal();
ret[i][1] = this.signal[i].getImaginary();
}
return ret;
}

/**
* This method returns the real part of the generated signal.
* @throws java.lang.ExceptionInInitializerError if called before executing idft() method
* @return double[] The signal (real part)
*/
public double[] getRealSignal() throws ExceptionInInitializerError {
if (this.signal == null) {
throw new ExceptionInInitializerError("Execute idft() function before returning result");
}
public double[] getReal() throws ExceptionInInitializerError {
checkOutput();
double[] ret = new double[this.signal.length];
for (int i=0; i<ret.length; i++) {
ret[i] = this.signal[i].getReal();
Expand All @@ -93,52 +118,47 @@ public double[] getRealSignal() throws ExceptionInInitializerError {
}

/**
* This method returns the absolute value of the generated signal.
* This method returns the imaginary part of the generated signal.
* @throws java.lang.ExceptionInInitializerError if called before executing idft() method
* @return double[] The signal (absolute)
* @return double[] The signal (imaginary part)
*/
public double[] getAbsoluteSignal() throws ExceptionInInitializerError {
if (this.signal == null) {
throw new ExceptionInInitializerError("Execute idft() function before returning result");
}
public double[] getImaginary() throws ExceptionInInitializerError {
checkOutput();
double[] ret = new double[this.signal.length];
for (int i=0; i<ret.length; i++) {
ret[i] = this.signal[i].abs();
ret[i] = this.signal[i].getImaginary();
}
return ret;
}

/**
* This method returns the complex value of the generated signal as a 2D matrix.
* This method returns the magnitude value of the IDFT result.
* @throws java.lang.ExceptionInInitializerError if called before executing idft() method
* @return double[][] The signal (complex)
* @return double[] The signal (magnitude)
*/
public double[][] getComplexSignal() throws ExceptionInInitializerError {
if (this.signal == null) {
throw new ExceptionInInitializerError("Execute idft() function before returning result");
}
double[][] ret = new double[this.signal.length][2];
public double[] getMagnitude() throws ExceptionInInitializerError {
checkOutput();
double[] ret = new double[this.signal.length];
for (int i=0; i<ret.length; i++) {
ret[i][0] = this.signal[i].getReal();
ret[i][1] = this.signal[i].getImaginary();
ret[i] = this.signal[i].abs();
}
return ret;
}

/**
* This method returns the complex value of the generated signal as a Complex array.
* This method returns the phase value of the IDFT result.
* @throws java.lang.ExceptionInInitializerError if called before executing idft() method
* @return double[] The signal (complex)
* @return double[] phase of the signal
*/
protected Complex[] getAsComplex() throws ExceptionInInitializerError
{
if (this.signal == null) {
throw new ExceptionInInitializerError("Execute idft() function before returning result");
public double[] getPhase() throws ExceptionInInitializerError {
checkOutput();
double[] ret = new double[this.signal.length];
for (int i=0; i<ret.length; i++) {
ret[i] = this.signal[i].getArgument();
}
return this.signal;
return ret;
}


private void idftRealFull() {
Complex[] out = new Complex[this.real_sequence.length];

Expand Down Expand Up @@ -228,4 +248,10 @@ private void idftComplexMirror() {
}
this.signal = out;
}

private void checkOutput() {
if (this.signal == null) {
throw new ExceptionInInitializerError("Execute idft() function before returning result");
}
}
}
Loading