-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel_matrix.c
145 lines (113 loc) · 3.02 KB
/
channel_matrix.c
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
/* channel_matrix.c
Build a channel matrix from the given samples.
This code is experimental, and error-handling is primitive.
*/
/* Copyright 2013, NICTA. See COPYRIGHT for license details. */
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sparse.h"
#define MAXLINE 1024
int
main(int argc, char *argv[]) {
bsc_hist_t *H;
csc_mat_t *M;
csc_errno_t e;
int c, r;
FILE *out;
int rmin= INT_MIN, rmax= INT_MAX;
int climit= -1;
int *counts= NULL;
int discard= 0;
size_t malformed= 0, out_of_range= 0;
char buf[MAXLINE];
if(argc != 2 && argc != 4 && argc != 5 && argc != 6) {
printf("Usage: %s <output_filename> [<row. min> <row. max> "
"[<count limit> [<discard>]]]\n",
argv[0]);
return 1;
}
out= fopen(argv[1], "wb");
if(!out) {
perror("fopen");
return 1;
}
if(argc >= 4) {
rmin= atoi(argv[2]);
rmax= atoi(argv[3]);
}
if(argc >= 5) {
climit= atoi(argv[4]);
counts= calloc(rmax - rmin + 1, sizeof(int));
if(!counts) {
perror("calloc");
exit(EXIT_FAILURE);
}
}
if(argc >= 6) {
discard= atoi(argv[5]);
}
printf("Building histogram...");
fflush(stdout);
H= bsc_hist_new();
while(fgets(buf, MAXLINE, stdin)) {
int n;
n= sscanf(buf, "%d %d\n", &r, &c);
if(n != 2) {
malformed++;
continue;
}
if(r < rmin || rmax < r) {
out_of_range++;
continue;
}
if(climit >= 0) {
int i= r - rmin;
if(counts[i] >= climit + discard)
continue;
counts[i]++;
if(counts[i] <= discard)
continue;
}
bsc_hist_count(H, c, r, 1);
}
printf(" done.\n");
bsc_stats(H);
if(!bsc_check(H, 1)) abort();
printf("Building matrix\n");
M= bsc_normalise(H);
csc_stats(M);
bsc_hist_destroy(H);
if(!csc_check(M, 1)) abort();
/* Check the row totals. */
{
float *row_tot= malloc(M->nrow * sizeof(float));
if(!row_tot) { perror("malloc"); abort(); }
bzero(row_tot, M->nrow * sizeof(float));
for(c= 0; c < M->ncol; c++) {
int i;
for(i= M->ci[c]; i < M->ci[c+1]; i++) {
row_tot[M->rows[i]]+= M->entries[i];
}
}
for(r= 0; r < M->nrow; r++) {
if(fabs(row_tot[r] - 1.0) > 1e-3 && row_tot[r] != 0.0) {
fprintf(stderr, "Row %d sums to %.12e\n", r, row_tot[r]);
abort();
}
}
free(row_tot);
}
printf("Writing matrix\n");
e= csc_store_binary(M, out);
if(e != E_CSC_SUCCESS) {
csc_perror(e, "csc_write_binary");
return 1;
}
fclose(out);
printf("%lu malformed entries, %lu columns out of range\n",
malformed, out_of_range);
return 0;
}