0

Im trying to get the spectral energy of a signal in java. I know by theory that the energy is the sum of absolute value square of the coefficient.

What I've done so far is to transform my input data by FFT using JTransforms. The result is array of complex numbers composed of real and imaginary part.

My question ist how the get the spectral energy if you only have the real and imaginary part of signal? Or is there another solution how to get the spectral energy on the basis of my java code?

What I have done is the following: fcoeff += (Math.pow(fft[i],2.0) + Math.pow(fft[i+1],2.0));

I took the assumption that the foruier coefficient is equal to real part + imaginary part.

I guess this isnt that right, is it? If it isnt right what I need to do to get the spectral energy in my java code??

    double energy = 0;
    double fcoeff = 0;

    //example real input data
    double[] input = new double[]{0.0176,-0.0620, 0.2467,0.4599,-0.0582,0.4694,0.0001,-0.2873};

    DoubleFFT_1D fftDo = new DoubleFFT_1D(input.length);

    double[] fft = new double[input.length * 2];

    System.arraycopy(input, 0, fft, 0, input.length);
    fftDo.realForwardFull(fft);

    for(int i = 0; i < fft.length;i=i+2) {

        //get the real part
        System.out.println("real part: " + fft[i]); 

        //get the imaginary part
        System.out.println("imaginary part : " +  fft[i+1]);


        /*calcluating the sum of the squared fourier coefficient by calculating the sum of the squared magnitude of a complex FFT's results*/
       fcoeff += (Math.pow(fft[i],2.0) + Math.pow(fft[i+1],2.0));

    }

    //normalised by number of samples
    energy = fcoeff/input.length;

    System.out.println();
    System.out.println("Spectral energy : " + energy); // Energy : 0.58278756
}

}

I know my input data size is very low, but it only serves to exemplify my problem.

Thanks in advance!!!

Best regards ben

  • 1
    Do you have any test data to suggest that your results are incorrect? If you use MATLAB, you can run this test, get the results and compare them with Java application output? – ha9u63a7 Jan 27 '15 at 23:53
  • That is a good point. Actually I dont use MATLAB but I can arranged it. Do you think the way I calculated the spectral energy only using the real and imaginary parts is theoretical correct???thx – Benaissa Tahiti Jan 27 '15 at 23:59
  • This feels more like a math question... –  Jan 28 '15 at 00:00
  • possible duplicate of [How to calcuate the energy spectrum of a signal?](http://stackoverflow.com/questions/28180857/how-to-calcuate-the-energy-spectrum-of-a-signal) –  Jan 28 '15 at 00:02
  • Yes.... you should read this and see if your solution does the same - http://stackoverflow.com/questions/24696122/calculating-the-power-spectral-density – ha9u63a7 Jan 28 '15 at 00:02

0 Answers0