V8: DIRECT SEQUENCE SPREAD SPECTRUM MODULATION AND DEMODULATION with MULTIPLE BIT STREAMS Table of Contents

advertisement
V8:DIRECTSEQUENCESPREADSPECTRUMMODULATIONAND
DEMODULATIONwithMULTIPLEBITSTREAMS
By Laurence G. Hassebrook
April 24, 2013
TableofContents
V8: DIRECT SEQUENCE SPREAD SPECTRUM MODULATION AND DEMODULATION with MULTIPLE BIT STREAMS ....................................................................................................................................................... 1 0. Overview ........................................................................................................................................... 1 1. SYSTEM ARCHITECTURE .................................................................................................................... 2 2. MESSAGE SIGNAL GENERATION ....................................................................................................... 4 2.1 Group Name, Number of Bits and Number of Bit Streams ............................................................. 4 2.2 Bit Sequence Generation ................................................................................................................ 5 2.3 Spread Code Generation ................................................................................................................. 5 3. DSSS MODULATION .......................................................................................................................... 5 4. CHANNEL MODEL .............................................................................................................................. 9 5. DSSS DEMODULATION .................................................................................................................... 11 6. PERFORMANCE VERIFICATION ........................................................................................................ 12 7. REFERENCES .................................................................................................................................... 14 A. APPENDIX: INSTRUCTOR PROGRAMS ............................................................................................. 14 A.1 BGEN ................................................................................................................................................. 14 A.2 SPREAD CODE GENERATION ............................................................................................................. 15 A.3 CHANNEL ........................................................................................................................................... 17 A.4 BITCHECK .......................................................................................................................................... 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 noise variance in the channel without receiving an error.
1 Baseline parameter values are given and the modulation and demodulation technique is given
mathematically.
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 student is required to formulate an alias name, send the instructor their modified
createBsize.m, modulator.m and demodulator.m and Key.mat files along with their alias
and the noise coefficient to be used for the channel.
1. SYSTEMARCHITECTURE
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_dsssKeygenV8.m program to generate two spreading
codes (“Keys”), one for each bit sequence.
Referring to Figs. 1-1 and 1-2, the procedure for running the system is as follows:
1. (student) Edit in the alias name and use the given number of bits (Nbit) and given number
of sequences (Nseq) into name_createBsize.m and change name to name_createBsize.m.
2. (student) Run name_createBsize.m. This stores Nseq, Nbit and aliasname.
3. (instructor) Edit in alias name into Bgenxx.m and run program. This stores two files, one
with the random bit sequence matrix of Nseq by Nbit bits and the other file contains the
active alias being processed.
4. (student) Run name_dsssKeygenV8.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 sequences are scaled to be bipolar. Using a kronecker product
operation. Each bipolar message sequence is upsampled and multiplied with a tiled
spreadcode to length N. Then those two DSSS signals are added together to form one
signal. The modulator outputs two files, the first with the modulated signal exactly N real
elements long and the other a special trinary signal matrix where 1 indicates time location
of a “1” bit and -1 indicates the location of a “0” bit. Each row of the bitcheck matrix is
associated with a bit sequence in pseudo-continuous time. 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
2 noise added. In this visualization the student tells the instructor how much noise is to be
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
the Channel. However, there is NO KNOWLEDGE of the random message bit sequences
allowed. The output of the demodulator, real matrix 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.
Figure 1‐1: Simulation Flow Chart with associated MATLAB programs and data variables and vectors. 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 Bs output vector from the demodulator as well as tests for detection errors based
on the Bcheck vector. 3 Figure 1‐2: Communications Simulation System Architecture. 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
represents the data storage and the right group shows the instructor controlled MATLAB
programs.
2. MESSAGESIGNALGENERATION
The student renames and uses the “name_Bsize.m” file to establish their alias and use the
number of sequences and the number bits specified 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
aliasname variable needs to be manually changed to match which student alias name that should
be run. This way the instructor can choose which students’ 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.1GroupName,NumberofBitsandNumberofBitStreams
Below is the source code for the name_createBsize.m program. For this example, “DSSS” has
been entered as the group name. We will be using 8192 bits per Nseq=2 bit streams.
% generate groupname_Bsize.mat
clear;
% INSERT GROUP NAME AND NUMBER OF BITS
groupname='DSSS' % name of group
Nbit=8192 % number of bits to transmit per sequence
Nseq=2 % number of sequences
% END OF INSERT
% name of output file that stores Nbit and filename
filename=sprintf('%s_Bsize.mat',groupname)
4 save(filename); % stores groupname, Nbit to DSSS_Bsize.mat
2.2BitSequenceGeneration
From Appendix: Bgen, the only line of code that would need to be changed is
groupname='DSSS' % instructor enters this name to select student project
In this case ‘DSSS’ is entered but should be whatever the group name is. Bgenxx.m generates a
pseudo-random set of bits, Nbit long, to be transmitted through the communication system.
2.3SpreadCodeGeneration
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;
groupname='DSSS' % instructor enters this name to select student project
filename=sprintf('%s_Bsize.mat',groupname);
load (filename) % retrieve data
filename
Nbit
N=131072*8 % N is set by instructor and cannot be changed
Nsample=floor(N/Nbit) % number of samples per Key
Ntiles=8; % must be greater than Nkeys
Key=GenDsssKeys(Nkeys,Nsample,Ntiles);
% same the Key
filename=sprintf('%s_Key.mat',groupname);
save(filename,'Key'); % save the random
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. DSSSMODULATION
The modulator inputs the M=2 bit sequences and knows the full number of samples N to be used
in the system. It first determines the number of samples, Nsample, per bit. Then it creates a positive
and negative bit complimentary sequence, b1 and b0, for each bit sequence from the original B
sequence. Then using the kronecker product to upsample the bit sequence and simultaneously
5 encode with one spread code per bit sequence, such that the full length sequences sm1 and sm0
are created. Then sm0 and sm1 are simply added together to create “seach” signal.
Figure 3‐1: M bipolar leg configuration for encoding the upsampled bit sequence with the spread codes.
The M bipolar legs 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, bu(t) is up sampled whereas in the program, the upsampling and encoding are all done at
once using the kron() function.
% INSERT MODULATION EQUATION:
% INSERT MODULATION EQUATION:
% INSERT MODULATION EQUATION: Inputs sm vector, Key matrix, t and N
% create DSSS modulation signal s
b0=zeros(Nseq,Nbit);
b1=zeros(Nseq,Nbit);
sm0=zeros(Nseq,Nbit*Nsample);
sm1=zeros(Nseq,Nbit*Nsample);
bmA=zeros(Nseq,Nbit*Nsample);
bm=zeros(Nseq,N);
seach=zeros(Nseq,1,N);
s=zeros(1,N);
unitv=ones(1,Nsample);
for nseq=1:Nseq
b1(nseq,1:Nbit)=B(nseq,1:Nbit); % encode 1 bit locations
b0(nseq,1:Nbit)=-(1-B(nseq,1:Nbit)); % encode 0 bit locations
sm1(nseq,:)=kron(b1(nseq,:),Key(nseq,:)); % form continuous time dsss signal for ones
sm0(nseq,:)=kron(b0(nseq,:),Key(nseq,:)); % form continuous time dsss signal for ones
bmA(nseq,:)=kron(b0(nseq,:),unitv)+kron(b1(nseq,:),unitv);
% combine 1s and 0s by adding the two signals sm1 and sm0
if N > (Nsample*Nbit)
seach(nseq,1:(Nsample*Nbit))=sm0(nseq,1:(Nsample*Nbit))+sm1(nseq,1:(Nsample*Nbit));
bm(nseq,1:(Nsample*Nbit))=bmA(nseq,1:(Nsample*Nbit));
else
seach(nseq,:)=sm1(nseq,:)+sm0(nseq,:); % number of bits * Nsample is equal to N
bm(nseq,:)=bmA(nseq,:);
end;
% normalize
seach(nseq,:)=seach(nseq,:)/max(max(abs(seach(nseq,:))));
% form composite
s=s+seach(nseq,:);
end;
% normalize
s=s/max(max(abs(s)));
size(s) % verify shape
% END OF MODULATION INSERT
% END OF MODULATION INSERT
% END OF MODULATION INSERT
A variable number of subplots is outputted with the composite signal plot using the following
code:
6 figure(3);
if Nbit<(Nshowbits+1)
for nseq=1:Nseq
subplot((Nseq+1),1,nseq);
plot(t,seach(nseq,:))
axis([1,N,-1.1,1.1]);
title('Message Signal');
end;
subplot((Nseq+1),1,(Nseq+1));
plot(t,s);
title('Composite Message Signal');
axis([1,N,-1.1,1.1]);
else
ts=0:(Nsample*Nshowbits-1);
for nseq=1:Nseq
subplot((Nseq+1),1,nseq);
plot(ts,seach(nseq,1:(Nsample*Nshowbits)),ts,bm(nseq,1:(Nsample*Nshowbits)));
axis([0,(Nsample*Nshowbits-1),-1.1,1.1]);
title('Sample section of Message Signal');
end;
subplot((Nseq+1),1,(Nseq+1));
plot(ts,s(1:(Nsample*Nshowbits)));
title('Composite Message Signal');
axis([0,(Nsample*Nshowbits-1),-1.1,1.1]);
end;
print -djpeg Modulator_figure3
Figure 3‐2: Sample section of Message sequences after upsampling and encoding with spread codes. Bottom subplot is the composite signal. The spectrum of the composite signal is shown in Fig. 3-3.
% FT of modulated waveform
Sall=abs(fftshift(fft(s)));
figure(4);
k=0:(N-1);
k=k-N/2;
plot(k,Sall);
title('Spectrum of DSSS Signal');
print -djpeg Modulator_figure4
7 Figure 3‐3: DSSS modulated signal spectra.
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.
% create the bit check matrix to only be used by the Bcheckxx.m file
% YOU CANNOT PASS THIS INFORMATION TO YOUR DEMODULATOR!!
samplepulse=zeros(1,Nsample);
samplepulse(floor(Nsample/2))=1;
Bcheck=zeros(Nseq,N);
b1check=zeros(Nseq,Nbit);
bchecktemp=zeros(1,Nsample*Nbit);
% modulate first sequence to either +1 and -1 values
for nseq=1:Nseq
b1check(nseq,1:Nbit)=2*B(nseq,1:Nbit)-1;
bchecktemp=kron(b1check(nseq,:),samplepulse);
if N > (Nsample*Nbit)
Bcheck(nseq,1:(Nsample*Nbit))=bchecktemp(1:(Nsample*Nbit));
else
Bcheck(nseq,:)=bchecktemp;
end;
end;
figure(5);
if Nbit<(Nshowbits+1)
for nseq=1:Nseq
subplot((Nseq+1),1,nseq);
plot(t,Bcheck(nseq,:))
axis([1,N,-1.1,1.1]);
title('Bit Check Signal');
end;
else
ts=0:(Nsample*Nshowbits-1);
for nseq=1:Nseq
subplot((Nseq+1),1,nseq);
plot(ts,Bcheck(nseq,1:(Nsample*Nshowbits)),ts,bm(nseq,1:(Nsample*Nshowbits)));
axis([0,(Nsample*Nshowbits-1),-1.1,1.1]);
title('Bit Check Signal');
end;
end;
print -djpeg Modulator_figure5
save 'DSSS_signal' s;
save 'DSSS_Bcheck' Bcheck;
8 Figure 3‐4: Sample section of the two bit check signal subplots.
There are two signals shown in Fig. 3-4, 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. CHANNELMODEL
See Appendix Channel code. The channel filters and adds noise to the input signal. The
mathematical representation of the channel output is
r t   s t   h t   ~ t 
(4.1)
where s(t) is the input signal, h(t) is the channel impulse response and ~t  is a white Gaussian
noise process.
9 Figure 4‐1: Log Magnitude Spectrum of input signal.
Fig. 4-1 is obtained by taking the log of the input spectrum such that
log  S  f   0.1
Figure 4‐2: Channel filter response.
The channel uses a butterworth frequency response as shown in Fig. 4-2.
Figure 4‐3: Input response after filtering.
10 5. DSSSDEMODULATION
The DSSS demodulation is accomplished with a M leg correlation process. A flow diagram is
shown in Fig. 5-1. The idea is to design a matched filter that multiplies the incoming signal with
each of the spread codes followed by correlation with the pulse shape. Depending on whether the
bits stream value is 1 or 0, the output is 1 or -1.
Figure 5‐1: DSSS correlation based demodulator.
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 elementwise 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 can be to binarize each of the rn signals or in the case below, store the
bipolar signals into matrix Bs.
% INSERT DEMODULATION CODE:
% INSERT DEMODULATION CODE:
% INSERT DEMODULATION CODE: input Keys, Nbit and r
S=fft(r);
% create the M-array bipolar detectors
t=0:(N-1);
u=ones(1,Nbit);
Ntemp=Nsample*Nbit;
% create detection filter
h=irect(1,Nsample,1,N);
H=fft(h);
%
for nseq=1:Nseq
Akey=Key(nseq,:);
11 stemp=kron(u,Akey); % form continuous time dsss signal for ones
sm=zeros(1,N);
sm(1:Ntemp)=stemp(1:Ntemp);
sm=sm.*r;
% convolve with moving average rectangle
Sm=fft(sm);
rn=real(ifft(Sm.*H));
rn=rn-min(rn);
rn=rn/max(rn);
Bs(nseq,1:N)=rn;
end;
% END OF DEMODULATION INSERT: output real vector rn that is N long
% END OF DEMODULATION INSERT:
% END OF DEMODULATION INSERT:
The final step is scaling the signal to be between 0 and 1 for the bit check system.
A section of bit reconstruction shown in Fig. 5-4.
Figure 5‐2: Reconstruction of first few bits of two bit stream signals.
6. PERFORMANCEVERIFICATION
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.
12 Figure 6‐1: Estimate of 1 and 0 bit signal pdf. Bitcheck also yields the “1” and “0” mean values {0, 1}, their Standard Deviations (STDs)
{0, 1}, their discrimination measure and their probability density function estimates shown in
Fig. 6-1. The discrimination measure is given by
J 01 
1   0 2

