close all,clear,clc;
%实带通仿真
for en = -50:1:20
Eb_N0 = en; %信噪比,以dB表示
%Eb_N0 = 15; %信噪比,以dB表示
p = 0.5; %信号中取0的概率
M = 256; %M进制QAM
num = 200; %产生num*log2(M)个信号,必须为2的倍数
data_in = rand(1,num*log2(M)); %伪随机数法产生2进制信号
data_in = double(data_in>p);
%串并转换
data_Iin = zeros(1,num); %用于记录I路成型脉冲的幅度
data_Qin = zeros(1,num); %用于记录Q路成型脉冲的幅度
data_I = zeros(1/2*log2(M),num); %用于记录I路发出的信息
data_Q = zeros(1/2*log2(M),num); %用于记录Q路发出的信息
for n = 1:log2(M):length(data_in)-log2(M)+1
data_I(:,(n-1)/log2(M)+1) = data_in(n:n+1/2*log2(M)-1)';
count = d2gray(data_in(n:n+1/2*log2(M)-1));
data_Iin(1,(n-1)/log2(M)+1) = -(sqrt(M)-1)/2+count;
data_Q(:,(n-1)/log2(M)+1) = data_in(n+1/2*log2(M):n+log2(M)-1)';
count = d2gray(data_in(n+1/2*log2(M):n+log2(M)-1));
data_Qin(1,(n-1)/log2(M)+1) = -(sqrt(M)-1)/2+count;
end
%发射端成型
shape = rcosfir(0.5,20,20,1,'sqrt'); %根升余弦滤波器
data_IIin = zeros(1,20*length(data_Iin));
for n = 1:length(data_Iin)
data_IIin(1,(n-1)*20+1) = data_Iin(1,n);
end
data_QQin = zeros(1,20*length(data_Qin));
for n = 1:length(data_Qin)
data_QQin(1,(n-1)*20+1) = data_Qin(1,n);
end
data_Iin_shape = conv(data_IIin,shape); %I路成型
data_Qin_shape = conv(data_QQin,shape); %Q路成型
%发射端调制
t = 0:1:length(data_Iin_shape)-1;
CI = cos(t*pi/2);
CQ = sin(t*pi/2);
transmit = CI.*data_Iin_shape - CQ.*data_Qin_shape;
%figure
%subplot(2,1,1);
%plot(transmit((length(shape)+1)/2:(length(shape)+1)/2+99)); %显示的时候去掉延时
%title('transmit wave','fontsize',20);
%加入AWGN
Es = (M-1)/12*sum(shape.^2); %计算码元平均能量
Eb = Es/(log2(M)); %计算比特平均能量
Eb_N0 = 10^(Eb_N0/10);
N0 = Eb/Eb_N0;
noise = normrnd(0,sqrt(N0/2),1,length(transmit)); %生成正态分布的噪声
transmit = transmit + noise; %加噪声
%subplot(2,1,2);
%plot(transmit((length(shape)+1)/2:(length(shape)+1)/2+99)); %显示的时候去掉延时
%title('transmit wave(with noise)','fontsize',20);
%接收端解调与匹配
receiveIRF = transmit.*CI; %I路解调
receive_Ifilter = 2*conv(shape,receiveIRF); %I路匹配
receive_Ifilter = receive_Ifilter((length(shape))/1:(length(shape))/1+length(data_IIin)-1); %去掉延时
%{
figure
subplot(2,1,1);
plot(receiveIRF((length(shape)+1)/2:(length(shape)+1)/2+99));
title('接收端I路射频波形','fontsize',20);
subplot(2,1,2);
plot(receive_Ifilter(1:100));
title('接收端I路匹配波形','fontsize',20);
%}
receiveQRF = -transmit.*CQ; %Q路解调
receive_Qfilter = 2*conv(shape,receiveQRF); %Q路匹配
receive_Qfilter = receive_Qfilter((length(shape))/1:(length(shape))/1+length(data_QQin)-1); %去掉延时
%{
figure
subplot(2,1,1);
plot(receiveQRF((length(shape)+1)/2:(length(shape)+1)/2+99));
title('接收端Q路射频波形','fontsize',20);
subplot(2,1,2);
plot(receive_Qfilter(1:100));
title('接收端Q路匹配波形','fontsize',20);
%}
%匹配信号判决
receiveI = zeros(1,length(data_Iin));
for n = 1:length(receiveI) %取出最佳采样点的信号
receiveI(1,n) = receive_Ifilter(1,(n-1)*20+1);
end
receiveII = zeros(1/2*log2(M),length(data_Iin));
for n = 1:length(receiveI)
count = 0; %计算最佳采样点的格雷码次序
temp = sqrt(M);
if receiveI(n)>=sqrt(M)/2-1
count = temp;
else
temp = temp - 1;
for m = sqrt(M)/2-1:-1:-(sqrt(M)/2-2)
if receiveI(n)<m && receiveI(n)>=m-1
count = temp;
break;
end
temp = temp - 1;
end
end
if count == 0
count = 1;
end
count = count - 1;
receiveII(:,n) = d2b(count,receiveII(:,n)); %把最佳采样点的电平值转换为二进制信号
end
receiveQ = zeros(1,length(data_Qin));
for n = 1:length(receiveQ) %取出最佳采样点的信号
receiveQ(1,n) = receive_Qfilter(1,(n-1)*20+1);
end
receiveQQ = zeros(1/2*log2(M),length(data_Qin));
for n = 1:length(receiveQ)
count = 0; %计算最佳采样点的格雷码次序
temp = sqrt(M);
if receiveQ(n)>=sqrt(M)/2-1
count = temp;
else
temp = temp - 1;
for m = sqrt(M)/2-1:-1:-(sqrt(M)/2-2)
if receiveQ(n)<m && receiveQ(n)>=m-1
count = temp;
break;
end
temp = temp - 1;
end
end
if count == 0
count = 1;
end
count = count - 1;
receiveQQ(:,n) = d2b(count,receiveQQ(:,n)); %把最佳采样点的电平值转换为二进制信号
end
%串并转换
receive = zeros(1,length(data_in));
for n = 1:log2(M):length(data_in)-log2(M)+1
receive(n:n+1/2*log2(M)-1) = receiveII(:,(n-1)/log2(M)+1)';
receive(n+1/2*log2(M):n+log2(M)-1) = receiveQQ(:,(n-1)/log2(M)+1)';
end
%data_in(1:10)
%receive(1:10)
%绘制眼图
%eye_picture(data_Iin_shape(41:length(data_Iin_shape)),20);
%title('发射端','fontsize',20);
%eye_picture(receive_Ifilter(41:length(receive_Ifilter)),20);
%title('接收端','fontsize',20);
%计算误符号率和误比特率
%
BERI = data_I~=receiveII;
BERQ = data_Q~=receiveQQ;
ber = (sum(sum(BERI))+sum(sum(BERQ)))/length(data_in);%计算误比特率
if M > 4
SER = sum(BERI) + sum(BERQ);
else
SER = BERI + BERQ;
end
SER = SER > 0;
ser = sum(SER)/length(SER);%计算误符号率
plot(en,ser,'b.');
hold on;
plot(en,ber,'r.');
hold on;
end
%理论计算误符号率和误比特率
en_theory_db = -50:1:20;
en_theory = 10.^(en_theory_db/10);
PsI = zeros(1,length(en_theory));
for n = 1:length(en_theory)
PsI(n) = 2*(sqrt(M)-1)/sqrt(M)/2*erfc(sqrt(3*log2(M)/(M-1)/2*en_theory(n)));
end
Ps = 2*PsI-PsI.^2; %计算误符号率
hold on;
plot(en_theory_db,Ps,'b','LineWidth',2);
Pb = zeros(1,length(en_theory)); %计算误比特率
for n = 1:length(en_theory)
Pb(n) = 1/log2(sqrt(M))*PsI(n);
end
hold on;
plot(en_theory_db,Pb,'r','LineWidth',2);
legend('PS','PB');
%}
- 1
- 2
前往页