-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchannel.cpp
More file actions
160 lines (130 loc) · 2.84 KB
/
Copy pathchannel.cpp
File metadata and controls
160 lines (130 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* File: GLDPC.cpp
* Author: masoud
*
* Created on February 13, 2012, 7:42 PM
*/
#include <iostream>
#include <fstream>
#include <cassert>
#include <cstdlib>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
#include <cmath>
#include "utils.h"
#ifdef USE_ITPP
#include <itpp/itcomm.h>
#endif
#include "channel.h"
#include "utils.h"
using namespace std;
//-------------------------------- Channels ---------------------------
Channel::Channel(double p) {
this->param=p;
}
BSC::BSC(double p):Channel(p){
#ifdef USE_ITPP
ch=new itpp::BSC(p);
#endif
}
BSC::~BSC(){
#ifdef USE_ITPP
if(ch) delete (itpp::BSC*)ch;
#endif
}
double BSC::capacity(double p){
return 1.0+p*log2(p)+(1-p)*log2(1-p);
}
double BSC::setCapacity(double c){
double p1=0.0,p,p2=0.5,cap;
do{
p=(p1+p2)/2.0;
cap=1.0+p*log2(p)+(1-p)*log2(1-p);
if(cap<c)
p2=p;
else
p1=p;
}
while(abs(cap-c)>1e-10);
param=p;
return param;
}
unsigned int BSC::transmitzero(std::vector<LLRType>& llr){
unsigned int N=llr.size();
LLRType p=(LLRType) param;
LLRType v0=mylog(p/(1.0-p));
for (unsigned int i=0;i<N;i++){
llr[i]=UniformRandom<param?-v0:v0;
}
return 0;
}
unsigned int BSC::transmitzero(LLRType* llr, unsigned int N){
LLRType p=(LLRType) param;
LLRType v0=mylog((1.0-p)/p);
unsigned int nerr=0;
for (unsigned int i=0;i<N;i++){
if(UniformRandom<param){ //flip
llr[i]=-v0;
nerr++;
}else
llr[i]=v0;
}
return nerr;
}
/*
void BSC::decode(const std::vector<LLRType>& llr,std::vector<bool>){
}
*/
void BSC::print(){
cout<<"BSC("<<param<<"), capacity="<<capacity()<<endl;
}
AWGN::AWGN(double p):Channel(p){
#ifdef USE_ITPP
ch=new itpp::AWGN_Channel(p);
#endif
variance=SNRtoVariance(param);
}
AWGN::~AWGN(){
#ifdef USE_ITPP
if(ch) delete (itpp::AWGN_Channel*)ch;
#endif
}
double AWGN::capacity(double p){
return std::log(1+p)/std::log(2.0);
}
double AWGN::setCapacity(double c){
double p1=-1.0,p,p2=10.0,cap;
do{
p=(p1+p2)/2.0;
cap=capacity(p);
if(cap<c)
p2=p;
else
p1=p;
}
while(abs(cap-c)>1e-10);
param=p;
return param;
variance=SNRtoVariance(param);
return param;
}
void AWGN::print(){
cout<<"AWGN(SNR="<<param<<",sigma="<< variance << "), capacity="<<capacity()<<endl;
}
unsigned int AWGN::transmitzero(std::vector<LLRType>& llr){
unsigned int N=llr.size();
return 0;
}
unsigned int AWGN::transmitzero(LLRType* llrs, unsigned int N){
unsigned int nerr=0;
RandomLib::NormalDistribution<> n;
for (unsigned int i=0;i<N;i++){
//llrs[i]=2.0*(n(RandomLib::Random::Global)*variance+1)/variance;
llrs[i]=2.0*n(RandomLib::Random::Global)+2.0/variance;
if(llrs[i]<0)
nerr++;
}
return nerr;
}