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
时间: 2024-02-14 14:19:45 浏览: 164
这是一段MATLAB代码,主要实现了对QAM16调制的信号进行卷积编码、交织、信道传输、解交织、Viterbi译码等过程,并计算了不同信噪比下的误比特率(BER)。其中,变量bits_option表示不同的比特流选项,0表示全零比特流、1表示全1比特流、2表示随机比特流;noise_option表示噪声选项,1表示加入高斯白噪声,0表示无噪声;SNRdBs为信噪比范围;raw_bit_len表示原始比特流长度;interleaving_num表示交织器大小;deinterleaving_num表示解交织器大小;N_frame为仿真帧数;nobe_target表示错误比特数的目标值;BER_target为误比特率的目标值。该代码主要用于QAM16调制系统的仿真和性能评估。
相关问题
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将这段代码改为有噪声的情况
以下是添加噪声后的修改代码:
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
clear all; close all; clc;ticits_option = 2;noise_option = 1;raw_bit_len = 2592-6;interleaving_num = 72;deinterleaving_num = 72;N_frame = 1e4;SNRdBs = [0:2:20];sq05 = sqrt(0.5);bits_options = [0, 1, 2]; % 三种bits-option情况obe_target = 500;BER_target = 1e-3;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 = 1; SNRdB = SNRdBs(i_SNR); sigma2 = sig_power * 10^(-SNRdB/10); sigma = sqrt(sigma2/2); nobe = 0; for i_frame = 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([0,1], 1, raw_bit_len); 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 = sigma * (randn(2,1) + j * randn(2,1)); y = y + noise; end W = inv(H' * H + sigma2 * diag(ones(1,2))) * H'; K_tilde = W * y; x_hat = QAM16_slicer(K_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 nobe >= obe_target break; end if received_bit(EC_dummy) ~= bits(EC_dummy) nobe = nobe + 1; end end if nobe >= obe_target break; end end BER(i_SNR) = nobe / (i_frame * raw_bit_len); 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注释这段matlab代码
这是一段 Matlab 代码,以下是对其的注释:
```
% 清空所有变量、关闭所有图形窗口、清空命令行窗口、开始计时
clear all; close all; clc; tic
% 设置模拟参数
its_option = 2; % 暂时不知道其含义
noise_option = 1; % 噪声选项,1表示添加高斯白噪声,0表示不添加
raw_bit_len = 2592-6; % 原始比特长度,不包括尾部比特
interleaving_num = 72; % 扰码器长度
deinterleaving_num = 72; % 解扰码器长度
N_frame = 1e4; % 帧数
SNRdBs = [0:2:20]; % 信噪比范围
sq05 = sqrt(0.5); % sqrt(0.5)的值
bits_options = [0, 1, 2]; % 三种bits-option情况
obe_target = 500; % 最大误比特数目
BER_target = 1e-3; % 误比特率目标
% 循环处理三种bits-option情况
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 = 1; % 信号功率
SNRdB = SNRdBs(i_SNR); % 当前信噪比
sigma2 = sig_power * 10^(-SNRdB/10); % 方差
sigma = sqrt(sigma2/2); % 标准差
nobe = 0; % 误比特数目
for i_frame = 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([0,1], 1, raw_bit_len);
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); % 16QAM调制
x(1,1) = QAM16_symbol(1);
x(2,1) = QAM16_symbol(2);
if rem(tx_time - 1, 81) == 0 % 每81个时隙变化一次信道
H = sq05 * (randn(2,2) + j * randn(2,2));
end
y = H * x; % 信道传输
if noise_option == 1 % 添加高斯白噪声
noise = sigma * (randn(2,1) + j * randn(2,1));
y = y + noise;
end
W = inv(H' * H + sigma2 * diag(ones(1,2))) * H'; % MMSE检测
K_tilde = W * y;
x_hat = QAM16_slicer(K_tilde, 2); % 16QAM解调
temp_bit = [temp_bit QAM16_demapper(x_hat, 2)]; % 16QAM解调映射
end
% 解扰码器
deinterleaved = [];
for i = 1:deinterleaving_num
deinterleaved = [deinterleaved temp_bit([i:deinterleaving_num:end])];
end
received_bit = Viterbi_decode(deinterleaved); % Viterbi译码
% 统计误比特数目
for EC_dummy = 1:1:raw_bit_len
if nobe >= obe_target
break;
end
if received_bit(EC_dummy) ~= bits(EC_dummy)
nobe = nobe + 1;
end
end
if nobe >= obe_target
break;
end
end
BER(i_SNR) = nobe / (i_frame * raw_bit_len); % 计算误比特率
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
% 结束计时
toc
```
注释解释:
- `%` 表示单行注释,用于注释代码的某一行。
- `clear all; close all; clc; tic` 表示清空所有变量、关闭所有图形窗口、清空命令行窗口、开始计时。
- `its_option = 2;` 表示设置模拟参数 `its_option`,但是没有注释说明其具体含义。
- `noise_option = 1;` 表示设置噪声选项,`1` 表示添加高斯白噪声,`0` 表示不添加。
- `raw_bit_len = 2592-6;` 表示设置原始比特长度,不包括尾部比特。
- `interleaving_num = 72;` 表示设置扰码器长度。
- `deinterleaving_num = 72;` 表示设置解扰码器长度。
- `N_frame = 1e4;` 表示设置帧数。
- `SNRdBs = [0:2:20];` 表示设置信噪比范围。
- `sq05 = sqrt(0.5);` 表示设置 sqrt(0.5) 的值。
- `bits_options = [0, 1, 2];` 表示设置三种 `bits-option` 情况。
- `obe_target = 500;` 表示设置最大误比特数目。
- `BER_target = 1e-3;` 表示设置误比特率目标。
- `for i_bits = 1:length(bits_options)` 开始循环处理三种 `bits-option` 情况。
- `switch bits_option` 根据 `bits-option` 的值选择不同的情况。
- `QAM16_symbol = QAM16_mod(tx_bits, 2);` 表示进行 16QAM 调制。
- `if rem(tx_time - 1, 81) == 0` 表示每 81 个时隙变化一次信道。
- `if noise_option == 1` 表示添加高斯白噪声。
- `x_hat = QAM16_slicer(K_tilde, 2);` 表示进行 16QAM 解调。
- `temp_bit = [temp_bit QAM16_demapper(x_hat, 2)];` 表示进行 16QAM 解调映射。
- `received_bit = Viterbi_decode(deinterleaved);` 表示进行 Viterbi 译码。
- `BER(i_SNR) = nobe / (i_frame * raw_bit_len);` 表示计算误比特率。
- `semilogy(SNRdBs, BER);` 表示画图,使用对数坐标轴。
- `toc` 表示结束计时。
阅读全文