Code Notes:
N = [64 128 256 512];
NumOfRuns = 50;
PerLength = 2048;
% freq = 0:2/(PerLength-1):2; % you are not using freq
% You are looping from 1:4, defining Px each time, not storing Px outside of the loop.
% After this loop, Px will just be in its 4th state.
for m = 1:length(N)
Px = zeros(NumOfRuns,PerLength);
for i = 1:NumOfRuns
x = randn(1,N(m));
Px(:,i) = periodogram(x);
end
end
Periodogram:
You're assuming periodogram returns the same number of elements as are input to it. A simple test will tell you that is not true!
a = periodogram([1, 2]);
numel(a)
% >> ans = 129
Read the documentation here:
https://uk.mathworks.com/help/signal/ref/periodogram.html
The number of points, nfft, in the discrete Fourier transform (DFT) is the maximum of 256 or the next power of two greater than the signal length
So above, the 129 is related to 2^7 = 128
Try using pxx = periodogram(x,window,nfft)