Skip to content

Commit d0a17ea

Browse files
author
隆曦
committed
添加了自动construct功能
1 parent 9fc693a commit d0a17ea

9 files changed

Lines changed: 349 additions & 83 deletions

OutputOfChannel.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function y=OutputOfChannel(x,channel_string,channel_state)
2+
%
3+
% USAGE:
4+
% y=OutputOfChannel(x,channel_string,channel_state)
5+
%
6+
% Given a binary "x" (matrix/vector), simulates a channel
7+
% specified by "channel_string" == 'BSC' or 'BEC' or 'AWGN';
8+
% modulates if necessary, adds noise and returns the output.
9+
% Modulation is assumed BPSK, by-default, only for AWGN channel.
10+
%
11+
% The "channel_state" is :
12+
% Ec/N0 -aka- SNR when channel_string=='AWGN'
13+
%
14+
15+
global PCparams;
16+
17+
% Assume the NORMALIZATION:
18+
% N0/2, the noise-variance is assumed to be unity.
19+
% BPSK: 0 -> +sqrt(R*Eb/N0); 1 -> -sqrt(R*Eb/N0)
20+
21+
sqrtEcN0 = sqrt(PCparams.K/PCparams.N) * 10^(channel_state/20);
22+
y = (2*x-1)*sqrtEcN0*sqrt(2) + randn(size(x)); %AWGN with normalization
23+

Sample_Longxi.txt

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
%ModelSim������u0-u15 16���ƣ� 0000000100020003001000110012001300200021002200230030003100320033
2+
N=8; K=8;
3+
u=[0;0;0;1;0;1;1;1];
4+
x=encodeLongxi(N,K,u,'AWGN',10)
5+
6+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7+
% NOTE: Except for plotPC(), it is a must to have
8+
% initPC() run before using any other routine.
9+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10+
11+
>> N=128; K=64;
12+
13+
>> %%%%%%%%%%%% 1. BSC Channel %%%%%%%%%%%%%
14+
>> initPC(N,K,'BSC',0.1); % Transition prob 'p' of BSC assumed during code-design = 0.1
15+
16+
All polar coding parameters & resources initialized. (in a structure - "PCparams")
17+
N: 128
18+
K: 64
19+
n: 7
20+
FZlookup: [128x1 double]
21+
design_channelstring: 'BSC'
22+
design_channelstate: 0.1000
23+
LLR: [1x255 double]
24+
BITS: [2x127 double]
25+
bitreversedindices: [128x1 double]
26+
index_of_first0_from_MSB: [128x1 double]
27+
index_of_first1_from_MSB: [128x1 double]
28+
29+
>> u=(rand(K,1)>0.5); %random message
30+
>> x=pencode(u); %polar encoding
31+
>> y=OutputOfChannel(x,'BSC',0.15); %simulate BSC channel with p=0.1
32+
>> uhat=pdecode(y,'BSC',0.15); %decode under the same BSC channel setting
33+
34+
>> logical( sum( uhat == u ) == K ) %check for Decoding success!
35+
36+
ans =
37+
38+
1
39+
40+
%%%%%%%%%%%%%%% PLOTTING PERFORMANCE CURVES %%%%%%%%%%%%%%%%%%
41+
>> plotPC(256,128,'BSC', 0.1, 0.01:0.02:0.16,10000) % Last is #Monte-Carlo-samples. This module runs everything necessary. Doesn't require anything to have initialized before.
42+
43+
* Max. Monte-Carlo Iterations = 10000, ensuring 100 frame errors, and a minimum of 1000 iterations
44+
45+
* Polar Code designed for BSC channel at p=0.100000
46+
47+
* Channel-states (BSC-p) to be run:
48+
0.0100 0.0300 0.0500 0.0700 0.0900 0.1100 0.1300 0.1500
49+
50+
51+
........(lots of text)
52+
53+
54+
BSC-p : 0.0100 0.0300 0.0500 0.0700 0.0900 0.1100 0.1300 0.1500
55+
56+
Frame Error Rates : 0 0.0081 0.0929 0.2880 0.6110 0.8580 0.9680 0.9980
57+
58+
Bit Error Rates : 0 0.0007 0.0125 0.0494 0.1564 0.2624 0.3650 0.4218
59+
60+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61+
62+
63+
64+
>> %%%%%%%%%%%%%%%%%%% 2. BEC Channel %%%%%%%%%%%%%%%%%%%%%
65+
>> initPC(N,K,'BEC',0.1); % Erasure prob 'epsilon' of BEC assumed during code-design = 0.1
66+
67+
All polar coding parameters & resources initialized. (in a structure - "PCparams")
68+
N: 128
69+
K: 64
70+
n: 7
71+
FZlookup: [128x1 double]
72+
design_channelstring: 'BEC'
73+
design_channelstate: 0.1000
74+
LLR: [1x255 double]
75+
BITS: [2x127 double]
76+
bitreversedindices: [128x1 double]
77+
index_of_first0_from_MSB: [128x1 double]
78+
index_of_first1_from_MSB: [128x1 double]
79+
80+
>> u=(rand(K,1)>0.5); %random message
81+
>> x=pencode(u); %polar encoding
82+
>> y=OutputOfChannel(x,'BEC',0.15); %simulate BEC channel with erasure prob=0.1
83+
>> uhat=pdecode(y,'BEC',0.15); %decode under the same BEC channel setting
84+
85+
>> logical( sum( uhat == u ) == K ) %check for Decoding success!
86+
87+
ans =
88+
89+
1
90+
91+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92+
93+
94+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95+
96+
>> %%%%%%%%%%%% 3. AWGN Channel %%%%%%%%%%%%%
97+
>> initPC(N,K,'AWGN',0); %designSNR=0dB
98+
99+
All polar coding parameters & resources initialized. (in a structure - "PCparams")
100+
N: 128
101+
K: 64
102+
n: 7
103+
FZlookup: [128x1 double]
104+
design_channelstring: 'AWGN'
105+
design_channelstate: 0
106+
LLR: [1x255 double]
107+
BITS: [2x127 double]
108+
bitreversedindices: [128x1 double]
109+
index_of_first0_from_MSB: [128x1 double]
110+
index_of_first1_from_MSB: [128x1 double]
111+
112+
>> u=(rand(K,1)>0.5); %random message
113+
>> x=pencode(u); %polar encoding
114+
>> y=OutputOfChannel(x,'AWGN',1); %simulate AWGN channel at Eb/N0=1dB
115+
>> uhat=pdecode(y,'AWGN',1); %decode under the same AWGN channel setting.
116+
117+
>> logical( sum( uhat == u ) == K ) %check for Decoding success!
118+
119+
ans =
120+
121+
1
122+
123+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

