# Produce PSD from a FFT output

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?

• 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.) – Marcus Müller Aug 6 '20 at 18:09
• (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.) – Marcus Müller Aug 6 '20 at 18:11
• 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... – Digerkam Aug 6 '20 at 18:13
• 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.) – Marcus Müller Aug 6 '20 at 18:13
• No... I follow the steps at youtu.be/Ktkm5KCryPw?t=386 – 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.