clear; clc;
SNR=0:0.5:10;
M = 4; % Modulation order (QPSK)
k = log2(M); % Number of bits per symbol(=2bit)
n = 500000; % Number of symbols per frame
N=n*k;
BER_Repetition=zeros(size(SNR));
BER_Convolutional=zeros(size(SNR));
BER_LDPC=zeros(size(SNR));
BER_Polar=zeros(size(SNR));
%% Parameter
% Repetition code
R=3;
% Convolutional code
trellis = poly2trellis(7, [171 133]); % (7, [171 133])은 일반적인 Convolutional 코드
tbl = 32; % traceback depth
% LDPC
P=dvbs2ldpc(1/2);
blockSize = 27;
maxnumiter = 5;
cfgLDPCEncoder = ldpcEncoderConfig(P); % LDPC 인코더 구조체
cfgLDPCDecoder = ldpcDecoderConfig(P); % LDPC 디코더 구조체
ldpc_input_length = cfgLDPCEncoder.NumInformationBits; % LDPC 인코더 입력 길이
data_sample = zeros(ldpc_input_length,1); % LDPC 인코더 입력 배열 초기화
blk_length = cfgLDPCEncoder.BlockLength; % LDPC 인코더 출력(블록) 길이
codeword_sample_ = zeros(blk_length,1);
% Polar Code
K = 100;
E = 256;
L = 8;
loop = 10000; % Number of iterations
%% Wireless Communication Simulation
tic;
for i=1:length(SNR)
% Data bit generation
msg = randi([0 1],N,1);
%%% 1. Repetition Code
% Repetition code encoding
msg_Rep = repelem(msg, R); % 3번 반복
modInput_Rep = bit2int(msg_Rep, k);
% Modulation (QPSK)
TxSig_Rep = pskmod(modInput_Rep, M);
% Channel
RxSig_Rep = awgn(TxSig_Rep, SNR(i));
% Demodulation
demodOutput_Rep = pskdemod(RxSig_Rep, M);
demod_msg_Rep = de2bi(demodOutput_Rep, k, 'left-msb');
demod_msg_Rep = demod_msg_Rep';
demod_msg_Rep = demod_msg_Rep(:);
% Repetition Code decoding
demod_msg_Rep = reshape(demod_msg_Rep, 3, []);
decoded_msg_Rep = mode(demod_msg_Rep, 1); % 다수결
% BER Calculation for Repetition Code
biterror_Rep = sum(abs(msg' - decoded_msg_Rep));
BER_Repetition(i) = biterror_Rep / N;
% 2. Convolutional Code
% Convolutional code encoding
msg_Conv = convenc(msg, trellis);
modInput_Conv = bit2int(msg_Conv, k);
% Modulation (QPSK)
TxSig_Conv = pskmod(modInput_Conv, M);
% Channel
RxSig_Conv = awgn(TxSig_Conv, SNR(i));
% Demodulation
demodOutput_Conv = pskdemod(RxSig_Conv, M);
demod_msg_Conv = de2bi(demodOutput_Conv, k, 'left-msb');
demod_msg_Conv = demod_msg_Conv';
demod_msg_Conv = demod_msg_Conv(:);
% Convolutional Code decoding
decoded_msg_Conv = vitdec(demod_msg_Conv, trellis, tbl, 'trunc', 'hard');
% BER Calculation for Convolutional Code
biterror_Conv = sum(abs(msg - decoded_msg_Conv));
BER_Convolutional(i) = biterror_Conv / N;
%%% 3. LDPC Code
bit_length = length(msg);
num_iteration = ceil(bit_length/ldpc_input_length); % LDPC Enc 반복 횟수
codeword = zeros(blk_length*num_iteration,1);
decoded_msg_LDPC = zeros(ldpc_input_length*num_iteration,1); % 출력 담을 배열 초기화
% LDPC encoding
for i1=1:num_iteration
idx_start=(i1-1)*ldpc_input_length+1;
idx_code_start=(i1-1)*blk_length+1;
if(i1==num_iteration) % 마지막 블록인 경우
% zero padding: 마지막 블록 크기가 모자랄 경우 0으로 패딩
last_block=length(msg(idx_start:end));
data_sample(1:last_block)=msg(idx_start:end);
else % 일반 블록인 경우
data_sample=msg(idx_start:(idx_start+ldpc_input_length-1));
end
codeword_sample=ldpcEncode(data_sample,cfgLDPCEncoder);
% codeword ← codeword_sample_1 + codeword_sample_2 + ...
codeword(idx_code_start:(idx_code_start+blk_length-1))=codeword_sample;
end
% Modulation (QPSK)
modInput_LDPC = bit2int(codeword,k);
TxSig_LDPC = pskmod(modInput_LDPC,M);
% Channel
RxSig_LDPC=awgn(TxSig_LDPC, SNR(i));
% Demodulation
demodOutput_LDPC = pskdemod(RxSig_LDPC,M,OutputType='approxllr');
% LDPC decoding
for i2=1:num_iteration
decoded_idx_start=(i2-1)*blk_length+1;
decoded_idx_bit_start=(i2-1)*ldpc_input_length+1;
codeword_sample_=demodOutput_LDPC(decoded_idx_start:(decoded_idx_start+blk_length-1));
bit_sample_=ldpcDecode(codeword_sample_, cfgLDPCDecoder, maxnumiter);
% decoded_data_ ← codeword_sample1_ + codeword_sample2_ + ...
decoded_msg_LDPC(decoded_idx_bit_start:(decoded_idx_bit_start+ldpc_input_length-1))=bit_sample_;
end
decoded_msg_LDPC = decoded_msg_LDPC(1:N);
% BER Calculation for LDPC
biterror_LDPC=sum(abs(msg-decoded_msg_LDPC));
BER_LDPC(i)=biterror_LDPC/(N);
end
% Polar Code
for i = 1:length(SNR)
for i2 = 1:loop
% Data bit generation
msg_p = randi([0 1], K, 1); % Message length set to K
% Polar code encoding
msg_Polar = nrPolarEncode(msg_p, E);
% Modulation (QPSK)
TxSig_Polar=nrSymbolModulate(msg_Polar,'QPSK');
% Channel
RxSig_Polar = awgn(TxSig_Polar, SNR(i), 'measured');
% Demodulation
demod_msg_Polar= nrSymbolDemodulate(RxSig_Polar,'QPSK');
% Polar Code decoding
decoded_msg_Polar = nrPolarDecode(demod_msg_Polar, K, E, L);
% BER Calculation for Polar Code
biterror_Polar = sum(msg_p ~= decoded_msg_Polar(1:K)); % Comparing length set to K
BER_Polar(i)=BER_Polar(i)+biterror_Polar/K;
end
BER_Polar(i) = BER_Polar(i) / loop;
end
elapsed_time=toc;
%% Plot BER Gragh
figure;
semilogy(SNR, BER_Repetition, 'k*-', 'LineWidth', 1);
hold on;
semilogy(SNR, BER_Convolutional, 'r*-', 'LineWidth', 1);
semilogy(SNR, BER_LDPC, 'b*-', 'LineWidth', 1);
semilogy(SNR, BER_Polar, 'g*-', 'LineWidth', 1);
axis([0 10 10^(-5) 1]);
xlabel('SNR(dB)');
ylabel('BER');
title('BER Performance by Channel Code Type', 'FontSize',13)
grid on;
legend('Repetition Code(R=3)', 'Conv. Code', 'LDPC Code', 'Polar Code');
%% Display elapsed time
minutes = floor(elapsed_time / 60);
seconds = floor(mod(elapsed_time, 60));
fprintf('Elapsed time for the simulation: %dm %ds\n', minutes, seconds);
'ICIS 학부 연구생 > 통신시스템 설계' 카테고리의 다른 글
[ 통신시스템설계 ] CDMA Multipath Fading (0) | 2024.08.23 |
---|---|
[ 통신시스템설계 ] CDMA 기초 (0) | 2024.08.23 |
[ 통신시스템설계 심화 ] 2. AES 실습3 Stream 암호 적용 (0) | 2024.01.27 |
[ 통신시스템설계 심화 ] 2. AES 실습2 암복호화 적용하기 (0) | 2024.01.27 |
[ 통신시스템설계 심화 ] 2. AES 실습1 txt → binary 변환 후 무선채널 송수신 (1) | 2024.01.27 |