% plot_SpecDSSS - DS/SS spectrum

%% parameters
fsT = 120;
Nseq =15;
fsTc = fsT/Nseq;

Seq = [1 -1  1  1 -1 -1  1 -1 -1 -1  1  1  1  1 -1]; % an M-sequence
Ns  = 100;

PulseBPSK = RectPulse( fsT );
PulseBPSK = PulseBPSK/norm(PulseBPSK);

PulseDSSS = DSSSPulse( Seq, fsTc);
PulseDSSS = PulseDSSS/norm(PulseDSSS);

M     = 500; % number of averages

Alphabet = [ 1, -1];   % BPSSK
Priors   = ones( size( Alphabet) ) / length(Alphabet);

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

 
for kk=1:M
    % generate signals
    Symbols     = RandomSymbols(Ns, Alphabet, Priors);
    
    SignalBPSK   = LinearModulation( Symbols, PulseBPSK, fsT);

    SignalDSSS   = LinearModulation( Symbols, PulseDSSS, fsT);
    
    % accumulate periodogram
    PsBPSK = PsBPSK + Periodogram( SignalBPSK );
    PsDSSS = PsDSSS + Periodogram( SignalDSSS );

end

%average
PsBPSK = PsBPSK/M;
PsDSSS = PsDSSS/M;

%% plot
% frequency axis
ffT  = (-Ns*fsT/2 : Ns*fsT/2-1)/Ns;
plot( ffT, 10*log10(PsBPSK), ffT, 10*log10(PsDSSS),...
    [-1 -1 1 1], [ -50 0 0 -50],'b--', ... % spectrum outline
    Nseq*[-1 -1 1 1], [ -50 -10*log10(Nseq) -10*log10(Nseq) -50],'g--') 

grid
xlabel('Normalized Frequency (fT)')
ylabel('PSD (dB)')
axis( [-20 20 -50 5])
legend( 'BPSK', 'DS/SS')


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