HDB3 编码部分
%将下面的代码保存在 hdb3.m 文件中
%对一个二进制序列进行 hdb3 编码运算
%y=hdb3(x)
%x 为原始序列
function y=hdb3(x)
n=length(x);
last_V=-1;
last_one=-1;
y=zeros(size(x)); %初始化输出序列
count=0; %连 0 计数器
for i=1:n
if x(i)==1 %遇 1 则极性反转
y(i)=-last_one;
last_one=y(i);
count=0;
else
count=count+1;
if count==4
count=0;
y(i)=-last_V; %遇 4 连零则置为 V,相邻的 V 极性反转
last_V=y(i);
if y(i)*last_one==-1 %V 与前一个非 0 符号必须同极性
y(i-3)=y(i); %否则置 B
end
last_one=y(i);
end
end
end
figure(1);
subplot(2,1,1); %分区绘制子图函数
a=x;
i=0:n-1;
stairs(i,a); %画二维阶梯图
axis([0 ,n,0,2]); %设定当前 X 轴和 Y 轴的范围
title('原码型'); %为当前坐标轴添加标题
xlabel('x'); %为 x 坐标轴命名
ylabel('y'); %为 y 坐标轴命名