?? ddss_matlab.txt
字號:
直接序列擴頻Matlab程序
function [Y]=DSSS(X, mode)
% 完成DSSS調制解調功能
% mode=[1,2]. 1進行調制,2進行解調,未指定時自動完成調制和解調兩個功能。
switch nargin
case 0
X='This is a test.';
Y=DSSS(X);
return
case 1
Y1=DSSS(X, 1);
Y2=DSSS(Y1, 2);
Y=Y2;
return;
case 2
if mode==1%調制
D=ones(1,7);
m_sequence=Msequence(D);
X_length=length(X);
ascii_value=abs(X);
ascii_binary=zeros(X_length,7);
%將數據轉換為ASCII二進制碼
for ii=1:X_length
ascii_binary(ii,:)=Binary(ascii_value(ii));
end
subplot(2,3,1);plot(reshape(ascii_binary,1,X_length*7));title('A:輸入數據');
%擴頻
Sp_expand=zeros(X_length,127*7);
for ii=1:X_length
for jj=1:7
Sp_expand(ii,127*jj-126:127*jj)=xor(m_sequence,ascii_binary(ii,jj));
end
end
subplot(2,3,2);plot(reshape(Sp_expand,1,X_length*127*7));title('B:數據擴展');
%將擴頻碼轉換為BPSK(1,-1)序列
for ii=1:X_length
for jj=1:127*7
if~(Sp_expand(ii,jj))
Sp_expand(ii,jj)=-1;
end
end
end
Sp_expand_bpsk=reshape(Sp_expand,1,X_length*127*7);
subplot(2,3,3);plot(Sp_expand_bpsk);title('C:BPSK調制')
Y=Sp_expand_bpsk;
elseif mode==2%解調
D=ones(1,7);
m_sequence=Msequence(D);
%將BPSK雙極性轉換為單極性
l=length(X)/(127*7);
X_length=length(X);
for ii=1:X_length
if X(ii)==-1
X(ii)=0;
end
end
Sp_expand=reshape(X,l,127*7);
subplot(2,3,4);plot(X);title('D:數據傳輸');
ascii_binary=zeros(l,7);
Demodulate_binary=zeros(l,127*7);
%接收處解調
for ii=1:l
for jj=1:7
Demodulate(ii,127*jj-126:127*jj)=xor(m_sequence,
Sp_expand(ii,127*jj-126:127*jj));
end
end
for ii=1:l
for jj=1:7
ascii_binary(ii,jj)=Demodulate(ii,127*jj-126);
end
end
subplot(2,3,6);plot(reshape(ascii_binary,1,l*7));title('E:數據輸出');
%將ASCII二進制轉換為輸出數據
A=zeros(1,l);
for ii=1:l
A(ii)=Ascii(ascii_binary(ii,:));
end
Y=char(A);
else
mode=1;
end
return
end
%代碼主體,執行中畫出各點波形。
%ASCII數值二進制比特轉換
function [YY]=Binary(Z1)
z=zeros(1,7);
z(1)=mod(Z1,2);
a=floor(Z1/2);
for ll=1:6
z(ll+1)=mod(a,2);
a=floor(a/2);
if a==0
break;
end
end
YY=z;
%二進制比特轉換為ASCII數值
function [ZZ]=Ascii(Z2)
l=length(Z2);
A=0;
for ii=1:l
A=Z2(ii)*2^(ii-1)+A;
end
ZZ=A;
%生成m序列
function [Y]=Msequence(X)
switch nargin
case 0
Y=Msequence(X);
return
case 1
l=length(X);
mp_register=X;
out_sequence=zeros(1,2^l-1);
sum_xor=0;
for ii=1:2^l-1
out_sequence(ii)=mp_register(l);
sum_xor=xor(mp_register(l),mp_register(l-1));
for jj=1:l-1
mp_register(l-jj+1)=mp_register(l-jj);
end
mp_register(1)=sum_xor;
if mp_register==X
break;
end
end
Y=out_sequence;
return
end
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -