Skip to content

Commit 17949c9

Browse files
author
Alex
committed
Updated simulator: better code-writing for matlab-run.
1 parent 07cb544 commit 17949c9

12 files changed

Lines changed: 101 additions & 88 deletions
File renamed without changes.

config.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
timestamp = [num2str(start_time(1),'%04d') '_' num2str(start_time(2),'%02d') '_' num2str(start_time(3),'%02d') '_' num2str(start_time(4),'%02d') '_' num2str(start_time(5),'%02d') '_' num2str(start_time(6),'%2.0f')];
2+
result_path = './results/';
3+
addpath('support');
4+
fast_run = 1; %1:run of optimal-matlab code, 0:run of hardware-code (suboptimal f/g)
5+
Fading_Channel = 0; %0: AWGN, 1:Fading Channel
6+
Fading_Independent = 0; %if Fading_Channel = 1
7+
quasi = 0; % quasi channel
8+
fading_channel = 'TU120'; %custom matlab channel
9+
fix_seed = 1; %1:fix data, 0:random data
10+
code_rate = 1/4;
11+
if fix_seed
12+
rng(sum(100*clock));
13+
else
14+
rand('seed',123456);
15+
end
16+
%Simulation values
17+
min_fer_errors = 100;
18+
min_codewords = 100;
19+
NbitsPerSymbol = 1;
20+
constDims = 1;
21+
%EbNo
22+
EbNo_dB = -2:4;
23+
snrdb_values =EbNo_dB+10*log10(double(code_rate*NbitsPerSymbol*2/constDims));
24+
%Polar-Code values
25+
capacity = 0.5; %I(W), Channel's W Capacity

