clc; close all;
clear all;
%% 参数
order_psk = [2,4,8,16,64]; % PSK符号阶数,2表示BPSK,4表示4PSK,以此类推
for order = 1:length(order_psk) %for循环
sym_total = 2.048e4*52; % 总符号数
len_turbo = 1280; % Turbo码长
bit_total = sym_total * log2(order_psk(order));%总比特数等于总符号数乘以符号阶数
N_ofdma_u1 = 26; % OFDMA用户1的子载波数
N_ofdma_u2 = 26; % OFDMA用户2的子载波数
p1 = 0.6; % NOMA用户1的功率
p2 = 0.4; % NOMA用户2的功率
N_ofdm = 64; % OFDM子载波数
N_data = 52; % 有效数据载波数
N_GB = [4;3]; % 载波间隔参数
N_P = [12; 26; 40; 54]; % 导频参数 间隔为14 不携带信息,双方已知数据,用于信道估计,对参考相位进行跟综,不产生相位偏移
CP = 1/4; % CP占比
Ts = 1/10000;
FD = 500; % 瑞利信道参数
SNR = 25:1:60; % 信噪比
%% 参数合理性判断
if (mod(bit_total, len_turbo)~=0)%求余函数 如果错误的话,则返回指定的数,否则返回公式结果
error('总比特数必须是Turbo编码长度的整数倍');
end
if (mod((3*len_turbo+12)*bit_total/len_turbo, N_data)~=0)
error('总比特数必须保证OFDM符号完整');
end
if (N_ofdma_u1+N_ofdma_u2~=N_data)
error('OFDMA中两用户的子载波数目之和必须等于数据载波总数');
end
if (p1+p2~=1)
error('功率系数p1与p2之和必须等于1');
end
%% 生成两个用户的发射符号序列,加入Turbo编码
[sym_seq_u1, bit_seq_u1] = data_gen(bit_total, len_turbo, order_psk(order));
[sym_seq_u2, bit_seq_u2] = data_gen(bit_total, len_turbo, order_psk(order));
% mean(abs(sym_seq_u1).^2)
% mean(abs(sym_seq_u2).^2)
%% NOMA和OFDMA编码
sym_seq_noma = noma_enc(sym_seq_u1, sym_seq_u2, p1, p2);
% mean(abs(sym_seq_noma).^2)
% 对两用户数据进行NOMA编码,返回数字绝对值
sym_seq_ofdma = ofdma_enc(sym_seq_u1, sym_seq_u2, N_ofdma_u1, N_ofdma_u2);
% mean(abs(sym_seq_ofdma).^2)
% 对两用户数据进行OFDMA编码
%% OFDM调制
num_ofdmsym_noma = length(sym_seq_noma)/N_data;
mod_ofdm_noma = comm.OFDMModulator(...
'FFTLength',N_ofdm,...
'NumGuardBandCarriers',N_GB,...
'PilotInputPort',true,...
'PilotCarrierIndices',N_P,...
'NumSymbols',num_ofdmsym_noma,...
'CyclicPrefixLength',N_ofdm*CP,...
'InsertDCNull',true);
% 构造NOMA的OFDM调制器
num_ofdmsym_ofdma = length(sym_seq_ofdma)/N_data;
mod_ofdm_ofdma = comm.OFDMModulator(...
'FFTLength',N_ofdm,...
'NumGuardBandCarriers',N_GB,...
'PilotInputPort',true,...
'PilotCarrierIndices',N_P,...
'NumSymbols',num_ofdmsym_ofdma,...
'CyclicPrefixLength',N_ofdm*CP,...
'InsertDCNull',true);
% 构造OFDMA的OFDM调制器
tx_noma = ofdm_tx(sym_seq_noma, mod_ofdm_noma);
% tx_noma = sym_seq_noma;
tx_ofdma = ofdm_tx(sym_seq_ofdma, mod_ofdm_ofdma);
% OFDM调制
% mean(abs(tx_noma).^2)
% mean(abs(tx_ofdma).^2)
for snr = 1:length(SNR)
%% 瑞利信道
% crl = rayleighchan(Ts, FD);
% tx_noma = filter(crl, tx_noma);
% tx_ofdma = filter(crl, tx_ofdma);
cawgn = comm.AWGNChannel('NoiseMethod', 'Signal to noise ratio (SNR)');
cawgn.SNR = SNR(snr);
rx_noma = step(cawgn, tx_noma);%阶跃响应绘制
rx_ofdma = step(cawgn, tx_ofdma);
% mean(abs(rx_noma).^2)
% mean(abs(rx_ofdma).^2)
% 接收信号经过瑞利信道与加入高斯噪声
%% OFDM解调
sym_seq_noma_mat = ofdm_rx(rx_noma, mod_ofdm_noma);
sym_seq_noma = reshape(sym_seq_noma_mat, numel(sym_seq_noma_mat), 1);
% sym_seq_noma = rx_noma;
sym_seq_ofdma_mat = ofdm_rx(rx_ofdma, mod_ofdm_ofdma);
sym_seq_ofdma = reshape(sym_seq_ofdma_mat, numel(sym_seq_ofdma_mat), 1);
% mean(abs(sym_seq_noma).^2)
% mean(abs(sym_seq_ofdma).^2)
%% NOMA和OFDMA解码 P1用户的功率分配比,谁优先解码
if (p1>p2)
[bit_u1, bit_u2] = noma_dec(sym_seq_noma, len_turbo, order_psk(order), p1, p2);
else
[bit_u2, bit_u1] = noma_dec(sym_seq_noma, len_turbo, order_psk(order), p2, p1);
end
[n1,r1(order, snr)] = biterr(bit_u1,bit_seq_u1);
[n2,r2(order, snr)] = biterr(bit_u2,bit_seq_u2);
% 按照功率顺序解码用户1和用户2的符号序列
[bit_u11, bit_u22] = ofdma_dec(sym_seq_ofdma, len_turbo, order_psk(order), N_ofdma_u1, N_ofdma_u2);
[n11,r11(order,snr)] = biterr(bit_u11,bit_seq_u1(1:length(bit_u11)));
[n22,r22(order,snr)] = biterr(bit_u22,bit_seq_u2(1:length(bit_u22)));
[r1(order,snr), r2(order,snr), r11(order,snr), r22(order,snr)]
end
end
%
save('ber.mat','r1','r2','r11','r22');
plot_ber(order_psk, SNR, N_data, p1, p2, N_ofdma_u1, N_ofdma_u2);
% rmpath('.\comm');
% rmpath('.\lte');
评论3