I am using an FFT function in PHP language (https://gist.github.com/mbijon/1332348) which works very well, but cant produce PSD from its output:

$fft = Fourier($data,1);
// $fft[0] is real portion of 1
// $fft[1] is imaginary portion of 1
// $fft[2] is real portion of 2
// $fft[3] is imaginary portion of 2
// ...
// $fft[n-1] is real portion of n
// $fft[n] is imaginary portion of n

PSD formula In Matlab:

PSD = Y.*conj(Y)/N

I cant understand this formula in math notation form...

How can I adapt this formula to work with that output?

  • $\begingroup$ wow, signal processing in PHP. Not really the language I'd recommend for that, and you can see here why: It lacks the proper data types or representation tricks to make this easy. Anyway, this isn't hard: you know what conj does, right? (if not: research that.) $\endgroup$ – Marcus Müller Aug 6 '20 at 18:09
  • $\begingroup$ (also, that FFT code is... well, it's terribly slow, probably. Any reasonable implementation would just call the FFTw's C interface instead of implementing a FFT in a scripting language that isn't optimized for numerical operations.) $\endgroup$ – Marcus Müller Aug 6 '20 at 18:11
  • $\begingroup$ The job has to be done in PHP... I checked mathworks.com/help/matlab/ref/conj.html but I couldnt understand how to implement it... $\endgroup$ – Digerkam Aug 6 '20 at 18:13
  • $\begingroup$ let me rephrase this: do you know what complex conjugation is? And the job is still done in PHP if you include a library. (also, PHP is really among the worst languages to write signal processing software in. Whoever defined that this has to be done in PHP should probably think really hard about why they're defining that.) $\endgroup$ – Marcus Müller Aug 6 '20 at 18:13
  • $\begingroup$ No... I follow the steps at youtu.be/Ktkm5KCryPw?t=386 $\endgroup$ – Digerkam Aug 6 '20 at 18:15

Indeed, PHP is not a weapon of choice to do signal processing. But if you absolutely need to "adapt" the matlab formula, here is an one-liner to "conjugate" the data, based on commented lines from the code of your question (// $fft[0] is real portion of 1, etc.):

// PHP's $conjdata[·] stands for matlab's conj[Y] 
for ($i = 0, $cnt = count($data), $ri = 1; $i < $cnt; $i++, $ri = -$ri) $conjdata[$i] = $data[$i]*$ri;

Also, the PSD formula is somewhat trickier than just Y*conj(Y)/N. See, for example, https://stackoverflow.com/questions/3949324/calculate-autocorrelation-using-fft-in-matlab, the second answer.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.