# Results do not match between manually computed PSD values and those from MATLAB pwelch function

The power spectral density (PSD) of the given input signal $$x[n]$$ is computed as follows:

close all;clear;clc

n = 8;
F = dftmtx(n);
invF = 1/n*F';

rng default
x = randn(100*n,1);

xx = buffer(x,n,n-1);
xx = xx(:, n:end);

XX = fft(xx, n);

phixx = mean(XX.*conj(XX),2)/n;
phixx = phixx.';


Then, the phixx is

phixx =
1.0837    1.1155    0.8099    0.9498    1.0470    0.9498    0.8099    1.1155


If the MATLAB built-in function pwelch is used to compute the PSD, then the results are quite different from what is shown above:

pp = pwelch(x, ones(n,1), n-1, n)'

0.1725    0.3551    0.2578    0.3023    0.1666


As it is one side, so the length is only n/2+1, but I expected the values to be identical to each other.

Was the way how pwelch was used wrong?

Please let me know if there should be more detailed information.

update

I just found out that the results are identical except for a $$\pi$$ or $$2\pi$$ difference between them, i.e.,

(phixx(1:n/2+1)./pp)/pi

ans =

2.0000    1.0000    1.0000    1.0000    2.0000


There seems a constant involved in computing pwelch.

Then, I am wondering which one should be used.

1. pwelch() can return a one sided (default) or two sided spectrum. For one sided, the energy of negative frequency is added to the symmetric positive ones.
2. pwelch() returns a spectral density in something like $$W/Hz$$ which is actually dependent on the sample rate. By default pwelch() assumes a sample rate of $$2\pi$$.
pp = pwelch(x, ones(n,1), n-1, n,1,'twosided')';

pwelch() is fairly complicated so it's typically not a bad idea to read through the entire documentation and understand all the different options before using it.
• Thanks for the answer! Indeed, the 'twosided' variable works and now it seems that the results coincide. This time could be a good chance to read through the entire doc pwelch. But in regard to the first bullet point in your answer, if the energy of negative frequency is added to the symmetric positive ones, then I expect the values of (phixx(1:n/2+1)./pp)/pi should be somehow 1.0 2.0 2.0 2.0 2.0 1.0. But the actual values are the other way around. Jul 23 at 20:19
• Yes if you use "onsided", no if you use "twosided". Note that twosided returns and 8 element vector and onesided a 5-element one. You want to maintain Perceval's theorem $\sum P(k) = \sum x[n]^2$ Jul 23 at 21:17