3.1 瑞利信道非相干检测 和 相干检测 38页

%非相干检测,bpsk 
clc; clear; close all;
N_symbol = 100000;
A = 2;

EbN0_dB = -20:40;
EbN0 = 10.^(EbN0_dB./10);
N0 = (A.^2)./2./EbN0; %这里除2是因为每两个时刻,发一个A

sig_original = randi([0,1], 1, N_symbol);%产生要发送的原始信号,0 1序列

sig_transmit = zeros(1, 2*N_symbol);
for i = 1:N_symbol
    if sig_original(i) == 0 
        sig_transmit(2*i-1: 2*i) = [A 0];
    else
        sig_transmit(2*i-1: 2*i) = [0 A];
    end
end

for i = 1:length(EbN0)
    sigma = sqrt( N0(i)/2 );
    noise = sigma*randn(1, length(sig_transmit))+ sigma*1j*randn(1, length(sig_transmit));
    h = sqrt(1/2)*randn(1, length(sig_transmit))+ sqrt(1/2)*1j*randn(1, length(sig_transmit));
    
    sig_receive = sig_transmit.*h + noise;
    %这里计算每个接收信号的能量,要用x.*conj(x),而不能是x.*x,因为是复数,直接平方并不是模值
    sig_receive_energy = sig_receive .* conj(sig_receive);
    sig_demod = zeros(1, N_symbol);
    for k = 1:N_symbol
        sig_demod(k) = sig_receive_energy(2*k-1) < sig_receive_energy(2*k);
    end
    
     err_simulation(i) = sum(sig_demod~=sig_original)/N_symbol; % 这里不要忘了除N_symbol
     err_theory(i) = 1/2./(1+ EbN0(i));
end  
figure;
semilogy(EbN0_dB, err_simulation,'o', EbN0_dB, err_theory);
axis([-20,40,10^-16,1]);

% bpsk通过瑞利信道,相干检测(知道信道信息)
clc; clear; close all;
N_symbol = 100000;
A = 2;

sig_original = randi([0, 1], 1, N_symbol);
sig_mod = (sig_original.*2 - 1).*A;

EbN0_dB = -20:40;
EbN0 = 10.^(EbN0_dB./10);
N0 = (A.^2)./EbN0;

for i = 1:length(EbN0_dB)
    sigma = sqrt(N0(i)/2);
    noise = sigma*randn(1, N_symbol) + sigma*1j*randn(1, N_symbol);
    h = sqrt(1/2)*randn(1, N_symbol) + sqrt(1/2)*1j*randn(1, N_symbol);
    
    sig_receive_awgn = sig_mod + noise;
    sig_decide = real(sig_receive_awgn) >= 0;
    sig_demod_awgn = (sig_decide*2 - 1)*A;
    
    sig_receive_rayleigh = sig_mod.*h + noise;
     
    % 计算充分统计量 注意:conj共轭;' 共轭转置; .'转置
    % 当h为复数时,h的平方不等于其模的平方,h乘h的共轭才是模的平方
    
    % SS = h./abs(h) .*sig_receive_rayleigh ;错误 
    SS = conj(h)./abs(h) .* sig_receive_rayleigh; % 别忘了取共轭
    % SS= abs(h).*sig_mod + randn(1,N_symbol)*sigma; 正确,是上面式子化简后的结果
    
    sig_demod_rayleigh = ((real(SS) >= 0)*2 - 1)*A;
    err_simulation(i) = sum(sig_demod_rayleigh~=sig_mod)/N_symbol; % 这里不要忘了除N_symbol
    err_simulation2(i) = sum(sig_demod_awgn~=sig_mod)/N_symbol; % 这里不要忘了除N_symbol
end 

figure;
semilogy(EbN0_dB, err_simulation, EbN0_dB, err_simulation2);
axis([-20,40,10^-16,1]);


你可能感兴趣的:(无线通信)