main.m

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,45 @@
11
clc;clear;
22
start_time = clock;
3-
timestamp = [num2str(start_time(1),'%04d') '_' num2str(start_time(2),'%02d') '_' num2str(start_time(3),'%02d') '_' num2str(start_time(4),'%02d') '_' num2str(start_time(5),'%02d') '_' num2str(start_time(6),'%2.0f')];
4-
result_path = './results/';
5-
file = fopen([result_path 'polar_test_' timestamp '.txt'],'w');
6-
addpath('support');
7-
Fading_Channel = 0; %0: AWGN, 1:Fading Channel
8-
Fading_Independent = 0; %if Fading_Channel = 1
9-
quasi = 0; % quasi channel
10-
fading_channel = 'TU120'; %custom matlab channel
11-
fix_seed = 1; %1:fix data, 0:random data
12-
if fix_seed
13-
rng(sum(100*clock));
14-
else
15-
rand('seed',123456);
16-
end
17-
for n = 6%[2,3,4,5,6,7,8,9] %N=2^n
3+
config;
4+
for n = [7,8,9,10] %N=2^n
185
N = power(2,n); %Code Length
19-
K = N/2; %Code keyword length
20-
capacity = 0.5; %I(W), Channel's W Capacity
6+
K = N* code_rate; %Code keyword length
7+
mat_file = [result_path 'polar_N' num2str(N) '_K' num2str(K) '_' timestamp '.mat'];
8+
bit_error_rate = zeros(1,length(snrdb_values));
9+
fer_error_rate = zeros(1,length(snrdb_values));
10+
codewords = zeros(1,length(snrdb_values));
11+
%Polar-code initializations
12+
Fn = fkronecker(N);
2113
frozen_bits = initialize_frozen_bits(N,K,capacity); %0=frozen, 1=not_frozen
22-
%%
23-
fprintf(file,'\n\n[%d,%d,%d,%d,%d,%d]\n',[0,2,4,6,8,10]);%[0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5]);
24-
fprintf(file,'N = %d\n[', N);
25-
SNRdb_values = 0:5;
26-
for SNRdb = SNRdb_values %[0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5]
14+
partial_sum_adders = partial_sums_initialize(N); %!!!!! NOT bit_reversed array -- %outputs(z,i,l) -- l stage ,bit Ui is added,z is the number of g adder
15+
[sc_functions,sc_2nd_indxs] = sc_array_initialize(N);
16+
for i_index = 1:length(snrdb_values) %[0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5]
17+
%Initialize temporary variables
18+
codewords_tmp = 0;
2719
fer_errors = 0;
2820
bit_errors = 0;
29-
codewords = 0;
30-
while (fer_errors<100 || codewords<100)
31-
codewords = codewords + 1;
32-
inputs = randi([0,1],1,K); %write random inputs
21+
snr = snrdb_values(i_index);
22+
while (fer_errors<min_fer_errors || codewords_tmp<min_codewords)
23+
codewords_tmp = codewords_tmp + 1;
24+
inputs = rand(1,K)>0.5; %write random inputs
3325
%transform inputs
3426
inputs_to_encode = transform_inputs(inputs,frozen_bits,N);
3527
%encode
36-
encoded_inputs = encode(inputs_to_encode); %Reversed Polar Encoding
28+
encoded_inputs = encode(inputs_to_encode,Fn); %Reversed Polar Encoding
3729
%modulate
3830
modulated_inputs = modulate(encoded_inputs);%encoded_inputs); %BPSK = 1-2*encoded_inputs(i)
3931
%noise
40-
noised_inputs = add_noise(modulated_inputs,Fading_Channel,Fading_Independent,fading_channel,SNRdb);
32+
noised_inputs = add_noise(modulated_inputs,constDims,Fading_Channel,Fading_Independent,fading_channel,snrdb_values(i_index));
4133
%demodulate
42-
llr = (2 * power(10,SNRdb/10))*noised_inputs; %CARE NEGATIVE SIGN.2*yi/(s^2) = ln(Li), s^2 = 1/ 10^ (SNRdb/10)
34+
llr = (2 * power(10,snrdb_values(i_index)/10))*noised_inputs; %CARE NEGATIVE SIGN.2*yi/(s^2) = ln(Li), s^2 = 1/ 10^ (SNRdb/10)
4335
%decode
44-
outputs = decode(llr,frozen_bits);
36+
if(fast_run)
37+
%optimal version (optimal-calculations of f/g)
38+
outputs = decode2(llr,frozen_bits);
39+
else
40+
%hardware-version (suboptimal-calculations of f/g)
41+
outputs = decode(llr,frozen_bits,partial_sum_adders,sc_functions,sc_2nd_indxs);
42+
end
4543
%or decode(llr,frozen_bits); for the other algorithm
4644
%choose A set (not frozen bits)
4745
%Calculate bit/frame errors
@@ -50,14 +48,13 @@
5048
bit_errors = bit_errors + temp_bit_errors;
5149
fer_errors = fer_errors + (temp_bit_errors>0);
5250
end
53-
bit_error_rate = bit_errors/(codewords*K);
54-
frame_error_rate = fer_errors/(codewords);
55-
fprintf(file,'SNRdb = %d\tber=%0.8f,fer=%0.8f\n',SNRdb,bit_error_rate,frame_error_rate);
56-
fprintf('SNRdb = %d\tber=%0.8f,fer=%0.8f\n',SNRdb,bit_error_rate,frame_error_rate);
51+
bit_error_rate(i_index) = bit_errors/(codewords_tmp*K);
52+
fer_error_rate(i_index) = fer_errors/(codewords_tmp);
53+
codewords(i_index) = codewords_tmp;
54+
save(mat_file,'snrdb_values','bit_error_rate','fer_error_rate','codewords','N','K');
55+
fprintf('SNRdb = %d\tber=%0.8f,fer=%0.8f\n',snrdb_values(i_index),bit_error_rate(i_index),fer_error_rate(i_index));
5756
end
58-
file = fopen([result_path 'results_N=' num2str(N) '_SNR=' num2str(SNRdb_values(1)) '-' num2str(SNRdb_values(end)) '.txt'],'w');
59-
fprintf(file,'0]\n');
57+
% file = fopen([result_path 'results_N=' num2str(N) '_SNR=' num2str(snrdb_values(1)) '-' num2str(snrdb_values(end)) '.txt'],'w');
58+
% fprintf(file,'0]\n');
6059
end
61-
%fprintf(file,'SNRmean = %2.5f]\n',SNRmean/20);
62-
fclose(file);
6360

-6.55 MB
Binary file not shown.

support/add_noise.m

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function channel_out = add_noise(inputs,Fading_Channel,Fading_Independent,fading_channel,snr)
1+
function channel_out = add_noise(inputs,constDims,Fading_Channel,Fading_Independent,fading_channel,snr)
22
%takes modulated inputs, adds AWGN/Fading noise with SNR in db.
33
if Fading_Channel && ~Fading_Independent
44
legacychannelsim(true);
@@ -40,10 +40,13 @@
4040
attenu = 1;
4141
phase = 0;
4242
end
43-
constDims = 2;
43+
4444
noise_dev = sqrt(10^(-snr/10)/constDims);
45-
channel_out = noise_dev*complex(randn(1,length(inputs)),randn(1,length(inputs))) ...
46-
+ in_decoder_tmp;
45+
%BPSK
46+
channel_out = noise_dev*(randn(1,length(inputs))) + in_decoder_tmp;
47+
%QPSK
48+
% channel_out = noise_dev*complex(randn(1,length(inputs)),randn(1,length(inputs))) ...
49+
% + in_decoder_tmp;
4750

4851
%old noise addition based on awgn
4952
% channel_out = awgn(inputs,snr,'measured');

