C++ - Complex Value mistake, computing Cross Spectral Density (CSD)

159 Views Asked by At

Dear community,

I am facing a rather annoying problem. I am calculating the Cross Spectral Density (CSD) between two time signals, which were already proccessed with FFT to two complex frequency vectors(Singal1 =>freqvec, Signal2 => freqvec2).

RowVectorXcd CSD(n_Epochs, fftsize);
for(int j = 0; j < fftsize; j++) {
    std::complex<double> cospectrum = freqvec(j).real() *  freqvec2(j).real() +  freqvec(j).imag() * freqvec2(j).imag() ;
    std::complex<double> quadspectrum = freqvec(j).real() * freqvec2(j).imag() - freqvec(j).imag() * freqvec2(j).real() ;
    std::cout << "cospectrum:"<<cospectrum<< std::endl;
    CSD(j) = sqrt( pow( cospectrum, 2 ) + pow( quadspectrum, 2) ) ;

For further computations I need to get the imaginary part of this calculation correctly.

The calculation does work, but somehow the result always has an imaginary value of zero.

0

There are 0 best solutions below