image thumbnail

DSSS enhanced with a coarse time synchronization loop

by

 

This is a DSSS Modulation Trx/Rcxr simulation, powered by a coarse initial code aquisition loop

DSSS_enhanced_with_a_coarse_time_synchronization_loop.m
%**************************************************************************

%By Farshid Zoghalchi #8423026.
%AmirKabir University of Technology
%Elec. Eng. Department.
%July 10,2009

%**************************************************************************
% Direct Sequence Spread Spectrum Transmitter/Receiver systems simulation
%               With Time-Synchronization Considrations.
%**************************************************************************

clc
close all
clear all

R = 20;                         % The length of the bit stream.
S = 21;                         % Bit duration(each bit is represented with 
                                % S samples of the waveform used in signaling.
bit_stream = round(rand(1,R));  % A Randomley Generated Bit Stream.    
PAM = zeros(1,S*R);             % Predefinition of the Base-band PAM signal.

% Generating the PAM signal using Polar Non-Return-to-Zero signaling. Every
% S samples correspond to one bit.
for i = 1:R
    if bit_stream(1,i)== 0
        PAM(1+(i-1)*S : i*S) = -1;
    else
        PAM(1+(i-1)*S : i*S) =  1;
    end
end

subplot(3,2,1)
plot(PAM);
axis([-1 S*R+10 -1.2 1.2]);
title('The Original Base-Band PAM Signal generated using Polar Non-Return-to-Zero Signaling');

% As we know, The Fourier transform of the autocorrelation function results 
% to the Power Spectral Density function.
z = abs(fft(xcorr(PAM))); % PSD of the PAM signal.
subplot(3,2,2);
plot(z);
axis([0 840 0 1.1*max(z)]);
title('PSD of the PAM signal');

% Generating the pseudo random SEQUENCE for spreading.
pn_seq = round(rand(1,60));

% Generating the pseudo random SIGNAL for spreading.
t = 0:2*pi/6:2*pi;           % Creating 7 samples for one cosine 
c = sin(t);
carrier=[];
pn_sig_txr1 = zeros(1,S*R);   % Pseudo Noise signal in the trx.
for i = 1:60
    if pn_seq(1,i)==0
        pn_sig_trx1(1+(i-1)*7:i*7) = -1;
    else
        pn_sig_trx1(1+(i-1)*7:i*7) =  1;
    end  
    carrier=[carrier c];
end

K = round(rand(1,1)*420);
pn_sig_trx = [pn_sig_trx1(K:end) pn_sig_trx1(1:K-1)];
% Spreading of sequence
spreaded_sig = PAM .* pn_sig_trx;
subplot(3,2,3);
plot(spreaded_sig);
axis([-1 S*R+10 -1.2 1.2]);
title('Spreaded signal');

z = abs(fft(xcorr(spreaded_sig)));   % PSD of the spreaded signal.
subplot(3,2,4);
plot(z);
axis([0 840 0 1.1*max(z)]);
title('PSD of the spreaded signal');

% BPSK Modulation of the spreaded signal
bpsk_sig = spreaded_sig .* carrier;   % Modulating the signal
subplot(3,2,5);
plot(bpsk_sig)
axis([-1 100 -1.2 1.2]); % for visual purpuses, just first 100 samples are plotted.
title('BPSK Modulated Signal(first 100 samples)');

% Plotting the PSD of DSSS signal. 
y = abs(fft(xcorr(bpsk_sig)));  
subplot(3,2,6)
plot(y)
xlabel('Frequency(Hz)')
ylabel('Power Density')
title('PowerSpectralDensity(Watts/Hz)')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%          Demodulation and Despreading of Received Signal.               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%==========================================================================
%                 Demodulation of the Recevded Signal                     %
%==========================================================================
integrand = bpsk_sig .* carrier;    % The Integrand in the Receiver Correlator.
demod_sig = zeros(1,S*R);           % Predefining the Demodulated Signal.
for i = 1:60
    if sum(integrand(1+(i-1)*7 : 7*i))>=0
        demod_sig(1+(i-1)*7 : 7*i) = 1;
    else
        demod_sig(1+(i-1)*7 : 7*i) = -1;
    end
end

%========================================================================== 
%Despreading the Demodulated signal with TIME-ASYNCHRONIZATION considration
%                 Using a Paralle-Search strategy.                        %
%==========================================================================
P = zeros(1,60);    % Stores the power of the LowPass-filtered despreaded signal.

% Paralle-Search strategy...Looking for the maximum-power lowpass-filtered
% signal
disp('Parallel Search strategy started...')
b_r = zeros(60,R);              % recovered bit stream corresponding to each hyp_sig signal.
BER = zeros(1,60);              % Bit Error Rate vector.
for i = 1:60
    
    % Creating different delayed versions of the PN-signal at receiver.(with an offset
    % asynchronization value of 3 that can be changed within the range[0,7-1]).
    pn_sig_rcx = [pn_sig_trx1(7*(i-1)+3:end) pn_sig_trx1(1:7*(i-1)+2)];
    
    % despreaded signal using pn_sig_trx[n-7(i-1)+3]:
    % delayed version of the PN-signal in the transmitter.
    hyp_sig = demod_sig .* pn_sig_rcx;      
    b_r(i,:) = hyp_sig(1:S:end);
    BER(i) = sum(abs(bit_stream - b_r(i,:)))/R;
    
    % attempt to calculate PSD using selected delayed version of PN-signal.
    % z = 0.5+0.5*xcorr(despread_sig);
    z = abs(fft(xcorr(hyp_sig)));
    
    % Collecting the LowPass power of the resulted despreaded signal.
    P(i) = sum(z(1:30));    
    
    % Plot some of the results.
    if mod(i-1,12) == 0
        figure
        subplot(2,1,1)
        plot(hyp_sig)
        %titel('Hypothesized despreaded signal')
        axis([-1 S*R+10 -1.2 1.2]);
        % title(['Despreaded data using a delay value of'],in2str(i),['samples in PN-signal']);
        
        %Power Spectrum of Despreaded data
        subplot(2,1,2)
        plot(z)
        axis([0 840 0 1.1*max(z)])
        xlabel('Frequency(Hz)')
        ylabel('Power Density')
        title('Corresponding Power Spectral Density')
    end
end

D = find(P == max(P));% The true delay value of the PN-signal at receiver 
                      % is the one which results in the the maximum low-pass energy.                  
s = demod_sig .* [pn_sig_trx1(7*(D-1)+3:end) pn_sig_trx1(1:7*(D-1)+2)];                                   
ss = abs(fft(xcorr(s))); % PSD of the maximum lowpass energy signal. 
figure
subplot(2,1,1);
plot(s);
axis([-1 S*R+10 -1.2 1.2]);
title('Maximum lowpass energy signal');

subplot(2,1,2);
plot(ss)
axis([0 840 0 1.1*max(ss)])
xlabel('Frequency(Hz)')
ylabel('Power Density')
title('Corresponding Power Spectral Density')

figure;
% colormap summer
bar(BER)
colormap(summer)
%stem(BER,'MarkerFaceColor','red','Marker','square');
axis([0 61 0.5*min(BER) 1.2*max(BER)]);
title('Bit Error Reate for different delay values.')

Contact us