You are on page 1of 21

1

V5: DIRECT SEQUENCE SPREAD SPECTRUM MODULATION AND


DEMODULATION

By Laurence G. Hassebrook

October 5, 2012
Table of Contents
V4:DIRECTSEQUENCESPREADSPECTRUMMODULATIONANDDEMODULATION.....................................1
0. Overview...........................................................................................................................................1
1. SYSTEMARCHITECTURE....................................................................................................................2
2. MESSAGESIGNALGENERATION.......................................................................................................4
2.1GroupNameandNumberofBits...................................................................................................4
2.2BitSequenceGeneration................................................................................................................4
2.3SpreadCodeGeneration.................................................................................................................4
3. DSSSMODULATION..........................................................................................................................5
4. CHANNELMODEL..............................................................................................................................8
5. DSSSDEMODULATION....................................................................................................................10
6. PERFORMANCEVERIFICATION........................................................................................................12
7. REFERENCES....................................................................................................................................13
A. APPENDIX:INSTRUCTORPROGRAMS.............................................................................................13
A.1BGEN.................................................................................................................................................14
A.2SPREADCODEGENERATION.............................................................................................................14
A.3CHANNEL...........................................................................................................................................16
A.4BITCHECK..........................................................................................................................................18

0. Overview

In this visualization we use our MATLAB based communication simulation system. The Direct
Sequence Spread Specturm (DSSS) modulation scheme is implemented with the simulation and
the student is asked to maximize the number of message bits without receiving an error. Baseline
parameter values are given and the modulation and demodulation technique is given
mathematically.

2

The student is required to implement the mathematics of the modulator and demodulator
to MATLAB code. In this visualization the student only needs to submit their simulation .m
files to the instructor. No written report is necessary.

The students are also required to formulate a group name, send the instructor their
modified createBsize.m, modulator.m and demodulator.m and Key.mat files along with
their group member names.

1. SYSTEM ARCHITECTURE

The system architecture includes programs that the student modifies and programs that the
instructor uses to evaluate their system with. The student has access to the instructor programs so
that they can pre-evaluate their systems performance. Given the student programs, the students
are required to comply with the File and Data formatting specifications. The student programs
have the I/O functionality already built in and specify sections where the students can insert their
code. The students may use the DSSS_dsssKeygenV5.m program to generate two spreading
codes (Keys).

Referring to Figs. 1-1 and 1-2, the procedure for running the system is as follows:

1. (student) Edit in the group name and number of bits (N
bit
) into name_createBsize.m and
change name to name_createBsize.m.
2. (student) Run name_createBsize.m. This stores N
bit
and groupname.
3. (instructor) Edit in group name into Bgenxx.m and run program. This stores two files, one
with the random bit sequence of N
bit
bits and the other file contains the active group being
processed.
4. (student) Run name_dsssKeygenV5.m to generate 2 spread codes stored in
name_Key.mat. These codes only need to be generated when there is a change in the
number of bits thereby changing the number of samples per bit. The student only needs to
send the Key.mat file to the instructor with his 3 other programs.
5. (student) Run name_modulator.m. The input is the Message signal is the random bit
sequence. The bit sequence is scaled to be bipolar. Using a kronecker product operation,
the bipolar message sequence is upsampled to length N. This in turn is modulated using
DSBSC modulation at a carrier frequency, k
c
, specified by the student. The modulator
outputs two files, the first with the modulated signal exactly N real elements long and the
other a special trinary signal where 1 indicates time location of a 1 bit and -1 indicates
the location of a 0 bit. This trinary signal is NOT passed to the demodulator but ONLY
ACCESSED by the bitcheckxx.m program.
6. (instructor) Run channelxx.m. The channel filters the spectrum of the modulated signal
and adds Gaussian white noise to the signal based on the minimum and maximum values
in the signal. The instructor determines the channel transfer function and the amount of
noise added. The filtered noisy output is stored in name_r.mat as the real vector r that
is N elements in length. Channel will also test for errors in the input data format.
7. (student) Run name_demodulator.m. The student may use any parameters used in the
modulator, including the number of bits, and will process the input r signal vector from
3

the Channel. However, there is NO KNOWLEDGE of the random bit sequence allowed.
The output of the demodulator, real vector Bs, is stored in name_Bs.mat.
8. (instructor) Run bitcheckxx.m. Bitcheck uses the Bcheck signal generated in the
Modulator to test for 1 or 0 bits. It prints out any errors in format but most importantly
how many false alarms and misses occur in the detection process. If there are errors in
detection then a figure will be generated revealing the local signal characteristics that led
to the error.

Figure11:SimulationFlowChartwithassociatedMATLABprogramsanddatavariablesandvectors.
The flowchart in Fig. 1-1 shows the relationship of the MATLAB functions with respect to a
standard communications system flowchart. Notice that name_modulator.m both encodes by
upsampling the bit sequence and modulates. The function Bitcheckxx.m both decodes by
thresholding the B
s
output vector from the demodulator as well as tests for detection errors based
on the B
check
vector.

