% Listing 11.2 of the book for Figure 11.4, 11.5 and 11.6
% "Principles of System Identification: Theory and Practice"

% Arun K. Tangirala
% April 02, 2015
% Freely distributed under no warranty

%% Data parameters
Nsamp = 2000; Nreal = 1000;
vk_psd = zeros(Nsamp/2,1);

%% Compute PSD for all realizations
for i = 1:Nreal,
    vki = randn(Nsamp,1);
    vkif = fft(vki);
    vki_psd = abs(vkif(1:end/2)).^2/Nsamp;
    vk_psd = vk_psd + vki_psd;
end
vk_psd = vk_psd/Nreal;

%% Plot the PSD
fvec = (0:1/Nsamp:0.5-1/Nsamp)';
figure; plot(fvec*2*pi,vk_psd);