UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
matrix-generator.cpp
Go to the documentation of this file.
1/*
2 * =====================================================================================
3 *
4 * Filename: matrix-generator.c
5 *
6 * Description:
7 *
8 * Version: 1.0
9 * Created: 04/30/2012 04:49:14 PM
10 * Revision: none
11 * Compiler: gcc
12 *
13 * Author: Milan Kabat (), kabat@ics.muni.cz
14 * Organization:
15 *
16 * =====================================================================================
17 */
18#include <stdlib.h>
19#include <unistd.h>
20#include <time.h>
21#include <stdio.h>
22#include <string.h>
23
24#include "ldpc-matrix.h"
25#include "matrix-generator.h"
26 #include "rand_pmms.h"
27
28int generate_ldgm_matrix(char *fname, unsigned int k, unsigned int m, unsigned int column_weight,
29 unsigned int seed,unsigned int extend_rows)
30{
31
32 int random = 0;
33 int rfc = 1;
34
35 if ( ! ( k > 0 && m > 0 && column_weight > 0 && (random > 0 || rfc > 0) ) )
36 {
37 return EXIT_FAILURE;
38 }
39
40 char **pc_matrix;
41
42 pc_matrix = (char**)malloc(m*sizeof(char*));
43 for ( unsigned int i = 0; i < m; i++)
44 pc_matrix[i] = (char*)calloc(k, sizeof(char));
45
46
47 //Generate random 1's per column
48 if (random)
49 {
50 srand(time(NULL));
51 for ( unsigned int i = 0; i < k; ++i ) {
52 for ( unsigned int j = 0; j < column_weight; ++j) {
53 pc_matrix[rand()%m][i] = 1;
54 }
55 }
56 }
57 //Use algorithm from RFC 5170
58 if (rfc)
59 {
60 left_matrix_init ( pc_matrix, k, k+m, column_weight, seed );
61 }
62
63 //Extend the matrix if necessary:
64 //Each additional row is computed as follows: pseudorandomly choose a row of parity matrix
65 //and shift it circularly one step to the right
66 if (extend_rows > 0)
67 {
68 Rand_pmms rand_gen;
69 rand_gen.seedi(seed);
70
71 for ( unsigned int i = m; i < m + extend_rows; i++ )
72 {
73 int row = rand_gen.pmms_rand(m - 1);
74 for ( unsigned int j = 1; j < k; j++)
75 {
76 if (pc_matrix[row][j - 1])
77 {
78 pc_matrix[i][j] = 1;
79 }
80 }
81 if (pc_matrix[row][k - 1])
82 {
83 pc_matrix[i][0] = 1;
84 }
85 }
86 }
87
88 m += extend_rows;
89
90 int max_weight = 0;
91 //Compute maximum row weight
92 for ( unsigned int i = 0; i < m; i++)
93 {
94 int m = 0;
95 for ( unsigned int j = 0; j < k; j++)
96 if( pc_matrix[i][j])
97 m++;
98 if ( m > max_weight )
99 max_weight = m;
100 }
101
102/* for ( int i = 0; i < m; i++)
103 * {
104 * for ( int j = 0; j < k; j++)
105 * printf ( "[%3d]", pc_matrix[i][j] );
106 * printf ( "\n" );
107 * }
108 * printf ( "\n" );
109 */
110
111 int **pcm;
112 pcm = (int**)malloc(m*sizeof(int*));
113 for ( unsigned int i = 0; i < m; i++)
114 pcm[i] = (int*)malloc((max_weight+2) * sizeof(int));
115
116
117 int columns = max_weight + 2;
118 int counter = 0;
119 for ( unsigned int i = 0; i < m; i++ )
120 {
121 for ( unsigned int j = 0; j < k; j++)
122 if( pc_matrix[i][j])
123 {
124 pcm[i][counter] = j;
125 counter++;
126 }
127 //add indices from staircase matrix
128 pcm[i][counter] = k + i;
129 counter++;
130
131 if ( i > 0 )
132 {
133 pcm[i][counter] = k + i - 1;
134 counter++;
135 }
136
137 if ( counter < columns )
138 for ( int j = counter; j < columns; j++)
139 pcm[i][j] = -1;
140 counter = 0;
141 }
142
143/* for ( int i = 0; i < m; i++)
144 * {
145 * for ( int j = 0; j < columns; j++)
146 * printf ( "[%3d]", pcm[i][j] );
147 * printf ( "\n" );
148 * }
149 */
150 FILE *out;
151
152 int tmp;
153
154 out = fopen (fname, "wb");
155 if(out)
156 {
157 fprintf( out, "%d ", k);
158 fprintf( out, "%d ", m);
159 fprintf( out, "%d\n", columns);
160 for ( unsigned int i = 0; i < m; i++)
161 {
162 for ( int j = 0; j < columns; j++)
163 {
164 int t = pcm[i][j];
165 tmp = fwrite(&t, sizeof(int), 1, out);
166 if (tmp != 1)
167 fprintf(stderr, "Cannot write to output file!");
168 }
169// printf ( "fwrite: %d\n", tmp );
170 }
171 fflush(out);
172 fclose(out);
173 } else
174 {
175 fprintf(stderr, "Cannot open file for writing.");
176 }
177
178/* out = fopen(fname, "rb");
179 *
180 * int k_f, m_f, w_f;
181 * fscanf(out, "%d %d %d", &k_f, &m_f, &w_f);
182 * printf ( "In matrix file: K %d M %d Columns %d\n", k_f, m_f, w_f );
183 *
184 * fseek ( out , 1 , SEEK_CUR );
185 * int *p;
186 * p = (int*) malloc(columns*m*sizeof(int));
187 * memset(p, 99, columns*m*sizeof(int));
188 * for ( int i = 0; i < m; i++)
189 * {
190 * for ( int j = 0; j < columns; j++)
191 * {
192 * int t;
193 * tmp = fread ( &t, sizeof(int), 1, out);
194 * p[i*columns + j] = t;
195 * }
196 * }
197 * // printf ( "fread: %d\n", tmp );
198 * for ( int i = 0; i < m; i++)
199 * {
200 * for ( int j = 0; j < columns; j++)
201 * printf ( "[%3d]", (unsigned int)(p[i*columns+j]) );
202 * printf ( "\n" );
203 * }
204 *
205 */
206
207
208//Print matrix
209/* for ( int i = 0; i < m; i++)
210 * {
211 * for ( int j = 0; j < k; j++)
212 * printf ( "%3d", pc_matrix[i][j] );
213 * printf ( "\n" );
214 * }
215 * printf ( "\n" );
216 */
217
218
219 for ( unsigned int i = 0; i < m; i++)
220 free(pc_matrix[i]);
221 free(pc_matrix);
222 for ( unsigned int i = 0; i < m; i++)
223 free(pcm[i]);
224 free(pcm);
225
226 return EXIT_SUCCESS;
227}
228
void seedi(unsigned long int s)
Definition rand_pmms.h:28
unsigned long int pmms_rand(unsigned long int maxv)
Definition rand_pmms.h:35
void left_matrix_init(char **matrix, int k, int n, int N1, int seed)
int generate_ldgm_matrix(char *fname, unsigned int k, unsigned int m, unsigned int column_weight, unsigned int seed, unsigned int extend_rows)