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) .* sigma; 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'); 这个程序出现了错误使用 .* 矩阵维度必须一致。 出错 Untitled (第 29 行) noises = randn(1, N/2) .* sigma; 错误,请更改形成新的MATLAB程序
时间: 2023-08-23 17:05:55 浏览: 151
根据错误提示,矩阵维度必须一致,说明 `sigma` 的维度与 `randn(1, N/2)`同。可以使用 `repmat` 函数将 `sigma` 扩展到与 `randn(1, N/2)` 维度相同的矩阵,如下所示:
```
noises = randn(1, N/2) .* repmat(sigma', 1, N/2);
```
将该代码替换原来的 `noises` 行即可。
相关问题
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程序
修改代码如下:
```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');
```
修改后代码可以正常运行。
clc; clear;%% 1. 信源 bits = randi([0,1],1,1000); % 生成随机的比特序列%% 2. 扩频% 生成Walsh函数 N = 8; walshMat = hadamard(N); walshSeq = walshMat(1,:);% 生成扩频码 spreadCode = repmat(walshSeq,1,length(bits)/N);% 扩频 spreadBits = bits.*spreadCode;%% 3. 信道% 添加高斯白噪声 SNRdB = 0:2:16; SNR = 10.^(SNRdB/10); for i = 1:length(SNR) noiseVar = 1/SNR(i); noise = sqrt(noiseVar)*randn(1,length(spreadBits)); channelOut = spreadBits + noise; %% 4. 解扩 % 解扩 despreadBits = channelOut./spreadCode; despreadBits(isnan(despreadBits)) = 0; %% 5. 信宿 % BPSK调制 modSignal = 1-2*despreadBits; % 接收滤波器 b = ones(1,N); a = 1; rxSignal = filter(b,a,modSignal); %% 6. BER-SNR曲线 % 计算误码率 err = sum(rxSignal<0) + sum(rxSignal>0); % 统计错误比特数 ber(i) = err/length(bits); % 计算误码率 % 绘制BER-SNR曲线 semilogy(SNRdB,ber,'-*'); xlabel('SNR(dB)'); ylabel('BER'); title('BER-SNR曲线'); grid on; hold on; ,优化代码
以下是优化后的代码:
clc;
clear;
%% 1. 信源
bits = randi([0,1],1,1000); % 生成随机的比特序列
%% 2. 扩频
% 生成Walsh函数
N = 8;
walshMat = hadamard(N);
walshSeq = walshMat(1,:);
% 生成扩频码
spreadCode = repmat(walshSeq,1,length(bits)/N);
% 扩频
spreadBits = bits.*spreadCode;
%% 3. 信道
% 添加高斯白噪声
SNRdB = 0:2:16;
SNR = 10.^(SNRdB/10);
for i = 1:length(SNR)
% 计算噪声方差
noiseVar = 1/SNR(i);
% 生成噪声
noise = sqrt(noiseVar)*randn(1,length(spreadBits));
% 信道传输
channelOut = spreadBits + noise;
%% 4. 解扩
% 解扩
despreadBits = channelOut./spreadCode;
despreadBits(isnan(despreadBits)) = 0;
%% 5. 信宿
% BPSK调制
modSignal = 1-2*despreadBits;
% 接收滤波器
b = ones(1,N);
a = 1;
rxSignal = filter(b,a,modSignal);
%% 6. BER-SNR曲线
% 计算误码率
err = sum(rxSignal~=bits); % 统计错误比特数
ber(i) = err/length(bits); % 计算误码率
end
% 绘制BER-SNR曲线
semilogy(SNRdB,ber,'-*');
xlabel('SNR(dB)');
ylabel('BER');
title('BER-SNR曲线');
grid on;
阅读全文