
%=======================================================================================
%    Power Spectrum Density
%
%    TKK - Comlab 2007 - SL
%=======================================================================================
%
% Purpose: simulate a communication system using QPSK 
%          and determine the Power Spectrum Density.
%
%
%=======================================================================================

%--------------------------------------
% Settings
%--------------------------------------

clear all

N = 512;                                    % number of information symbols

MaskLen= 10;                                % length of vector defining the pulse

TotalLen = N * MaskLen;

NoiseStdDev = 0.01;                         % Noise standard deviation

%----------------------------------------
% Generate random information for Real 
% and Imaginary components and combine
% in vector info[]
%----------------------------------------


info_R = randint(1,N,2);
info_I = randint(1,N,2);

info = info_R + j*info_I;

%----------------------------------------
% MAP to +1 and -1 to simulate QPSK
% 
%----------------------------------------

info = 2*info-(1+j);

info = sqrt(0.5).*info;

%----------------------------------------
% Part (a) : Rectangular pulse 
% g1 vector contains a sampled
% rectangular pulse with MaskLen samples
%----------------------------------------

g1 = ones(1,MaskLen);


%----------------------------------------
% Use a rectangular pulse with information
% data.
% Just multiply the pulse g with the info 
% symbol(ix) and store in column(ix) of
% matrix temp
%----------------------------------------

for ix=1:N
temp(:,ix)= g1 * info(ix);
end

%----------------------------------------
%form a vector by joining all the columns
% in matrix temp 
%----------------------------------------

Y1 = reshape(temp,1,TotalLen);



%----------------------------------------
% Part (b) : Sinusoid pulse 
% g2 vector contains a sampled
% sinusoidal pulse with MaskLen samples
%----------------------------------------


interval = pi / MaskLen;
for ix = 1 : MaskLen
 g2(ix) = sin(interval*(ix-1));
end


%----------------------------------------
% Use a sinuosidal pulse with information
% data.
% Just multiply the pulse g with the info
% symbol(ix)
% and store in column(ix) of matrix temp
%----------------------------------------

for ix=1:N
temp(:,ix)= g2 * info(ix);
end


%----------------------------------------
%form a vector by joining all the columns
% in matrix temp 
%----------------------------------------

Y2 = reshape(temp,1,TotalLen);


%----------------------------------------
% calculate Noise
%----------------------------------------

Noise = randn(1,TotalLen) * NoiseStdDev;


%----------------------------------------
% add noise to data (cases (a) and (b))
%----------------------------------------

Y1 = Y1+Noise;
Y2 = Y2+Noise;

%----------------------------------------
% Autocorrelate data
%----------------------------------------

corr1 = xcorr(Y1,'none');
corr2 = xcorr(Y2,'none');

%----------------------------------------
% Fourier transform of autocorrelation
%----------------------------------------

F1 = fftshift(fft(corr1));
F2 = fftshift(fft(corr2));


%----------------------------------------
% normalize frequency in x axis 
% and plot the power spectrum density
%----------------------------------------

xaxis = ((-TotalLen)+1):((TotalLen)-1);
xaxis = xaxis/(2*N);

semilogy(xaxis,abs(F1),'b')
hold on
semilogy(xaxis,abs(F2),'r')
hold off

grid

xlabel('Normalized frequency')
ylabel('Power Spectrum Density')
legend('Rectangular pulse','Sinusoidal pulse')




