📄 factorgraph.c
字号:
#include <stdio.h>#include <iostream.h>#include <fstream.h>#include <string.h>#include "FactorGraph.h"FactorGraph::FactorGraph(const BlockCodeGenerator *blockCodeGenerator) { N = (*blockCodeGenerator).N; K = (*blockCodeGenerator).K; M = (*blockCodeGenerator).M; m = (*blockCodeGenerator).m; T = (*blockCodeGenerator).T; infoBitsLocation = (*blockCodeGenerator).infoBitsLocation; int *onesInH = new int[N]; for (int i = 0; i < N; i++) { onesInH[i] = 0; } int id; char nodeName[20]; probabilities = new double*[K]; for (i = 0; i < K; i++) { probabilities[i] = new double[2]; } getOnesInColsOfH(onesInH); codeBits = new VariableNode*[N]; for (i = 0; i < N; i++) { sprintf(nodeName, "CodeBit_%d", i); codeBits[i] = new VariableNode(nodeName, i + id, onesInH[i] + 1, // onesInH[i] + 2, if extended graph is used!! 2); } id += N; delete [] onesInH; onesInH = new int[M]; for (i = 0; i < M; i++) { onesInH[i] = 0; } getOnesInRowsOfH(onesInH); parityChecks = new ParityCheckNode*[M]; for (i = 0; i < M; i++) { sprintf(nodeName, "ParityCheck_%d", i); parityChecks[i] = new ParityCheckNode(nodeName, i + id, onesInH[i]); } id += M; channelTransitions = new ChannelTransition*[N]; for (i = 0; i < N; i++) { sprintf(nodeName, "ChannelTransition_%d", i); channelTransitions[i] = new ChannelTransition(nodeName, i + id); } delete [] onesInH; onesInH = NULL; // connect all nodes int *portV; int portF; portV = new int[N]; for (i = 0; i < N; i++) { portV[i] = 1; } for (i = 0; i < M; i++) { // all parity check nodes portF = 0; for (int j = 0; j < m; j++) { // all entries in row i of S if (T[i][j] >= 0) { // entry found ==> connect Node::connect(parityChecks[i], codeBits[T[i][j]], portF, portV[T[i][j]], 2); portF++; portV[T[i][j]]++; } } } delete [] portV; for (i = 0; i < N; i++) { // all channel transition nodes Node::connect(channelTransitions[i], codeBits[i], 0, 0, 2); }}FactorGraph::~FactorGraph(void) { for (int i = 0; i < M; i++) { // all parity check nodes for (int j = 0; j < m; j++) { // all entries in row i of S if (T[i][j] >= 0) { // non-zero entry found ==> connect Node::disconnect(parityChecks[i], codeBits[T[i][j]]); } } } for (i = 0; i < N; i++) { Node::disconnect(channelTransitions[i], codeBits[i]); } for (i = 0; i < N; i++) { delete codeBits[i]; codeBits[i] = NULL; } delete [] codeBits; codeBits = NULL; for (i = 0; i < N; i++) { delete channelTransitions[i]; channelTransitions[i] = NULL; } delete []channelTransitions; channelTransitions = NULL; for (i = 0; i < M; i++) { delete parityChecks[i]; parityChecks[i] = NULL; } delete []parityChecks; parityChecks = NULL; for (i = 0; i < K; i++) { delete[] probabilities[i]; probabilities[i] = NULL; } delete[] probabilities; probabilities = NULL; }void FactorGraph::getOnesInRowsOfH(int *ones) { for (int r = 0; r < M; r++) { for (int c = 0; c < m; c++) { if (T[r][c] >= 0) { ones[r]++; } } }}void FactorGraph::getOnesInColsOfH(int *ones) { //for (int c = 0; c < N; c++) { for (int r = 0; r < M; r++) { for (int i = 0; i < m; i++) { if (T[r][i] >= 0) { ones[T[r][i]]++; } } } //}}void FactorGraph::initialize(double *(*prob)) { // reset the whole factor graph reset(); for (int i = 0; i < N; i++) { (*channelTransitions[i]).setProbabilities(prob[i]); } // update all nodes that have to be updated only once before all iterations are going to be made for (i = 0; i < N; i++) { (*codeBits[i]).update(0); }}void FactorGraph::update(void) { LDPCupdate();}void FactorGraph::LDPCupdate(void) { for (int i = 0; i < N; i++) { (*codeBits[i]).updateAll(); } for (i = 0; i < M; i++) { (*parityChecks[i]).updateAll(); }}void FactorGraph::LDPCupdateExBitK(int k) { for (int i = 0; i < N; i++) { if(i!=k) (*codeBits[i]).updateAll(); } for (i = 0; i < M; i++) { (*parityChecks[i]).updateAll(); }}void FactorGraph::LDPCupdateExBitK(int k, int j) { for (int i = 0; i < N; i++) { if(i!=k && i!=j) (*codeBits[i]).updateAll(); } for (i = 0; i < M; i++) { (*parityChecks[i]).updateAll(); }}void FactorGraph::reset(void) { for (int i = 0; i < N; i++) { (*codeBits[i]).reset(); } for (i = 0; i < M; i++) { (*parityChecks[i]).reset(); } for (i = 0; i < N; i++) { (*channelTransitions[i]).reset(); } }int FactorGraph::getAbsSyndrome(int *codeWord){ //for every component of the syndrome vector int absSyndrome = 0; int sum; for (int i = 0; i < M; i++){ sum = 0; //compute the value of this syndrome component for (int j = 0; j < m; j++){ if (T[i][j] >= 0) { sum += codeWord[T[i][j]]; } } sum = sum%2; absSyndrome += sum; } return absSyndrome;}void FactorGraph::getCodeWord(int *codeWord, double *codeWordLLR){ //double *codeWordLLR; //codeWordLLR=new double[N]; for (int i = 0; i < N; i++) { codeWordLLR[i]=(*codeBits[i]).getOutMessage(0)[0]+(*channelTransitions[i]).getOutMessage(0)[0]; if (codeWordLLR[i] > 0.0) { codeWord[i] = 0; } else { codeWord[i] = 1; } } //delete [] codeWordLLR; //codeWordLLR=NULL;}void FactorGraph::getCodeWord(int *codeWord, double *codeWordLLR, int pos){ //double *codeWordLLR; //codeWordLLR=new double[N]; for (int i = 0; i < N; i++) { if(i!=pos) codeWordLLR[i]=(*codeBits[i]).getOutMessage(0)[0]+(*channelTransitions[i]).getOutMessage(0)[0]; else codeWordLLR[i]=-1e308; //tentatively hard decision to 1; if (codeWordLLR[i] > 0.0) { codeWord[i] = 0; } else { codeWord[i] = 1; } } //delete [] codeWordLLR; //codeWordLLR=NULL;}void FactorGraph::decodeCodeWord(int *codeWord, int *decidedBits){ for (int i = 0; i < K; i++){ decidedBits[i] = codeWord[infoBitsLocation[i]]; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -