10.6.1 RC4 Encryption %Initialize the secret key MyKey(1)=164; MyKey(2)=4; MyKey(3)=24; MyKey(4)=107; MyKey(5)=251; MyKey(6)=66; MyKey(7)=186; MyKey(8)=95; %Initialize data data=round(rand(1,300)*256); %Create an array of size 256 filled with numbers from 0 to 255. for i=1:256 S(i)=i-1; end % Create another array of size 256 filled with repetitions of the key. for i=0:255 K(i+1)=MyKey(mod(i,length(MyKey))+1); end % Initialize the order of S according to the secret key. j=0; for i=0:255 j=mod(j+S(i+1)+K(i+1),256); temp=S(i+1); S(i+1)=S(j+1); S(j+1)=temp; end % The encrypted data is generated one byte at a time. i=0; j=0; for k=1: length(data) i=mod(i+1, 256); j=mod(j+S(i+1), 256); temp=S(i+1); S(i+1)=S(j+1); S(j+1)=temp; t=mod(S(i+1)+S(j+1),256); PS=S(t+1); edata(k)=bitxor(data(k),PS); end 10.6.2 Gaussian Filter Coefficients % Obtain normalized Gaussian filter coefficients f3dbT=0.5; T=1/1e6; f3db=0.5/T; alp=0.5887/f3db; dt=1/5e6; k=[-5:5]; hg=exp(-(pi^2/alp^2)*dt^2*k.^2); 10.6.3 Complementary Codewords % Matlab code to produce the 0 phase version of % length 8 complementary code set % Set up data bits matrix db=zeros(64,6); db(2,6)=1; for i=1:2 db(i+2,6)=db(i,6); db(i+2,5)=1; end for i=1:4 db(i+4,5:6)=db(i,5:6); db(i+4,4)=1; end for i=1:8 db(i+8,4:6)=db(i,4:6); db(i+8,3)=1; end for i=1:16 db(i+16,3:6)=db(i,3:6); db(i+16,2)=1; end for i=1:32 db(i+32,2:6)=db(i,2:6); db(i+32,1)=1; end % Set up imaginary number and phase table jj=sqrt(-1); b2p=[0 pi/2 pi 3*pi/2]; % Form codewords for i=1:64 p1=b2p(1+db(i,1)+2*db(i,2)); p2=b2p(1+db(i,3)+2*db(i,4)); p3=b2p(1+db(i,5)+2*db(i,6)); p1a=p1/pi*2; p2a=p2/pi*2; p3a=p3/pi*2; cc(i,1)=exp(jj*(p1+p2+p3)); cc(i,2)=exp(jj*(p2+p3)); cc(i,3)=exp(jj*(p1+p3)); cc(i,4)=-exp(jj*(p3)); cc(i,5)=exp(jj*(p1+p2)); cc(i,6)=exp(jj*(p2)); cc(i,7)=-exp(jj*(p1)); cc(i,8)=1; ca(i,1)=mod(p1a+p2a+p3a,4); ca(i,2)=mod(p2a+p3a,4); ca(i,3)=mod(p1a+p3a,4); ca(i,4)=mod(p3a+2,4); ca(i,5)=mod(p1a+p2a,4); ca(i,6)=mod(p2a,4); ca(i,7)=mod(p1a+2,4); ca(i,8)=0; end 10.6.4 A Multipath Channel Model % Set up sampling time and delay spread TS=1/11e6; TRMS=200e-9; n=ceil(10*TRMS/TS); jj=sqrt(-1); % Set up channel model delt0=1-exp(-TS/TRMS); hk(1)=(randn(1)+jj*randn(1))*sqrt(delt0/2); for i=1:n delt(i)=delt0*exp(-i*TS/TRMS); hk(i+1)=(randn(1)+jj*randn(1))*sqrt(delt(i)/2); end plot([1:n+1],real(hk),[1:n+1],imag(hk),[1:n+1],abs(hk)) xlabel('Sample Index') ylabel('Amplitude') grid gtext('Magnitude') gtext('Real') gtext('Imaginary') 10.6.5 Fast Walsh Transform % Fast Walsh Transform % FWT size n=8; % Create a test data vector H1=1; H2=[H1 H1; H1 -H1]; H4=[H2 H2; H2 -H2]; H8=[H4 H4; H4 -H4]; x=H8(5,:); % Carry out log2(n) stages of % n/2 additions and subtractions for i=1:log2(n) blk=2^(3-i); dis=2^(i-1); for j=0:2*dis:2*dis*(blk-1) for k=1:dis y(j+k)=x(j+k)+x(j+k+dis); y(j+k+dis)=x(j+k)-x(j+k+dis); end end x=y; end 10.6.6 Modified Fast Walsh Transform % Fast Walsh Transform for quadrature phase % complementary code words jj=sqrt(-1); for k=1:64 for i=1:4 x2((i-1)*4+1)=cc(k,8-2*(i-1))+cc(k,7-2*(i-1)); x2((i-1)*4+2)=cc(k,8-2*(i-1))+jj*cc(k,7-2*(i-1)); x2((i-1)*4+3)=cc(k,8-2*(i-1))-cc(k,7-2*(i-1)); x2((i-1)+4+4)=cc(k,8-2*(i-1))-jj*cc(k,7-2*(i-1)); end for i=1:2 for j=1:4 x3((i-1)*16+j)=x2((i-1)*8+j)+x2((i-1)*8+j+4); x3((i-1)*16+j+4)=x2((i-1)*8+j)+jj*x2((i-1)*8+j+4); x3((i-1)*16+j+8)=x2((i-1)*8+j)-x2((i-1)*8+j+4); x3((i-1)*16+j+12)=x2((i-1)*8+j)-jj*x2((i-1)*8+j+4); end end for i=1:16 y(i)=x3(i)+x3(i+16); y(i+16)=x3(i)+jj*x3(i+16); y(i+32)=x3(i)-x3(i+16); y(i+48)=x3(i)-jj*x3(i+16); end end |