2
1
  02

(6.1)
The specific bitcheck code for these performance values is
% STATISTICAL ANALYSIS OF BINARY SIGNAL
Bit1index=find(Bcheck>0.5);
Bit0index=find(Bcheck<-0.5);
[MBit1 NBit1]=size(Bit1index);
[MBit0 NBit0]=size(Bit0index);
Bits1=zeros(1,NBit1);
Bits0=zeros(1,NBit0);
Bits1=Bs(Bit1index);
Bits0=Bs(Bit0index);
mu1=mean(Bits1,2)
mu0=mean(Bits0,2)
var1=var(Bits1);
var0=var(Bits0);
STD1=sqrt(var1)
STD0=sqrt(var0)
SQRTofSNR1=mu1/STD1
SQRTofSNR0=mu0/STD0
Discriminate=abs(mu1-mu0)/sqrt(var0+var1)
The pdf estimation is performed by the following histogram operations:
% HISTOGRAM OF BITS
W=100;
w=1:W;
maxhist=1.5;
minhist=-.5;
13 % find coefficients to map from the received values to the histogram index
% W=a*maxhist+b, 1=a*minhist+b, a=(W-1)/(maxhist-minhist) b=1-a*minhist
acoef=(W-1)/(maxhist-minhist);bcoef=1-acoef*minhist;
h1=zeros(1,W);
for n=1:NBit1
m=floor(acoef*Bits1(n)+bcoef);
if m>0
if m<(W+1)
h1(m)=h1(m)+1;
end;
end; % if m>0
end; % for n
h1=h1/NBit1;
h0=zeros(1,W);
for n=1:NBit0
m=floor(acoef*Bits0(n)+bcoef);
if m>0
if m<(W+1)
h0(m)=h0(m)+1;
end;
end; % if m>0
end; % for n
h0=h0/NBit0;
maxhisto=max(h0);
if maxhisto<max(h1)
maxhisto=max(h1);
end;
figure(1);
v=(w-bcoef)/acoef; % make horizontal axis be minhist to maxhist units
plot(v,h0,v,h1);
xlabel('Received Bit value');
ylabel('pdf Estimate');
axis([minhist maxhist 0 maxhisto]);
legend('f(bit0)','f(bit1)');
print -djpeg Bitcheck_figure1
7. REFERENCES
1. Principles of Communications, Systems, Modulation, and Noise by R, E. Ziemer and W.
H. Tranter, 6th Edition.
A. APPENDIX:INSTRUCTORPROGRAMS
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.1BGEN
14 % generate bit matrix based on groupname_Bsize.mat
clear all;
groupname='DSSS' % instructor enters this name to select student project
filename=sprintf('%s_Bsize.mat',groupname);
load (filename) % retrieve data
filename
Nbit
B=rand(1,Nbit); % generate uniformly distributed random sequence
B=binarize(B); % threshold sequence into 0s and 1s
size(B)
filename=sprintf('%s_B.mat',groupname);
save(filename); % save the random bit sequence B
% save the active groupname
save 'activegroup' groupname;
A.2SPREADCODEGENERATION
There is one program, “DSSS_dsssKeygenxx.m” that uses one function “GenDsssKeys.m”
which are shown below:
% generate orthogonalized signal keys for DSSS based on groupname_Bsize.mat
clear all;
Nkeys=2;
groupname='DSSS' % instructor enters this name to select student project
filename=sprintf('%s_Bsize.mat',groupname);
load (filename) % retrieve data
filename
Nbit
N=131072*8 % N is set by instructor and cannot be changed
Nsample=floor(N/Nbit) % number of samples per Key
Ntiles=8; % must be greater than Nkeys
Key=GenDsssKeys(Nkeys,Nsample,Ntiles);
% same the Key
filename=sprintf('%s_Key.mat',groupname);
save(filename,'Key'); % save the random bit sequence B
% display Keys
n=1:Nsample;
figure(1);
plot(n,Key(1,:),n,Key(2,:));
title('DSSS Key signals');
xlabel('Time Index of Keys');
ylabel('Signal value');
legend('Key A','Key B');
print -djpeg dsssKeygen_figure1
% display correlation matrix
R=abs(Key*Key');
Rmax=max(max(R))
Rmin=min(min(R))
figure(2);
imagesc(R);
colormap gray;
title('DSSS Key Correlation Matrix');
xlabel('Key number');
15 ylabel('Key number');
print -djpeg dsssKeygen_figure2
% Generate orthogonalized keys for DSSS
% Key amplitude < 1
function [Keys] = GenDsssKeys(Nkeys,Nsample,Ntiles)
%
N=Nsample*Ntiles;
w=2*rand(1,N);
N2=N/Nsample;
R=ones(N2,N2);
Rmask=ones(N2,N2); % indicate non-usable pairs
for m=1:N2;
for n=m:N2;
km0=1+(m-1)*Nsample;
km1=m*Nsample;
kn0=1+(n-1)*Nsample;
kn1=n*Nsample;
A=w(km0:km1);
A=A-(sum(A)/Nsample);
A=A/sqrt(A*A');
B=w(kn0:kn1);
B=B-(sum(B)/Nsample);
B=B/sqrt(B*B');
R(m,n)=abs(A*B');
Rmask(m,n)=0;
end; % end of n
end; % end of m
Keys=zeros(Nkeys,Nsample);
% find keys with minimal correlation
if Nkeys>2
% this is not optimized yet
% just grab keys with first key as first tile
% other keys chosen to have minimum correlation with 1st one
Row=1;Col=1;
km0=1+(Row-1)*Nsample;km1=Row*Nsample;
A=w(km0:km1);
A=A-(sum(A)/Nsample);
A=sqrt(Nsample)*A/sqrt(A*A');
Keys(1,1:Nsample)=A;
for nkey=2:Nkeys;
Col=nkey;
kn0=1+(Col-1)*Nsample;kn1=Col*Nsample;
B=w(kn0:kn1);
B=B-(sum(B)/Nsample);
B=sqrt(Nsample)*B/sqrt(B*B');
Keys(nkey,1:Nsample)=B;
end; % for nkeys
else
% first and second keys with minimum correlation
J=find(R==min(min(R)));
% J = m * N2 + n
Col=floor((J(1)-1)/N2);
Row=floor((((J(1)-1)/N2)-Col)*N2);
Row=Row+1; % convert to Matlab index
Col=Col+1; % convert to Matlab index
km0=1+(Row-1)*Nsample;km1=Row*Nsample;
16 A=w(km0:km1);
A=A-(sum(A)/Nsample);
%
A=sqrt(Nsample)*A/sqrt(A*A');
Keys(1,1:Nsample)=A;
kn0=1+(Col-1)*Nsample;kn1=Col*Nsample;
B=w(kn0:kn1);
B=B-(sum(B)/Nsample);
%
B=sqrt(Nsample)*B/sqrt(B*B');
Keys(2,1:Nsample)=B;
end; % end of Nkeys
end
A.3CHANNEL
% channel function
clear all;
noiseCoef=0.01
% input active group
load 'activegroup' groupname;
groupname
% input groupname_signal.mat
filename=sprintf('%s_signal.mat',groupname);
load(filename);
% make sure s is real
signal=real(s);
[M,N]=size(signal)
if N~= 1048576
'Incorrect vector length, should be 1048576'
end;
%
% form filter
fc=N/4;
Norder=6;
fmax=N/2;
n=1:N;
K=1; % filter gain
% low pass filter
[f HLP]=lp_butterworth_oN_dft(fc,K,fmax,N,Norder);
% filter signal through channel
S=fft(signal);
H=HLP;
R=S.*H;
sn=real(ifft(R));
k=n;k=k-N/2;
figure(1);
plot(k,log(abs(fftshift(S))+.1));
xlabel('Log Magnitude Spectrum of Input Signal');
print -djpeg Channel_figure1
figure(2);
plot(k,abs(fftshift(H)));
axis([k(1),k(N),-.1, 1.1]);
xlabel('Spectrum of Channel');
print -djpeg Channel_figure2
figure(3);
plot(k,fftshift(log(abs(R)+.1)));
17 xlabel('Log Spectrum of Output Signal, No Noise');
print -djpeg Channel_figure3
% find noise deviation
sigma=noiseCoef*(max(signal)-min(signal))
% add noise
w=sigma*randn(1,N);
r=sn+w;
% store result in groupname_r.mat
filename=sprintf('%s_r.mat',groupname);
save(filename,'r');
% PLOT spectrum and sample sections of the signal
figure(4);
Nsamplesection=20;
Nsamples=floor(N/Nsamplesection);
if N<Nsamples
plot(n,signal);
axis([1,N,-1.1,1.1]);
xlabel('Input Signal');
else
plot(signal(1:Nsamples));
axis([1,Nsamples,-1.1,1.1]);
xlabel('Sample section of Input Signal');
end;
print -djpeg Channel_figure4
figure(5);
if N<Nsamples
plot(n,sn);
axis([1,N,-1.1,1.1]);
xlabel('Output Signal, No Noise');
else
plot(sn(1:Nsamples));
axis([1,Nsamples,-1.1,1.1]);
xlabel('Sample section of Output Signal, No Noise');
end;
print -djpeg Channel_figure5
figure(6);
if N<Nsamples
plot(n,r);
axis([1,N,-1.1,1.1]);
xlabel('Output Signal');
else
plot(r(1:Nsamples));
axis([1,Nsamples,-1.1,1.1]);
xlabel('Sample section of Output Signal with Noise');
end;
print -djpeg Channel_figure6
A.4BITCHECK
% bit check
clear all;
% input active group
load 'activegroup' groupname;
groupname
18 % input original size
filename=sprintf('%s_Bsize.mat',groupname);
load (filename) % retrieve matrix size
filename
Nbitb4=Nbit
% input original bit matrix
filename=sprintf('%s_B.mat',groupname);
load (filename);
% load bitcheck
filename=sprintf('%s_Bcheck.mat',groupname);
load (filename);
% load received signal
filename=sprintf('%s_Bs.mat',groupname);
load (filename);
% check for consistancy
[Nseqnow,N]=size(Bs);
N
if Nseqnow~=1
'ERROR:bitcheck matrices inconsistant'
else
'OK: bitcheck matrices consistant'
end;
% plot
k=1:N;
f=1;
figure(f+1);
bs1(1:N)=Bs(f,1:N);
bcheck1(1:N)=Bcheck(f,1:N);
k1=1:128;
bs1temp(1:128)=bs1(1:128);
bcheck1temp(1:128)=bcheck1(1:128);
plot(k1,bs1temp,k1,bcheck1temp);
axis([1 128 -0.1 1.1]);
xlabel('first few samples of signal');
% loop through bits
Btest=zeros(1,Nbit);
miss=0;
false=0;
Nerror=0;
nbreceived=0;
m=1;
nb=1;
for n=1:N
if Bcheck(m,n) > 0.5 % "1" should be present in check signal
if Bs(m,n)>0.5
Btest(m,nb)=1;
else
'ERROR, missing 1'
'Bcheck'
Bcheck(m,n)
'Bs'
Bs(m,n)
m
n
nb
if Nerror<10
figure(2+1+Nerror);
19 istart=n-(2*N/Nbit);
istop=n+(2*N/Nbit);
if istart<1
istart=1
end;
if istop>N
istop=N
end;
clear x;
x=1:(1+istop-istart);
btemp=x;
bchecktemp=x;
btemp(1:(1+istop-istart))=Bs(m,istart:istop);
bchecktemp(1:(1+istop-istart))=Bcheck(m,istart:istop);
plot(x,btemp,x,bchecktemp);
%clear x,btemp,bchecktemp;
end;
miss=miss+1;
Nerror=Nerror+1;
end;
nb=nb+1;
nbreceived=nbreceived+1;
end;
if Bcheck(m,n) < -0.5 % "-1" should be present in check signal
if Bs(m,n) < 0.5 % "0" is present demodulated/binarized signal
Btest(m,nb)=0;
else
'ERROR, missing 0'
'Bcheck'
Bcheck(m,n)
'Bs'
Bs(m,n)
m
n
nb
if Nerror<10
figure(2+1+Nerror);
istart=n-(2*N/Nbit);
istop=n+(2*N/Nbit);
if istart<1
istart=1
end;
if istop>N
istop=N
end;
clear x;
x=1:(1+istop-istart);
btemp=x;
bchecktemp=btemp;
btemp(1:(1+istop-istart))=Bs(m,istart:istop);
bchecktemp(1:(1+istop-istart))=Bcheck(m,istart:istop);
istart
istop
size(x)
size(btemp)
size(bchecktemp)
plot(x,btemp,x,bchecktemp);
20 end;
false=false+1;
Nerror=Nerror+1;
end;
nb=nb+1;
nbreceived=nbreceived+1;
end;
end;
nbsent=Nbit
nbreceived
miss
false
Nerror
if nbsent~=nbreceived
'Error between sent and recieved'
'Number of ones and zeros sent'
Nones=sum(sum(B))
Nzeros=nbsent-Nones
end;
% STATISTICAL ANALYSIS OF BINARY SIGNAL
Bit1index=find(Bcheck>0.5);
Bit0index=find(Bcheck<-0.5);
[MBit1 NBit1]=size(Bit1index);
[MBit0 NBit0]=size(Bit0index);
Bits1=zeros(1,NBit1);
Bits0=zeros(1,NBit0);
Bits1=Bs(Bit1index);
Bits0=Bs(Bit0index);
mu1=mean(Bits1,2)
mu0=mean(Bits0,2)
var1=var(Bits1);
var0=var(Bits0);
STD1=sqrt(var1)
STD0=sqrt(var0)
SQRTofSNR1=mu1/STD1
SQRTofSNR0=mu0/STD0
Discriminate=abs(mu1-mu0)/sqrt(var0+var1)
% HISTOGRAM OF BITS
W=100;
w=1:W;
maxhist=1.5;
minhist=-.5;
% find coefficients to map from the received values to the histogram index
% W=a*maxhist+b, 1=a*minhist+b, a=(W-1)/(maxhist-minhist) b=1-a*minhist
acoef=(W-1)/(maxhist-minhist);bcoef=1-acoef*minhist;
h1=zeros(1,W);
for n=1:NBit1
m=floor(acoef*Bits1(n)+bcoef);
if m>0
if m<(W+1)
h1(m)=h1(m)+1;
end;
end; % if m>0
end; % for n
h1=h1/NBit1;
h0=zeros(1,W);
for n=1:NBit0
21 m=floor(acoef*Bits0(n)+bcoef);
if m>0
if m<(W+1)
h0(m)=h0(m)+1;
end;
end; % if m>0
end; % for n
h0=h0/NBit0;
maxhisto=max(h0);
if maxhisto<max(h1)
maxhisto=max(h1);
end;
figure(1);
v=(w-bcoef)/acoef; % make horizontal axis be minhist to maxhist units
plot(v,h0,v,h1);
xlabel('Received Bit value');
ylabel('pdf Estimate');
axis([minhist maxhist 0 maxhisto]);
legend('f(bit0)','f(bit1)');
print -djpeg Bitcheck_figure1
22 
Download