Figure12:CommunicationsSimulationSystemArchitecture.
DSSS requires the generation of Spread Codes to be provided to the modulator and demodulator.
These are stored in a Key matrix in MATLAB. The simulation architecture is shown in Fig. 1-2.
The left group corresponds to the student controlled MATLAB programs, the center group
4

represents the data storage and the right group shows the instructor controlled MATLAB
programs.
2. MESSAGE SIGNAL GENERATION

The student renames and uses the name_Bsize.m file to establish their groups name and
number bits to be transmitted through the communications simulator. Once that is run, it stores
these two parameters name_Bsize.mat. The next step is to run Bgenxx.m, given in Appendix:
Bgen. But before running Bgenxx.m, the groupname variable needs to be manually changed to
match which student group name that should be run. This way the instructor can choose which
groups program set is to be run. If the students are using Bgen in development, then they just
need to edit in their name one time.

2.1 Group Name and Number of Bits

Below is the source code for the name_createBsize.m program. For this example, FM has been
entered as the group name and ???? need to be replaced by a number. For example, replace ????
with 2*2048 which means that 4096 bits will be transmitted through the communication system.

%gener at e gr oupname_Bsi ze. mat
cl ear ;
%I NSERT GROUP NAME AND NUMBER OF BI TS
gr oupname=' DSSS' %name of gr oup
Nbi t =?*2048 %number of bi t s t o t r ansmi t t ( 4 wor ks)
%END OF I NSERT
%name of out put f i l e t hat st or es Nbi t and f i l ename
f i l ename=spr i nt f ( ' %s_Bsi ze. mat ' , gr oupname)
save( f i l ename) ; %st or es gr oupname, Nbi t t o DSSS_Bsi ze. mat

2.2 Bit Sequence Generation

From Appendix: Bgen, the only line of code that would need to be changed is

gr oupname=' DSSS' %i nst r uct or ent er s t hi s name t o sel ect st udent pr oj ect

In this case DSSS is entered but should be whatever the group name is. Bgenxx.m generates a
pseudo-random set of bits, N
bit
long, to be transmitted through the communication system.

2.3 Spread Code Generation

A function set is provided to the student to generate two spread codes. It is not an ideal generator
but simply generates an excess of pseudo-random uniformly distributed sequences, partitions that
into a large set of spread codes and then selects the two codes that are this least correlated. All
the code is given in Appendix A.2 but the first part of the function is

Nkeys=2;
5

gr oupname=' DSSS' %i nst r uct or ent er s t hi s name t o sel ect st udent pr oj ect
f i l ename=spr i nt f ( ' %s_Bsi ze. mat ' , gr oupname) ;
l oad ( f i l ename) %r et r i eve dat a
f i l ename
Nbi t
N=131072*8 %N i s set by i nst r uct or and cannot be changed
Nsampl e=f l oor ( N/ Nbi t ) %number of sampl es per Key
Nt i l es=8; %must be gr eat er t han Nkeys
Key=GenDsssKeys( Nkeys, Nsampl e, Nt i l es) ;
%same t he Key
f i l ename=spr i nt f ( ' %s_Key. mat ' , gr oupname) ;
save( f i l ename, ' Key' ) ; %save t he r andom

The student should change the DSSS to their group name. The idea is that you let Nkeys=2 to
get two spread codes, Nsample is determined by the number of samples per bit and Ntiles is
chosen to be much larger than Nkeys so that there is a good selection for the two most
orthogonal sequences. The two sequences are stored in Key matrix which is Nkeys x Nsample.
The spread codes only need to be regenerated when the number of bits changes such that the
number of samples per bit changes. The spread codes are needed by both the modulator and the
demodulator.
3. DSSS MODULATION

The modulator inputs the bit sequence and knows the full number of samples N to be used in the
system. It first determines the number of samples, N
sample
, per bit. Then it creates a bit sequence
b1 from the original B sequence and then complements b1 into b0. Then using the kronecker
product to upsample the bit sequence and simultaneously encode with the spread codes {Akey
and Bkey}, the full length sequences sm1 and sm0 are created. We call these two operations
legs and the student is required to determine the code in the second leg as indicated by the
????? marks.

Figure31:Twolegconfigurationforencodingtheupsampledbitsequencewiththespreadcodes.
The two leg DSSS modulation is shown in Fig. 3-1. There are other ways to encode the same
signal but this is more representative of what is being done in the program. Note that in the
figure, b
u
(t) is up sampled whereas in the program, the upsampling and encoding are all done at
once using the kron() function.

%I NSERT MODULATI ON EQUATI ON:
%I NSERT MODULATI ON EQUATI ON:
%I NSERT MODULATI ON EQUATI ON: I nput s smvect or , Key mat r i x, t and N
6

%cr eat e DSSS modul at i on si gnal s


