├── pc_simulate_launcher.sh ├── pc_construction_launcher.sh ├── MATLAB ├── mex_pc_decode.mexmaci64 ├── mex_pc_encode.mexmaci64 ├── pc_encode.m ├── pc_decode.m ├── pc_compile.m ├── get_bhattacharyya.m └── get_error_rate.m ├── PolarCodes.xcodeproj ├── project.xcworkspace │ └── contents.xcworkspacedata ├── xcuserdata │ └── Nicola.xcuserdatad │ │ └── xcschemes │ │ ├── xcschememanagement.plist │ │ └── PolarCodes.xcscheme └── project.pbxproj ├── PolarCodes ├── double_ei.h ├── pc_encode.h ├── pc_decode.h ├── pc_construction.h ├── pc_decode_internal.h ├── channels.h ├── polar_codes.h ├── error_rate.h ├── bhattacharyya_files.h ├── pc_encode.c ├── polar_codes.c ├── double_ei.c ├── pc_decode.c ├── channels.c ├── pc_decode_internal.c ├── pc_construction.c ├── error_rate.c └── bhattacharyya_files.c ├── qbatch_pc_construction.sh ├── qbatch_pc_simulate.sh ├── batch_pc_construction.sh ├── PCTest_double_ei ├── main.c └── Test_double_ei.1 ├── test.sh ├── Makefile ├── PC_pc_construction ├── main.c └── pc_construction.1 ├── PC_pc_simulate ├── pc_simulate.1 └── main.c └── .gitignore /pc_simulate_launcher.sh: -------------------------------------------------------------------------------- 1 | 2 | /home/ndf/pc/pc_simulate $* 3 | -------------------------------------------------------------------------------- /pc_construction_launcher.sh: -------------------------------------------------------------------------------- 1 | 2 | /home/ndf/pc/pc_construction $* 3 | -------------------------------------------------------------------------------- /MATLAB/mex_pc_decode.mexmaci64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicoladefranceschi/PolarCodes/HEAD/MATLAB/mex_pc_decode.mexmaci64 -------------------------------------------------------------------------------- /MATLAB/mex_pc_encode.mexmaci64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nicoladefranceschi/PolarCodes/HEAD/MATLAB/mex_pc_encode.mexmaci64 -------------------------------------------------------------------------------- /PolarCodes.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /MATLAB/pc_encode.m: -------------------------------------------------------------------------------- 1 | function [ x ] = pc_encode( u ) 2 | %PC_ENCODE_C Summary of this function goes here 3 | % Detailed explanation goes here 4 | u=uint8(u); 5 | x=uint8(zeros(size(u))); 6 | for i=1:size(u,1) 7 | x(i,:)=mex_pc_encode(u(i,:)); 8 | end 9 | 10 | end 11 | 12 | -------------------------------------------------------------------------------- /PolarCodes/double_ei.h: -------------------------------------------------------------------------------- 1 | // 2 | // double_ei.h 3 | // 4 | // 5 | // Created by Nicola De Franceschi on 25/10/13. 6 | // 7 | // 8 | 9 | #ifndef double_ei_h 10 | #define double_ei_h 11 | 12 | #include 13 | 14 | void double_fput(FILE *out, double x); 15 | double double_fget(FILE *in); 16 | 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /MATLAB/pc_decode.m: -------------------------------------------------------------------------------- 1 | function [ u_ ] = pc_decode( y, ch_type, ch_par, A) 2 | %PC_DECODE_C Summary of this function goes here 3 | % Detailed explanation goes here 4 | 5 | y=uint8(y); 6 | u_=uint8(zeros(size(y))); 7 | for i=1:size(y,1) 8 | u_(i,:) = mex_pc_decode(y(i,:), ch_type, ch_par, uint8(A)); 9 | end 10 | 11 | 12 | end 13 | 14 | -------------------------------------------------------------------------------- /MATLAB/pc_compile.m: -------------------------------------------------------------------------------- 1 | 2 | % compile mex files 3 | 4 | 5 | 6 | mex ../PolarCodes/pc_encode_mex.c ../PolarCodes/pc_encode.c -output 'mex_pc_encode' 7 | mex ../PolarCodes/pc_decode_mex.c ../PolarCodes/channels.c ../PolarCodes/polar_codes.c ../PolarCodes/pc_decode.c ../PolarCodes/pc_decode_internal.c -output 'mex_pc_decode' 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /PolarCodes/pc_encode.h: -------------------------------------------------------------------------------- 1 | // 2 | // pc_encode.h 3 | // PolarCodes 4 | // 5 | // Created by Nicola De Franceschi on 03/10/13. 6 | // Copyright (c) 2013 Nicola De Franceschi. All rights reserved. 7 | // 8 | 9 | #ifndef PolarCodes_pc_encode_h 10 | #define PolarCodes_pc_encode_h 11 | 12 | #include "polar_codes.h" 13 | 14 | 15 | void pc_encode(Bit* xOut, Bit* u, u_int64_t n); 16 | 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /PolarCodes/pc_decode.h: -------------------------------------------------------------------------------- 1 | // 2 | // pc_decode.h 3 | // PolarCodes 4 | // 5 | // Created by Nicola De Franceschi on 03/10/13. 6 | // Copyright (c) 2013 Nicola De Franceschi. All rights reserved. 7 | // 8 | 9 | #ifndef PolarCodes_pc_decode_h 10 | #define PolarCodes_pc_decode_h 11 | 12 | #include "polar_codes.h" 13 | #include "channels.h" 14 | 15 | void pc_decode(Bit *u_, Bit *y, const Channel *channel, Bit *A, u_int64_t n); 16 | 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /PolarCodes/pc_construction.h: -------------------------------------------------------------------------------- 1 | // 2 | // pc_construction.h 3 | // PolarCodes 4 | // 5 | // Created by Nicola De Franceschi on 03/10/13. 6 | // Copyright (c) 2013 Nicola De Franceschi. All rights reserved. 7 | // 8 | 9 | #ifndef PolarCodes_pc_construction_h 10 | #define PolarCodes_pc_construction_h 11 | 12 | #include "polar_codes.h" 13 | #include "channels.h" 14 | 15 | 16 | void pc_construction(double *Z, double *Zvar, u_int64_t n, const Channel *channel, u_int64_t n_samples); 17 | 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /qbatch_pc_construction.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | HOMEDIR="/home/ndf/pc" 4 | 5 | NN="8 9 10 11 12 13 14 15 16 17 18 19 20" 6 | CH_TYPE="1" 7 | CH_PAR="0.2 0.25 0.3 0.35 0.4 0.45" 8 | 9 | N_SAMPLES=100 10 | MAX_N_SAMPLES=-1 11 | 12 | OUTDIR="outputs/" 13 | 14 | 15 | for N in $NN; do 16 | for ctype in $CH_TYPE; do 17 | for cpar in $CH_PAR; do 18 | 19 | qsub $HOMEDIR/pc_construction_launcher.sh $N $ctype $cpar $N_SAMPLES $MAX_N_SAMPLES "$HOMEDIR/$OUTDIR" 20 | 21 | done 22 | done 23 | done 24 | 25 | 26 | -------------------------------------------------------------------------------- /qbatch_pc_simulate.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | HOMEDIR="/home/ndf/pc" 4 | 5 | NN="8 9 10 11 12 13 14 15 16 17 18 19 20" 6 | CH_TYPE="1" 7 | CH_PAR="0.2 0.25 0.3 0.35 0.4 0.45" 8 | Kmin="0.1" 9 | Kdelta="0.05" 10 | Kmax="0.5" 11 | 12 | N_SAMPLES=100 13 | MAX_N_SAMPLES=-1 14 | 15 | OUTDIR="outputs2/" 16 | 17 | for N in $NN; do 18 | for ctype in $CH_TYPE; do 19 | for cpar in $CH_PAR; do 20 | 21 | qsub $HOMEDIR/pc_simulate_launcher.sh $N $ctype $cpar $Kmin $Kdelta $Kmax $N_SAMPLES $MAX_N_SAMPLES "$HOMEDIR/$OUTDIR" 22 | 23 | done 24 | done 25 | done 26 | 27 | 28 | -------------------------------------------------------------------------------- /PolarCodes/pc_decode_internal.h: -------------------------------------------------------------------------------- 1 | // 2 | // pc_decode_internal.h 3 | // PolarCodes 4 | // 5 | // Created by Nicola De Franceschi on 03/10/13. 6 | // Copyright (c) 2013 Nicola De Franceschi. All rights reserved. 7 | // 8 | 9 | #ifndef PolarCodes_pc_decode_internal_h 10 | #define PolarCodes_pc_decode_internal_h 11 | 12 | #include "polar_codes.h" 13 | 14 | 15 | void pc_decode_calc_f(double *new_val, double *val, u_int64_t step, u_int64_t n); 16 | void pc_decode_calc_g(double *new_val, double *val, u_int64_t step, u_int64_t n, Bit *u, u_int64_t uindex); 17 | 18 | 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /batch_pc_construction.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 4 | cd "$DIR" 5 | 6 | NN="8 9 10 11 12 13 14 15 16 17 18 19 20" 7 | CH_TYPE="1" 8 | CH_PAR="0.2 0.25 0.3 0.35 0.4 0.45" 9 | 10 | N_SAMPLES=100 11 | MAX_N_SAMPLES=-1 12 | 13 | OUTDIR="outputs/" 14 | 15 | echo "Press any key to exit... " 16 | 17 | for N in $NN; do 18 | for ctype in $CH_TYPE; do 19 | for cpar in $CH_PAR; do 20 | 21 | ./pc_construction $N $ctype $cpar $N_SAMPLES $MAX_N_SAMPLES $OUTDIR > /dev/null & 22 | 23 | done 24 | done 25 | done 26 | 27 | read -p "Press any key to exit... " -n1 -s 28 | 29 | killall pc_construction 30 | 31 | -------------------------------------------------------------------------------- /PCTest_double_ei/main.c: -------------------------------------------------------------------------------- 1 | // 2 | // main.c 3 | // PolarCodes 4 | // 5 | // Created by Nicola De Franceschi on 25/10/13. 6 | // Copyright (c) 2013 Nicola De Franceschi. All rights reserved. 7 | // 8 | 9 | #include 10 | 11 | #include "../PolarCodes/double_ei.h" 12 | 13 | int main(int argc, const char * argv[]) 14 | { 15 | 16 | double d = 1234.0/(1<<5); 17 | 18 | printf("%f ",d); 19 | 20 | double_fput(stdout, d); 21 | printf("\n"); 22 | 23 | double i = double_fget(stdin); 24 | double i2 = double_fget(stdin); 25 | 26 | printf("%f\n",i); 27 | printf("%f\n",i2); 28 | 29 | 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 4 | cd "$DIR" 5 | 6 | NN="8 9 10 11 12 13 14 15 16 17 18 19 20" 7 | CH_TYPE="1" 8 | CH_PAR="0.2 0.25 0.3 0.35 0.4 0.45" 9 | 10 | N_SAMPLES=100 11 | MAX_N_SAMPLES=-1 12 | 13 | OUTDIR="outputs/" 14 | 15 | echo "Press any key to exit... " 16 | 17 | for N in $NN; do 18 | for ctype in $CH_TYPE; do 19 | for cpar in $CH_PAR; do 20 | 21 | ./pc_construction $N $ctype $cpar $N_SAMPLES $MAX_N_SAMPLES $OUTDIR > /dev/null & 22 | 23 | cat 24 | 25 | exit 26 | 27 | done 28 | done 29 | done 30 | 31 | read -p "Press any key to exit... " -n1 -s 32 | 33 | killall pc_construction 34 | 35 | -------------------------------------------------------------------------------- /PolarCodes/channels.h: -------------------------------------------------------------------------------- 1 | // 2 | // channels.h 3 | // PolarCodes 4 | // 5 | // Created by Nicola De Franceschi on 03/10/13. 6 | // Copyright (c) 2013 Nicola De Franceschi. All rights reserved. 7 | // 8 | 9 | #ifndef PolarCodes_channels_h 10 | #define PolarCodes_channels_h 11 | 12 | #include "polar_codes.h" 13 | 14 | typedef enum { 15 | ChannelType_BSC = 1, 16 | ChannelType_BEC = 2 17 | } ChannelType; 18 | 19 | 20 | typedef struct { 21 | ChannelType channel_type; 22 | double par; 23 | } Channel; 24 | 25 | 26 | void apply_channel(Bit *yOut, Bit *x, const Channel *channel, u_int64_t n); 27 | 28 | double channel_probability(const Channel *channel, Bit bOut, Bit bIn); 29 | 30 | double channel_capacity(const Channel *channel); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /MATLAB/get_bhattacharyya.m: -------------------------------------------------------------------------------- 1 | function [ total_samples, Z, Zstd ] = get_bhattacharyya( fold, n, ch_type, ch_par ) 2 | %UNTITLED2 Summary of this function goes here 3 | % Detailed explanation goes here 4 | 5 | ch_par_txt = num2str(ch_par,'%0.6f'); 6 | filename = [fold '/' 'bh_' num2str(n) '_ch_' num2str(ch_type) '_par_' ch_par_txt '.txt']; 7 | 8 | fid = fopen(filename, 'r'); 9 | 10 | if fid == -1 11 | error 'no file' 12 | end 13 | total_samples=fscanf(fid, '%ld',1); 14 | 15 | N=2^n; 16 | Z=zeros(1,N); 17 | Zvar=zeros(1,N); 18 | 19 | for i=1:N 20 | 21 | Z(i)=double_fget(fid); 22 | Zvar(i)=double_fget(fid); 23 | 24 | end 25 | 26 | Z=Z./double(total_samples); 27 | Zstd=sqrt( Zvar./double(total_samples) - Z.^2 ); 28 | 29 | fclose(fid); 30 | 31 | end 32 | 33 | -------------------------------------------------------------------------------- /PolarCodes/polar_codes.h: -------------------------------------------------------------------------------- 1 | // 2 | // polar_codes.h 3 | // PolarCodes 4 | // 5 | // Created by Nicola De Franceschi on 03/10/13. 6 | // Copyright (c) 2013 Nicola De Franceschi. All rights reserved. 7 | // 8 | 9 | #ifndef PolarCodes_polar_codes_h 10 | #define PolarCodes_polar_codes_h 11 | 12 | #include 13 | 14 | 15 | #define MIN_TRANSITION_PROBABILITY (1.0 / (double)(1 << 30)) 16 | #define MAX_TRANSITION_PROBABILITY ((double)(1 << 30)) 17 | 18 | 19 | typedef u_int8_t Bit; 20 | 21 | 22 | void random_bits(Bit *bits, u_int64_t N); 23 | void random_bits_f(Bit *bits, Bit *A, u_int64_t N); 24 | 25 | void print_bits(Bit *bits, u_int64_t N); 26 | void print_doubles(double *doubles, u_int64_t N); 27 | 28 | u_int64_t bit_reverse_index(u_int64_t index, u_int64_t n); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /PolarCodes/error_rate.h: -------------------------------------------------------------------------------- 1 | // 2 | // error_rate.h 3 | // PolarCodes 4 | // 5 | // Created by Nicola De Franceschi on 27/10/13. 6 | // Copyright (c) 2013 Nicola De Franceschi. All rights reserved. 7 | // 8 | 9 | #ifndef PolarCodes_error_rate_h 10 | #define PolarCodes_error_rate_h 11 | 12 | #include 13 | #include 14 | #include "channels.h" 15 | 16 | typedef struct { 17 | u_int64_t n; 18 | Channel channel; 19 | u_int64_t K; 20 | u_int64_t total_samples; 21 | 22 | double BER; 23 | double BER_2; 24 | 25 | u_int64_t frame_errors; 26 | 27 | } ErrorRate; 28 | 29 | 30 | void get_error_rate(ErrorRate *er, const char *dir, u_int64_t n, Channel *channel, u_int64_t K); 31 | void get_or_create_error_rate(ErrorRate *er, const char *dir, u_int64_t n, Channel *channel, u_int64_t K); 32 | void save_error_rate(ErrorRate *er, const char *dir); 33 | 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /PolarCodes/bhattacharyya_files.h: -------------------------------------------------------------------------------- 1 | // 2 | // bhattacharyya_files.h 3 | // PolarCodes 4 | // 5 | // Created by Nicola De Franceschi on 25/10/13. 6 | // Copyright (c) 2013 Nicola De Franceschi. All rights reserved. 7 | // 8 | 9 | #ifndef PolarCodes_bhattacharyya_files_h 10 | #define PolarCodes_bhattacharyya_files_h 11 | 12 | #include 13 | #include 14 | #include "channels.h" 15 | 16 | typedef struct { 17 | u_int64_t n; 18 | Channel channel; 19 | u_int64_t total_samples; 20 | double *Z; 21 | double *Zvar; 22 | } Bhattacharyya; 23 | 24 | void get_bhattacharyya(Bhattacharyya *b, const char *dir, u_int64_t n, Channel *channel); 25 | void get_or_create_bhattacharyya(Bhattacharyya *b, const char *dir, u_int64_t n, Channel *channel); 26 | void save_bhattacharyya(Bhattacharyya *b, const char *dir); 27 | 28 | void get_frozen_bits(Bit *bits, u_int64_t K, Bhattacharyya *b); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /MATLAB/get_error_rate.m: -------------------------------------------------------------------------------- 1 | function [ total_samples, BER, BERstddev, frame_errors ] = get_error_rate( fold, n, ch_type, ch_par, K) 2 | %GET_ERROR_RATE Summary of this function goes here 3 | % Detailed explanation goes here 4 | % 5 | % GET_ERROR_RATE(dir, n, ch_type, ch_par, K) 6 | % 7 | % GET_ERROR_RATE(filename) 8 | 9 | if nargin == 1 10 | filename = fold; 11 | else 12 | ch_par_txt = num2str(ch_par,'%0.6f'); 13 | filename = [fold '/' 'er_' num2str(n) '_ch_' num2str(ch_type) '_par_' ch_par_txt '_k_' num2str(K) '.txt']; 14 | end 15 | 16 | fid = fopen(filename, 'r'); 17 | 18 | if fid == -1 19 | error 'no file' 20 | end 21 | 22 | total_samples=fscanf(fid, '%ld',1); 23 | BER=double_fget(fid); 24 | BER_2=double_fget(fid); 25 | frame_errors=fscanf(fid, '%ld',1); 26 | 27 | fclose(fid); 28 | 29 | 30 | BER = BER / double(total_samples); 31 | BERstddev = sqrt( BER_2 / double(total_samples) - BER^2 ); 32 | 33 | end 34 | 35 | -------------------------------------------------------------------------------- /PolarCodes/pc_encode.c: -------------------------------------------------------------------------------- 1 | // 2 | // pc_encode.c 3 | // PolarCodes 4 | // 5 | // Created by Nicola De Franceschi on 03/10/13. 6 | // Copyright (c) 2013 Nicola De Franceschi. All rights reserved. 7 | // 8 | 9 | #include 10 | #include "pc_encode.h" 11 | #include 12 | 13 | void pc_encode(Bit* xOut, Bit* u, u_int64_t n) { 14 | 15 | u_int64_t N = 1 << n; 16 | 17 | Bit *in = u; 18 | 19 | u_int64_t step; 20 | for(step = 1; step <= n; step++){ 21 | 22 | u_int64_t group_size = 1 << step; 23 | u_int64_t op_per_group = group_size >> 1; 24 | 25 | u_int64_t i; 26 | for(i=0; i 1 36 | } 37 | 38 | in = xOut; 39 | 40 | } 41 | 42 | } 43 | 44 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | HEADERS = PolarCodes/bhattacharyya_files.h \ 3 | PolarCodes/channels.h \ 4 | PolarCodes/double_ei.h \ 5 | PolarCodes/pc_construction.h \ 6 | PolarCodes/pc_decode_internal.h \ 7 | PolarCodes/pc_decode.h \ 8 | PolarCodes/pc_encode.h \ 9 | PolarCodes/polar_codes.h \ 10 | PolarCodes/error_rate.h 11 | 12 | OBJECTS = PolarCodes/bhattacharyya_files.o \ 13 | PolarCodes/channels.o \ 14 | PolarCodes/double_ei.o \ 15 | PolarCodes/pc_construction.o \ 16 | PolarCodes/pc_decode_internal.o \ 17 | PolarCodes/pc_decode.o \ 18 | PolarCodes/pc_encode.o \ 19 | PolarCodes/polar_codes.o \ 20 | PolarCodes/error_rate.o 21 | 22 | 23 | MAIN_FILES = PC_pc_construction/main.o PC_pc_simulate/main.o 24 | 25 | EXECUTABLES = pc_construction pc_simulate 26 | 27 | 28 | all: $(EXECUTABLES) 29 | default: all 30 | 31 | 32 | 33 | pc_construction: $(OBJECTS) PC_pc_construction/main.o 34 | gcc $(OBJECTS) PC_pc_construction/main.o -lm -o $@ 35 | 36 | pc_simulate: $(OBJECTS) PC_pc_simulate/main.o 37 | gcc $(OBJECTS) PC_pc_simulate/main.o -lm -o $@ 38 | 39 | 40 | 41 | 42 | %.o: %.c $(HEADERS) 43 | gcc -c $< -o $@ 44 | 45 | clean: 46 | -rm -f $(OBJECTS) $(MAIN_FILES) 47 | -rm -f $(EXECUTABLES) 48 | 49 | -------------------------------------------------------------------------------- /PolarCodes/polar_codes.c: -------------------------------------------------------------------------------- 1 | // 2 | // polar_codes.c 3 | // PolarCodes 4 | // 5 | // Created by Nicola De Franceschi on 03/10/13. 6 | // Copyright (c) 2013 Nicola De Franceschi. All rights reserved. 7 | // 8 | 9 | #include 10 | #include "polar_codes.h" 11 | 12 | 13 | void random_bits(Bit *bits, u_int64_t N) { 14 | u_int64_t i; 15 | for(i=0; i> i) & 1) << (n-i-1); 60 | } 61 | return index2; 62 | } 63 | -------------------------------------------------------------------------------- /PolarCodes.xcodeproj/xcuserdata/Nicola.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | PolarCodes.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | Test_bec.xcscheme 13 | 14 | orderHint 15 | 4 16 | 17 | Test_double_ei.xcscheme 18 | 19 | orderHint 20 | 1 21 | 22 | pc_construction.xcscheme 23 | 24 | orderHint 25 | 2 26 | 27 | pc_simulate.xcscheme 28 | 29 | orderHint 30 | 3 31 | 32 | 33 | SuppressBuildableAutocreation 34 | 35 | B31E0A2A181CBCF100E941E5 36 | 37 | primary 38 | 39 | 40 | B3B7193B181B092300055AD4 41 | 42 | primary 43 | 44 | 45 | B3B71975181B0C5500055AD4 46 | 47 | primary 48 | 49 | 50 | B3B71991181B0CFC00055AD4 51 | 52 | primary 53 | 54 | 55 | B3E4D42F1843CE3700EAA309 56 | 57 | primary 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /PolarCodes/double_ei.c: -------------------------------------------------------------------------------- 1 | // 2 | // double_ei.c 3 | // 4 | // 5 | // Created by Nicola De Franceschi on 25/10/13. 6 | // 7 | // 8 | 9 | #include 10 | #include 11 | #include 12 | #include "double_ei.h" 13 | 14 | 15 | #define FRAC_MAX 9223372036854775807LL /* 2**63 - 1 */ 16 | 17 | 18 | struct double_packed 19 | { 20 | int exp; 21 | long long frac; 22 | }; 23 | 24 | void double_pack(double x, struct double_packed *r); 25 | double double_unpack(const struct double_packed *p); 26 | 27 | 28 | void double_pack(double x, struct double_packed *r) 29 | { 30 | double xf = fabs(frexp(x, &r->exp)) - 0.5; 31 | 32 | if (xf < 0.0) 33 | { 34 | r->frac = 0; 35 | return; 36 | } 37 | 38 | r->frac = 1 + (long long)(xf * 2.0 * (FRAC_MAX - 1)); 39 | 40 | if (x < 0.0) 41 | r->frac = -r->frac; 42 | } 43 | 44 | double double_unpack(const struct double_packed *p) 45 | { 46 | double xf, x; 47 | 48 | if (p->frac == 0) 49 | return 0.0; 50 | 51 | xf = ((double)(llabs(p->frac) - 1) / (FRAC_MAX - 1)) / 2.0; 52 | 53 | x = ldexp(xf + 0.5, p->exp); 54 | 55 | if (p->frac < 0) 56 | x = -x; 57 | 58 | return x; 59 | } 60 | 61 | void double_fput(FILE *out, double x) { 62 | struct double_packed dp; 63 | double_pack(x, &dp); 64 | fprintf(out, "%d %lld", dp.exp, dp.frac); 65 | } 66 | 67 | double double_fget(FILE *in) { 68 | struct double_packed dp; 69 | int n = fscanf(in, "%d %lld", &dp.exp, &dp.frac); 70 | if(n != 2) 71 | exit(1); 72 | return double_unpack(&dp); 73 | } 74 | -------------------------------------------------------------------------------- /PC_pc_construction/main.c: -------------------------------------------------------------------------------- 1 | // 2 | // main.c 3 | // pc_construction 4 | // 5 | // Created by Nicola De Franceschi on 25/10/13. 6 | // Copyright (c) 2013 Nicola De Franceschi. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "../PolarCodes/pc_construction.h" 15 | #include "../PolarCodes/bhattacharyya_files.h" 16 | 17 | 18 | #define DEFAULT_max_n_samples (1<<30) 19 | 20 | 21 | // call with arguments: n ch_type ch_par n_samples max_n_samples dir 22 | // max_n_samples==-1 for default 23 | int main(int argc, const char * argv[]) 24 | { 25 | 26 | //random initialization 27 | struct timeval time; 28 | gettimeofday(&time,NULL); 29 | srand((unsigned int)(time.tv_sec + time.tv_usec)); 30 | 31 | 32 | Channel channel; 33 | u_int64_t n; 34 | u_int64_t n_samples; 35 | int64_t max_n_samples; 36 | const char *dir; 37 | 38 | if(argc != 7) 39 | exit(2); 40 | 41 | 42 | //n 43 | sscanf(argv[1], "%" PRIu64, &n); 44 | 45 | //ch_type 46 | sscanf(argv[2], "%d", &(channel.channel_type)); 47 | 48 | //ch_par 49 | sscanf(argv[3], "%lf", &(channel.par)); 50 | 51 | //n_samples 52 | sscanf(argv[4], "%" PRIu64, &n_samples); 53 | 54 | //max_n_samples 55 | sscanf(argv[5], "%" PRId64, &max_n_samples); 56 | if(max_n_samples == -1) 57 | max_n_samples=DEFAULT_max_n_samples; 58 | 59 | //dir 60 | dir = argv[6]; 61 | 62 | Bhattacharyya b; 63 | get_or_create_bhattacharyya(&b, dir, n, &channel); 64 | 65 | while (1) { 66 | 67 | pc_construction(b.Z, b.Zvar, n, &(b.channel), n_samples); 68 | b.total_samples += n_samples; 69 | 70 | save_bhattacharyya(&b, dir); 71 | printf("total_samples = %lld\n", b.total_samples); 72 | 73 | if(b.total_samples > max_n_samples) 74 | break; 75 | } 76 | 77 | return 0; 78 | } 79 | 80 | 81 | -------------------------------------------------------------------------------- /PolarCodes/pc_decode.c: -------------------------------------------------------------------------------- 1 | // 2 | // pc_decode.c 3 | // PolarCodes 4 | // 5 | // Created by Nicola De Franceschi on 03/10/13. 6 | // Copyright (c) 2013 Nicola De Franceschi. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | #include 12 | #include "pc_decode.h" 13 | #include "pc_decode_internal.h" 14 | 15 | 16 | void pc_decode_step(Bit *u_, Bit *A, u_int64_t n, u_int64_t step, double *val, u_int64_t *uindex); 17 | 18 | 19 | void pc_decode(Bit *u_, Bit *y, const Channel *channel, Bit *A, u_int64_t n) { 20 | 21 | u_int64_t N = 1 << n; 22 | u_int64_t uindex = 0; 23 | 24 | double *delta = malloc(N*sizeof(double)); 25 | u_int64_t i; 26 | for(i = 0; i < N; i++){ 27 | 28 | u_int64_t i2 = bit_reverse_index(i, n); 29 | double n,d,v; 30 | 31 | n = channel_probability(channel, y[i2], 0); 32 | d = channel_probability(channel, y[i2], 1); 33 | 34 | if(d == 0){ 35 | v = MAX_TRANSITION_PROBABILITY; 36 | }else{ 37 | v = n/d; 38 | if(v < MIN_TRANSITION_PROBABILITY){ 39 | v = MIN_TRANSITION_PROBABILITY; 40 | }else if(v > MAX_TRANSITION_PROBABILITY){ 41 | v = MAX_TRANSITION_PROBABILITY; 42 | } 43 | } 44 | 45 | delta[i] = v; 46 | 47 | } 48 | 49 | pc_decode_step(u_, A, n, 1, delta, &uindex); 50 | 51 | free(delta); 52 | 53 | } 54 | 55 | void pc_decode_update_u_(Bit *u_, Bit *A, double v, u_int64_t *uindex){ 56 | 57 | if(A[*uindex] == 0){ 58 | u_[*uindex] = 0; 59 | }else if(v >= 1){ 60 | u_[*uindex] = 0; 61 | }else{ 62 | u_[*uindex] = 1; 63 | } 64 | 65 | (*uindex)++; 66 | } 67 | 68 | void pc_decode_step(Bit *u_, Bit *A, u_int64_t n, u_int64_t step, double *val, u_int64_t *uindex){ 69 | 70 | u_int64_t D = 1 << (n-step); 71 | double *new_val = malloc(sizeof(double)*D); 72 | 73 | //calcolate f 74 | pc_decode_calc_f(new_val, val, step, n); 75 | 76 | if(step == n){ 77 | pc_decode_update_u_(u_, A, new_val[0], uindex); 78 | }else{ 79 | pc_decode_step(u_, A, n, step+1, new_val, uindex); 80 | } 81 | 82 | 83 | //calcolate f 84 | pc_decode_calc_g(new_val, val, step, n, u_, *uindex); 85 | 86 | if(step == n){ 87 | pc_decode_update_u_(u_, A, new_val[0], uindex); 88 | }else{ 89 | pc_decode_step(u_, A, n, step+1, new_val, uindex); 90 | } 91 | 92 | free(new_val); 93 | } 94 | 95 | 96 | -------------------------------------------------------------------------------- /PolarCodes/channels.c: -------------------------------------------------------------------------------- 1 | // 2 | // channels.c 3 | // PolarCodes 4 | // 5 | // Created by Nicola De Franceschi on 03/10/13. 6 | // Copyright (c) 2013 Nicola De Franceschi. All rights reserved. 7 | // 8 | 9 | #include 10 | #include "channels.h" 11 | #include 12 | 13 | 14 | void apply_channel(Bit *yOut, Bit *x, const Channel *channel, u_int64_t n) { 15 | 16 | switch (channel->channel_type) { 17 | 18 | case ChannelType_BSC: 19 | { 20 | u_int64_t N = 1 << n; 21 | 22 | u_int64_t i; 23 | for(i=0; ipar) 28 | yOut[i] = (x[i] + 1) & 1; 29 | else 30 | yOut[i] = x[i]; 31 | 32 | } 33 | 34 | break; 35 | } 36 | 37 | case ChannelType_BEC: 38 | { 39 | 40 | u_int64_t N = 1 << n; 41 | 42 | u_int64_t i; 43 | for(i=0; ipar) 48 | yOut[i] = 2; 49 | else 50 | yOut[i] = x[i]; 51 | 52 | } 53 | 54 | break; 55 | } 56 | 57 | } 58 | 59 | } 60 | 61 | double channel_probability(const Channel *channel, Bit bOut, Bit bIn) { 62 | 63 | switch (channel->channel_type) { 64 | case ChannelType_BSC: 65 | { 66 | if(bOut != bIn) 67 | return channel->par; 68 | else 69 | return 1-channel->par; 70 | } 71 | 72 | case ChannelType_BEC: 73 | { 74 | if(bOut == 2) 75 | return channel->par; 76 | else if(bOut == bIn) 77 | return 1-channel->par; 78 | else 79 | return 0; 80 | 81 | } 82 | } 83 | 84 | } 85 | 86 | double channel_capacity(const Channel *channel) { 87 | 88 | switch (channel->channel_type) { 89 | case ChannelType_BSC: 90 | { 91 | double entropy = - channel->par*log2(channel->par) - (1-channel->par)*log2(1-channel->par); 92 | return 1-entropy; 93 | } 94 | 95 | case ChannelType_BEC: 96 | { 97 | return 1-channel->par; 98 | } 99 | } 100 | 101 | } 102 | 103 | -------------------------------------------------------------------------------- /PolarCodes/pc_decode_internal.c: -------------------------------------------------------------------------------- 1 | // 2 | // pc_decode_internal.c 3 | // PolarCodes 4 | // 5 | // Created by Nicola De Franceschi on 16/10/13. 6 | // Copyright (c) 2013 Nicola De Franceschi. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | #include "pc_decode_internal.h" 12 | 13 | void pc_decode_calc_f(double *new_val, double *val, u_int64_t step, u_int64_t n) { 14 | 15 | u_int64_t D = 1 << (n-step); 16 | 17 | u_int64_t i; 18 | for(i = 0; i < D; i++){ 19 | double a = val[2*i]; 20 | double b = val[2*i+1]; 21 | 22 | double v = (1+a*b) / (a+b); 23 | 24 | if(v < MIN_TRANSITION_PROBABILITY){ 25 | v = MIN_TRANSITION_PROBABILITY; 26 | }else if(v > MAX_TRANSITION_PROBABILITY){ 27 | v = MAX_TRANSITION_PROBABILITY; 28 | } 29 | 30 | new_val[i] = v; 31 | 32 | } 33 | 34 | } 35 | 36 | void pc_decode_calc_us(Bit *usOut, u_int64_t stage, Bit *u, u_int64_t uindex){ 37 | 38 | u_int64_t dim = 1 << stage; 39 | u_int64_t dim2 = 1; 40 | 41 | memcpy(usOut, u + uindex - dim, dim); 42 | 43 | u_int64_t s; 44 | 45 | Bit *temp2 = malloc(dim*sizeof(Bit)); 46 | 47 | for(s = 1; s <= stage; s++){ 48 | 49 | u_int64_t i1, i2; 50 | for(i1 = 0; i1 < 1<<(stage-s); i1++){ 51 | for(i2 = 0; i2 < 1<<(s-1); i2++){ 52 | 53 | Bit even = usOut[(2*i1+1)*dim2+i2]; //even in matlab! 54 | Bit odd = usOut[(2*i1)*dim2+i2]; //even in matlab! 55 | 56 | temp2[i1*dim2*2 + i2*2] = (even + odd) & 1; 57 | temp2[i1*dim2*2 + i2*2+1] = even; 58 | 59 | } 60 | } 61 | 62 | dim2 *= 2; 63 | 64 | memcpy(usOut, temp2, dim); 65 | 66 | } 67 | 68 | free(temp2); 69 | 70 | } 71 | 72 | void pc_decode_calc_g(double *new_val, double *val, u_int64_t step, u_int64_t n, Bit *u, u_int64_t uindex) { 73 | 74 | 75 | u_int64_t D = 1 << (n-step); 76 | 77 | u_int64_t i; 78 | 79 | Bit us[D]; 80 | pc_decode_calc_us(us, n-step, u, uindex); 81 | 82 | for(i = 0; i < D; i++){ 83 | double a = val[2*i]; 84 | double b = val[2*i+1]; 85 | double v; 86 | 87 | if(us[i] == 0){ 88 | v = b*a; 89 | }else{ 90 | v = b/a; 91 | } 92 | 93 | if(v < MIN_TRANSITION_PROBABILITY){ 94 | v = MIN_TRANSITION_PROBABILITY; 95 | }else if(v > MAX_TRANSITION_PROBABILITY){ 96 | v = MAX_TRANSITION_PROBABILITY; 97 | } 98 | 99 | new_val[i] = v; 100 | 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /PC_pc_simulate/pc_simulate.1: -------------------------------------------------------------------------------- 1 | .\"Modified from man(1) of FreeBSD, the NetBSD mdoc.template, and mdoc.samples. 2 | .\"See Also: 3 | .\"man mdoc.samples for a complete listing of options 4 | .\"man mdoc for the short list of editing options 5 | .\"/usr/share/misc/mdoc.template 6 | .Dd 26/10/13 \" DATE 7 | .Dt pc_simulate 1 \" Program name and manual section number 8 | .Os Darwin 9 | .Sh NAME \" Section Header - required - don't modify 10 | .Nm pc_simulate, 11 | .\" The following lines are read in generating the apropos(man -k) database. Use only key 12 | .\" words here as the database is built based on the words here and in the .ND line. 13 | .Nm Other_name_for_same_program(), 14 | .Nm Yet another name for the same program. 15 | .\" Use .Nm macro to designate other names for the documented program. 16 | .Nd This line parsed for whatis database. 17 | .Sh SYNOPSIS \" Section Header - required - don't modify 18 | .Nm 19 | .Op Fl abcd \" [-abcd] 20 | .Op Fl a Ar path \" [-a path] 21 | .Op Ar file \" [file] 22 | .Op Ar \" [file ...] 23 | .Ar arg0 \" Underlined argument - use .Ar anywhere to underline 24 | arg2 ... \" Arguments 25 | .Sh DESCRIPTION \" Section Header - required - don't modify 26 | Use the .Nm macro to refer to your program throughout the man page like such: 27 | .Nm 28 | Underlining is accomplished with the .Ar macro like this: 29 | .Ar underlined text . 30 | .Pp \" Inserts a space 31 | A list of items with descriptions: 32 | .Bl -tag -width -indent \" Begins a tagged list 33 | .It item a \" Each item preceded by .It macro 34 | Description of item a 35 | .It item b 36 | Description of item b 37 | .El \" Ends the list 38 | .Pp 39 | A list of flags and their descriptions: 40 | .Bl -tag -width -indent \" Differs from above in tag removed 41 | .It Fl a \"-a flag as a list item 42 | Description of -a flag 43 | .It Fl b 44 | Description of -b flag 45 | .El \" Ends the list 46 | .Pp 47 | .\" .Sh ENVIRONMENT \" May not be needed 48 | .\" .Bl -tag -width "ENV_VAR_1" -indent \" ENV_VAR_1 is width of the string ENV_VAR_1 49 | .\" .It Ev ENV_VAR_1 50 | .\" Description of ENV_VAR_1 51 | .\" .It Ev ENV_VAR_2 52 | .\" Description of ENV_VAR_2 53 | .\" .El 54 | .Sh FILES \" File used or created by the topic of the man page 55 | .Bl -tag -width "/Users/joeuser/Library/really_long_file_name" -compact 56 | .It Pa /usr/share/file_name 57 | FILE_1 description 58 | .It Pa /Users/joeuser/Library/really_long_file_name 59 | FILE_2 description 60 | .El \" Ends the list 61 | .\" .Sh DIAGNOSTICS \" May not be needed 62 | .\" .Bl -diag 63 | .\" .It Diagnostic Tag 64 | .\" Diagnostic informtion here. 65 | .\" .It Diagnostic Tag 66 | .\" Diagnostic informtion here. 67 | .\" .El 68 | .Sh SEE ALSO 69 | .\" List links in ascending order by section, alphabetically within a section. 70 | .\" Please do not reference files that do not exist without filing a bug report 71 | .Xr a 1 , 72 | .Xr b 1 , 73 | .Xr c 1 , 74 | .Xr a 2 , 75 | .Xr b 2 , 76 | .Xr a 3 , 77 | .Xr b 3 78 | .\" .Sh BUGS \" Document known, unremedied bugs 79 | .\" .Sh HISTORY \" Document history if command behaves in a unique manner -------------------------------------------------------------------------------- /PCTest_double_ei/Test_double_ei.1: -------------------------------------------------------------------------------- 1 | .\"Modified from man(1) of FreeBSD, the NetBSD mdoc.template, and mdoc.samples. 2 | .\"See Also: 3 | .\"man mdoc.samples for a complete listing of options 4 | .\"man mdoc for the short list of editing options 5 | .\"/usr/share/misc/mdoc.template 6 | .Dd 25/10/13 \" DATE 7 | .Dt Test_double_ei 1 \" Program name and manual section number 8 | .Os Darwin 9 | .Sh NAME \" Section Header - required - don't modify 10 | .Nm Test_double_ei, 11 | .\" The following lines are read in generating the apropos(man -k) database. Use only key 12 | .\" words here as the database is built based on the words here and in the .ND line. 13 | .Nm Other_name_for_same_program(), 14 | .Nm Yet another name for the same program. 15 | .\" Use .Nm macro to designate other names for the documented program. 16 | .Nd This line parsed for whatis database. 17 | .Sh SYNOPSIS \" Section Header - required - don't modify 18 | .Nm 19 | .Op Fl abcd \" [-abcd] 20 | .Op Fl a Ar path \" [-a path] 21 | .Op Ar file \" [file] 22 | .Op Ar \" [file ...] 23 | .Ar arg0 \" Underlined argument - use .Ar anywhere to underline 24 | arg2 ... \" Arguments 25 | .Sh DESCRIPTION \" Section Header - required - don't modify 26 | Use the .Nm macro to refer to your program throughout the man page like such: 27 | .Nm 28 | Underlining is accomplished with the .Ar macro like this: 29 | .Ar underlined text . 30 | .Pp \" Inserts a space 31 | A list of items with descriptions: 32 | .Bl -tag -width -indent \" Begins a tagged list 33 | .It item a \" Each item preceded by .It macro 34 | Description of item a 35 | .It item b 36 | Description of item b 37 | .El \" Ends the list 38 | .Pp 39 | A list of flags and their descriptions: 40 | .Bl -tag -width -indent \" Differs from above in tag removed 41 | .It Fl a \"-a flag as a list item 42 | Description of -a flag 43 | .It Fl b 44 | Description of -b flag 45 | .El \" Ends the list 46 | .Pp 47 | .\" .Sh ENVIRONMENT \" May not be needed 48 | .\" .Bl -tag -width "ENV_VAR_1" -indent \" ENV_VAR_1 is width of the string ENV_VAR_1 49 | .\" .It Ev ENV_VAR_1 50 | .\" Description of ENV_VAR_1 51 | .\" .It Ev ENV_VAR_2 52 | .\" Description of ENV_VAR_2 53 | .\" .El 54 | .Sh FILES \" File used or created by the topic of the man page 55 | .Bl -tag -width "/Users/joeuser/Library/really_long_file_name" -compact 56 | .It Pa /usr/share/file_name 57 | FILE_1 description 58 | .It Pa /Users/joeuser/Library/really_long_file_name 59 | FILE_2 description 60 | .El \" Ends the list 61 | .\" .Sh DIAGNOSTICS \" May not be needed 62 | .\" .Bl -diag 63 | .\" .It Diagnostic Tag 64 | .\" Diagnostic informtion here. 65 | .\" .It Diagnostic Tag 66 | .\" Diagnostic informtion here. 67 | .\" .El 68 | .Sh SEE ALSO 69 | .\" List links in ascending order by section, alphabetically within a section. 70 | .\" Please do not reference files that do not exist without filing a bug report 71 | .Xr a 1 , 72 | .Xr b 1 , 73 | .Xr c 1 , 74 | .Xr a 2 , 75 | .Xr b 2 , 76 | .Xr a 3 , 77 | .Xr b 3 78 | .\" .Sh BUGS \" Document known, unremedied bugs 79 | .\" .Sh HISTORY \" Document history if command behaves in a unique manner -------------------------------------------------------------------------------- /PC_pc_construction/pc_construction.1: -------------------------------------------------------------------------------- 1 | .\"Modified from man(1) of FreeBSD, the NetBSD mdoc.template, and mdoc.samples. 2 | .\"See Also: 3 | .\"man mdoc.samples for a complete listing of options 4 | .\"man mdoc for the short list of editing options 5 | .\"/usr/share/misc/mdoc.template 6 | .Dd 25/10/13 \" DATE 7 | .Dt pc_construction 1 \" Program name and manual section number 8 | .Os Darwin 9 | .Sh NAME \" Section Header - required - don't modify 10 | .Nm pc_construction, 11 | .\" The following lines are read in generating the apropos(man -k) database. Use only key 12 | .\" words here as the database is built based on the words here and in the .ND line. 13 | .Nm Other_name_for_same_program(), 14 | .Nm Yet another name for the same program. 15 | .\" Use .Nm macro to designate other names for the documented program. 16 | .Nd This line parsed for whatis database. 17 | .Sh SYNOPSIS \" Section Header - required - don't modify 18 | .Nm 19 | .Op Fl abcd \" [-abcd] 20 | .Op Fl a Ar path \" [-a path] 21 | .Op Ar file \" [file] 22 | .Op Ar \" [file ...] 23 | .Ar arg0 \" Underlined argument - use .Ar anywhere to underline 24 | arg2 ... \" Arguments 25 | .Sh DESCRIPTION \" Section Header - required - don't modify 26 | Use the .Nm macro to refer to your program throughout the man page like such: 27 | .Nm 28 | Underlining is accomplished with the .Ar macro like this: 29 | .Ar underlined text . 30 | .Pp \" Inserts a space 31 | A list of items with descriptions: 32 | .Bl -tag -width -indent \" Begins a tagged list 33 | .It item a \" Each item preceded by .It macro 34 | Description of item a 35 | .It item b 36 | Description of item b 37 | .El \" Ends the list 38 | .Pp 39 | A list of flags and their descriptions: 40 | .Bl -tag -width -indent \" Differs from above in tag removed 41 | .It Fl a \"-a flag as a list item 42 | Description of -a flag 43 | .It Fl b 44 | Description of -b flag 45 | .El \" Ends the list 46 | .Pp 47 | .\" .Sh ENVIRONMENT \" May not be needed 48 | .\" .Bl -tag -width "ENV_VAR_1" -indent \" ENV_VAR_1 is width of the string ENV_VAR_1 49 | .\" .It Ev ENV_VAR_1 50 | .\" Description of ENV_VAR_1 51 | .\" .It Ev ENV_VAR_2 52 | .\" Description of ENV_VAR_2 53 | .\" .El 54 | .Sh FILES \" File used or created by the topic of the man page 55 | .Bl -tag -width "/Users/joeuser/Library/really_long_file_name" -compact 56 | .It Pa /usr/share/file_name 57 | FILE_1 description 58 | .It Pa /Users/joeuser/Library/really_long_file_name 59 | FILE_2 description 60 | .El \" Ends the list 61 | .\" .Sh DIAGNOSTICS \" May not be needed 62 | .\" .Bl -diag 63 | .\" .It Diagnostic Tag 64 | .\" Diagnostic informtion here. 65 | .\" .It Diagnostic Tag 66 | .\" Diagnostic informtion here. 67 | .\" .El 68 | .Sh SEE ALSO 69 | .\" List links in ascending order by section, alphabetically within a section. 70 | .\" Please do not reference files that do not exist without filing a bug report 71 | .Xr a 1 , 72 | .Xr b 1 , 73 | .Xr c 1 , 74 | .Xr a 2 , 75 | .Xr b 2 , 76 | .Xr a 3 , 77 | .Xr b 3 78 | .\" .Sh BUGS \" Document known, unremedied bugs 79 | .\" .Sh HISTORY \" Document history if command behaves in a unique manner -------------------------------------------------------------------------------- /PolarCodes.xcodeproj/xcuserdata/Nicola.xcuserdatad/xcschemes/PolarCodes.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 51 | 52 | 58 | 59 | 60 | 61 | 62 | 63 | 69 | 70 | 76 | 77 | 78 | 79 | 81 | 82 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /PolarCodes/pc_construction.c: -------------------------------------------------------------------------------- 1 | // 2 | // pc_construction.c 3 | // PolarCodes 4 | // 5 | // Created by Nicola De Franceschi on 03/10/13. 6 | // Copyright (c) 2013 Nicola De Franceschi. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | #include 12 | #include "pc_construction.h" 13 | #include "pc_encode.h" 14 | #include "pc_decode_internal.h" 15 | 16 | /* 17 | int pc_sort_cmp(const void *x, const void *y) 18 | { 19 | double xx = *(double*)x, yy = *(double*)y; 20 | if (xx < yy) return 1; 21 | if (xx > yy) return -1; 22 | return 0; 23 | } 24 | */ 25 | 26 | void pc_decode_construction_step(double *Z, double *Zvar, Bit *u, u_int64_t n, u_int64_t step, double *val, u_int64_t *uindex); 27 | void pc_decode_construction(double *Z, double *Zvar, Bit *y, const Channel *channel, Bit *u, u_int64_t n); 28 | 29 | 30 | void pc_construction(double *Z, double *Zvar, u_int64_t n, const Channel *channel, u_int64_t n_samples) { 31 | 32 | u_int64_t N = 1 << n; 33 | 34 | // memset(Z, 0, sizeof(double)*N); 35 | // memset(Zvar, 0, sizeof(double)*N); 36 | 37 | u_int64_t i; 38 | Bit *u = malloc(N*sizeof(Bit)); 39 | Bit *x = malloc(N*sizeof(Bit)); 40 | for(i = 0; i < n_samples; i++){ 41 | 42 | //printf("samples: %d\n",i); 43 | 44 | random_bits(u, N); 45 | 46 | pc_encode(x, u, n); 47 | 48 | Bit *y = x; 49 | apply_channel(y, x, channel, n); 50 | 51 | pc_decode_construction(Z, Zvar, y, channel, u, n); 52 | 53 | } 54 | 55 | free(u); 56 | free(x); 57 | 58 | // qsort(Z, N, sizeof(double), &pc_sort_cmp); 59 | 60 | // [Z,I]=sort(Z_sum,'descend'); 61 | // A(I(1:N-K))=0; 62 | 63 | 64 | 65 | } 66 | 67 | 68 | void pc_decode_construction(double *Z, double *Zvar, Bit *y, const Channel *channel, Bit *u, u_int64_t n) { 69 | 70 | u_int64_t N = 1 << n; 71 | u_int64_t uindex = 0; 72 | 73 | double *delta = malloc(N*sizeof(double)); 74 | 75 | u_int64_t i; 76 | for(i = 0; i < N; i++){ 77 | 78 | u_int64_t i2 = bit_reverse_index(i, n); 79 | double n_,d_,v; 80 | 81 | n_ = channel_probability(channel, y[i2], 0); 82 | d_ = channel_probability(channel, y[i2], 1); 83 | 84 | if(d_ == 0){ 85 | v = MAX_TRANSITION_PROBABILITY; 86 | }else{ 87 | v = n_/d_; 88 | if(v < MIN_TRANSITION_PROBABILITY){ 89 | v = MIN_TRANSITION_PROBABILITY; 90 | }else if(v > MAX_TRANSITION_PROBABILITY){ 91 | v = MAX_TRANSITION_PROBABILITY; 92 | } 93 | } 94 | 95 | delta[i] = v; 96 | 97 | } 98 | 99 | pc_decode_construction_step(Z, Zvar, u, n, 1, delta, &uindex); 100 | 101 | free(delta); 102 | 103 | } 104 | 105 | 106 | void pc_decode_construction_update_Z(double *Z, double *Zvar, double v, u_int64_t *uindex){ 107 | 108 | if(v > 1){ 109 | v = 1/v; 110 | } 111 | 112 | Zvar[*uindex] += v; 113 | 114 | v = sqrt(v); 115 | 116 | Z[*uindex] += v; 117 | 118 | (*uindex)++; 119 | } 120 | 121 | 122 | void pc_decode_construction_step(double *Z, double *Zvar, Bit *u, u_int64_t n, u_int64_t step, double *val, u_int64_t *uindex){ 123 | 124 | u_int64_t D = 1 << (n-step); 125 | double *new_val = malloc(D*sizeof(double)); 126 | 127 | //calcolate f 128 | pc_decode_calc_f(new_val, val, step, n); 129 | 130 | if(step == n){ 131 | pc_decode_construction_update_Z(Z, Zvar, new_val[0], uindex); 132 | }else{ 133 | pc_decode_construction_step(Z, Zvar, u, n, step+1, new_val, uindex); 134 | } 135 | 136 | //calcolate f 137 | pc_decode_calc_g(new_val, val, step, n, u, *uindex); 138 | 139 | if(step == n){ 140 | pc_decode_construction_update_Z(Z, Zvar, new_val[0], uindex); 141 | }else{ 142 | pc_decode_construction_step(Z, Zvar, u, n, step+1, new_val, uindex); 143 | } 144 | 145 | free(new_val); 146 | 147 | } 148 | 149 | 150 | -------------------------------------------------------------------------------- /PolarCodes/error_rate.c: -------------------------------------------------------------------------------- 1 | // 2 | // error_rate.c 3 | // PolarCodes 4 | // 5 | // Created by Nicola De Franceschi on 27/10/13. 6 | // Copyright (c) 2013 Nicola De Franceschi. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "error_rate.h" 15 | #include "double_ei.h" 16 | 17 | void get_error_rate_file_name(char *filename, u_int64_t n, Channel *channel, u_int64_t K) { 18 | sprintf(filename, "er_%" PRIu64 "_ch_%d_par_%f" "_k_%" PRIu64 ".txt", n, channel->channel_type, channel->par, K); 19 | } 20 | 21 | void get_error_rate_file_name_tmp(char *filename, u_int64_t n, Channel *channel, u_int64_t K) { 22 | sprintf(filename, "er_%" PRIu64 "_ch_%d_par_%f" "_k_%" PRIu64 ".tmp.txt", n, channel->channel_type, channel->par, K); 23 | } 24 | 25 | FILE *open_error_rate_file_in(const char *dir, u_int64_t n, Channel *channel, u_int64_t K) { 26 | char filename[1024]; 27 | strcpy(filename, dir); 28 | char *name = filename + strlen(filename); 29 | get_error_rate_file_name(name, n, channel, K); 30 | return fopen(filename, "r"); 31 | } 32 | 33 | FILE *open_error_rate_file_out(const char *dir, u_int64_t n, Channel *channel, u_int64_t K) { 34 | char filename[1024]; 35 | strcpy(filename, dir); 36 | char *name = filename + strlen(filename); 37 | get_error_rate_file_name(name, n, channel, K); 38 | return fopen(filename, "w"); 39 | } 40 | 41 | FILE *open_error_rate_file_out_temp(const char *dir, u_int64_t n, Channel *channel, u_int64_t K) { 42 | char filename[1024]; 43 | strcpy(filename, dir); 44 | char *name = filename + strlen(filename); 45 | get_error_rate_file_name_tmp(name, n, channel, K); 46 | return fopen(filename, "w"); 47 | } 48 | 49 | 50 | void get_or_create_error_rate(ErrorRate *er, const char *dir, u_int64_t n, Channel *channel, u_int64_t K) { 51 | er->n = n; 52 | er->channel.channel_type = channel->channel_type; 53 | er->channel.par = channel->par; 54 | er->K = K; 55 | 56 | FILE *in = open_error_rate_file_in(dir, n, channel, K); 57 | if(in){ 58 | if(1 != fscanf(in, "%" PRIu64, &(er->total_samples))) 59 | exit(1); 60 | er->BER = double_fget(in); 61 | er->BER_2 = double_fget(in); 62 | if(1 != fscanf(in, "%" PRIu64, &(er->frame_errors))) 63 | exit(1); 64 | 65 | fclose(in); 66 | in = NULL; 67 | }else{ 68 | er->total_samples = 0; 69 | er->BER = 0; 70 | er->BER_2 = 0; 71 | er->frame_errors = 0; 72 | } 73 | } 74 | 75 | void get_error_rate(ErrorRate *er, const char *dir, u_int64_t n, Channel *channel, u_int64_t K) { 76 | er->n = n; 77 | er->channel.channel_type = channel->channel_type; 78 | er->channel.par = channel->par; 79 | er->K = K; 80 | 81 | FILE *in = open_error_rate_file_in(dir, n, channel, K); 82 | if(in){ 83 | if(1 != fscanf(in, "%" PRIu64, &(er->total_samples))) 84 | exit(1); 85 | er->BER = double_fget(in); 86 | er->BER_2 = double_fget(in); 87 | if(1 != fscanf(in, "%" PRIu64, &(er->frame_errors))) 88 | exit(1); 89 | 90 | fclose(in); 91 | in = NULL; 92 | }else{ 93 | exit(4); 94 | } 95 | 96 | } 97 | 98 | void save_error_rate(ErrorRate *er, const char *dir) { 99 | FILE *out = open_error_rate_file_out_temp(dir, er->n, &(er->channel), er->K); 100 | if(!out) 101 | exit(1); 102 | 103 | fprintf(out, "%" PRIu64 "\n", er->total_samples); 104 | double_fput(out, er->BER); 105 | fprintf(out, " "); 106 | double_fput(out, er->BER_2); 107 | fprintf(out, " "); 108 | fprintf(out, "%" PRIu64 "\n", er->frame_errors); 109 | 110 | fclose(out); 111 | out = NULL; 112 | 113 | char old[1024]; 114 | strcpy(old, dir); 115 | char *name = old + strlen(dir); 116 | get_error_rate_file_name_tmp(name, er->n, &(er->channel), er->K); 117 | 118 | char new[1024]; 119 | strcpy(new, dir); 120 | name = new + strlen(dir); 121 | get_error_rate_file_name(name, er->n, &(er->channel), er->K); 122 | 123 | rename(old, new); 124 | 125 | } 126 | -------------------------------------------------------------------------------- /PC_pc_simulate/main.c: -------------------------------------------------------------------------------- 1 | // 2 | // main.c 3 | // pc_simulate 4 | // 5 | // Created by Nicola De Franceschi on 26/10/13. 6 | // Copyright (c) 2013 Nicola De Franceschi. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "../PolarCodes/polar_codes.h" 15 | #include "../PolarCodes/bhattacharyya_files.h" 16 | #include "../PolarCodes/pc_encode.h" 17 | #include "../PolarCodes/pc_decode.h" 18 | #include "../PolarCodes/channels.h" 19 | #include "../PolarCodes/error_rate.h" 20 | 21 | 22 | #define DEFAULT_max_n_samples (1<<30) 23 | 24 | 25 | // call with arguments: n ch_type ch_par Kmin Kdelta Kmax n_samples max_n_samples dir 26 | // max_n_samples==-1 for default 27 | // Kmin Kdelta Kmax are actually Kmin/N Kdelta/N Kmax/N 28 | int main(int argc, const char * argv[]) 29 | { 30 | 31 | //random initialization 32 | struct timeval time; 33 | gettimeofday(&time,NULL); 34 | srand((unsigned int)(time.tv_sec + time.tv_usec)); 35 | 36 | 37 | Channel channel; 38 | u_int64_t n; 39 | double Kmin, Kdelta, Kmax; 40 | u_int64_t n_samples; 41 | int64_t max_n_samples; 42 | const char *dir; 43 | 44 | if(argc != 10) 45 | exit(2); 46 | 47 | 48 | //n 49 | sscanf(argv[1], "%" PRIu64, &n); 50 | 51 | //ch_type 52 | sscanf(argv[2], "%d", &(channel.channel_type)); 53 | 54 | //ch_par 55 | sscanf(argv[3], "%lf", &(channel.par)); 56 | 57 | //Kmin Kdelta Kmax 58 | sscanf(argv[4], "%lf", &Kmin); 59 | sscanf(argv[5], "%lf", &Kdelta); 60 | sscanf(argv[6], "%lf", &Kmax); 61 | 62 | //n_samples 63 | sscanf(argv[7], "%" PRIu64, &n_samples); 64 | 65 | //max_n_samples 66 | sscanf(argv[8], "%" PRId64, &max_n_samples); 67 | if(max_n_samples == -1) 68 | max_n_samples=DEFAULT_max_n_samples; 69 | 70 | //dir 71 | dir = argv[9]; 72 | 73 | 74 | u_int64_t N = 1 << n; 75 | 76 | Bhattacharyya b; 77 | get_bhattacharyya(&b, dir, n, &channel); 78 | 79 | 80 | u_int64_t lastK=0; 81 | u_int64_t numK = (u_int64_t)((Kmax-Kmin)/Kdelta)+1; 82 | u_int64_t realNumK=0; 83 | Bit **As = malloc(sizeof(Bit*)*numK); 84 | ErrorRate *ers = malloc(sizeof(ErrorRate)*numK); 85 | 86 | u_int64_t i; 87 | for(i=0;i N) 92 | break; 93 | 94 | lastK = K; 95 | Bit *A = malloc(sizeof(Bit)*N); 96 | get_frozen_bits(A, K, &b); 97 | As[realNumK]=A; 98 | 99 | get_or_create_error_rate(&ers[realNumK], dir, n, &channel, K); 100 | 101 | realNumK++; 102 | } 103 | 104 | numK=realNumK; 105 | 106 | Bit *u = malloc(sizeof(Bit)*N); 107 | Bit *x = malloc(sizeof(Bit)*N); 108 | 109 | while (1) { 110 | 111 | u_int64_t num_done = 0; 112 | 113 | u_int64_t k; 114 | for(k=0; k max_n_samples){ 117 | num_done++; 118 | continue; 119 | } 120 | 121 | Bit *A = As[k]; 122 | for(i=0; i < n_samples; i++){ 123 | 124 | random_bits_f(u, A, N); 125 | pc_encode(x, u, n); 126 | apply_channel(x, x, &channel, n); 127 | pc_decode(x, x, &channel, A, n); 128 | 129 | u_int64_t bit_errors = 0; 130 | u_int64_t j; 131 | for(j=0; j < N; j++){ 132 | if(u[j] != x[j]) 133 | bit_errors++; 134 | } 135 | 136 | double ber = (double)bit_errors / (double)N; 137 | 138 | ers[k].total_samples++; 139 | ers[k].BER += ber; 140 | ers[k].BER_2 += ber*ber; 141 | if(bit_errors > 0) 142 | ers[k].frame_errors++; 143 | 144 | } 145 | 146 | save_error_rate(&ers[k], dir); 147 | printf("K = %lld, total_samples = %lld\n", ers[k].K, ers[k].total_samples); 148 | 149 | } 150 | 151 | if(num_done == numK) 152 | break; 153 | 154 | } 155 | 156 | return 0; 157 | } 158 | 159 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ######################### 2 | # .gitignore file for Xcode4 / OS X Source projects 3 | # 4 | # Version 2.1 5 | # For latest version, see: http://stackoverflow.com/questions/49478/git-ignore-file-for-xcode-projects 6 | # 7 | # 2013 updates: 8 | # - fixed the broken "save personal Schemes" 9 | # - added line-by-line explanations for EVERYTHING (some were missing) 10 | # 11 | # NB: if you are storing "built" products, this WILL NOT WORK, 12 | # and you should use a different .gitignore (or none at all) 13 | # This file is for SOURCE projects, where there are many extra 14 | # files that we want to exclude 15 | # 16 | ######################### 17 | 18 | #### 19 | # make generated files 20 | 21 | .o 22 | 23 | 24 | #### 25 | # Xcode 5 - Source Control files 26 | # 27 | # Xcode 5 introduced a new file type .xccheckout. This files contains VCS metadata 28 | # and should therefore not be checked into the VCS. 29 | 30 | *.xccheckout 31 | 32 | ##### 33 | # OS X temporary files that should never be committed 34 | # 35 | # c.f. http://www.westwind.com/reference/os-x/invisibles.html 36 | 37 | .DS_Store 38 | 39 | # c.f. http://www.westwind.com/reference/os-x/invisibles.html 40 | 41 | .Trashes 42 | 43 | # c.f. http://www.westwind.com/reference/os-x/invisibles.html 44 | 45 | *.swp 46 | 47 | # *.lock - this is used and abused by many editors for many different things. 48 | # For the main ones I use (e.g. Eclipse), it should be excluded 49 | # from source-control, but YMMV 50 | 51 | *.lock 52 | 53 | # 54 | # profile - REMOVED temporarily (on double-checking, this seems incorrect; I can't find it in OS X docs?) 55 | #profile 56 | 57 | 58 | #### 59 | # Xcode temporary files that should never be committed 60 | # 61 | # NB: NIB/XIB files still exist even on Storyboard projects, so we want this... 62 | 63 | *~.nib 64 | 65 | 66 | #### 67 | # Xcode build files - 68 | # 69 | # NB: slash on the end, so we only remove the FOLDER, not any files that were badly named "DerivedData" 70 | 71 | DerivedData/ 72 | 73 | # NB: slash on the end, so we only remove the FOLDER, not any files that were badly named "build" 74 | 75 | build/ 76 | 77 | 78 | ##### 79 | # Xcode private settings (window sizes, bookmarks, breakpoints, custom executables, smart groups) 80 | # 81 | # This is complicated: 82 | # 83 | # SOMETIMES you need to put this file in version control. 84 | # Apple designed it poorly - if you use "custom executables", they are 85 | # saved in this file. 86 | # 99% of projects do NOT use those, so they do NOT want to version control this file. 87 | # ..but if you're in the 1%, comment out the line "*.pbxuser" 88 | 89 | # .pbxuser: http://lists.apple.com/archives/xcode-users/2004/Jan/msg00193.html 90 | 91 | *.pbxuser 92 | 93 | # .mode1v3: http://lists.apple.com/archives/xcode-users/2007/Oct/msg00465.html 94 | 95 | *.mode1v3 96 | 97 | # .mode2v3: http://lists.apple.com/archives/xcode-users/2007/Oct/msg00465.html 98 | 99 | *.mode2v3 100 | 101 | # .perspectivev3: http://stackoverflow.com/questions/5223297/xcode-projects-what-is-a-perspectivev3-file 102 | 103 | *.perspectivev3 104 | 105 | # NB: also, whitelist the default ones, some projects need to use these 106 | !default.pbxuser 107 | !default.mode1v3 108 | !default.mode2v3 109 | !default.perspectivev3 110 | 111 | 112 | #### 113 | # Xcode 4 - semi-personal settings 114 | # 115 | # 116 | # OPTION 1: --------------------------------- 117 | # throw away ALL personal settings (including custom schemes! 118 | # - unless they are "shared") 119 | # 120 | # NB: this is exclusive with OPTION 2 below 121 | xcuserdata 122 | 123 | # OPTION 2: --------------------------------- 124 | # get rid of ALL personal settings, but KEEP SOME OF THEM 125 | # - NB: you must manually uncomment the bits you want to keep 126 | # 127 | # NB: this *requires* git v1.8.2 or above; you may need to upgrade to latest OS X, 128 | # or manually install git over the top of the OS X version 129 | # NB: this is exclusive with OPTION 1 above 130 | # 131 | #xcuserdata/**/* 132 | 133 | # (requires option 2 above): Personal Schemes 134 | # 135 | #!xcuserdata/**/xcschemes/* 136 | 137 | #### 138 | # XCode 4 workspaces - more detailed 139 | # 140 | # Workspaces are important! They are a core feature of Xcode - don't exclude them :) 141 | # 142 | # Workspace layout is quite spammy. For reference: 143 | # 144 | # /(root)/ 145 | # /(project-name).xcodeproj/ 146 | # project.pbxproj 147 | # /project.xcworkspace/ 148 | # contents.xcworkspacedata 149 | # /xcuserdata/ 150 | # /(your name)/xcuserdatad/ 151 | # UserInterfaceState.xcuserstate 152 | # /xcsshareddata/ 153 | # /xcschemes/ 154 | # (shared scheme name).xcscheme 155 | # /xcuserdata/ 156 | # /(your name)/xcuserdatad/ 157 | # (private scheme).xcscheme 158 | # xcschememanagement.plist 159 | # 160 | # 161 | 162 | #### 163 | # Xcode 4 - Deprecated classes 164 | # 165 | # Allegedly, if you manually "deprecate" your classes, they get moved here. 166 | # 167 | # We're using source-control, so this is a "feature" that we do not want! 168 | 169 | *.moved-aside 170 | 171 | #### 172 | # UNKNOWN: recommended by others, but I can't discover what these files are 173 | # 174 | # ...none. Everything is now explained. 175 | -------------------------------------------------------------------------------- /PolarCodes/bhattacharyya_files.c: -------------------------------------------------------------------------------- 1 | // 2 | // bhattacharyya_files.c 3 | // PolarCodes 4 | // 5 | // Created by Nicola De Franceschi on 25/10/13. 6 | // Copyright (c) 2013 Nicola De Franceschi. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include "channels.h" 14 | #include "bhattacharyya_files.h" 15 | #include "double_ei.h" 16 | 17 | void get_bhattacharyya_file_name(char *filename, u_int64_t n, Channel *channel) { 18 | sprintf(filename, "bh_%" PRIu64 "_ch_%d_par_%f" ".txt", n, channel->channel_type, channel->par); 19 | } 20 | 21 | void get_bhattacharyya_file_name_tmp(char *filename, u_int64_t n, Channel *channel) { 22 | sprintf(filename, "bh_%" PRIu64 "_ch_%d_par_%f" ".tmp.txt", n, channel->channel_type, channel->par); 23 | } 24 | 25 | FILE *open_bhattacharyya_file_in(const char *dir, u_int64_t n, Channel *channel) { 26 | char filename[1024]; 27 | strcpy(filename, dir); 28 | char *name = filename + strlen(filename); 29 | get_bhattacharyya_file_name(name, n, channel); 30 | return fopen(filename, "r"); 31 | } 32 | 33 | FILE *open_bhattacharyya_file_out(const char *dir, u_int64_t n, Channel *channel) { 34 | char filename[1024]; 35 | strcpy(filename, dir); 36 | char *name = filename + strlen(filename); 37 | get_bhattacharyya_file_name(name, n, channel); 38 | return fopen(filename, "w"); 39 | } 40 | 41 | FILE *open_bhattacharyya_file_out_temp(const char *dir, u_int64_t n, Channel *channel) { 42 | char filename[1024]; 43 | strcpy(filename, dir); 44 | char *name = filename + strlen(filename); 45 | get_bhattacharyya_file_name_tmp(name, n, channel); 46 | return fopen(filename, "w"); 47 | } 48 | 49 | void get_or_create_bhattacharyya(Bhattacharyya *b, const char *dir, u_int64_t n, Channel *channel) { 50 | u_int64_t N = 1<n = n; 53 | b->channel.channel_type = channel->channel_type; 54 | b->channel.par = channel->par; 55 | b->Z = malloc(sizeof(double)*N); 56 | b->Zvar = malloc(sizeof(double)*N); 57 | b->total_samples = 0; 58 | 59 | u_int64_t i; 60 | FILE *in = open_bhattacharyya_file_in(dir, n, channel); 61 | if(in){ 62 | if(1 != fscanf(in, "%" PRIu64, &(b->total_samples))) 63 | exit(1); 64 | for(i = 0; i < N; i++){ 65 | b->Z[i] = double_fget(in); 66 | b->Zvar[i] = double_fget(in); 67 | } 68 | fclose(in); 69 | in = NULL; 70 | }else{ 71 | memset(b->Z, 0, sizeof(double)*N); 72 | memset(b->Zvar, 0, sizeof(double)*N); 73 | } 74 | 75 | } 76 | 77 | void get_bhattacharyya(Bhattacharyya *b, const char *dir, u_int64_t n, Channel *channel) { 78 | u_int64_t N = 1<n = n; 81 | b->channel.channel_type = channel->channel_type; 82 | b->channel.par = channel->par; 83 | b->Z = malloc(sizeof(double)*N); 84 | b->Zvar = malloc(sizeof(double)*N); 85 | b->total_samples = 0; 86 | 87 | u_int64_t i; 88 | FILE *in = open_bhattacharyya_file_in(dir, n, channel); 89 | if(in){ 90 | if(1 != fscanf(in, "%" PRIu64, &(b->total_samples))) 91 | exit(1); 92 | for(i = 0; i < N; i++){ 93 | b->Z[i] = double_fget(in); 94 | b->Zvar[i] = double_fget(in); 95 | } 96 | fclose(in); 97 | in = NULL; 98 | }else{ 99 | exit(4); 100 | } 101 | 102 | } 103 | 104 | void save_bhattacharyya(Bhattacharyya *b, const char *dir) { 105 | u_int64_t N = 1 << b->n; 106 | u_int64_t i; 107 | FILE *out = open_bhattacharyya_file_out_temp(dir, b->n, &(b->channel)); 108 | if(!out) 109 | exit(1); 110 | fprintf(out, "%" PRIu64 "\n", b->total_samples); 111 | for(i = 0; i < N; i++){ 112 | double_fput(out, b->Z[i]); 113 | fprintf(out, " "); 114 | double_fput(out, b->Zvar[i]); 115 | fprintf(out, "\n"); 116 | } 117 | fclose(out); 118 | out = NULL; 119 | 120 | char old[1024]; 121 | strcpy(old, dir); 122 | char *name = old + strlen(dir); 123 | get_bhattacharyya_file_name_tmp(name, b->n, &(b->channel)); 124 | 125 | char new[1024]; 126 | strcpy(new, dir); 127 | name = new + strlen(dir); 128 | get_bhattacharyya_file_name(name, b->n, &(b->channel)); 129 | 130 | rename(old, new); 131 | 132 | } 133 | 134 | struct frozen_bits_index_struct { 135 | u_int64_t index; 136 | Bhattacharyya *b; 137 | }; 138 | 139 | int frozen_bits_sort_cmp(const void *x, const void *y) 140 | { 141 | struct frozen_bits_index_struct *xx = (struct frozen_bits_index_struct*)x; 142 | struct frozen_bits_index_struct *yy = (struct frozen_bits_index_struct*)y; 143 | Bhattacharyya *b = xx->b; 144 | if (b->Z[xx->index] < b->Z[yy->index]) return -1; 145 | if (b->Z[xx->index] > b->Z[yy->index]) return 1; 146 | return 0; 147 | } 148 | 149 | void get_frozen_bits(Bit *bits, u_int64_t K, Bhattacharyya *b) { 150 | u_int64_t N = 1 << b->n; 151 | struct frozen_bits_index_struct *indexes = (struct frozen_bits_index_struct*)malloc(sizeof(struct frozen_bits_index_struct)*N); 152 | u_int64_t i; 153 | for(i=0;i