# Question on PSD calculation in GUI

Can someone please comment on why the Power Spectrum Density code in the GUI is different than other published implementations? In particular why there is a factor of 4 in the default case and no factor for the first and last frequency case?

Here is the code segment in DataProcessing.pde:

```
if (Ibin != 0 && Ibin != Nfft/2) {
psdx = fftBuff[Ichan].getBand(Ibin) *
fftBuff[Ichan].getBand(Ibin) * Nfft/currentBoard.getSampleRate() / 4;
} else {
psdx = fftBuff[Ichan].getBand(Ibin) *
fftBuff[Ichan].getBand(Ibin) * Nfft/currentBoard.getSampleRate();
}
```

All other implementation references I have found use the following code :

```
psdx = (1/(Fs*N)) * abs(xdft).^2;
psdx(2:end-1) = 2*psdx(2:end-1);
```

The code comment in DataProcessing.pde says:

```
// calculate single-sided psd by single-sided FFT amplitude spectrum
// PSD ref: https://www.mathworks.com/help/dsp/ug/estimate-the-power-spectral-density-in-matlab.html
// when i = 1 ~ (N/2-1), psd = (N / fs) * mag(i)^2 / 4
// when i = 0 or i = N/2, psd = (N / fs) * mag(i)^2
```

But that reference does not exist at that link, and I can't find it anywhere why the GUI code is different.

## Comments

https://www.mathworks.com/help/dsp/ug/estimate-the-power-spectrum-in-matlab.html

The above link does not address my question about why the GUI code in DataProcessing.pde is different from other cited implementations, including explanation cited at Mathworks.

@mcneillk This code in DataProcessing.pde is very old, and was likely made by someone who was quite familiar with Matlab. This person then likely ported the solution over to Java.

Thankfully, there are a few methods in BrainFlow used to get PSD, and it's available for all bindings I think. I have thought about adding this to the GUI. Here is the Java example:

https://brainflow.readthedocs.io/en/stable/Examples.html#java-band-power

Ok, that makes sense. I was aiming to determine how accurate the band power values are as implemented in the old code because I currently use them (via UDP) to drive downstream calculations for experimental neurofeedback application. I would prefer not to re-implement this element with the Java BrainFlow bindings if the values implemented in DataProcessing are good enough.

I think the BrainFlow PSD could be more accurate and have some configuration options. Would you like to see this as a new feature in the GUI, if it turns out to perform same or better?

Yes, that would absolutely be a valuable upgrade to the GUI since power values are so widely used. Also, I had the thought of proposing to re-implement the old "focus" widget in the GUI using a common EEG feedback approach, which centers around power values of the SMR, or low-beta band. A cleaner band power implementation would support that task. I first prototyped my EEG project in Processing, and I would be happy to contribute back if there is interest.