b1( 1: Nbi t ) =B( 1, 1: Nbi t ) ; %encode 1 bi t l ocat i ons
b0( 1: Nbi t ) =1- B( 1, 1: Nbi t ) ; %encode 0 bi t l ocat i ons
[ Nkey Nkeyl engt h] =si ze( Key) ;
Nkeyl engt h %shoul d be same as Nsampl e
Nsampl e
Akey=Key( 1, : ) ; %key f or ones
Bkey=Key( 2, : ) ; %key f or zer os
t =0: ( N- 1) ;
sm1=kr on( b1, Akey) ; %f or mcont i nuous t i me dsss si gnal f or ones
sm0=????; %f or mcont i nuous t i me dsss si gnal f or zer os
%combi ne 1s and 0s by addi ng t he t wo si gnal s sm1 and sm0
sm=zer os( 1, N) ;
i f N > ( Nsampl e*Nbi t )
s( 1: ( Nsampl e*Nbi t ) ) =sm0( 1: ( Nsampl e*Nbi t ) ) +sm1( 1: ( Nsampl e*Nbi t ) ) ;
el se
s=sm1+sm0; %number of bi t s * Nsampl e i s equal t o N
end;
si ze( s) %ver i f y shape
%END OF MODULATI ON I NSERT
%END OF MODULATI ON I NSERT
%END OF MODULATI ON I NSERT

Figure32:SamplesectionofMessagesequenceafterupsamplingandencodingwithspreadcodes(greenversusblue).

%FT of message wavef or m
Sm=abs( f f t shi f t ( f f t ( sm) ) ) ;
f i gur e( 2) ;
k=0: ( N- 1) ;
k=k- N/ 2;
pl ot ( k, Sm) ;
xl abel ( ' DFT spect r umof Message Si gnal ' ) ;
pr i nt - dj peg Modul at or _f i gur e2

7

Figure33:DSSSmodulatedsignalspectra.

It can be seen from Figs. 3-2 and 3-3 that the modulated signal and associated spectrum look like
noise. In this regard DSSS gives a certain amount of security and to demodulate it, one must
know the spread codes.

%cr eat e t he bi t check mat r i x t o onl y be used by t he Bcheckxx. mf i l e
%YOU CANNOT PASS THI S I NFORMATI ON TO YOUR DEMODULATOR! !
sampl epul se=zer os( 1, Nsampl e) ;
sampl epul se( f l oor ( Nsampl e/ 2) ) =1;
Bcheck=zer os( 1, N) ;
%modul at e f i r st sequence t o ei t her +1 and - 1 val ues
b1check( 1: Nbi t ) =2*B( 1, 1: Nbi t ) - 1;
bcheckt emp=kr on( b1check, sampl epul se) ;
Bcheck=zer os( 1, N) ;
i f N > ( Nsampl e*Nbi t )
Bcheck( 1: ( Nsampl e*Nbi t ) ) =bcheckt emp( 1: ( Nsampl e*Nbi t ) ) ;
el se
Bcheck=bcheckt emp;
end;
f i gur e( 5) ;
i f Nbi t <( Nshowbi t s+1)
n=1: N;
pl ot ( n, Bcheck) ;
axi s( [ 1, N, - 1. 1, 1. 1] ) ;
xl abel ( ' Bi t Check Si gnal ' ) ;
el se
Nt emp=Nsampl e*Nshowbi t s;
n=1: Nt emp;
pl ot ( n, ( 0. 9*Bcheck( 1: Nt emp) ) ) ;
axi s( [ 1, Nt emp, - 1. 1, 1. 1] ) ;
xl abel ( ' Sampl e Sect i on of Bi t Check Si gnal ' ) ;
end;
pr i nt - dj peg Modul at or _f i gur e5

save ' DSSS_si gnal ' s;
save ' DSSS_Bcheck' Bcheck;


8

Figure34:Samplesectionofthebitchecksignal.
There are two signals shown in Fig. 3-5, the bipolar binary signal in blue and the bitcheck signal
in green. The bitcheck has been slightly attenuated from its true +/- 1 value to disconnect from
the binary signal curve but it really has 3 values +1, 0 and -1. A 0 indicates no bit value in that
time location, a 1 indicates there should be a high bit value and -1 indicates a low bit value for
that time location. The student is not allowed to pass this information to the demodulator so there
is no prior knowledge of the random bit sequence being used in the demodulator. However,
bitcheckxx.m uses this information to test the demodulator output.

4. CHANNEL MODEL

See Appendix Channel code. The channel filters and adds noise to the input signal. The
mathematical representation of the channel output is

( ) ( ) ( ) ( ) t t h t s t r e
~
+ - = (4.1)

where s(t) is the input signal, h(t) is the channel impulse response and ( ) t e
~
is a white Gaussian
noise process.

9

Figure41:LogMagnitudeSpectrumofinputsignal.
Fig. 4-1 is obtained by taking the log of the input spectrum such that

( ) ( ) 1 . 0 log + f S

Figure42:Channelfilterresponse.
The channel uses a butterworth frequency response as shown in Fig. 4-2.

Figure43:Inputresponseafterfiltering.

10

5. DSSS DEMODULATION

The DSSS demodulation is accomplished with a 2 leg correlation process. A flow diagram is
shown in Fig. 5-1. The idea is to design a low pass filter so that the frequency range is within the
roll off of the filter centered around its cutoff frequency. This filter attenuates the signal based on
its instantaneous frequency. That result is followed by an envelope detector similar to the one
used to demodulate AM signals.

Figure51:DSSScorrelationbaseddemodulator.

