-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvspectra_pci.c
306 lines (278 loc) · 9.82 KB
/
vspectra_pci.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
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
#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/io.h>
#include <math.h>
#include <string.h>
#include <sched.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <errno.h>
#include "d1cons.h"
#include "d1proto.h"
#include "d1typ.h"
#include "d1glob.h"
#include "pci-das4020.h"
int ADC_Mode = ADC_DMA_CONVERSION;
int Count = 1;
int Board = 0;
int Channel = 0;
int dmamap = 0;
int Status;
int freq_A2D = 20000000;
unsigned short *readBuff;
int fdDAC0, fdDAC1; /* D/A file descriptors */
int fdADC, fdADC0, fdADC1, fdADC2, fdADC3; /* A/D file descriptors */
int fdDIOA, fdDIOB, fdDIOC; /* DIO file descriptors */
static unsigned short buffer1[0x200000];
void fft_init(int, float *, double *, int *);
void cfft(int, float *, double *, int *);
void DoOpenDevices();
int get_pci(unsigned short value[], int count);
void Init_Device(int mode)
{
DoOpenDevices();
mode = 0;
}
void vspectra(void)
{
int i, j, size, sizep, kk, kkk, num, numm, i4;
int k, m, mm, blsiz, blsiz2, nsam, info, maxi;
double avsig, av, wid, noise, max;
static double vspec[NSPEC];
static int wtt[NSPEC];
static float ream[NSPEC * 4];
static double re[NSPEC * 2], am[NSPEC * 2];
double smax, aam, rre, aam2, rre2;
double *comm;
d1.bw = 10.0; // 10 MHz
d1.lofreq = 1416.0; // Luff 1416 MHz
d1.efflofreq = d1.lofreq;
d1.f1 = (d1.freq - d1.lofreq) / d1.bw - (d1.fbw / d1.bw) * 0.5;
d1.f2 = (d1.freq - d1.lofreq) / d1.bw + (d1.fbw / d1.bw) * 0.5;
d1.fc = (d1.f1 + d1.f2) * 0.5;
blsiz = NSPEC * 2;
info = 0;
comm = 0;
if (!d1.fftsim) {
comm = (double *) malloc((5 * blsiz + 100) * sizeof(double));
fft_init(blsiz, ream, comm, &info);
}
// printf("comm %x\n",comm);
nsam = 0x100000;
blsiz2 = blsiz / 2;
num = 20; // was 100
d1.nsam = nsam * num;
avsig = 0;
size = 0;
numm = 0;
smax = 0;
av = 0;
for (i = 0; i < blsiz2; i++)
vspec[i] = 0.0;
size = -1;
if (!d1.radiosim)
while (size != nsam)
size = get_pci(buffer1, nsam); // wait for transfer to complete
else {
av = 5.0;
if (d1.elnow < 5.0)
av = av * (d1.tsys + d1.tcal) / d1.tsys;
if (strstr(soutrack, "Sun")) {
av = sqrt(d1.eloff * d1.eloff +
d1.azoff * d1.azoff * cos(d1.elnow * PI / 180.0) * cos(d1.elnow * PI / 180.0) + 1e-6);
if (av > d1.beamw)
av = d1.beamw;
av = 5.0 + 25.0 * cos(av * PI * 0.5 / d1.beamw) * cos(av * PI * 0.5 / d1.beamw);
}
for (i = 0; i < nsam; i++)
buffer1[i] = 2048 + sqrt(av) * gauss();
size = nsam;
} // simulate transfer
sizep = size;
for (k = 0; k < num; k++) {
if (k < num - 1) {
if (!d1.radiosim)
size = get_pci(buffer1, nsam); // start new transfer
else {
for (i = 0; i < nsam; i++)
buffer1[i] = 2048 + sqrt(av) * gauss();
size = nsam;
} // simulate transfer
}
if (sizep == nsam) { // work on previous buffer
for (kk = 0; kk < sizep / blsiz; kk++) {
avsig = 2048; // should be 2048
// avsig = 2300;
kkk = kk * blsiz;
for (j = 0; j < blsiz; j++) {
// if(j==0 && kkk==0) printf("sam %f\n",(buffer1[j + kkk] & 0xfff) - avsig);
if (kk % 2 == 0)
ream[2 * j] = ((double) (buffer1[j + kkk] & 0xfff) - avsig);
else
ream[2 * j + 1] = ((double) (buffer1[j + kkk] & 0xfff) - avsig);
if (j && ream[2 * j] > smax)
smax = ream[2 * j];
}
if (kk % 2 == 1) {
if (d1.fftsim) {
for (i = 0; i < blsiz2; i++) {
re[i] = ream[2 * i];
am[i] = ream[2 * i + 1];
}
Four(re, am, blsiz2);
} else
cfft(blsiz, ream, comm, &info);
for (i = 0; i < blsiz2; i++) {
if (i >= 1) {
rre = ream[2 * i] + ream[2 * (blsiz - i)];
aam = ream[2 * i + 1] - ream[2 * (blsiz - i) + 1];
aam2 = -ream[2 * i] + ream[2 * (blsiz - i)];
rre2 = ream[2 * i + 1] + ream[2 * (blsiz - i) + 1];
} else {
rre = ream[2 * i] + ream[0];
aam = ream[2 * i + 1] - ream[1];
aam2 = -ream[2 * i] + ream[0];
rre2 = ream[2 * i + 1] + ream[1];
}
vspec[i] += rre * rre + aam * aam + rre2 * rre2 + aam2 * aam2;
}
}
}
numm++;
}
while (size != nsam)
if (!d1.radiosim)
size = get_pci(buffer1, nsam); // wait for transfer to complete
else {
for (i = 0; i < nsam; i++)
buffer1[i] = 2048 + sqrt(av) * gauss();
size = nsam;
} // simulate transfer
sizep = size;
}
if (d1.rms >= 0) {
d1.rms = 0;
for (i = 0; i < blsiz2; i++)
d1.rms += vspec[i];
d1.rms = sqrt(d1.rms / ((double) (nsam * 2.0 * numm))); // 2 for re and am
}
av = max = 0;
maxi = 0;
for (i = 0; i < blsiz2; i++)
wtt[i] = 1;
if (numm > 0) {
if (d1.nfreq == blsiz2) {
for (i = 0; i < blsiz2; i++) {
if (i > 10)
spec[i] = vspec[i] / (double) numm;
else
spec[i] = 0;
}
} else {
m = blsiz2 / d1.nfreq;
for (i = 0; i < d1.nrfi; i++) {
i4 = (d1.rfi[i] - d1.lofreq) * blsiz2 / d1.bw + 0.5; // index of rfi MHz
wid = 0.5 * d1.rfiwid[i] / (d1.bw / NSPEC);
for (j = -wid; j <= wid; j++)
if ((i4 + j) >= 0 && (i4 + j) < blsiz2)
wtt[i4 + j] = 0;
}
for (j = 0; j < d1.nfreq; j++) {
av = mm = 0;
for (i = j * m - m / 2; i <= j * m + m / 2; i++) {
if (i > 10 && i < blsiz2 && wtt[i]) { // wtt=0 removal of spurs
av += vspec[i] / (double) numm;
if (vspec[i] > max) {
max = vspec[i];
maxi = i;
}
mm++;
}
}
if (mm > 0)
spec[j] = av / mm;
else {
spec[j] = 0;
if (j > 10)
printf("check RFI settings in srt.cat data deleted at %8.3f\n",
j * d1.bw / d1.nfreq + d1.lofreq);
}
}
max = max / (double) numm;
noise = spec[maxi / m] * sqrt(2.0 * blsiz2 / (double) d1.nsam);
if (max > spec[maxi / m] + d1.rfisigma * noise && d1.printout) // rfisigma sigma
printf("check for RFI at %8.4f MHz max %5.0e av %5.0e smax %5.0f %3.0f sigma\n",
maxi * d1.bw / blsiz2 + d1.lofreq, max, spec[maxi / m], smax,
(max - spec[maxi / m]) / noise);
}
}
d1.smax = smax;
if (!d1.fftsim)
free(comm);
}
void DoOpenDevices()
{
char str[80];
int *adc_fds[] = { &fdADC0 };
int *dio_fds[] = { &fdDIOA };
sprintf(str, "/dev/das4020-12/ad%d_%d", Board, Channel);
if ((*adc_fds[0] = open(str, ADC_Mode | O_NONBLOCK)) < 0) {
perror(str);
printf("error opening device %s\n", str);
exit(2);
}
ioctl(fdADC0, ADC_SET_GAINS, BP_1_00V);
ioctl(fdADC0, ADC_SET_PACER_FREQ, freq_A2D);
ioctl(fdADC0, ADC_SET_FIFO_SIZE, 0x8000); // default?
sprintf(str, "/dev/das4020-12/dio%d_0%c", Board, 'A');
if ((*dio_fds[0] = open(str, O_RDWR)) < 0) {
perror(str);
printf("error opening device %s\n", str);
exit(2);
}
ioctl(fdDIOA, DIO_SET_DIRECTION, PORT_OUTPUT);
fdADC = *adc_fds[0];
}
int get_pci(unsigned short value[], int count)
{
int i;
int bytesRead;
unsigned int toggle = 0x0;
Count = count;
if (ADC_Mode == ADC_SOFT_CONVERSION) {
toggle ^= 0x1;
ioctl(fdADC, ADC_PSC_ENB, toggle);
bytesRead = read(fdADC, value, Count);
if (bytesRead != Count) {
printf("testADC: Error on read() \n");
printf("bytesRead = %d, and specified Count = %d\n", bytesRead, Count);
}
} else {
if (!dmamap) {
if ((readBuff = mmap(0, Count * 2, PROT_READ, MAP_PRIVATE, fdADC, 0 * getpagesize()))
== (unsigned short *) MAP_FAILED) {
printf("Test Failed: Mmap call failed %x \n", (int) readBuff);
printf(" %d\n", errno);
sleep(3);
return (0);
} else {
printf("Test Passed: Succesfully mmaped %d bytes\n", Count * 2);
}
}
dmamap = 1;
/* In the following read calls, the argument value will be ignored */
/* Since we DMA to stuff over to the address held by readBuff */
bytesRead = read(fdADC, value, Count);
if (bytesRead != Count) {
return bytesRead;
}
for (i = 0; i < Count; i++) {
value[i] = readBuff[i];
}
}
return (Count);
}