|
1 | 1 | clc;clear; |
2 | 2 | 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 |
18 | 5 | 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); |
21 | 13 | 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; |
27 | 19 | fer_errors = 0; |
28 | 20 | 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 |
33 | 25 | %transform inputs |
34 | 26 | inputs_to_encode = transform_inputs(inputs,frozen_bits,N); |
35 | 27 | %encode |
36 | | -encoded_inputs = encode(inputs_to_encode); %Reversed Polar Encoding |
| 28 | +encoded_inputs = encode(inputs_to_encode,Fn); %Reversed Polar Encoding |
37 | 29 | %modulate |
38 | 30 | modulated_inputs = modulate(encoded_inputs);%encoded_inputs); %BPSK = 1-2*encoded_inputs(i) |
39 | 31 | %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)); |
41 | 33 | %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) |
43 | 35 | %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 |
45 | 43 | %or decode(llr,frozen_bits); for the other algorithm |
46 | 44 | %choose A set (not frozen bits) |
47 | 45 | %Calculate bit/frame errors |
|
50 | 48 | bit_errors = bit_errors + temp_bit_errors; |
51 | 49 | fer_errors = fer_errors + (temp_bit_errors>0); |
52 | 50 | 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)); |
57 | 56 | 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'); |
60 | 59 | end |
61 | | -%fprintf(file,'SNRmean = %2.5f]\n',SNRmean/20); |
62 | | -fclose(file); |
63 | 60 |
|
0 commit comments