-
Notifications
You must be signed in to change notification settings - Fork 0
/
psd.c
356 lines (301 loc) · 11.3 KB
/
psd.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//======================================================================================
//
// psd.c
//
//======================================================================================
//
//
//
//======================================================================================
#include <syslog.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <math.h>
#include <getopt.h>
#include <stdio.h>
#include "ipps.h"
#include "daq_i.h"
#include "data_writer.h"
#include "logger.h"
//======================================================================================
// Macros
//======================================================================================
#define SLICE_SIZE 512
#define N_IPARAMETERS 4
#define N_FPARAMETERS 2
#define MAGIC_SCALING 0.1915
//======================================================================================
// Handle the terminate interuption to ensure reliable data
//======================================================================================
static void sig_int( int );
int halt = 0; // Stop flag
//======================================================================================
// Routines for the parsing of input arguments.
//======================================================================================
int parse_inputs(int argsc, char** argsv, float* statistic,
float* period, int* maxiter, char** runid);
void print_usage(char* proccess);
//======================================================================================
int main( int argsc, char *argsv[] )
//======================================================================================
{
int nslice = daq_buffer_size()/SLICE_SIZE;
int start_apex = 0; // 0) Slave mode, 1) Master mode
int period = 1; // Period over which data are accumulated
int ret; // Return value
int irq_count = 0; // Use this to track which data buffer we are at
int last_irq_count = 0;
int irq_after = 0;
float scaling = MAGIC_SCALING;
int Nsample = -1;
char timefilename[] = "PSD_time.bin";
char datafilename[] = "PSD_data.bin";
char hostname[ 65 ];
int iparameters[ N_IPARAMETERS ];
float fparameters[ N_FPARAMETERS ];
char *runnumber;
int islice, nstat;
// Allocate memory for FFT
//===
const int FFT_order = (int)( log( SLICE_SIZE )/log( 2 ) + 0.05 );
const int FFT_size = (int)( SLICE_SIZE/2+1 );
const int CCS_size = (int)( SLICE_SIZE+2 );
IppsFFTSpec_R_32f* pFFT;
IppStatus status = ippsFFTInitAlloc_R_32f( &pFFT, FFT_order, IPP_FFT_DIV_FWD_BY_N, ippAlgHintAccurate );
if ( status != ippStsNoErr )
{
notify(ERROR, "Could not allocate FFT structure memory. Aborting." );
return( 0 );
}
int FFT_buffer_size = 0;
status = ippsFFTGetBufSize_R_32f( pFFT, &FFT_buffer_size );
Ipp8u* FFT_buffer = ippsMalloc_8u( FFT_buffer_size );
if ( FFT_buffer == NULL )
{
notify(ERROR, "Could not allocate memory for FFT buffer. Aborting." );
return( 0 );
}
Ipp32f* FFT_data = ippsMalloc_32f( CCS_size );
Ipp32f* psd = ippsMalloc_32f( CCS_size );
Ipp32f* FFT_win = ippsMalloc_32f( SLICE_SIZE );
if ( ( FFT_data == NULL ) || ( psd == NULL ) || ( FFT_win == NULL ) )
{
notify(ERROR, "Could not allocate memory for FFT data. Aborting." );
return( 0 );
}
ippsSet_32f( 1, FFT_win, SLICE_SIZE );
ippsWinHann_32f_I( FFT_win, SLICE_SIZE );
//====================================================================================
// Parse arguments
//====================================================================================
float statistic = 1e5;
float period_s = 0;
if (parse_inputs(argsc, argsv, &statistic, &period_s, &Nsample, &runnumber) < 0)
return(0);
period = (int)(period_s/1.3422 + 0.4999);
if ( period <= 0 )
period = 1;
scaling = statistic*MAGIC_SCALING/1e5/period;
//====================================================================================
// Configure I/Os
//====================================================================================
gethostname( hostname, sizeof( hostname ) );
int irun = atoi(runnumber);
int ihost = atoi(hostname+1);
dw_initialise(irun, ihost);
dw_clear(datafilename);
dw_clear(timefilename);
//====================================================================================
// Start the DAQ
//====================================================================================
if (daq_start() < 0)
return 0;
//====================================================================================
// Remap terminate interupt
//====================================================================================
signal( SIGINT, sig_int );
//====================================================================================
// Acquisition loop on data buffers
//====================================================================================
srand48( time( NULL ) );
int count = 0;
int nacc = 0;
while ( halt == 0 )
{
// Synchronize with a buffer switch.
//===
daq_synchronise();
irq_count = daq_counter();
// Check for interupt
//===
if (halt != 0)
break;
// Map the iddle buffer
//===
unsigned char* dma_buf = daq_data();
//==================================================================================
// Analyse the sub buffers
//==================================================================================
if ( nacc == 0 ) // Initialisation
{
nstat = 0;
memset( fparameters, 0x0, N_FPARAMETERS*sizeof( float ) );
ippsZero_32f( psd, CCS_size );
}
// Accumulation
//===
for ( islice = 0; islice < nslice; islice++ )
{
if ( drand48() < scaling )
{
float p[ 2 ];
ippsConvert_8u32f( dma_buf, FFT_data, SLICE_SIZE );
ippsMeanStdDev_32f( FFT_data, SLICE_SIZE, p, p+1,
ippAlgHintAccurate );
fparameters[ 0 ] += p[ 0 ];
fparameters[ 1 ] += p[ 1 ]*p[ 1 ];
ippsMul_32f_I( FFT_win, FFT_data, SLICE_SIZE );
ippsFFTFwd_RToCCS_32f_I( FFT_data, pFFT, FFT_buffer );
ippsSqr_32f_I( FFT_data, CCS_size );
ippsAdd_32f_I( FFT_data, psd, CCS_size );
nstat++;
}
dma_buf += SLICE_SIZE;
}
nacc++;
//==================================================================================
// Write to file & Verbose
//==================================================================================
if ( nacc >= period )
{
nacc = 0;
if ( nstat == 0 )
continue;
for ( islice = 0; islice < FFT_size; islice++ )
psd[ 2*islice ] += psd[ 2*islice +1 ];
for ( islice = 1; islice < FFT_size; islice++ )
psd[ islice ] = psd[ 2*islice ];
Ipp32f norm = 1.0/nstat;
ippsCopy_32f( fparameters, psd+FFT_size, N_FPARAMETERS );
ippsMulC_32f_I( norm, psd, FFT_size+N_FPARAMETERS );
psd[ FFT_size+1 ] = sqrt( psd[ FFT_size+1 ] );
irq_after = daq_counter();
iparameters[ 0 ] = time( NULL );
iparameters[ 1 ] = irq_count;
iparameters[ 2 ] = irq_after;
iparameters[ 3 ] = nstat;
dw_dump(timefilename, N_IPARAMETERS, iparameters);
dw_dump(datafilename, FFT_size-1+N_FPARAMETERS, psd+1);
notify(INFO, "irq_count=%d/%d, stat=%d.",
iparameters[ 1 ], iparameters[ 2 ], iparameters[ 3 ] );
count++;
}
// Check the number of samples recorded
//===
if (( count >= Nsample ) && ( Nsample > 0 ))
break;
}
//====================================================================================
// Close $ free
//====================================================================================
ippsFFTFree_R_32f( pFFT );
ippsFree( FFT_buffer );
ippsFree( FFT_data );
ippsFree( psd );
ippsFree( FFT_win );
daq_close();
return( 0 );
}
//======================================================================================
static void sig_int( int signo )
//======================================================================================
{
notify(WARNING, "Caught SIGINT\n\tTerminating program ...");
halt = 1;
return;
}
//================================================================
int parse_inputs(int argsc, char** argsv, float* statistic,
float* period, int* maxiter, char** runid)
//================================================================
//
// Parse the inputs arguments.
//
//================================================================
{
char c;
// Initialisation of mandatory arguments.
*runid = NULL;
// Parse the command line.
while (1)
{
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{"statistic", required_argument, 0, 's'},
{"period", required_argument, 0, 'p'},
{"runid", required_argument, 0, 'r'},
{"maxiter", required_argument, 0, 'm'},
DAQ_LONG_OPTIONS,
DW_LONG_OPTIONS,
LOGGER_LONG_OPTIONS
};
int option_index = 0;
c = getopt_long(argsc, argsv,
"hl:p:r:" DAQ_GETOPT_DESCRIPTOR DW_GETOPT_DESCRIPTOR LOGGER_GETOPT_DESCRIPTOR,
long_options, &option_index
);
if (c == -1)
break;
else if (c == 'h')
{
print_usage(argsv[0]);
return(-1);
}
else if (c == 's')
*statistic = strtod(optarg, NULL);
else if (c == 'p')
*period = strtod(optarg, NULL );
else if (c == 'r')
*runid = optarg;
else if (c == 'm')
*maxiter = atoi(optarg);
else
{
daq_parse_option(c, optarg);
dw_parse_option(c, optarg);
logger_parse_option(c, optarg);
}
}
// Check if mandatory arguments where provided.
if (*runid == NULL)
{
print_usage(argsv[0]);
return(-1);
}
return(0);
}
//================================================================
void print_usage(char* proccess)
//================================================================
//
// Show help text on usage.
//
//================================================================
{
printf(
"Usage: %s --runid=[int] (--period=[float]) (--statistic=[float]) (--maxiter=[int]) %s %s %s\n"
"* runid: the runnumber for the data file name.\n"
"* period: the periodicity of the psd measurement, in unit second. Defaults to 1.3 s.\n"
"* statistic: the statistic used for the psd. Defaults to 1e5.\n"
"* maxiter: the maximum number of psd measurements. A negative value indicates infinite looping. Defaults to -1.\n",
proccess, daq_usage_text(), dw_usage_text(), logger_usage_text()
);
printf(daq_help_text());
printf(dw_help_text());
printf(logger_help_text());
}