The signal r(t) comes from the channel model and includes filtering and noise addition. The
spread codes are tiled into a vector of length N. These vectors are element wise multiplied by the
input signal, one spread code for each leg of the demodulator. The multiplication is followed by
a convolution with a rectangle function the length of the spread codes. This is effectively a
moving average filter and the filter is centered such that its peak response occurs at the bit
centers. The last operation simply subtracts one legs results from the other to yield a
discriminable binary sequence. The student should fill in the ????? marks with the correct code.

%I NSERT DEMODULATI ON CODE:
%I NSERT DEMODULATI ON CODE:
%I NSERT DEMODULATI ON CODE: i nput Keys, Nbi t and r
S=f f t ( r ) ;
%cr eat e t he t wo l eg mi xer det ect or s
t =0: ( N- 1) ;
Akey=Key( 1, : ) ; %key f or ones
Bkey=???????? %key f or zer os
u=ones( 1, Nbi t ) ;
Nt emp=Nsampl e*Nbi t ;
st emp=kr on( u, Akey) ; %f or mcont i nuous t i me dsss si gnal f or ones
sm1=zer os( 1, N) ;
sm1( 1: Nt emp) =st emp( 1: Nt emp) ;
st emp=?????; %f or mcont i nuous t i me dsss si gnal f or zer os
sm0=zer os( 1, N) ;
sm0( 1: Nt emp) =st emp( 1: Nt emp) ;
%el ement wi se mul t i pl y l eg by si gnal
sm0=sm0. *r ;
sm1=sm1. *r ;
%convol ve wi t h movi ng aver age r ect angl e
h=i r ect ( 1, Nsampl e, 1, N) ;
11

H=f f t ( h) ;
Sm0=f f t ( sm0) ;
Sm1=????????
r n0=r eal ( i f f t ( Sm0. *H) ) ;
r n1=???????;
r n=r n1- r n0;
%END OF DEMODULATI ON I NSERT: out put r eal vect or r n t hat i s N l ong
%END OF DEMODULATI ON I NSERT:
%END OF DEMODULATI ON I NSERT:

The final step is scaling the signal to be between 0 and 1 for the bit check system. The code for
this is

%nor mal i ze t he out put t o be t est ed
%Bs must be scal ed f r omabout 0 t o 1 so i t can be t hr eshol ded at 0. 5 by
%Bcheck
Bs=r n;
Bs=Bs- mi n( Bs) ;
Bs=Bs/ max( Bs) ;
save ' DSSS_Bs' Bs;
f i gur e( 4)
i f Nbi t <( Nshowbi t s+1)
pl ot ( Bs) ;
axi s( [ 1, N, - 0. 1, 1. 1] ) ;
xl abel ( ' Demodul at ed DSSS Si gnal ' ) ;
el se
Nt emp=Nsampl e*Nshowbi t s;
pl ot ( Bs( 1: Nt emp) ) ;
axi s( [ 1, Nt emp, - 0. 1, 1. 1] ) ;
xl abel ( ' Sampl e sect i on of Demodul at ed DSSS Si gnal wi t h Noi se' ) ;
end;
pr i nt - dj peg Demod_f i gur e4


A section of bit reconstruction shown in Fig. 5-4.

Figure52:Reconstructionoffirstfewbits.

12

6. PERFORMANCE VERIFICATION

The detection error is determined by bitcheckxx.m See Appendix A.3 Bitcheck for source code.
The bitcheck program checks for error in formatting and provides the false alarm and miss count.
If a miss or false alarm occurs then the program will create a figure of the local region where the
error occurred.

Figure61:Estimateof1and0bitsignalpdf.
Bitcheck also yields the 1 and 0 mean values {
0
,
1
}, their Standard Deviations (STDs)
{o
0
, o
1
}, their discrimination measure and their probability density function estimates shown in
Fig. 6-1. The discrimination measure is given by
( )
( )
2
0
2
1
2
0 1
01
o o

+

= J (6.1)
The specific bitcheck code for these performance values is
%STATI STI CAL ANALYSI S OF BI NARY SI GNAL
Bi t 1i ndex=f i nd( Bcheck>0. 5) ;
Bi t 0i ndex=f i nd( Bcheck<- 0. 5) ;
[ MBi t 1 NBi t 1] =si ze( Bi t 1i ndex) ;
[ MBi t 0 NBi t 0] =si ze( Bi t 0i ndex) ;
Bi t s1=zer os( 1, NBi t 1) ;
Bi t s0=zer os( 1, NBi t 0) ;
Bi t s1=Bs( Bi t 1i ndex) ;
Bi t s0=Bs( Bi t 0i ndex) ;
mu1=mean( Bi t s1, 2)
mu0=mean( Bi t s0, 2)
var 1=var ( Bi t s1) ;
var 0=var ( Bi t s0) ;
STD1=sqr t ( var 1)
STD0=sqr t ( var 0)
SQRTof SNR1=mu1/ STD1
SQRTof SNR0=mu0/ STD0
Di scr i mi nat e=abs( mu1- mu0) / sqr t ( var 0+var 1)
13


The pdf estimation is performed by the following histogram operations:

