编写matlab程序,绘制以下四种数字基带信号的波形:单极性(非)归零码,双极性(非)归零码。
clc;
clear all;
M=10; %码元数
T = 1; %码元时间间隔
bits = randi([0,1],1,M);%产生随机信号
N=128; % 单个码元抽样点数
dt = T / N; % 抽样时间间隔
t = 0 : dt : (M * N - 1) * dt; % 序列传输时间
%%Unipolar RZ
sig2 = [];
for i = 1:length(bits)
if bits(i) == 1
sig2 = [sig2 ones(1,N)];
sig2 = [sig2 zeros(1,N)];
else
sig2 = [sig2 zeros(1,N)];
sig2 = [sig2 zeros(1,N)];
end
end
% 生成时间轴
t1 = linspace(0, length(bits)*T, length(sig2));
% 画出波形图
figure(1)
plot(t1, sig2);
xlabel('时间');
ylabel('幅值');
title('单极性归零码波形图');
%%Unipolar NRZ
sig1 = [];
for i = 1:length(bits)
if bits(i) == 1
sig1 = [sig1 ones(1,N)];
else
sig1 = [sig1 -ones(1,N)];
end
end
% 生成时间轴
t2 = linspace(0, length(bits)*T, length(sig1));
% 画出波形图
figure(2)
plot(t2, sig1);
xlabel('时间');
ylabel('幅值');
title('单极性非归零码波形图');
%%DPNRZ
gt1 = ones(1, N);
sig3 = [];
for i = 1 : M % 生成序列
if bits(i)==1
sig3 = [sig3 gt1];
else
sig3 = [sig3 -1*gt1];
end
end
figure(3);
plot(t, sig3);
xlabel('时间');
ylabel('幅值');
title('双极性非归零码波形图');
%%DPRZ
gt2 = [ones(1, N / 2), zeros(1, N / 2)];
sig4 = [];
for i = 1 : M % 生成序列
if bits(i)==1
sig4 = [sig4 gt2];
else
sig4 = [sig4 -1*gt2];
end
end
figure(4);
plot(t, sig4);
xlabel('时间');
ylabel('幅值');
title('双极性归零码波形图');