fs = 1e6; dt = 1/fs; t = 0:dt:0.01-dt; fc= 32e3; carrier = sin(2*pi*fc*t); SRate = 2e3; SWidth = fs/SRate; N=length(t)/SWidth; PNCode = round(rand(1,N)); for i=0:N- 1 if(PNCode(i+1)==1) PNWave(i*SWidth+1:(i+1)*SWidth)=ones(1,SWidth); else PNWave(i*SWidth+1:(i+1)*SWidth)=ones(1,SWidth)*(- 1); end end BPSK = PNWave.*carrier; %%%++++++++++++++产生 m 序列++++++++++++++++%%% n=7; %阶数 n Connection = [3 7]; Initialstate=[1 1 1 0 1 1 0]; num=1; out = zeros(num,2^n- 1); pos = zeros(n,1); pos(Connection) = 1; for ii=1:2^n- 1 out(1,ii) = Initialstate(n); temp = mod(Initialstate*pos,2); Initialstate(2:n) = Initialstate(1:n- 1); Initialstate(1) = temp; end %%%++++++++++++++产生 m 序列脉冲++++++++++++++++%%% SRatem=1e4; SWidth1 = fs/SRatem; N1=length(t)/SWidth1; for i=0:N1- 1 if(out(1,i+1)==1) PN(i*SWidth1+1:(i+1)*SWidth1)=ones(1,SWidth1); else PN(i*SWidth1+1:(i+1)*SWidth1)=ones(1,SWidth1)*(- 1); end end %%%++++++++++++++扩频通信++++++++++++++++%%% DS_BPSK=BPSK.*PN; %%%++++++++++++++解扩++++++++++++++++%%% BPSK1 = DS_BPSK.*PN; %%%++++++++++++++解调++++++++++++++++%%% seq = BPSK1.*carrier; fp1 = 2e3+10; %通带截止频率 fs1 = 4e3; %阻带截止频率 Ws=(fp1+fs1)/fs; M=250; %截止频率归一化处理[(fp+fs)/2]/(fs/2),处理信号最高频率上限为 fs/2 %计算所需滤波器的阶数 hanming=hamming(M+1); LPF=fir1(M,Ws,hanming); BPSK_De=filter(LPF,1,seq); %生成长度为 M+1 的汉明窗窗 %生成汉明窗设计的fir 滤波器 %用滤波器对信号进行滤波 LenPlot = 2000; figure; subplot(4,1,1); plot(PNWave(1:LenPlot),'linewidth',3); title('双极性不归零随机序列'); subplot(4,1,2); plot(BPSK(1:LenPlot)); title('二进制绝对相移键控'); subplot(4,1,3); plot( PN(1:LenPlot)); title('m 序列脉冲'); subplot(4,1,4); plot(DS_BPSK(1:LenPlot)); title('直接序列扩频波形'); figure; subplot(4,1,1); plot(PNWave(1:LenPlot),'linewidth',3);title('双极性不归零随机序列'); subplot(4,1,2); plot(BPSK1(1:LenPlot));title('解扩信号'); subplot(4,1,3); plot(seq(1:LenPlot));title('乘法器-解调信号'); subplot(4,1,4); plot(BPSK_De(1:LenPlot));title('解调信号');逐句注释一下这部分代码
时间: 2023-09-06 07:11:29 浏览: 34
这段代码实现了一个基于直接序列扩频技术的通信系统。下面逐句进行注释:
```matlab
fs = 1e6; dt = 1/fs; t = 0:dt:0.01-dt; % 设定采样率和时刻向量
fc= 32e3; carrier = sin(2*pi*fc*t); % 产生载波信号
SRate = 2e3; SWidth = fs/SRate; N=length(t)/SWidth; % 设定码片速率和码片宽度
PNCode = round(rand(1,N)); % 随机产生伪随机码
% 根据伪随机码产生相应的码片序列
for i=0:N-1
if(PNCode(i+1)==1)
PNWave(i*SWidth+1:(i+1)*SWidth)=ones(1,SWidth);
else
PNWave(i*SWidth+1:(i+1)*SWidth)=ones(1,SWidth)*(-1);
end
end
BPSK = PNWave.*carrier; % 产生二进制相移键控信号
% 产生 m 序列
n=7; % 设定阶数
Connection = [3 7]; Initialstate=[1 1 1 0 1 1 0]; num=1; out = zeros(num,2^n-1); pos = zeros(n,1); pos(Connection) = 1;
for ii=1:2^n-1
out(1,ii) = Initialstate(n);
temp = mod(Initialstate*pos,2);
Initialstate(2:n) = Initialstate(1:n-1);
Initialstate(1) = temp;
end
% 根据 m 序列产生相应的码片脉冲序列
SRatem=1e4; SWidth1 = fs/SRatem; N1=length(t)/SWidth1;
for i=0:N1-1
if(out(1,i+1)==1)
PN(i*SWidth1+1:(i+1)*SWidth1)=ones(1,SWidth1);
else
PN(i*SWidth1+1:(i+1)*SWidth1)=ones(1,SWidth1)*(-1);
end
end
DS_BPSK=BPSK.*PN; % 直接序列扩频
BPSK1 = DS_BPSK.*PN; % 解扩
% 解调
seq = BPSK1.*carrier;
fp1 = 2e3+10; fs1 = 4e3; Ws=(fp1+fs1)/fs; M=250; hanming=hamming(M+1); LPF=fir1(M,Ws,hanming); BPSK_De=filter(LPF,1,seq);
% 绘制波形图
LenPlot = 2000;
figure;
subplot(4,1,1); plot(PNWave(1:LenPlot),'linewidth',3); title('双极性不归零随机序列');
subplot(4,1,2); plot(BPSK(1:LenPlot)); title('二进制相移键控');
subplot(4,1,3); plot(PN(1:LenPlot)); title('m序列脉冲');
subplot(4,1,4); plot(DS_BPSK(1:LenPlot)); title('直接序列扩频波形');
figure;
subplot(4,1,1); plot(PNWave(1:LenPlot),'linewidth',3); title('双极性不归零随机序列');
subplot(4,1,2); plot(BPSK1(1:LenPlot)); title('解扩信号');
subplot(4,1,3); plot(seq(1:LenPlot)); title('乘法器-解调信号');
subplot(4,1,4); plot(BPSK_De(1:LenPlot)); title('解调信号');
```
其中,PNCode 是伪随机码序列,PNWave 是相应的码片序列,DS_BPSK 是直接序列扩频的波形,BPSK1 是解扩后的信号,seq 是乘法器-解调信号,BPSK_De 是最终的解调信号。在绘制波形图时,对原始信号和处理后的信号进行了展示。