-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathincoh_beamformer.c
168 lines (141 loc) · 3.51 KB
/
incoh_beamformer.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
gcc -o incoh_beamformer incoh_beamformer.c -I/usr/local/include -L/usr/local/lib -lm -g -O2 -L/usr/lib/gcc/x86_64-linux-gnu/5 -lgfortran
inoherent beamformer
This code should take 5 parameters:
* data file name
* output file name
greg hellbourg
*/
#include "stdio.h"
#include "stdlib.h"
#include "sys/types.h"
#include "sys/socket.h"
#include "string.h"
#include "netinet/in.h"
#include "netdb.h"
#include <unistd.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <math.h>
void beamformer(char *input, int nAnts, int nChans, int nTimes, int nPols, unsigned char *output) {
float inr_x, ini_x, inr_y, ini_y;
float tmp;
for(int nt=0;nt<nTimes;nt++){
for(int nc=0;nc<nChans;nc++){
tmp = 0;
for(int na=0;na<nAnts;na++){
inr_x = (float)(((char)((input[na*(nChans*nTimes*nPols)+nc*(nTimes*nPols)+nt*nPols] & 15) << 4)) >> 4);
ini_x = (float)(((char)((input[na*(nChans*nTimes*nPols)+nc*(nTimes*nPols)+nt*nPols] & 240))) >> 4);
inr_y = (float)(((char)((input[na*(nChans*nTimes*nPols)+nc*(nTimes*nPols)+nt*nPols+1] & 15) << 4)) >> 4);
ini_y = (float)(((char)((input[na*(nChans*nTimes*nPols)+nc*(nTimes*nPols)+nt*nPols+1] & 240))) >> 4);
tmp += (inr_x*inr_x+ini_x*ini_x+inr_y*inr_y+ini_y*ini_y);
}
tmp /= nAnts;
output[nt*nChans+nc] = (unsigned char)tmp;
}
}
}
void usage()
{
fprintf (stdout,
"t3_INCOHERENT_beamformer [options]\n"
" -d voltage data file name [no default]\n"
" -o output file name[no default]\n"
" -a number of antennas in voltage file[default : 24]\n"
" -u number of antennas used in beamformer [default : 24]\n"
" -h print usage\n");
}
int main (int argc, char *argv[]) {
// data file constants
int nAnts = 24;
int nUsedAnts = 24;
int nChans = 384;
int nTimes = 2;
int nPols = 2;
// read params : fch1, fnam, fdataname, sep
int arg = 0;
char * fdata;
fdata=(char *)malloc(sizeof(char)*200);
sprintf(fdata,"nofile");
char * fout;
fout=(char *)malloc(sizeof(char)*200);
sprintf(fout,"nofile");
while ((arg=getopt(argc,argv,"d:o:a:u:h")) != -1)
{
switch (arg)
{
case 'd':
if (optarg)
{
strcpy(fdata,optarg);
break;
}
else
{
printf("-d flag requires argument");
usage();
return EXIT_FAILURE;
}
case 'o':
if (optarg)
{
strcpy(fout,optarg);
break;
}
else
{
printf("-o flag requires argument");
usage();
return EXIT_FAILURE;
}
case 'a':
if (optarg)
{
nAnts = atoi(optarg);
break;
}
else
{
printf("-a flag requires argument");
usage();
return EXIT_FAILURE;
}
case 'u':
if (optarg)
{
nUsedAnts = atoi(optarg);
break;
}
else
{
printf("-u flag requires argument");
usage();
return EXIT_FAILURE;
}
case 'h':
usage();
return EXIT_SUCCESS;
}
}
// compute beamformer weights
unsigned char * output = (char *)malloc(sizeof(char)*384*2);
unsigned char * input = (char *)malloc(sizeof(char)*nAnts*384*2*2);
FILE *ptr;
FILE *write_ptr;
ptr = fopen(fdata,"rb"); // r for read, b for binary
write_ptr = fopen(fout,"wb"); // w for write, b for binary
int rd;
long int sz;
fseek(ptr, 0L, SEEK_END);
sz = ftell(ptr);
rewind(ptr);
int nTotSam = (int)(floor(sz / (nAnts*nChans*nTimes*nPols)));
for(int nSam = 0; nSam < nTotSam; nSam++) {
rd = fread(input,nAnts*nChans*nTimes*nPols,1,ptr);
beamformer(input,nUsedAnts,nChans,nTimes,nPols,output);
fwrite(output,nChans*nTimes,1,write_ptr);
}
fclose(ptr);
fclose(write_ptr);
}