File: LdpcEncode.cpp

package info (click to toggle)
codec2 1.2.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 76,376 kB
  • sloc: ansic: 436,819; cpp: 2,091; objc: 1,736; sh: 1,510; python: 1,405; asm: 683; makefile: 605
file content (172 lines) | stat: -rw-r--r-- 4,820 bytes parent folder | download | duplicates (3)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* File: LdpcEncode.c

   Description: Encodes a single LDPC codeword.  Code must be an "eIRA-LDPC" type code,
                such as the one in the DVB-S2 standard or WiMax standard.

   The calling syntax is:
   codeword    = LdpcEncode( data, H_rows, [P])

   Where:
      codeword = the encoded codeword

      data   = a row vector containing the data
      H_rows = a M-row matrix containing the indices of the non-zero rows of H excluding the dual-diagonal part.
      P      = (optional) z times z matrix used to generate the first z check bits for WiMax (default =[])

   Copyright (C) 2005-2007, Rohit Iyer Seshadri and Matthew C. Valenti

   Last updated on June. 23, 2007

   Function LdpcEncode is part of the Iterative Solutions 
   Coded Modulation Library. The Iterative Solutions Coded Modulation 
   Library is free software; you can redistribute it and/or modify it 
   under the terms of the GNU Lesser General Public License as published 
   by the Free Software Foundation; either version 2.1 of the License, 
   or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
  
   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
# include<mex.h>
# include<stdio.h>
# include<math.h>
# include<stdlib.h>

void encode(double u[],double H_rows[],double *c_in,int nldpc,int kldpc,int mldpc, int wid_Hrows, double P[], int shift)
{
	int count,i,j, k,sum,temp, cnt;
	int *x,*sum2, *sum_tmp, *tmp;   
	
	x       =(int*)calloc((mldpc+1),sizeof(int));
	sum2    =(int*)calloc(shift,sizeof(int));
    sum_tmp =(int*)calloc(shift,sizeof(int));
    tmp     =(int*)calloc(shift,sizeof(int));
    
	
	for (i=0;i<kldpc;i++){
		c_in[i]=u[i];
 	 } 
	for (i=0;i<mldpc;i++){
        c_in[kldpc+i]=0; 
	}			
	
	if (shift ==0){
		sum=0;	 
         for(i=0;i<mldpc;i++){		
	  	    for(k=1;k<=wid_Hrows;k++){
			   if(H_rows[i+mldpc*(k-1)]!=0){
			    	count=(int)H_rows[i+mldpc*(k-1)];
				    x[i]=((int)c_in[count-1])^((int)x[i]) ;
			   }
		   }
	    	c_in[kldpc+i]=x[i]^sum; /* Differential encoding */
		    sum=c_in[kldpc+i];
	}
	   
}		
    else{	 
	    for (j=0;j<kldpc/shift;j++){
	       cnt=0;
	       for (i =0;i<mldpc;i++){
	          
	           for(k=1;k<=wid_Hrows;k++){
			       if(H_rows[i+mldpc*(k-1)]!=0){			      
			          if (((int)H_rows[i+mldpc*(k-1)]>j*shift )&&((int)H_rows[i+mldpc*(k-1)]<=(j+1)*shift)){
				         count=(int)H_rows[i+mldpc*(k-1)];
				         tmp[cnt]=((int)c_in[count-1])^((int)tmp[cnt]) ;
			          }
	    	       }  
	    	   }
	     
	          sum_tmp[cnt]=sum_tmp[cnt]^tmp[cnt];
	          tmp[cnt]=0;
	          if (((i+1) % shift) ==0){
	              cnt=0;
	          }
	          else{	      
	             cnt++;      
	          }
		   }
	   }
	   for (k=0;k<shift;k++){
	         sum=0;
	         for (i =0;i<shift;i++){
	             sum=sum^(((int)P[k+(i)*shift])*sum_tmp[i]);              
	          }
	         c_in[kldpc+k]=sum;
	     }
	     
	   cnt=0;
       for(i=0;i<mldpc-shift;i++){		
           for(k=1;k<=wid_Hrows;k++){
			   if(H_rows[i+mldpc*(k-1)]!=0){
				   count=(int)H_rows[i+mldpc*(k-1)];
				   x[i]=((int)c_in[count-1])^((int)x[i]) ;
			   }
	    	}  
	        c_in[kldpc+shift+i]=x[i]^sum2[cnt]; /* Differential encoding */
	        sum2[cnt]=c_in[kldpc+i+shift];
	            if (((i+1)%shift)==0){
	                cnt=0;
	            }
	            else{
	                 cnt=cnt+1;
	            }	       	       
	   } 
	        
	 }
     
	free(x);
    free(sum2);
    free(sum_tmp);
    free(tmp);
    return;	
}


void mexFunction(int nlhs,mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
	double *u,*c_in, *H_rows, *P;
	int index,nldpc,kldpc,mldpc,wid_Hrows,shift;
	
	/* Error checks	*/
	if ((nrhs <2)||(nrhs >3)){
		mexErrMsgTxt(" Usage:    codeword    = LdpcEncode( data, H_rows, [P]) \n");
	}
	else if (nlhs >1){
		mexErrMsgTxt(" only 1 output ");
	}	
	
	
   /* default values */
   shift=0;
   P= (double*)calloc(shift,sizeof(int));

	/* Assign the variables to corresp. mlab pointers */
	u=mxGetPr(prhs[0]);
	H_rows=mxGetPr(prhs[1]);
	
	if (nrhs >2) {
	    P=mxGetPr(prhs[2]);
	    shift=mxGetM(prhs[2]);
	}

	kldpc=mxGetN(prhs[0]);
	mldpc= mxGetM(prhs[1]);
	wid_Hrows=mxGetN(prhs[1]);
	nldpc=kldpc+mldpc;
 	 
	/* create output m array */
	plhs[0]= mxCreateDoubleMatrix(1,nldpc,mxREAL);
	
	/* create output m array */
	c_in=mxGetPr(plhs[0]);
	encode(u,H_rows,c_in,nldpc,kldpc,mldpc, wid_Hrows,P,shift);
}