support/capacities.m

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
function capacities = capacities( N, capacity)
22
%Estimates the capacities of all channels
33
%Outputs array with the capacities of the N channels
4-
capacities = zeros(1,N);
54
bits = log2(N);
6-
for i=1:1:N
7-
capacities(i) = capacity;
8-
end
9-
for j=0:1:bits-1
10-
step = power(2,j);
11-
for i=1:2*step:power(2,bits)
12-
for z=i:1:i+step-1
13-
temp = capacities(z) * capacities(z);
14-
capacities(z+step) = 2*capacities(z+step) - temp;
15-
capacities(z)= temp;
16-
end
5+
capacities = ones(1,N)*capacity;
6+
for j=0:1:bits-1
7+
step = power(2,j);
8+
for i=1:2*step:power(2,bits)
9+
for z=i:1:i+step-1
10+
temp = capacities(z) * capacities(z);
11+
capacities(z+step) = 2*capacities(z+step) - temp;
12+
capacities(z)= temp;
1713
end
1814
end
15+
end
1916

2017

2118
end

support/decode.m

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
function outputs = decode(llr_inputs,frozen_bits)
1+
function outputs = decode(llr_inputs,frozen_bits,partial_sum_adders,sc_functions,sc_2nd_indxs)
22
bits = length(llr_inputs);
33
reverse_order = bitrevorder(1:1:bits); %same as 0:1:bits-1
4-
sc_array = sc_array_initialize(bits,llr_inputs);%--BIT_REVERSED ARRAY-- outputs(i,l,dimension) // dimension= 1 values -- 2 !! if 0 then f else g !!-- 3 2nd input
5-
partial_sum_adders = partial_sums_initialize(bits); %!!!!! NOT bit_reversed array -- %outputs(z,i,l) -- l stage ,bit Ui is added,z is the number of g adder
4+
sc_array = zeros(bits,log2(bits)+1);
5+
sc_array(:,log2(bits)+1) = llr_inputs.';
66
outputs = zeros(1,bits); %NOT bit_reversed_array
7-
for bit=1:1:bits %Arikan 0:1:bits-1
7+
non_frozen_indxs = find(frozen_bits == 1);
8+
for bit=non_frozen_indxs %Arikan 0:1:bits-1
89
for l= log2(bits):-1:1 %Arikan log2(bits)-1:-1:0
910
partial_sums_l = partial_sum_adders(:,:,l) * (transpose(outputs)); %NOT bit_reversed_array
1011
partial_sums_l = mod(partial_sums_l,2); %mod 2
1112
z = 0; %number of partial sum
1213
for i = 1:1:bits
13-
if(sc_array(i,l,2) == 0)
14-
sc_array(i,l,1) = f(sc_array(i+sc_array(i,l,3),l+1,1),sc_array(i,l+1,1)); %getting values from l+1 stage
14+
if(sc_functions(i,l) == 0)
15+
sc_array(i,l,1) = f(sc_array(i+sc_2nd_indxs(i,l),l+1,1),sc_array(i,l+1,1)); %getting values from l+1 stage
1516
else
1617
z = z+1; %the number of adder
17-
sc_array(i,l,1) = g(sc_array(i+sc_array(i,l,3),l+1,1),sc_array(i,l+1,1),partial_sums_l(z)); %getting values from l+1 stage + partial_sum
18+
sc_array(i,l,1) = g(sc_array(i+sc_2nd_indxs(i,l),l+1,1),sc_array(i,l+1,1),partial_sums_l(z)); %getting values from l+1 stage + partial_sum
1819
end
1920
end
2021
end
21-
if(sc_array(reverse_order(bit),1,1)<0 && frozen_bits(bit)==1) %if it's not frozen bit, update value
22+
if(sc_array(reverse_order(bit),1,1)<0) %if it's not frozen bit, update value
2223
outputs(bit)=1;
2324
end
2425
end

support/encode.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
function outputs = encode(inputs)
1+
function outputs = encode(inputs,Fn)
22
%outputs = x where x= u * G , where G = B(N) * Fn
33
%basic encoding scheme with kronecker power of Fn.
4-
Fn = fkronecker(length(inputs));
54
outputs = mod(bitrevorder(inputs) * Fn,2); %Reversed outputs
65
end
76

support/initialize_frozen_bits.m

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
%inputs N codelength, K code keyword length, capacity of the channel
33
%outputs a N-length array with 0,1. If 0 then the channel is frozen
44
%if 1 not frozen.
5+
%calculate capacities and reverse them (reverse-encode)
56
capacity_array = bitrevorder(capacities(N, capacity));
6-
[capacity_array,sortIndex] = sort(capacity_array(:));
7+
[~,sortIndex] = sort(capacity_array(:));
8+
%freeze N-K worst-channels and keep K best-channels
79
frozen_bits = ones(1,N);
8-
for i=1:1:N-K
9-
frozen_bits(sortIndex(i)) = 0;
10-
end
11-
10+
frozen_bits(sortIndex(1:N-K)) = 0;
1211
end

support/matlab.mat

-739 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)