%HI STOGRAM OF BI TS
W=100;
w=1: W;
maxhi st =1. 5;
mi nhi st =- . 5;
%f i nd coef f i ci ent s t o map f r omt he r ecei ved val ues t o t he hi st ogr ami ndex
%W=a*maxhi st +b, 1=a*mi nhi st +b, a=( W- 1) / ( maxhi st - mi nhi st ) b=1- a*mi nhi st
acoef =( W- 1) / ( maxhi st - mi nhi st ) ; bcoef =1- acoef *mi nhi st ;
h1=zer os( 1, W) ;
f or n=1: NBi t 1
m=f l oor ( acoef *Bi t s1( n) +bcoef ) ;
i f m>0
i f m<( W+1)
h1( m) =h1( m) +1;
end;
end; %i f m>0
end; %f or n
h1=h1/ NBi t 1;
h0=zer os( 1, W) ;
f or n=1: NBi t 0
m=f l oor ( acoef *Bi t s0( n) +bcoef ) ;
i f m>0
i f m<( W+1)
h0( m) =h0( m) +1;
end;
end; %i f m>0
end; %f or n
h0=h0/ NBi t 0;
maxhi st o=max( h0) ;
i f maxhi st o<max( h1)
maxhi st o=max( h1) ;
end;
f i gur e( 1) ;
v=( w- bcoef ) / acoef ; %make hor i zont al axi s be mi nhi st t o maxhi st uni t s
pl ot ( v, h0, v, h1) ;
xl abel ( ' Recei ved Bi t val ue' ) ;
yl abel ( ' pdf Est i mat e' ) ;
axi s( [ mi nhi st maxhi st 0 maxhi st o] ) ;
l egend( ' f ( bi t 0) ' , ' f ( bi t 1) ' ) ;
pr i nt - dj peg Bi t check_f i gur e1


7. REFERENCES

1. Principles of Communications, Systems, Modulation, and Noise by R, E. Ziemer and W.
H. Tranter, 6th Edition.

A. APPENDIX: INSTRUCTOR PROGRAMS

14

There are 3 instructor controlled programs, Bgenxx.m, Channelxx.m and bitcheckxx.m. The xx
will be different for different years or visualizations or projects. These programs are in sections
A.1, A.3 and A.4 respectively. Section A.2 contains the code for creating almost orthogonal
spread codes.
A.1 BGEN

%gener at e bi t mat r i x based on gr oupname_Bsi ze. mat
cl ear al l ;
gr oupname=' DSSS' %i nst r uct or ent er s t hi s name t o sel ect st udent pr oj ect
f i l ename=spr i nt f ( ' %s_Bsi ze. mat ' , gr oupname) ;
l oad ( f i l ename) %r et r i eve dat a
f i l ename
Nbi t
B=r and( 1, Nbi t ) ; %gener at e uni f or ml y di st r i but ed r andomsequence
B=bi nar i ze( B) ; %t hr eshol d sequence i nt o 0s and 1s
si ze( B)
f i l ename=spr i nt f ( ' %s_B. mat ' , gr oupname) ;
save( f i l ename) ; %save t he r andombi t sequence B
%save t he act i ve gr oupname
save ' act i vegr oup' gr oupname;


A.2 SPREAD CODE GENERATION

There is one program, DSSS_dsssKeygenxx.m that uses one function GenDsssKeys.m
which are shown below:

%gener at e or t hogonal i zed si gnal keys f or DSSS based on gr oupname_Bsi ze. mat
cl ear al l ;
Nkeys=2;
gr oupname=' DSSS' %i nst r uct or ent er s t hi s name t o sel ect st udent pr oj ect
f i l ename=spr i nt f ( ' %s_Bsi ze. mat ' , gr oupname) ;
l oad ( f i l ename) %r et r i eve dat a
f i l ename
Nbi t
N=131072*8 %N i s set by i nst r uct or and cannot be changed
Nsampl e=f l oor ( N/ Nbi t ) %number of sampl es per Key
Nt i l es=8; %must be gr eat er t han Nkeys
Key=GenDsssKeys( Nkeys, Nsampl e, Nt i l es) ;
%same t he Key
f i l ename=spr i nt f ( ' %s_Key. mat ' , gr oupname) ;
save( f i l ename, ' Key' ) ; %save t he r andombi t sequence B
%di spl ay Keys
n=1: Nsampl e;
f i gur e( 1) ;
pl ot ( n, Key( 1, : ) , n, Key( 2, : ) ) ;
t i t l e( ' DSSS Key si gnal s' ) ;
xl abel ( ' Ti me I ndex of Keys' ) ;
yl abel ( ' Si gnal val ue' ) ;
l egend( ' Key A' , ' Key B' ) ;
pr i nt - dj peg dsssKeygen_f i gur e1
15

%di spl ay cor r el at i on mat r i x


