📄 main1003.asv
字号:
%Cooperative Diversity - Main Sequence
tic
% --------------
% Set Parameters
nr_of_iterations = 100;
SNR = [-8:2:8];
use_direct_link = 1;
use_relay = 1;
global statistic;
statistic = generate_statistic_structure;
global signal;
signal = generate_signal_structure;
channel = generate_channel_structure;%信道结构
%channel(1):源端到目的端 channel(2):源端到中继端 channel(3):中继到目的端
channel(1).attenuation(1).pattern = 'no';% 'no','Rayleigh'
%channel(1).attenuation(1).pattern = 'Rayleigh';% 'no','Rayleigh'
channel.attenuation.block_length = 1;
channel(2) = channel(1);
channel(3) = channel(1);
channel(1).attenuation.distance = 1;
channel(2).attenuation.distance = 1;
channel(3).attenuation.distance = 1;
crc_check=1;
rx = generate_rx_structure;%接收端结构
rx(1).combining_type = 'MRC'; %'ERC','FRC','SNRC','ESNRC','MRC'
rx(1).sd_weight = 1.0;
global relay;
relay = generate_relay_structure;
relay(1).mode = 'TURBOCC'; %'AF', 'DF','RCPC','TURB0CC','TURBO' 选择分集协议类型
coop_rate=1/2;
relay.magic_genie = 0;
relay(1).rx(1) = rx(1); % same beahaviour
% ---------------
% Start Simulation
BER = zeros(size(SNR));
for iSNR = 1:size(SNR,2)
error_bit =zeros(1,nr_of_iterations);
channel(1).noise(1).SNR = SNR(iSNR);
channel(2).noise(1).SNR = SNR(iSNR)+10;
channel(3).noise(1).SNR = SNR(iSNR);
disp(['progress: ',int2str(iSNR),'/',int2str(size(SNR,2))])
% --------------
% Reset receiver
rx = rx_reset(rx);
relay.rx = rx_reset(relay.rx);
% -----------
for it = 1:nr_of_iterations; %迭代计算次数
switch relay.mode
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
case 'TURBOCC'
dec_alg = 1; % the decoding algorithm. (0:Log-MAP, 1:SOVA) default 0)
niter = 5; % number of iterations for each frame
L_total = 130; %L_total = L_info + m;
g = [ 1 1 1; 1 0 1 ]; % 生成矩阵
[n,K] = size(g);
m = K - 1; % m=2
nstates = 2^m; % 4
puncture = 1; %unpuncture: 1, puncture:0
rate = 1/(2+puncture);
% Fading amplitude; a=1 in AWGN channel
a = 1;
x1 = round(rand(1, L_total-m-16)); % info. bits 信息比特112
CRC_CCITT = CRC_16(x1);
x =[x1,CRC_CCITT]; %加上CRC 比特信息128
L_info = length(x);
input = x;
output1 = rsc_encode(g,input,1); %经RSC1编码后的输出信息
transmit_symbol = output1;
signal(1).nr_of_bits = length(transmit_symbol);
signal.modulation_type = 'BPSK'; % 'BPSK', 'QPSK'
calculate_signal_parameter(transmit_symbol); %准备发送信息modulate
if (use_direct_link == 1)
[channel(1), rx] = add_channel_effect(channel(1), rx,signal(1).symbol_sequence,1/3); %S-D传输
rx = rx_correct_phaseshift(rx, channel(1).attenuation.phi); %校正相位
rx.signal2analyse = real(rx.signal2analyse); %取实部信息
y(1,:) = rx.signal2analyse(1:2:2*L_total); %取系统位
y(2,:) = rx.signal2analyse(2:2:2*L_total); %取RSC1校验位
%a = channel(1).attenuation.h_mag;
end
if (use_relay == 1)
[channel(2), relay.rx] = add_channel_effect(channel(2),relay.rx, signal(1).symbol_sequence,1/3); %S-R传输
relay.rx =rx_correct_phaseshift(relay.rx,channel(2).attenuation.phi);
relay.rx.signal2analyse = real(relay.rx.signal2analyse); %1*260
%y2(1,:) = relay.rx.signal2analyse(1:2:2*L_total)
qcode = quantiz(relay.rx.signal2analyse,[0.001,.1,.3,.5,.7,.9,.999]); % 软判决
tblen = 48; delay = tblen; % Traceback length
add =zeros(1,2*delay);
qcode = [qcode,add]; %因为延时,末位补0
t = poly2trellis(3,[7 5],7); % Define trellis.
decoded = vitdec(qcode,t,tblen,'cont','soft',3); % Decode. 维特比译码1*130
err_RSC = sum(abs(decoded(delay+1:end-2) - x)); % 译码错误比特数
y1(1,:) = decoded(delay+1:end); % y: unpuncture output of encoder; y(1,:) has m bits more than input bits
%检查CRC
crc_check = 1;
y2 = y1(1:L_total-m-16);
y1_CRC = CRC_16(y2);
if(((y1(L_total-m-15:L_total-m) - y1_CRC) ~=0) & (crc_check ~= 0) )
%S发送RSC2部份信息
y1(1,:) = output1(1:2:2*L_total);
[temp, alpha] = sort(rand(1,L_total)); % random interleaver mapping 随机交织映射
for i = 1:L_total
input1(1,i) = y1(1,alpha(i)); %alpha--index of interleaver
end
output2 = rsc_encode(g, input1(1,1:L_total), -1 ); %生成RSC2部份信息比特
%y(3,:) = output2(2:2:2*L_total);
calculate_signal_parameter(output2(2:2:2*L_total)); % 经过BPSK调制
%relay.signal2send = signal.symbol_sequence;
[channel(1), rx] = add_channel_effect(channel(1), rx, signal.symbol_sequence,1/3);
rx = rx_correct_phaseshift(rx,channel(1).attenuation.phi);
rx.signal2analyse = real(rx.signal2analyse);
else
[temp, alpha] = sort(rand(1,L_total)); % random interleaver mapping 随机交织映射
for i = 1:L_total
input1(1,i) = y1(1,alpha(i)); %alpha--index of interleaver
end
output2 = rsc_encode(g, input1(1,1:L_total), -1 ); %生成RSC2部份信息比特
%relay to destination
calculate_signal_parameter(output2(2:2:2*L_total)); % 经过BPSK调制
relay.signal2send = signal.symbol_sequence;
[channel(3), rx] = add_channel_effect(channel(3), rx,relay.signal2send,1/3); %relay发送信息至receive
rx = rx_correct_phaseshift(rx,channel(3).attenuation.phi);
rx.signal2analyse = real(rx.signal2analyse);
end
y(3,:) = rx.signal2analyse;
%receive_value =[receive_value;rx.signal2analyse];
% 删除器
if puncture > 0 % unpunctured
for i = 1:L_total
for j = 1:3
en_output(1,3*(i-1)+j) = y(j,i); % put the 3 bits of the same colomn to a sequential outputs 1*390
end
end
else % punctured into rate 1/2
for i=1:L_total
en_output(1,n*(i-1)+1) = y(1,i);
if rem(i,2) % output check bits by turns
% odd check bits from RSC1
en_output(1,n*i) = y(2,i);
else
% even check bits from RSC2
en_output(1,n*i) = y(3,i); %1*260
end
end
end
end
yk = demultiplex(en_output,alpha,puncture); %解复用
en = 10^(SNR(iSNR)/10); % convert Eb/N0 from unit db to normal numbers
L_c = 4*a*en*rate; % reliability value of the channel
%sigma1 = 1/sqrt(2*rate*en); % standard deviation of AWGN noise
errs(1,1:niter) = zeros(1,niter);
nferr(1,1:niter) = zeros(1,niter);
% Scale the received bits
rec_s = 0.5*L_c*yk;
% Initialize extrinsic information
L_e(1:L_total) = zeros(1,L_total);
for iter = 1:niter
% Decoder one
% deinterleave the extrinsic information for first decoder -yzh
L_a(alpha) = L_e; % a priori info. 解交织
if dec_alg == 0
L_all = logmapo(rec_s(1,:), g, L_a, 1); % complete info.
else
L_all = sova0(rec_s(1,:), g, L_a, 1); % complete info.
end
L_e = L_all - 2*rec_s(1,1:2:2*L_total) - L_a; % extrinsic info.
% Decoder two
L_a = L_e(alpha); % a priori info.交织
if dec_alg == 0
L_all = logmapo(rec_s(2,:), g, L_a, 2); % complete info.
else
L_all = sova0(rec_s(2,:), g, L_a, 2); % complete info.
end
L_e = L_all - 2*rec_s(2,1:2:2*L_total) - L_a; % extrinsic info.
% Estimate the info. bits
xhat(alpha) = (sign(L_all)+1)/2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -