/
powerline.c
205 lines (189 loc) · 6.23 KB
/
powerline.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
#include <fftw3.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "Monarch.hpp"
/*---Configurable Settings---*/
int fft_size=256;
//int fft_size=128;
int max_number_of_events=1024;
char format='j'; //b=binary, a=ascii, j=json
char eggname[512];
//ChIdType on_channel=1;
int on_channel=1;
/*---------------------------*/
/*---FFT buffers and such--*/
int nffts_per_event;
float *fft_input;
fftwf_plan fft_plan=NULL;
fftwf_complex *fft_output;
int fft_output_size;
double *output_powerspectrum;
int sampling_rate_mhz;
/*----------------*/
void print_usage(); //print out instructions
int handle_options(int argc,char *argv[]); //command line options
int main(int argc,char *argv[])
{
//this is C, so I have to declare all sorts of variables in advance
//update, I guess it's C++ now so this is a luxury
int i;
int j;
eggname[0]='\0';
//handle the command line options
int onindex;
if(((onindex=handle_options(argc,argv))==-1)||(argc-onindex<0))
{print_usage(); return -1;};
// char *eggname=argv[onindex];
if(eggname[0]=='\0') {
fprintf(stderr,"no input file given, use -i option\n");
return -1;
}
//open the egg
/*
struct egg current;
mBreakEgg(eggname,¤t);
mParseEggHeader(¤t);
sampling_rate_mhz=current.data->sample_rate;
*/
// Monarch *egg=Monarch::OpenForReading(eggname);
// Monarch *egg=Monarch::Open(std::string(eggname),ReadMode);
const Monarch *egg=Monarch::OpenForReading(std::string(eggname));
egg->ReadHeader();
const MonarchHeader *eggheader=egg->GetHeader();
const MonarchRecord *event;
sampling_rate_mhz=eggheader->GetAcquisitionRate();
// printf("record size: %d\n",eggheader->GetRecordSize());
//decide the optimal size for ffts and allocate memory
if(eggheader->GetRecordSize()<(unsigned int)fft_size) {
// if(current.data->record_size<fft_size) {
fprintf(stderr,"fft size larger than record size. make fft size smaller. aborting");
return -1;
}
//nffts_per_event=current.data->record_size/fft_size;
nffts_per_event=eggheader->GetRecordSize()/fft_size;
fft_output_size=fft_size/2+1;
//fft_input=fftwf_alloc_real(fft_size*nffts_per_event);
fft_input=(float*)fftwf_malloc(sizeof(float)*nffts_per_event*fft_size);
//fft_output=fftwf_alloc_complex(fft_size*nffts_per_event);
fft_output=(fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex)*fft_output_size*nffts_per_event);
output_powerspectrum=(double*)malloc(sizeof(double)*fft_output_size);
for(i=0;i<fft_output_size;i++) output_powerspectrum[i]=0;
//create the fft plan
fft_plan=fftwf_plan_many_dft_r2c(1,&fft_size,nffts_per_event,fft_input,NULL,1,fft_size,fft_output,NULL,1,fft_output_size,FFTW_ESTIMATE);
//perform ffts
//int on_event=0;
int nffts_so_far=0;
//while((mHatchNextEvent(¤t)!=1)&&(on_event<=max_number_of_events)) {
//while((event=egg->GetNextEvent())!=NULL&&(on_event<=max_number_of_events)) {
while(egg->ReadRecord()) {
if(on_channel==1)
event=egg->GetRecordSeparateOne();
else
event=egg->GetRecordSeparateTwo();
if(event==NULL) {
fprintf(stderr,"ERROR: event was null. aborting\n");
return -1;
}
//convert data to floats
//for(i=0;i<current.data->record_size;i++)
for(i=0;i<eggheader->GetRecordSize();i++)
//fft_input[i]=(float)(current.data->record[i])-128.0;
fft_input[i]=(float)(event->fData[i])-128.0;
//perform the ffts
fftwf_execute(fft_plan);
//pack in to power spectrum
int on_pt=0;
for(i=0;i<nffts_per_event;i++)
for(j=0;j<fft_output_size;j++) {
output_powerspectrum[j]+=fft_output[on_pt][0]*fft_output[on_pt][0]+fft_output[on_pt][1]*fft_output[on_pt][1];
on_pt++;
}
nffts_so_far+=nffts_per_event;
}
//normalize to power in milliwatts
// each sample * 0.5 (volts fullscale)/255 (levels) /(sqrt(fftlength)
// power *2 (positive and negative freqs) *1000 mW/W / naverages
//1000 (milliwatts/watt) * 0.5 (volts fullscale)/256 (levels) / (sqrt(fft_length)*(number of averages) / 50 ohms
double normalization=2.0*(1000.0*0.5*0.5/(256.0*256.0))*(1.0/(((double)fft_size*fft_size)*((double)nffts_so_far)))/50.0;
for(i=0;i<fft_output_size;i++)
output_powerspectrum[i]*=normalization;
//print out result
if(format=='j') { //ASCII output, JSON
printf("{ \"sampling_rate\": %d , ",sampling_rate_mhz);
printf("\"data\": [");
for(i=0;i<fft_output_size;i++) {
if(i!=0) printf(",");
printf("%g",output_powerspectrum[i]);
}
printf("] }");
} else if(format=='a') {
for(i=0;i<fft_output_size;i++)
printf("%g %g\n",sampling_rate_mhz*((double)i)/((double)(2*fft_output_size)),output_powerspectrum[i]);
} else { //binary
fwrite(output_powerspectrum,sizeof(double),fft_output_size,stdout);
}
//clean up
egg->Close();
fftwf_destroy_plan(fft_plan);
fftwf_free(fft_input);
fftwf_free(fft_output);
free(output_powerspectrum);
//mCleanUp(¤t);
return 0;
}
void print_usage()
{
printf("powerline\n");
printf("prints out a power spectrum from an egg file");
printf("Usage: powerline [options]\n");
printf(" options:\n");
printf(" -i (filename) sets the input egg file MANDATORY\n");
printf(" -a sets output to plain ASCII (default JSON)\n");
printf(" -b sets output to binary (default JSON)\n");
printf(" -f (integer) sets the number of points in the fft (default %d)\n",fft_size);
printf(" -n (integer) sets the maximum number of events to scan (default %d)\n",max_number_of_events);
printf(" -c (1 or 2) sets the channed being used (default %d)\n",on_channel);
}
int handle_options(int argc,char *argv[])
{
int c;
const char *okopts="abf:n:i:c:";
while((c=getopt(argc,argv,okopts))!=-1)
switch(c)
{
case 'a':
format='a';
break;
case 'c':
on_channel=atoi(optarg);
break;
case 'b':
format='b';
break;
case 'f':
fft_size=atoi(optarg);
if((fft_size<2)||(fft_size>1024*1024)) {
fprintf(stderr,"fft size is insane. aborting.\n");
return -1;
}
break;
case 'n':
max_number_of_events=atoi(optarg);
if(max_number_of_events<1) {
fprintf(stderr,"max number of events is insane. aborting.\n");
return -1;
}
break;
case 'i':
strcpy(eggname,optarg);
break;
case '?':
if(index(okopts,optopt)==NULL)
fprintf(stderr,"unknown option: %c\n, aborting",optopt);
else
fprintf(stderr,"option %c does not take an argument, aborting\n",optopt);
return -1;
}
return optind;
}