% plot_AvgPeriodogramRect - averaged periodogram estimate with rectangular pulses

%% parameters
fsT = 20;
Ns  = 100;
Alphabet = [ 1, -1, j, -j];
Priors   = ones( size( Alphabet) ) / length(Alphabet);
Pulse = RectPulse( fsT );
Pulse = Pulse/norm(Pulse);  % ensures proper scaling
M     = 500; % number of averages

%% Loop for accumulating periodiograms
Ps = zeros( 1, Ns*fsT );

 
for kk=1:M
    % generate signal
    Symbols  = RandomSymbols(Ns, Alphabet, Priors);
    Signal   = LinearModulation(Symbols,Pulse,fsT);

    % accumulate periodogram
    Ps = Ps + Periodogram( Signal );

end

%average
Ps = Ps/M;

%% plot
% frequency axis
ffT = (-Ns*fsT/2 : Ns*fsT/2-1)/Ns;
plot( ffT, 10*log10(Ps), 'r' ) % normalize to Es=0dB

%% plot theoretical spectrum for comparison
hold on
plot_PSDRect
axis([ -fsT/2 fsT/2 -50 5])
hold off

%% Store name of script for automated figure generation
ScriptName = mfilename;