construct.m

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
function frozenlookup = pcc(N,K,channelstring,channelstate,frozenbits)%'frozenbits' is optional
2+
%
3+
% Perform the polar code construction aka, setting the frozen-parameters
4+
% of the polar codes.
5+
%
6+
% We use the simplest of all construction algorithms [1].
7+
%
8+
% Currently supported channels:
9+
% 1. AWGN(designSNR)
10+
% 2. BSC(p)
11+
% 3. BEC(eps)
12+
% 1.
13+
% Perform the PCC at "design-SNR" := (Eb/N0) = channelparam,
14+
% given in dB. The initial Bhattacharyya parameter = exp(-(K/N)*(Eb/N0))
15+
%
16+
% 2.
17+
% Perform the PCC at the given "design-p"= channelparam.
18+
% The initial Bhattacharyya parameter = 2*sqrt(p(1-p)), where p is the channelparam
19+
%
20+
% 3.
21+
% Perform the PCC at the given "design-epsilon"= channelparam.
22+
% The initial Bhattacharyya parameter = epsilon, where epsilon=channelparam
23+
%
24+
% [1] Vangala, H.; Viterbo, E. & Hong, Y.
25+
% "A Comparative Study of Polar Code Constructions for the AWGN Channel",
26+
% arXiv:1501.02473 [cs.IT], 2015.
27+
%
28+
% USAGE:
29+
%
30+
% frozenlookup = pcc(N,K,channelparam,ChannelString)
31+
%
32+
% N - Blocklength
33+
% K - Message length
34+
%
35+
% channelstring - Must be one of (case insensitive)
36+
% 'AWGN' (default)
37+
% or 'BSC'
38+
% or 'BEC'
39+
%
40+
% channelstate - Channel's state to be assumed during the PCC algorithm.
41+
% Usually as an initial. It is one of:
42+
% design-SNR (Eb/N0)
43+
% or design-p
44+
% or design-eps
45+
% *** Must match the ChannelString parameter (above) ***
46+
%
47+
% frozenbits (optional) - User-defined (N-K)x1 frozenbits (ideally
48+
% the FER/BER performance is identical for any choice,
49+
% but are critical to be known at receiver. May be used
50+
% in cryptographical ideas, for e.g.)
51+
%
52+
% frozenlookup - An easy Nx1 look-up table for knowing whether a
53+
% bit-index is frozen or not. Its definition:
54+
% -----------------------------------------------------------
55+
% frozenlookup(i) = 0 or 1 --- if "i" is frozen index
56+
% = -1 --- if "i" is non-frozen/information index
57+
% -----------------------------------------------------------
58+
% @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
59+
% WRITTEN BY: Harish Vangala, Emanuele Viterbo, and Yi Hong,
60+
% Dept of ECSE, Monash University, Australia.
61+
%
62+
% - Latest as on 2016-March-03
63+
% - Available ONLINE for free: is.gd/polarcodes
64+
% - Freely distributed for educational and research purposes
65+
% @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
66+
67+
if nargin==4 %default frozenbits are zeros
68+
frozenbits=zeros(N-K,1);
69+
end
70+
71+
% fprintf(' ... '); %DEBUG
72+
73+
z = zeros(N,1);
74+
%%%%%%%%%%%%%%%%%% INITIALIZATION %%%%%%%%%%%%%%%%%%%
75+
if(strcmpi(channelstring,'AWGN'))
76+
designSNRdB=channelstate;
77+
designSNR = 10^(designSNRdB/10);
78+
z(1) = -(K/N)*designSNR; % In logdomain. Actual initial Bh.Param = exp(-(K/N) * (Eb/N0))
79+
elseif(strcmpi(channelstring,'BSC'))
80+
designp=channelstate;
81+
z(1) = log(2)+0.5*log(designp)+0.5*log(1-designp);
82+
% In logdomain. Actual initial Bh.Param = 2*sqrt(p(1-p))
83+
elseif(strcmpi(channelstring,'BEC'))
84+
designeps=channelstate;
85+
z(1) = log(designeps);
86+
% In logdomain. Actual initial Bh.Param = epsilon (erasure-prob)
87+
else
88+
fprintf('\n\n ERROR: invalid channel string "%s" supplied. It must be one of "AWGN" or "BSC" or "BEC"\n\n',channelstring);
89+
return;
90+
end
91+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92+
93+
%DEBUG
94+
% fprintf('\n\n PolarCodeConstruction -- %s %f \n\n',channelstring,channelstate);
95+
96+
for lev=1:log2(N)
97+
B=2^lev;
98+
for j=1:B/2
99+
T = z(j);
100+
z(j) = logdomain_diff( log(2)+T, 2*T ); % 2z - z^2
101+
z(B/2 + j) = 2*T; % z^2
102+
end
103+
end
104+
105+
[~,indices] = sort(z,1,'ascend'); %default sort is "Ascending";
106+
% 1 refers to sort "columns" (not rows)
107+
108+
% %DEBUG-START
109+
% fprintf('\n\n\tEstimated BLER of %s channel at param=%.2f is: %f\n\n',channelstring,channelstate,1-prod(1-exp(z(indices(1:K)))));
110+
% %DEBUG-END
111+
112+
frozenlookup(sort(indices(K+1:end))) = frozenbits; % Could as well be userdefined, esp when called by initPC().
113+
% Sorting is important, as we assume frozenbits are given as a subvector with natural order.
114+
115+
for j=1:K
116+
frozenlookup(indices(j)) = -1; %locations in z containing least K values
117+
end
118+
119+
end

