clc;clear all;close all;data=input('Enter original data:');x=input('ENTER INTIAL STATE:');n=input('no. of bits to be find');fprintf('\nINTIAL STATE:');r1=input('enter polynomial');r2=input('enter polynomial');[p1,len]=pnseq(x,n,r1);[p2,len]=pnseq(x,n,r2);y=xor(p1,0);Rb=1;Tb=1/Rb;amplitude=1;y=2*y-1;k=length(data);l=round(n/k);figure(1)subplot(4,2,1)stem(p1)xlabel('No. of samples')ylabel('Amplitude')title('Pn sequence ')sch=ceil((k/n)*(1:n));dsch=data(sch);dpol=2*dsch-1;subplot(4,2,2)stem(data)xlabel('No. of samples')ylabel('Amplitude')title('original data ')spd=y.*dpol;[time,nrzData,Fs]=NRZ_Encoder(spd,Rb,amplitude);subplot(4,2,3)stem(spd);xlabel('No. of samples')ylabel('Amplitude')title('spreaded sequence')subplot(4,2,4)plot(time,nrzData);xlabel('No. of samples')ylabel('Amplitude')title('NRZ data')Fc=2*Rb;osc = sin(2*3.14*Fc*time);subplot(4,2,5)plot(time,osc);xlabel('No. of samples')ylabel('Amplitude')title('carrier signal')%BPSK modulationbpskModulated = nrzData.*osc;subplot(4,2,6);plot(time,bpskModulated);xlabel('Time');ylabel('Amplitude');
title('BPSK Modulated Data');%AWGN%%%%%%%%%%%%%%%%%%%%%%%%q=input('enter the SNR vector');for e=1:length(q) aw=awgn(bpskModulated,q(e));figure(2);subplot(length(q),1,e);plot(aw)v = aw.*osc;figure(3);subplot(length(q),1,e);plot(v) %Integrator integrationBase = 0:1/Fs:Tb-1/Fs; for i = 0:(length(v)/(Tb*Fs))-1 z(i+1)=trapz(integrationBase,v((int32(i*Tb*Fs+1)):int32((i+1)*Tb*Fs))); end figure(4)subplot(length(q),1,e);stem(z);xlabel('No. of samples')ylabel('Amplitude')title('demodulated data') %Threshold Comparator estimatedBits=(z>=0.5);figure(5)subplot(length(q),1,e); stem(estimatedBits);xlabel('Samples');ylabel('Amplitude');title('Estimated Binary Data'); rx=2*estimatedBits-1;dspd=rx.*y;figure(6)subplot(length(q),1,e);stem(dspd);xlabel('No. of samples')ylabel('Amplitude')title('original signal at ') odata=dspd(1:l:n);odata=(odata+1)/2
figure(7)subplot(length(q),1,e);stem(odata);xlabel('No. of samples')ylabel('Amplitude')title('despreaded original signal at ') %ber% BER=0; total=0;for g=1:length(data) if data(g)~=odata(g) BER=BER+1; else BER=BER; endend total=BER end
outputEnter original data:[1 1 1]
ENTER INTIAL STATE:[1 0]
no. of bits to be find50
INTIAL STATE:enter polynomial[1 0 1]
enter polynomial[1 1 1]
enter the SNR vector[0 4 60 120]
odata =
1 0 1
total =
1
odata =
1 0 1
total =
1
odata =
1 0 1
total =
1
odata =
1 0 1
total =
1
>>
Figures
0 20 40 600
0.51
No. of samples
Am
plitu
dePn sequence
1 1.5 2 2.5 30
0.51
No. of samples
Am
plitu
de
original data
0 20 40 60-101
No. of samples
Am
plitu
de
spreaded sequence
0 20 40 60-101
No. of samples
Am
plitu
de
NRZ data
0 20 40 60-101
No. of samples
Am
plitu
de
carrier signal
0 20 40 60-101
TimeA
mpl
itude
BPSK Modulated Data
0 100 200 300 400 500 600 700 800-5
0
5
0 100 200 300 400 500 600 700 800-5
0
5
0 100 200 300 400 500 600 700 800-2
0
2
0 100 200 300 400 500 600 700 800-1
0
1
0 100 200 300 400 500 600 700 800-5
0
5
0 100 200 300 400 500 600 700 800-5
0
5
0 100 200 300 400 500 600 700 800-2
0
2
0 100 200 300 400 500 600 700 800-1
0
1
0 5 10 15 20 25 30 35 40 45 50-101
No. of samples
Am
plitu
de
demodulated data
0 5 10 15 20 25 30 35 40 45 50-101
No. of samples
Am
plitu
de
demodulated data
0 5 10 15 20 25 30 35 40 45 50-0.5
00.5
No. of samples
Am
plitu
de
demodulated data
0 5 10 15 20 25 30 35 40 45 50-0.5
00.5
No. of samples
Am
plitu
de
demodulated data
0 5 10 15 20 25 30 35 40 45 500
0.51
Samples
Am
plitu
deEstimated Binary Data
0 5 10 15 20 25 30 35 40 45 500
0.51
Samples
Am
plitu
de
Estimated Binary Data
0 5 10 15 20 25 30 35 40 45 50-101
Samples
Am
plitu
de
Estimated Binary Data
0 5 10 15 20 25 30 35 40 45 50-101
Samples
Am
plitu
de
Estimated Binary Data
0 5 10 15 20 25 30 35 40 45 50-101
No. of samples
Am
plitu
deoriginal signal at
0 5 10 15 20 25 30 35 40 45 50-101
No. of samples
Am
plitu
de
original signal at
0 5 10 15 20 25 30 35 40 45 50-101
No. of samples
Am
plitu
de
original signal at
0 5 10 15 20 25 30 35 40 45 50-101
No. of samples
Am
plitu
de
original signal at
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 30
0.51
No. of samples
Am
plitu
de
despreaded original signal at
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 30
0.51
No. of samples
Am
plitu
de
despreaded original signal at
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 30
0.51
No. of samples
Am
plitu
de
despreaded original signal at
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 30
0.51
No. of samples
Am
plitu
de
despreaded original signal at
Lab 06
% WiCom_3
% By Kashif Shahzad
% 01-ET-31
% 3rd July 2004
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Direct Sequence Spread Spectrum
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc
clear
% Generating the bit pattern with each bit 6 samples long
b=round(rand(1,20));
pattern=[];
for k=1:20
if b(1,k)==0
sig=zeros(1,6);
else
sig=ones(1,6);
end
pattern=[pattern sig];
end
plot(pattern);
axis([-1 130 -.5 1.5]);
title('\bf\it Original Bit Sequence');
% Generating the pseudo random bit pattern for spreading
spread_sig=round(rand(1,120));
figure,plot(spread_sig);
axis([-1 130 -.5 1.5]);
title('\bf\it Pseudorandom Bit Sequence');
% XORing the pattern with the spread signal
hopped_sig=xor(pattern,spread_sig);
% Modulating the hopped signal
dsss_sig=[];
t=[0:100];
fc=.1
c1=cos(2*pi*fc*t);
c2=cos(2*pi*fc*t+pi);
for k=1:120
if hopped_sig(1,k)==0
dsss_sig=[dsss_sig c1];
else
dsss_sig=[dsss_sig c2];
end
end
figure,plot([1:12120],dsss_sig);
axis([-1 12220 -1.5 1.5]);
title('\bf\it DSSS Signal');
% Plotting the FFT of DSSS signal
figure,plot([1:12120],abs(fft(dsss_sig)))