File: interleaver.h

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 (328 lines) | stat: -rw-r--r-- 8,249 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* File interleaver.h
   
   Description: Functions used to create the UMTS/3GPP and CCSDS interleavers.

   Copyright (C) 2005-2006, Matthew C. Valenti

   Last updated on June 24, 2006

   Functions  gcd and CreateUmtsInterleaver are 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
*/

/* Function gcd()

  Copyright 2001, Matthew C. Valenti.
	
  Description: Finds the greatest common divisor of the two integer-valued inputs.

	Input parameters:
		m		Input argument one (an integer).
		n		Input argument two (also an integer).

	Output parameters:
		out[]	The greatest common divisor of m and n.
	
  This function is used by UmtsTurboEncode()   */

int gcd( int m, int n )
{
    /*  The following is no longer needed 7/29/01 MCV
    int Rn;

    do {
        Rn = a % b;
        a = b;
        b = Rn;
    }while(Rn != 0);

    return a; */

	/* Note: For the UMTS interleaver, the first of these two
	values is prime.  Therefore we just have to check to see if the
	first number divides into the second one */

	if ( n%m == 0 ) {
		/* printf( "%d",m ); */
		return m;
	} else {
		/* printf( "x" ); */
		return 1;
	}
}


/* Function CreateCcsdsInterleaver()

  Copyright 2006, Matthew C. Valenti.
	
  Description: Creates the CCSDS interleaver.

	Input parameters:
		input		The input data frame of length LL bits.
					Ideally, this should be a vector containing the integers
					0 to L-1 in ascending order.
		LL			The number of data bits in the input frame.

	Output parameters:
		output[]	The interleaved output.
	
*/

void CreateCcsdsInterleaver( 
		int LL,
		int *output )

{
	int m, index, k1, k1by2, k2, i, j, t, q, c;
	int pTable[8] = { 31, 37, 43, 47, 53, 59, 61, 67 };

	/* deteremine parameter values */
	k1 = 8;
	k1by2 = 4;
	if ( ( LL == 1784 )||( LL == 3568 )||( LL == 7136 )||( LL == 8920 ) ) {
		k2 = (int) (LL/k1);
	} else {
		printf( "Error: CCSDS Interleaver size must be 7184, 3568, 7136, or 8920" );
		return;
	}

	/* determine interleaving pattern */
	for (index=0;index<LL;index++) {
		m = index%2;
		i = (int) (index/(2*k2));
		j = ( (int) (index/2) ) - i*k2;
		t = ( 19*i + 1 )%(k1by2);
		q = t%8;
		c = (pTable[q]*j+21*m)%k2;
		output[index] = 2*(t+c*k1by2+1)-m-1;
		/* printf( "%d %d %d %d %d %d %d\n", m, i, j, t, q, c, output[index] ); */
	}

	
}


/* Function CreateUmtsInterleaver()

  Copyright 2001-2006, Matthew C. Valenti.
	
  Description: Creates the UMTS interleaver.

	Input parameters:
		input		The input data frame of length LL bits.
					Ideally, this should be a vector containing the integers
					0 to L-1 in ascending order.
		LL			The number of data bits in the input frame.

	Output parameters:
		output[]	The interleaved output.
	
*/

