% Preliminary exercise 1

%
% BPSK signal spectrum
% 
clear all
%close all

% generation of a  bit
% The BPSK signal spectrum is the same as the pulse shaping filter spectrum
% 
% We generate one sample 
% filter if with the pulse shaping filter
% compute the power spectrum of the filtered signal

% Kalle Ruttik
% 19.11.2012

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Computing spectrum of a BPSK signal shaped by a square pulses. 
%

In = 1;                               % 1. generate one sample of input In
nFFT=1024;                               % 2. size of the FFT
nSamples_per_symbol=16;
nPadded_symbols=nFFT/nSamples_per_symbol;   % 3. how many symbols fit into N

In_reserved=zeros(1,nFFT);               % 4. Reserve the size for FFT input vector
In_reserved(1)=In;

gtRect=rectpulse(1,nSamples_per_symbol)...
    /sqrt(nSamples_per_symbol);         % 5. create the shaping filter gtRect 
                                      %    where the lenght of the filter 
                                      %    describes how many samples 
                                      %    are inserted
                                      %    between two subsequent 
                                      %    data samples - In 

tx_rect_signal_shaped=...
    filter(gtRect,1,In_reserved);     % 6. filter the sample with the pulse shaping filter
tx_rect_signal_FFT=...
    fft(tx_rect_signal_shaped,nFFT);     % 7. take N point fft  
                                      %    if tx_signal_shaped is less than N points 
                                      %    the signal is padded (extened) with zeros  


% Plot the power spectrum of the signal

F=5e6;                                % data rate frequency
sampling_rate=F*nSamples_per_symbol;     % Sampling frequency
FFT_bins_frequencies=-(sampling_rate/2-F/(nPadded_symbols))...
                    :F/(nPadded_symbols)...
                    :sampling_rate/2;

tx_rect_signal_FFT_shifted=...
    fftshift(tx_rect_signal_FFT);     % shifts the zero frequency into the center of the spectrum 
tx_rect_signal_FFT_shifted_dB=...
    10*log10(1/(sampling_rate)*abs(tx_rect_signal_FFT_shifted).^2); % power spectrum values in dB

plot(FFT_bins_frequencies/1e6,tx_rect_signal_FFT_shifted_dB,'b*')
grid
xlabel('frequency [MHz]');
ylabel('magnitude [dB]');
axis([-40 40 -110 -60]);
