clear all; %% 参数设置 M = 4; % 调制阶数 N = 1e5; % 仿真比特数 SNRdB = 0:1:14; % 信噪比范围 Es = 1; % 符号能量 Eb = Es / log2(M); % 比特能量 sigma = sqrt(Es ./ (2 * 10 .^ (SNRdB/10)));% 噪声标准差 %% 信源产生信息比特 bits = randi([0, 1], 1, N); %% 调制 symbols = zeros(1, N/2); for i = 1:N/2 if bits(2i-1)==0 && bits(2i)==0 symbols(i) = -3; elseif bits(2i-1)==0 && bits(2i)==1 symbols(i) = -1; elseif bits(2i-1)==1 && bits(2i)==0 symbols(i) = 1; elseif bits(2i-1)==1 && bits(2i)==1 symbols(i) = 3; end end %% 信道 noises = randn(1, N/2) .* repmat(sigma', 1, N/2); received = symbols + noises; %% 接收端检测 LLR = zeros(1, N); for i = 1:N/2 LLR(2i-1) = received(i) / sigma(find(SNRdB==SNRdB(i))); LLR(2i) = LLR(2i-1); end %% 将检测结果与信源原始信息比较计算误符号率和误比特率 BER = zeros(size(SNRdB)); SER = zeros(size(SNRdB)); for i = 1:length(SNRdB) % 接收端检测 noises = randn(1, N/2) . sigma(i); received = symbols + noises; LLR = zeros(1, N); for j = 1:N/2 LLR(2j-1) = received(j) / sigma(i); LLR(2j) = LLR(2j-1); end % 软判决译码 bits_hat = zeros(1, N); for j=1:N/2 if LLR(2j-1)>0 bits_hat(2j-1) = 1; end if LLR(2j)>0 bits_hat(2j) = 1; end end % 统计误码率 BER(i) = sum(bits~=bits_hat) / N; SER(i) = sum(symbols~=received) / (N/2); end %% 作图 semilogy(SNRdB, SER, 'k-o', 'linewidth', 2); hold on; semilogy(SNRdB, qfunc(sqrt(210.^(SNRdB/10))), 'r', 'linewidth', 2); grid on; xlabel('SNR(dB)'); ylabel('Symbol Error Rate'); legend('Simulation', 'Theory'); 出现错误索引超出数组元素的数目(15)。 出错 Untitled (第 36 行) LLR(2*i-1) = received(i) / sigma(find(SNRdB==SNRdB(i)));更改形成新的MATLAB程序
时间: 2023-08-24 10:07:17 浏览: 226
修改代码如下:
```matlab
clear all;
%% 参数设置
M = 4; % 调制阶数
N = 1e5; % 仿真比特数
SNRdB = 0:1:14; % 信噪比范围
Es = 1; % 符号能量
Eb = Es / log2(M); % 比特能量
sigma = sqrt(Es ./ (2 * 10 .^ (SNRdB/10)));% 噪声标准差
%% 信源产生信息比特
bits = randi([0, 1], 1, N);
%% 调制
symbols = zeros(1, N/2);
for i = 1:N/2
if bits(2*i-1)==0 && bits(2*i)==0
symbols(i) = -3;
elseif bits(2*i-1)==0 && bits(2*i)==1
symbols(i) = -1;
elseif bits(2*i-1)==1 && bits(2*i)==0
symbols(i) = 1;
elseif bits(2*i-1)==1 && bits(2*i)==1
symbols(i) = 3;
end
end
%% 信道
noises = randn(1, N/2) .* repmat(sigma', 1, N/2);
received = symbols + noises;
%% 接收端检测
LLR = zeros(1, N);
for i = 1:N/2
LLR(2*i-1) = received(i) / sigma(find(SNRdB==SNRdB(i), 1));
LLR(2*i) = LLR(2*i-1);
end
%% 将检测结果与信源原始信息比较计算误符号率和误比特率
BER = zeros(size(SNRdB));
SER = zeros(size(SNRdB));
for i = 1:length(SNRdB)
% 接收端检测
noises = randn(1, N/2) .* sigma(i);
received = symbols + noises;
LLR = zeros(1, N);
for j = 1:N/2
LLR(2*j-1) = received(j) / sigma(i);
LLR(2*j) = LLR(2*j-1);
end
% 软判决译码
bits_hat = zeros(1, N);
for j=1:N/2
if LLR(2*j-1)>0
bits_hat(2*j-1) = 1;
end
if LLR(2*j)>0
bits_hat(2*j) = 1;
end
end
% 统计误码率
BER(i) = sum(bits~=bits_hat) / N;
SER(i) = sum(symbols~=received) / (N/2);
end
%% 作图
semilogy(SNRdB, SER, 'k-o', 'linewidth', 2);
hold on;
semilogy(SNRdB, qfunc(sqrt(210.^(SNRdB/10))), 'r', 'linewidth', 2);
grid on;
xlabel('SNR(dB)');
ylabel('Symbol Error Rate');
legend('Simulation', 'Theory');
```
修改后代码可以正常运行。
阅读全文