|
1 | | -clc;clear; |
| 1 | +clc;clear all; |
2 | 2 | start_time = clock; |
3 | 3 | config; |
4 | | -for n = [7,8,9,10] %N=2^n |
5 | | -N = power(2,n); %Code Length |
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); |
13 | | -frozen_bits = initialize_frozen_bits(N,K,capacity); %0=frozen, 1=not_frozen |
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; |
19 | | -fer_errors = 0; |
20 | | -bit_errors = 0; |
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 |
25 | | -%transform inputs |
26 | | -inputs_to_encode = transform_inputs(inputs,frozen_bits,N); |
27 | | -%encode |
28 | | -encoded_inputs = encode(inputs_to_encode,Fn); %Reversed Polar Encoding |
29 | | -%modulate |
30 | | -modulated_inputs = modulate(encoded_inputs);%encoded_inputs); %BPSK = 1-2*encoded_inputs(i) |
31 | | -%noise |
32 | | -noised_inputs = add_noise(modulated_inputs,constDims,Fading_Channel,Fading_Independent,fading_channel,snrdb_values(i_index)); |
33 | | -%demodulate |
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) |
35 | | -%decode |
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); |
| 4 | +%% N LOOP |
| 5 | +for index_n = 1:length(n_values) %N=2^n |
| 6 | + N = power(2,n_values(index_n)); %Code Length |
| 7 | + K = N* code_rate; %Code keyword length |
| 8 | + mat_file = [result_path 'polar_N' num2str(N) '_K' num2str(K) '_' timestamp '.mat']; |
| 9 | + %Polar-code initializations |
| 10 | + [Fn,frozen_bits,partial_sum_adders, sc_functions, sc_2nd_indxs] = polar_initialization(N, K, capacity); |
| 11 | + fprintf("Polar Code %d/%d running:\n",N,K); |
| 12 | + %% SNR LOOP |
| 13 | + for i_index = 1:length(snrdb_values) |
| 14 | + %Initialize temporary variables |
| 15 | + codewords_tmp = 0;fer_errors = 0;bit_errors = 0; |
| 16 | + snr = snrdb_values(i_index); |
| 17 | + while (fer_errors<min_fer_errors || codewords_tmp<min_codewords) |
| 18 | + codewords_tmp = codewords_tmp + 1; |
| 19 | + inputs = rand(1,K)>0.5; %write random inputs |
| 20 | + %transform inputs |
| 21 | + inputs_to_encode = transform_inputs(inputs,frozen_bits,N); |
| 22 | + %encode |
| 23 | + encoded_inputs = encode(inputs_to_encode,Fn); %Reversed Polar Encoding |
| 24 | + %modulate |
| 25 | + modulated_inputs = modulate(encoded_inputs);%encoded_inputs); %BPSK = 1-2*encoded_inputs(i) |
| 26 | + %noise |
| 27 | + noised_inputs = add_noise(modulated_inputs,constDims,Fading_Channel,Fading_Independent,fading_channel,snrdb_values(i_index)); |
| 28 | + %demodulate |
| 29 | + 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) |
| 30 | + %decode |
| 31 | + if(fast_run) |
| 32 | + %optimal version (optimal-calculations of f/g) |
| 33 | + outputs = decode2(llr,frozen_bits); |
| 34 | + else |
| 35 | + %hardware-version (suboptimal-calculations of f/g) |
| 36 | + outputs = decode(llr,frozen_bits,partial_sum_adders,sc_functions,sc_2nd_indxs); %or decode2(llr,frozen_bits); for the other algorithm |
| 37 | + end |
| 38 | + %Calculate temporary bit/frame errors |
| 39 | + final_outputs = transform_outputs(outputs,frozen_bits,N); |
| 40 | + temp_bit_errors = size(find(final_outputs ~= inputs),2); |
| 41 | + bit_errors = bit_errors + temp_bit_errors; |
| 42 | + fer_errors = fer_errors + (temp_bit_errors>0); |
| 43 | + end |
| 44 | + %Calculate snr bit/frame errors |
| 45 | + bit_error_rate(index_n,i_index) = bit_errors/(codewords_tmp*K); |
| 46 | + fer_error_rate(index_n,i_index) = fer_errors/(codewords_tmp); |
| 47 | + codewords(index_n,i_index) = codewords_tmp; |
| 48 | + legends(index_n) = [num2str(N), '/', num2str(K)]; |
| 49 | + %Update plot figures |
| 50 | + %Save results for particular n & snr |
| 51 | + save(mat_file,'snrdb_values','EbNo_dB','bit_error_rate','fer_error_rate','codewords','N','K','Fading_Channel','Fading_Independent','fading_channel'); |
| 52 | + %Display |
| 53 | + fprintf('EbNo = %.1f\tber=%0.5f,fer=%0.5f\n',EbNo_dB(i_index),bit_error_rate(index_n,i_index),fer_error_rate(index_n,i_index)); |
| 54 | + end |
| 55 | + % file = fopen([result_path 'results_N=' num2str(N) '_SNR=' num2str(snrdb_values(1)) '-' num2str(snrdb_values(end)) '.txt'],'w'); |
42 | 56 | end |
43 | | -%or decode(llr,frozen_bits); for the other algorithm |
44 | | -%choose A set (not frozen bits) |
45 | | -%Calculate bit/frame errors |
46 | | -final_outputs = transform_outputs(outputs,frozen_bits,N); |
47 | | -temp_bit_errors = size(find(final_outputs ~= inputs),2); |
48 | | -bit_errors = bit_errors + temp_bit_errors; |
49 | | -fer_errors = fer_errors + (temp_bit_errors>0); |
50 | | -end |
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 = %.1f\tber=%0.2f,fer=%0.2f\n',snrdb_values(i_index),bit_error_rate(i_index),fer_error_rate(i_index)); |
56 | | -end |
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'); |
59 | | -end |
60 | | - |
| 57 | +plot_script; |
0 commit comments