R=abs( Key*Key' ) ;
Rmax=max( max( R) )
Rmi n=mi n( mi n( R) )
f i gur e( 2) ;
i magesc( R) ;
col or map gr ay;
t i t l e( ' DSSS Key Cor r el at i on Mat r i x' ) ;
xl abel ( ' Key number ' ) ;
yl abel ( ' Key number ' ) ;
pr i nt - dj peg dsssKeygen_f i gur e2

%Gener at e or t hogonal i zed keys f or DSSS
%Key ampl i t ude < 1
f unct i on [ Keys] = GenDsssKeys( Nkeys, Nsampl e, Nt i l es)
%
N=Nsampl e*Nt i l es;
w=2*r and( 1, N) ;
N2=N/ Nsampl e;
R=ones( N2, N2) ;
Rmask=ones( N2, N2) ; %i ndi cat e non- usabl e pai r s
f or m=1: N2;
f or n=m: N2;
km0=1+( m- 1) *Nsampl e;
km1=m*Nsampl e;
kn0=1+( n- 1) *Nsampl e;
kn1=n*Nsampl e;
A=w( km0: km1) ;
A=A- ( sum( A) / Nsampl e) ;
A=A/ sqr t ( A*A' ) ;
B=w( kn0: kn1) ;
B=B- ( sum( B) / Nsampl e) ;
B=B/ sqr t ( B*B' ) ;
R( m, n) =abs( A*B' ) ;
Rmask( m, n) =0;
end; %end of n
end; %end of m
Keys=zer os( Nkeys, Nsampl e) ;
%f i nd keys wi t h mi ni mal cor r el at i on
i f Nkeys>2
%t hi s i s not opt i mi zed yet
%j ust gr ab keys wi t h f i r st key as f i r st t i l e
%ot her keys chosen t o have mi ni mumcor r el at i on wi t h 1st one
Row=1; Col =1;
km0=1+( Row- 1) *Nsampl e; km1=Row*Nsampl e;
A=w( km0: km1) ;
A=A- ( sum( A) / Nsampl e) ;
A=sqr t ( Nsampl e) *A/ sqr t ( A*A' ) ;
Keys( 1, 1: Nsampl e) =A;
f or nkey=2: Nkeys;
Col =nkey;
kn0=1+( Col - 1) *Nsampl e; kn1=Col *Nsampl e;
B=w( kn0: kn1) ;
B=B- ( sum( B) / Nsampl e) ;
B=sqr t ( Nsampl e) *B/ sqr t ( B*B' ) ;
Keys( nkey, 1: Nsampl e) =B;
end; %f or nkeys
16

el se
%f i r st and second keys wi t h mi ni mumcor r el at i on
J =f i nd( R==mi n( mi n( R) ) ) ;
%J = m* N2 + n
Col =f l oor ( ( J ( 1) - 1) / N2) ;
Row=f l oor ( ( ( ( J ( 1) - 1) / N2) - Col ) *N2) ;
Row=Row+1; %conver t t o Mat l ab i ndex
Col =Col +1; %conver t t o Mat l ab i ndex
km0=1+( Row- 1) *Nsampl e; km1=Row*Nsampl e;
A=w( km0: km1) ;
A=A- ( sum( A) / Nsampl e) ;
% A=sqr t ( Nsampl e) *A/ sqr t ( A*A' ) ;
Keys( 1, 1: Nsampl e) =A;
kn0=1+( Col - 1) *Nsampl e; kn1=Col *Nsampl e;
B=w( kn0: kn1) ;
B=B- ( sum( B) / Nsampl e) ;
% B=sqr t ( Nsampl e) *B/ sqr t ( B*B' ) ;
Keys( 2, 1: Nsampl e) =B;
end; %end of Nkeys
end

A.3 CHANNEL

%channel f unct i on
cl ear al l ;
noi seCoef =0. 02
%i nput act i ve gr oup
l oad ' act i vegr oup' gr oupname;
gr oupname
%i nput gr oupname_si gnal . mat
f i l ename=spr i nt f ( ' %s_si gnal . mat ' , gr oupname) ;
l oad( f i l ename) ;
%make sur e s i s r eal
si gnal =r eal ( s) ;
[ M, N] =si ze( si gnal )
i f N~= 1048576
' I ncor r ect vect or l engt h, shoul d be 1048576'
end;
%
%f or mf i l t er
f c=N/ 4;
Nor der =6;
f max=N/ 2;
n=1: N;
K=1; %f i l t er gai n
%l ow pass f i l t er
[ f HLP] =l p_but t er wor t h_oN_df t ( f c, K, f max, N, Nor der ) ;
%f i l t er si gnal t hr ough channel
S=f f t ( si gnal ) ;
H=HLP;
R=S. *H;
sn=r eal ( i f f t ( R) ) ;
k=n; k=k- N/ 2;
f i gur e( 1) ;
pl ot ( k, l og( abs( f f t shi f t ( S) ) +. 1) ) ;
17

xl abel ( ' Log Magni t ude Spect r umof I nput Si gnal ' ) ;
pr i nt - dj peg Channel _f i gur e1
f i gur e( 2) ;
pl ot ( k, abs( f f t shi f t ( H) ) ) ;
axi s( [ k( 1) , k( N) , - . 1, 1. 1] ) ;
xl abel ( ' Spect r umof Channel ' ) ;
pr i nt - dj peg Channel _f i gur e2
f i gur e( 3) ;
pl ot ( k, f f t shi f t ( l og( abs( R) +. 1) ) ) ;
xl abel ( ' Log Spect r umof Out put Si gnal , No Noi se' ) ;
pr i nt - dj peg Channel _f i gur e3
%f i nd noi se devi at i on
si gma=noi seCoef *( max( si gnal ) - mi n( si gnal ) )
%add noi se
w=si gma*r andn( 1, N) ;
r =sn+w;
%st or e r esul t i n gr oupname_r . mat
f i l ename=spr i nt f ( ' %s_r . mat ' , gr oupname) ;
save( f i l ename, ' r ' ) ;
%PLOT spect r umand sampl e sect i ons of t he si gnal
f i gur e( 4) ;
Nsampl esect i on=20;
Nsampl es=f l oor ( N/ Nsampl esect i on) ;
i f N<Nsampl es
pl ot ( n, si gnal ) ;
axi s( [ 1, N, - 1. 1, 1. 1] ) ;
xl abel ( ' I nput Si gnal ' ) ;
el se
pl ot ( si gnal ( 1: Nsampl es) ) ;
axi s( [ 1, Nsampl es, - 1. 1, 1. 1] ) ;
xl abel ( ' Sampl e sect i on of I nput Si gnal ' ) ;
end;
pr i nt - dj peg Channel _f i gur e4
f i gur e( 5) ;
i f N<Nsampl es
pl ot ( n, sn) ;
axi s( [ 1, N, - 1. 1, 1. 1] ) ;
xl abel ( ' Out put Si gnal , No Noi se' ) ;
el se
pl ot ( sn( 1: Nsampl es) ) ;
axi s( [ 1, Nsampl es, - 1. 1, 1. 1] ) ;
xl abel ( ' Sampl e sect i on of Out put Si gnal , No Noi se' ) ;
end;
pr i nt - dj peg Channel _f i gur e5
f i gur e( 6) ;
i f N<Nsampl es
pl ot ( n, r ) ;
axi s( [ 1, N, - 1. 1, 1. 1] ) ;
xl abel ( ' Out put Si gnal ' ) ;
el se
pl ot ( r ( 1: Nsampl es) ) ;
axi s( [ 1, Nsampl es, - 1. 1, 1. 1] ) ;
xl abel ( ' Sampl e sect i on of Out put Si gnal wi t h Noi se' ) ;
end;
pr i nt - dj peg Channel _f i gur e6

18


A.4 BITCHECK
%bi t check
cl ear al l ;
%i nput act i ve gr oup
l oad ' act i vegr oup' gr oupname;
gr oupname

%i nput or i gi nal si ze
f i l ename=spr i nt f ( ' %s_Bsi ze. mat ' , gr oupname) ;
l oad ( f i l ename) %r et r i eve mat r i x si ze
f i l ename
Nbi t b4=Nbi t
%i nput or i gi nal bi t mat r i x
f i l ename=spr i nt f ( ' %s_B. mat ' , gr oupname) ;
l oad ( f i l ename) ;
%l oad bi t check
f i l ename=spr i nt f ( ' %s_Bcheck. mat ' , gr oupname) ;
l oad ( f i l ename) ;
%l oad r ecei ved si gnal
f i l ename=spr i nt f ( ' %s_Bs. mat ' , gr oupname) ;
l oad ( f i l ename) ;
%check f or consi st ancy
[ Nseqnow, N] =si ze( Bs) ;
N
i f Nseqnow~=1
' ERROR: bi t check mat r i ces i nconsi st ant '
el se
' OK: bi t check mat r i ces consi st ant '
end;
%pl ot
k=1: N;
f =1;
f i gur e( f +1) ;
bs1( 1: N) =Bs( f , 1: N) ;
bcheck1( 1: N) =Bcheck( f , 1: N) ;
k1=1: 128;
bs1t emp( 1: 128) =bs1( 1: 128) ;
bcheck1t emp( 1: 128) =bcheck1( 1: 128) ;
pl ot ( k1, bs1t emp, k1, bcheck1t emp) ;
axi s( [ 1 128 - 0. 1 1. 1] ) ;
xl abel ( ' f i r st f ew sampl es of si gnal ' ) ;
%l oop t hr ough bi t s
Bt est =zer os( 1, Nbi t ) ;
mi ss=0;
f al se=0;
Ner r or =0;
nbr ecei ved=0;
m=1;
nb=1;
f or n=1: N
i f Bcheck( m, n) > 0. 5 %" 1" shoul d be pr esent i n check si gnal
i f Bs( m, n) >0. 5
Bt est ( m, nb) =1;
el se
19

' ERROR, mi ssi ng 1'


' Bcheck'
Bcheck( m, n)
' Bs'
Bs( m, n)
m
n
nb
i f Ner r or <10
f i gur e( 2+1+Ner r or ) ;
i st ar t =n- ( 2*N/ Nbi t ) ;
i st op=n+( 2*N/ Nbi t ) ;
i f i st ar t <1
i st ar t =1
end;
i f i st op>N
i st op=N
end;
cl ear x;
x=1: ( 1+i st op- i st ar t ) ;
bt emp=x;
bcheckt emp=x;
bt emp( 1: ( 1+i st op- i st ar t ) ) =Bs( m, i st ar t : i st op) ;
bcheckt emp( 1: ( 1+i st op- i st ar t ) ) =Bcheck( m, i st ar t : i st op) ;
pl ot ( x, bt emp, x, bcheckt emp) ;
%cl ear x, bt emp, bcheckt emp;
end;
mi ss=mi ss+1;
Ner r or =Ner r or +1;
end;
nb=nb+1;
nbr ecei ved=nbr ecei ved+1;
end;
i f Bcheck( m, n) < - 0. 5 %" - 1" shoul d be pr esent i n check si gnal
i f Bs( m, n) < 0. 5 %" 0" i s pr esent demodul at ed/ bi nar i zed si gnal
Bt est ( m, nb) =0;
el se
' ERROR, mi ssi ng 0'
' Bcheck'
Bcheck( m, n)
' Bs'
Bs( m, n)
m
n
nb
i f Ner r or <10
f i gur e( 2+1+Ner r or ) ;
i st ar t =n- ( 2*N/ Nbi t ) ;
i st op=n+( 2*N/ Nbi t ) ;
i f i st ar t <1
i st ar t =1
end;
i f i st op>N
i st op=N
end;
cl ear x;
x=1: ( 1+i st op- i st ar t ) ;
20

bt emp=x;
bcheckt emp=bt emp;
bt emp( 1: ( 1+i st op- i st ar t ) ) =Bs( m, i st ar t : i st op) ;
bcheckt emp( 1: ( 1+i st op- i st ar t ) ) =Bcheck( m, i st ar t : i st op) ;
i st ar t
i st op
si ze( x)
si ze( bt emp)
si ze( bcheckt emp)
pl ot ( x, bt emp, x, bcheckt emp) ;
end;
f al se=f al se+1;
Ner r or =Ner r or +1;
end;
nb=nb+1;
nbr ecei ved=nbr ecei ved+1;
end;
end;
nbsent =Nbi t
nbr ecei ved
mi ss
f al se
Ner r or
i f nbsent ~=nbr ecei ved
' Er r or bet ween sent and r eci eved'
' Number of ones and zer os sent '
Nones=sum( sum( B) )
Nzer os=nbsent - Nones
end;
%STATI STI CAL ANALYSI S OF BI NARY SI GNAL
Bi t 1i ndex=f i nd( Bcheck>0. 5) ;
Bi t 0i ndex=f i nd( Bcheck<- 0. 5) ;
[ MBi t 1 NBi t 1] =si ze( Bi t 1i ndex) ;
[ MBi t 0 NBi t 0] =si ze( Bi t 0i ndex) ;
Bi t s1=zer os( 1, NBi t 1) ;
Bi t s0=zer os( 1, NBi t 0) ;
Bi t s1=Bs( Bi t 1i ndex) ;
Bi t s0=Bs( Bi t 0i ndex) ;
mu1=mean( Bi t s1, 2)
mu0=mean( Bi t s0, 2)
var 1=var ( Bi t s1) ;
var 0=var ( Bi t s0) ;
STD1=sqr t ( var 1)
STD0=sqr t ( var 0)
SQRTof SNR1=mu1/ STD1
SQRTof SNR0=mu0/ STD0
Di scr i mi nat e=abs( mu1- mu0) / sqr t ( var 0+var 1)
%HI STOGRAM OF BI TS
W=100;
w=1: W;
maxhi st =1. 5;
mi nhi st =- . 5;
%f i nd coef f i ci ent s t o map f r omt he r ecei ved val ues t o t he hi st ogr ami ndex
%W=a*maxhi st +b, 1=a*mi nhi st +b, a=( W- 1) / ( maxhi st - mi nhi st ) b=1- a*mi nhi st
acoef =( W- 1) / ( maxhi st - mi nhi st ) ; bcoef =1- acoef *mi nhi st ;
h1=zer os( 1, W) ;
f or n=1: NBi t 1
21

m=f l oor ( acoef *Bi t s1( n) +bcoef ) ;


i f m>0
i f m<( W+1)
h1( m) =h1( m) +1;
end;
end; %i f m>0
end; %f or n
h1=h1/ NBi t 1;
h0=zer os( 1, W) ;
f or n=1: NBi t 0
m=f l oor ( acoef *Bi t s0( n) +bcoef ) ;
i f m>0
i f m<( W+1)
h0( m) =h0( m) +1;
end;
end; %i f m>0
end; %f or n
h0=h0/ NBi t 0;
maxhi st o=max( h0) ;
i f maxhi st o<max( h1)
maxhi st o=max( h1) ;
end;
f i gur e( 1) ;
v=( w- bcoef ) / acoef ; %make hor i zont al axi s be mi nhi st t o maxhi st uni t s
pl ot ( v, h0, v, h1) ;
xl abel ( ' Recei ved Bi t val ue' ) ;
yl abel ( ' pdf Est i mat e' ) ;
axi s( [ mi nhi st maxhi st 0 maxhi st o] ) ;
l egend( ' f ( bi t 0) ' , ' f ( bi t 1) ' ) ;
pr i nt - dj peg Bi t check_f i gur e1

You might also like