clear all; close all; clc; tic bits_options = [0,1,2]; noise_option = 1; b = 4; NT = 2; SNRdBs =[0:2:20]; sq05=sqrt(0.5); nobe_target = 500; BER_target = 1e-3; raw_bit_len = 2592-6; interleaving_num = 72; deinterleaving_num = 72; N_frame = 1e8; for i_bits=1:length(bits_options) bits_option=bits_options(i_bits); BER=zeros(size(SNRdBs)); for i_SNR=1:length(SNRdBs) sig_power=NT; SNRdB=SNRdBs(i_SNR); sigma2=sig_power10^(-SNRdB/10)noise_option; sigma1=sqrt(sigma2/2); nobe = 0; Viterbi_init for i_frame=1:1:N_frame switch (bits_option) case {0}, bits=zeros(1,raw_bit_len); case {1}, bits=ones(1,raw_bit_len); case {2}, bits=randi(1,raw_bit_len,[0,1]); end encoding_bits = convolution_encoder(bits); interleaved=[]; for i=1:interleaving_num interleaved=[interleaved encoding_bits([i:interleaving_num:end])]; end temp_bit =[]; for tx_time=1:648 tx_bits=interleaved(1:8); interleaved(1:8)=[]; QAM16_symbol = QAM16_mod(tx_bits, 2); x(1,1) = QAM16_symbol(1); x(2,1) = QAM16_symbol(2); if rem(tx_time-1,81)==0 H = sq05(randn(2,2)+jrandn(2,2)); end y = Hx; if noise_option==1 noise = sqrt(sigma2/2)(randn(2,1)+j*randn(2,1)); y = y + noise; end W = inv(H'H+sigma2diag(ones(1,2)))H'; X_tilde = Wy; X_hat = QAM16_slicer(X_tilde, 2); temp_bit = [temp_bit QAM16_demapper(X_hat, 2)]; end deinterleaved=[]; for i=1:deinterleaving_num deinterleaved=[deinterleaved temp_bit([i:deinterleaving_num:end])]; end received_bit=Viterbi_decode(deinterleaved); for EC_dummy=1:1:raw_bit_len, if bits(EC_dummy)~=received_bit(EC_dummy), nobe=nobe+1; end if nobe>=nobe_target, break; end end if (nobe>=nobe_target) break; end end = BER(i_SNR) = nobe/((i_frame-1)*raw_bit_len+EC_dummy); fprintf('bits_option:%d,SNR:%d dB,BER:%1.4f\n',bits_option,SNRdB,BER(i_SNR)); end figure; semilogy(SNRdBs,BER); xlabel('SNR(dB)'); ylabel('BER'); title(['Bits_option:',num2str(bits_option)]); grid on; end将这段代码改为有噪声的情况
时间: 2024-03-26 07:37:10 浏览: 165
ga_main_gamain_tic函数_遗传算法源码_AllClear_
以下是添加噪声后的修改代码:
clear all; close all; clc;
tic
bits_options = [0,1,2];
noise_option = 1;
b = 4;
NT = 2;
SNRdBs =[0:2:20];
sq05=sqrt(0.5);
nobe_target = 500;
BER_target = 1e-3;
raw_bit_len = 2592-6;
interleaving_num = 72;
deinterleaving_num = 72;
N_frame = 1e8;
for i_bits=1:length(bits_options)
bits_option=bits_options(i_bits);
BER=zeros(size(SNRdBs));
for i_SNR=1:length(SNRdBs)
sig_power=NT;
SNRdB=SNRdBs(i_SNR);
sigma2=sig_power*10^(-SNRdB/10)+noise_option;
sigma1=sqrt(sigma2/2);
nobe = 0;
Viterbi_init
for i_frame=1:1:N_frame
switch (bits_option)
case {0}, bits=zeros(1,raw_bit_len);
case {1}, bits=ones(1,raw_bit_len);
case {2}, bits=randi(1,raw_bit_len,[0,1]);
end
encoding_bits = convolution_encoder(bits);
interleaved=[];
for i=1:interleaving_num
interleaved=[interleaved encoding_bits([i:interleaving_num:end])];
end
temp_bit =[];
for tx_time=1:648
tx_bits=interleaved(1:8);
interleaved(1:8)=[];
QAM16_symbol = QAM16_mod(tx_bits, 2);
x(1,1) = QAM16_symbol(1);
x(2,1) = QAM16_symbol(2);
if rem(tx_time-1,81)==0
H = sq05*(randn(2,2)+j*randn(2,2));
end
y = H*x;
if noise_option==1
noise = sqrt(sigma2/2)*(randn(2,1)+j*randn(2,1));
y = y + noise;
end
W = inv(H'*H+sigma2*diag(ones(1,2)))*H';
X_tilde = W*y;
X_hat = QAM16_slicer(X_tilde, 2);
temp_bit = [temp_bit QAM16_demapper(X_hat, 2)];
end
deinterleaved=[];
for i=1:deinterleaving_num
deinterleaved=[deinterleaved temp_bit([i:deinterleaving_num:end])];
end
received_bit=Viterbi_decode(deinterleaved);
for EC_dummy=1:1:raw_bit_len,
if bits(EC_dummy)~=received_bit(EC_dummy),
nobe=nobe+1;
end
if nobe>=nobe_target,
break;
end
end
if (nobe>=nobe_target)
break;
end
end
BER(i_SNR) = nobe/((i_frame-1)*raw_bit_len+EC_dummy);
fprintf('bits_option:%d,SNR:%d dB,BER:%1.4f\n',bits_option,SNRdB,BER(i_SNR));
end
figure;
semilogy(SNRdBs,BER);
xlabel('SNR(dB)');
ylabel('BER');
title(['Bits_option:',num2str(bits_option)]);
grid on;
end
阅读全文