% Preliminary exercise 1

%
% BPSK signal spectrum
% 
clear all
close all

% generation on 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 sinc pulse. 
%

In = 1;                               %  generate one sample of input In
nFFT=1024;                            % size of the FFT
nSamples_per_symbol=16;
nPadded_symbols=nFFT/nSamples_per_symbol;   % how many symbols fit into N
In_reserved=zeros(1,nFFT);               % 4. Reserve the size for FFT input vector
In_reserved(1)=In;



% The code generates a shaping filter gtSinc
% filters the pulse In from above with the filter gtSing
% computes the power spectrum of the filtered signal
% and plots the signal

% filter generation
nSym=6;                                 % how many symbols the filter spans in both sides
                                        % of the center symbol
loc=-nSym:1/nSamples_per_symbol:nSym;     % positions where the filter coefficient are computed    
gtSinc=sin(pi*loc)./(pi*loc);           % generation of the sinc
gtSinc(nSym*nSamples_per_symbol+1)=1;
gtSinc=(gtSinc./sqrt(sum(gtSinc.^2)));  % power of the filter is normalized to 1

                                        % at the neighboring symbol
                                        % positions the filter coefficients
                                        % are zero. 
                                        % You can check it by plotting the
                                        % signal

B=5e6;                                % data rate frequency
sampling_rate=B*nSamples_per_symbol;     % Sampling frequency
FFT_bins_frequencies=-(sampling_rate/2-B/(nPadded_symbols))...
                    :B/(nPadded_symbols)...
                    :sampling_rate/2;
                                        
tx_sinc_signal_shaped=...
    filter(gtSinc,1,In_reserved);       % filter the sample with the shaping filter
tx_sinc_signal_FFT=...
    fft(tx_sinc_signal_shaped,nFFT);       % take N point fft  
tx_sinc_signal_FFT_shifted=...
    fftshift(tx_sinc_signal_FFT);       % shifts the zero into center of the spectrum 
tx_sinc_signal_FFT_shifted_dB=...
    10*log10(1/sampling_rate*abs(tx_sinc_signal_FFT_shifted).^2); % power spectrum values in dB



hold on
plot(FFT_bins_frequencies/1e6,tx_sinc_signal_FFT_shifted_dB,'--r')
axis([-40 40 -150 -60]);
grid
xlabel('frequency [MHz]');
ylabel('magnitude [dB]');