void CreateUmtsInterleaver( 
		int LL,
		int *input,
		int *output )
{
	int RR, CC;
	int index, i, j, itemp;
	int pValue, vValue;
	int pTable[52] = {	7,	11,	13,	17,	19,	23,	29,	31,	37,	41,	43,
						47,	53,	59,	61,	67,	71,	73,	79,	83,	89,	97,
						101,103,107,109,113,127,131,137,139,149,151,
						157,163,167,173,179,181,191,193,197,199,211,
						223,227,229,233,239,241,251,257 };
	int vTable[52] = {	3,	2,	2,	3,	2,	5,	2,	3,	2,	6,	3,
						5,	2,	2,	2,	2,	7,	5,	3,	2,	3,	5,
						2,	5,	2,	6,	3,	3,	2,	3,	2,	2,	6,
						5,	2,	5,	2,	2,	2,	19,	5,	2,	3,	2,
						3,	2,	6,	3,	7,	7,	6,	3 };
	int T5[5] =		{	4,	3,	2,	1,	0	};
	int T10[10] =	{	9,	8,	7,	6,	5,	4,	3,	2,	1,	0 };
	int T20a[20] =	{	19,	9,	14,	4,	0,	2,	5,	7,	12,	18,
						16,	13,	17,	15,	3,	1,	6,	11,	8,	10 };
	int T20b[20] =	{	19,	9,	14,	4,	0,	2,	5,	7,	12,	18,
						10,	8,	13,	17,	3,	1,	16,	6,	15,	11 };
	
	int *Matrix, *IntraMatrix, *InterMatrix, *s, *q, *r;

	/* Step (1): Determine number of rows */
	if ( ( 40<=LL )&&( LL <= 159 ) ) {
		RR = 5;
	} else if ( ( 160<=LL )&&( LL <= 200 ) ) {
		RR = 10;
	} else if ( ( 481<=LL )&&( LL <= 530 ) ) {
		RR = 10;
	} else {
		RR = 20;
	}

	/* Step (2): Determine the prime number for intra-permutation 
			     and the number of columns */
	if ( ( 481<=LL )&&( LL <= 530 ) ) {
		pValue = 53;
		CC = pValue;
		vValue = 2;
	} else {
		for (index=0;index<52;index++) {
			if ( LL <= RR*(pTable[index] + 1)  ) {
				break;
			}
		}
		pValue = pTable[index];
		vValue = vTable[index];
		if ( LL <=RR*(pValue-1) ) {
			CC = pValue - 1;
		} else if ( RR*pValue < LL ) {
			CC = pValue + 1;
		} else {
			CC = pValue;
		}
	}

	/* Step (3): Stuff the bits into a rectangular matrix */
	Matrix = (int*)calloc( RR*CC, sizeof( int ) );
	InterMatrix = (int*)calloc( RR*CC, sizeof( int ) );
	IntraMatrix = (int*)calloc( RR*CC, sizeof( int ) );

	index = 0;
	for( i=0; i<RR; i++ ) {
		for ( j=0;j<CC;j++ ) {
			if ( index < LL ) {
				Matrix[i+j*RR] = input[index];
			} else {
				/* Insert Dummy Bits */
				Matrix[i+j*RR] = -1;
			}
			index++;
		}
	}

	/* Step (4): Construct base sequence for intra-row permutations */
	s = (int*)calloc( pValue-1, sizeof( int) );
	s[0] = 1;
	
	for (j=1;j<pValue-1;j++) {
		s[j] = (vValue*s[j-1])%pValue;
        /* printf( "s[%d] = %d\n", j, s[j] ); */
	}

	/* Step (5): Construct q-sequence --- this is a little confusing */
	q = (int*)calloc( RR, sizeof( int ) );
	q[0] = 1;

	index = 0;
	for (i=1;i<RR;i++) {
		itemp = index;
		for (index=itemp;index<52;index++ ) {
			if ( ( gcd( pTable[index], pValue-1 ) == 1 )&&( pTable[index] > q[i-1] ) ) {
				q[i] = pTable[index];
				break;
			}
		}
	}

	/* Step (6): Permute the q-sequence to make the r-sequence */
	r = (int*)calloc( RR, sizeof( int ) );
	if ( RR == 5 ) {
		for (i=0;i<RR;i++) {
			r[ T5[i] ] = q[i];            
		}
	} else if (RR == 10) {
		for (i=0;i<RR;i++) {
			r[ T10[i] ] = q[i];
		}
	} else if ( ( ( 2281<=LL )&&( LL<=2480) )||( ( 3161<=LL )&&( LL<=3210 ) ) ) {
		for (i=0;i<RR;i++) {
			r[ T20a[i] ] = q[i];
		}
	} else {
		for (i=0;i<RR;i++) {
			r[ T20b[i] ] = q[i];
		}
	}
    
    /* for (i=0;i<RR;i++)
        printf( "r[%d] = %d\n", i, r[i] );    */

	/* Step (7): Perform intra-row permutations */
	for (i=0;i<RR;i++) {
		if ( CC == pValue ) {
			for (j=0;j<=pValue-2;j++) {
				index = s[ (j*r[i])%(pValue-1) ];
				IntraMatrix[ i+j*RR ] = Matrix[ i+index*RR ];
			}
			IntraMatrix[ i + (pValue-1)*RR ] = Matrix[ i ];
		} else if ( CC == pValue + 1 ) {
			for (j=0;j<=pValue-2;j++) {
				index = s[ (j*r[i])%(pValue-1) ];
				IntraMatrix[ i+j*RR ] = Matrix[ i+index*RR ];
			}
			IntraMatrix[ i + (pValue-1)*RR ] = Matrix[ i ];
			IntraMatrix[ i + pValue*RR ] = Matrix[ i + pValue*RR ];
			/* Exchange  */
			if ( ( LL == RR*CC )&&(i==RR-1) ) {
				/* Just exchange bits in the last row */
				itemp = IntraMatrix[RR-1];
				IntraMatrix[RR-1] = IntraMatrix[(RR-1)+RR*pValue];
				IntraMatrix[(RR-1)+RR*pValue]=itemp;
			}
		} else {
			for (j=0;j<=pValue-2;j++) {
				index = s[ (j*r[i])%(pValue-1) ] - 1;
				IntraMatrix[ i+j*RR ] = Matrix[ i+index*RR ];
			}
		}
	}

	/* Step (8): Perform inter-row permutations */
	for (i=0;i<RR;i++) {
		if ( RR == 5 ) {
			index = T5[i];
		} else if (RR == 10) {
			index = T10[i];
		} else if ( ( ( 2281<=LL )&&( LL<=2480) )||( ( 3161<=LL )&&( LL<=3210 ) ) ) {
			index = T20a[i];
		} else {
			index = T20b[i];
		}

		for (j=0;j<CC;j++) {
			InterMatrix[ i+j*RR ] = IntraMatrix[ index+j*RR];
		}
	}

	/* Step (9): Read the bits back out from the matrix */
	index = 0;
	for( j=0; j<CC; j++ ) {
		for ( i=0;i<RR;i++ ) {
			if ( InterMatrix[i+j*RR] >= 0 ) {
				output[index] = InterMatrix[i+j*RR];
				index++;
			}
		}
	}

	free( Matrix );
	free( InterMatrix );
	free( IntraMatrix );
	free( s );
	free( q );
	free( r );

}