encodeLongxi.asv

Lines changed: 0 additions & 34 deletions
This file was deleted.

encodeLongxi.m

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
function x=encodeLongxi(N,K,design_channelstring,design_channelstate,silentflag,frozenbits) %Optional: N0, designSNRdB and silentflag (last three arguments)
1+
function x=encodeLongxi(N,K,u,design_channelstring,design_channelstate) %Optional: N0, designSNRdB and silentflag (last three arguments)
22
%输入global PCparams可以看全局参数
33
global PCparams;
4+
5+
frozenbits=zeros(N-K,1);
6+
47
n = ceil(log2(N));
58
N = 2^n;
69
PCparams = struct('N', N, ...
@@ -18,13 +21,14 @@
1821

1922
disp(PCparams);
2023

21-
PCparams.FZlookup = [0;0;0;-1;0;-1;-1;-1];
22-
24+
%PCparams.FZlookup = [0;0;0;-1;0;-1;-1;-1];
25+
% PCparams.FZlookup = -ones(16,1);
26+
PCparams.FZlookup = pcc(N,K,design_channelstring,design_channelstate,frozenbits);
2327
F=kronF(PCparams.n);
24-
u=[1;1;1;1];
28+
% u=[1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0];
2529
x = PCparams.FZlookup; %loads all frozenbits, incl. -1
2630
x (x == -1) = u;%混合信息比特和冻结比特
27-
x=mod(x'*F,2);%mod2
31+
x=mod(x*F,2);%mod2
2832
tempX = zeros(N,1);%tempX用来暂时存储反序重排后的x
2933
for i=1:N
3034
tempX(i) = x(PCparams.bitreversedindices(i)+1);

logdomain_diff.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function z=logdomain_diff(x,y)
2+
%
3+
% Usage:
4+
% z=logdomain_sum(x,y)
5+
%
6+
% Perform difference of two real-numbers in logarithmic domain. The given two
7+
% numbers should already be in logarithmic domain. The natural logarithm
8+
% is used.
9+
% No argument check is performed to see if x > y
10+
% x MUST be greater than y, and is assumed.
11+
%
12+
z = x + log1p(-exp(y-x)); % log(1 - exp(y-x)); efficient replacement
13+
end

logdomain_sum.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function z=logdomain_sum(x,y)
2+
%
3+
% Usage:
4+
% z=logdomain_sum(x,y)
5+
%
6+
% Perform sum of two real-numbers in logarithmic domain. The given two
7+
% numbers should already be in logarithmic domain. The natural logarithm
8+
% is used.
9+
%
10+
11+
if(x<y)
12+
z = y+log1p(exp(x-y)); % y+log(1+exp(x-y)); efficient version
13+
else
14+
z = x+log1p(exp(y-x)); % x+log(1+exp(y-x)); efficient version
15+
end
16+
end

0 commit comments

Comments
 (0)