简单介绍数字基带传输的线路码型的信号波形的特点,以及生成方法。注意观察频谱。文末附Matlab代码。
以下包括双极性NRZ、单极型NRZ、双极型RZ、单极型RZ、差分码,曼切斯特码/数字双相码、密勒码、CMI码、AMI码、HDB3码。
参数:Rb = 20 bps;fs = 200 Hz;仿真时间2s。
双极性NRZ
单极性NRZ
双极性RZ
单极性RZ
差分码
% ---------------------------------------------
% 作者:wsg
% 日期:2022年12月4日
% 邮箱:shouguo19@163.com
% 主题:基带传输的线路码型的仿真
% 参考:李晓峰《通信原理(第二版)》,第四章第九节。
% ---------------------------------------------
% 基本参数
Rb = 20; % 信息bit速率
t_span = 2; % 仿真时间
fs = 200; % 采样频率
unitBitSa = fs/Rb; % 单位原始信息采样点数
bit_length = Rb*t_span; % 信息序列长度
t = transpose(0:1/fs:(t_span-1/fs)); % 仿真时间轴
N_points = length(t);
f = (1/N_points:1/N_points:1/2)*fs; % 频率轴
m = randi([0 1], bit_length, 1); % 随机产生的bit序列
name_list = ["双极性NRZ","单极性NRZ","双极性 RZ", "单极性 RZ", "差分码",...
"曼切斯特码", "密勒码", "传号反转码CMI", "交替传号反转码AMI","HDB3码"];
%% part1
rz_gt = zeros(unitBitSa,1);
rz_gt(1:unitBitSa/2) = ones(unitBitSa/2, 1); % 归零码生成滤波器
nrz_gt = ones(unitBitSa,1); % 不归零码生成滤波器
% 1. 双极性NRZ
s1 = filter(nrz_gt, 1, upsample(m*2-1, unitBitSa));
% 2. 单极性NRZ
s2 = filter(nrz_gt, 1, upsample(m, unitBitSa));
% 3. 双极性 RZ
s3 = filter(rz_gt, 1, upsample(m*2-1, unitBitSa));
% 4. 单极性 RZ
s4 = filter(rz_gt, 1, upsample(m, unitBitSa));
% 5. 差分码
m5 = zeros(bit_length,1);
tmp = 0; % 初相
for i = 1:bit_length
tmp = xor(tmp,m(i));
m5(i) = tmp;
end
s5 = filter(nrz_gt, 1, upsample(m5, unitBitSa));
%% part2
down_pulse = zeros(unitBitSa,1)-1;
down_pulse(1:unitBitSa/2) = ones(unitBitSa/2, 1);
up_pulse = flipud(down_pulse);
% 6. 曼切斯特码/数字双相码
m_tmp = [m,~m]';
m6 = m_tmp(:);
s6 = filter(ones(unitBitSa/2,1),1,upsample(m6*2-1,unitBitSa/2));
% 7. 密勒码/延迟调制码
bph = [m,~m]';
bph = bph(:);
m7 = zeros(bit_length*2,1);
tmp = 0;
for i = 1:bit_length*2
if isequal([tmp,bph(i)], [1,0])
m7(i) = ~m7(i-1);
else
if i==1
m7(i) = tmp;
else
m7(i) = m7(i-1);
end
end
tmp = bph(i);
end
s7 = filter(ones(unitBitSa/2,1),1,upsample(m7*2-1,unitBitSa/2));
% 8. CMI
s8 = zeros(N_points,1);
flag = 1;
for i = 1:bit_length
if m(i)==1
if flag == 1
s8((1:unitBitSa)+(i-1)*unitBitSa) = 1;
else
s8((1:unitBitSa)+(i-1)*unitBitSa) = -1;
end
flag = ~flag;
else
s8((1:unitBitSa)+(i-1)*unitBitSa) = up_pulse;
end
end
% 9.AMI
s9 = zeros(N_points,1);
flag = 1;
for i = 1:bit_length
if m(i)==1
if flag == 1
s9((1:unitBitSa)+(i-1)*unitBitSa) = 1;
else
s9((1:unitBitSa)+(i-1)*unitBitSa) = -1;
end
flag = ~flag;
else
s9((1:unitBitSa)+(i-1)*unitBitSa) = 0;
end
end
% 10. HDB3
m10 = zeros(bit_length,1);
flagb = 0;
flagv = 1;
for i = 1:bit_length
if m(i)==1
flagb = ~flagb;
if flagb == 1
m10(i) = 1;
else
m10(i) = -1;
end
else
m10(i) = 0;
if i>3 && isequal(m10(i-3:i),zeros(4,1))
if flagv == flagb
if flagv == 1
m10(i) = 1;
else
m10(i) = -1;
end
else
if flagv == 1
m10(i-3:i) = [1;0;0;1];
else
m10(i-3:i) = [-1;0;0;-1];
end
flagb = ~flagb;
end
flagv = ~flagv;
end
end
end
s10 = filter(ones(unitBitSa,1),1,upsample(m10,unitBitSa));
%% 绘图
figure(3)
for i = 1:5
s = eval("s"+num2str(i));
S = abs(fft(s));
subplot(5,2,i*2-1)
plot(t,s);xlabel("t/s");ylim([-1.2 1.2]);title(name_list(i))
subplot(5,2,i*2)
plot(f,S(1:N_points/2));xlabel("f/Hz");
end
figure(2)
for i = 1:5
s = eval("s"+num2str(i+5));
S = abs(fft(s));
subplot(5,2,i*2-1)
plot(t,s);xlabel("t/s");ylim([-1.2 1.2]);title(name_list(i+5))
subplot(5,2,i*2)
plot(f,S(1:N_points/2));xlabel("f/Hz");
end