forked from xhacks/sndfile2fft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sndfile2fft.c
414 lines (328 loc) · 9.84 KB
/
sndfile2fft.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <sndfile.h>
#include "fftw3.h"
char *progname, *infilename, *outfilename ;
// TODO run with larges and small window?
#define MAX_BLOCK_SIZE 102400
#define MAX_CHANS 10
#define BLOCK_SIZE (1024*8)
//#define BLOCK_SIZE 128
double g_div = 0;
static double getwindowcoef(int i)
{
double mult = 1;
double PI = 3.1415926535;
#if 1
// Blackman-harris
double a0 = 0.35875;
double a1 = 0.48829;
double a2 = 0.14128;
double a3 = 0.01168;
mult = a0 - a1*cos(2*PI*i/(BLOCK_SIZE-1)) + a2 * cos(4*PI*i/(BLOCK_SIZE-1)) - a3*cos(6*PI*i/(BLOCK_SIZE-1));
#endif
#if 0
//Hann
double x = sin(PI * i / (BLOCK_SIZE-1));
double hann = x*x;
mult = hann;
#endif
#if 0
// Blackman
double a0 = 7938.0/18608.0;
double a1 = 9240.0/18608.0;
double a2 = 1430.0/18608.0;
double black = a0 -(a1*cos(2*PI*i/(BLOCK_SIZE-1))) + (a2*cos(4*PI*i)/(BLOCK_SIZE-1));
mult = black;
#endif
#if 0
double hamming = 0.54 -0.46*cos(2*PI*i/(BLOCK_SIZE-1));
mult = hamming;
#endif
return mult;
}
fftw_complex out[MAX_BLOCK_SIZE];
double input[MAX_BLOCK_SIZE];
double mag[MAX_BLOCK_SIZE];
double peakhold[MAX_CHANS][MAX_BLOCK_SIZE];
double otherEnergy[MAX_CHANS];
double peakEnergy[MAX_CHANS];
int g_peakIndex[MAX_CHANS];
int g_channels = 0;
int g_samplerate = 0;
int g_expectedFreq = 0;
int g_blockCount = 0;
// N set to sample rate for 1 second blocks
static void
print_usage (char *progname)
{ fprintf (stderr, "\nUsage : %s <input file> <output file >\n", progname) ;
puts ("\n"
" Where the output file will contain a line for each freq bin\n"
" and a column for each channel.\n"
) ;
} /* print_usage */
int properfft(int nreal, int channel)
{
fftw_plan p;
int i;
double max = 0;
double max2 = 0;
int maxIndex = 0;
g_blockCount++;
p = fftw_plan_dft_r2c_1d(BLOCK_SIZE, input, out, FFTW_ESTIMATE);
fftw_execute(p);
fftw_destroy_plan(p);
for(i=0;i<nreal;i++)
{
mag[i] = hypot(out[i][0], out[i][1]);
if (mag[i] > max2 && i != 0)
{
max2 = mag[i];
}
}
for(i=0;i<nreal;i++)
{
mag[i] = 20*log10(mag[i]/max2+1e-10);
if(mag[i] > max)
{
max = mag[i];
maxIndex = i;
}
if(mag[i]>peakhold[channel][i])
{
peakhold[channel][i] = mag[i];
}
}
//fprintf(stderr, "i: %d\n", maxIndex);
return maxIndex;
}
static int doBlock(float buf[], int readcount, int lastIndex)
{
int i = 0;
double mult = 0;
// int m = 0;
double val;
int index;
for(int c = 0; c < g_channels; c++)
{
i = 0;
for (int k = 0 ; k < readcount ; k++)
{
mult = getwindowcoef(i);
val = buf[k*g_channels+c];
input[i] = val*mult;// /(float)((unsigned)(1<<31)); // real
i++;
if (i >= BLOCK_SIZE) break;
} ;
/* Do the fft over the block */
g_peakIndex[c] = properfft(i, c);
#if 0
if(lastIndex != -1)
{
if(lastIndex!=index)
{
//fprintf(stderr, "PEAK MOVED\n");
//exit(1);
}
}
#endif
}
/* Return index of peak */
return index;
}
static void convert_to_fft(SNDFILE * infile, FILE * outfile, int channels)
{
float buf1 [channels * BLOCK_SIZE] ;
float buf2 [channels * BLOCK_SIZE] ;
float block [channels * BLOCK_SIZE] ;
int k, m, readcount ;
double edge, val, time = 0;
int index = -1;
int seconds = 0;
int firstRun = 1;
double mult;
// Init peak hold
for(int i = 0; i < BLOCK_SIZE; i++)
for(int j = 0; j < g_channels; j++)
{
peakhold[j][i] = -100000;
otherEnergy[j] = 0.0;
peakEnergy[j] = 0.0;
}
/* Ignore first few blocks - hack to avoid 0's in start of wav */
while(1)
{
if((readcount = sf_readf_float (infile, buf1, 1)) <= 0)
{
exit(1);
}
if(buf1[0] != 0.0)
break;
}
if((readcount = sf_readf_float (infile, buf1, BLOCK_SIZE)) <= 0)
exit(1);
while (1)
{
readcount = sf_readf_float (infile, buf2, BLOCK_SIZE);
if(readcount < 0)
{
break;
}
else if(readcount != BLOCK_SIZE)
{
fprintf(stderr, "Info: Dropped last block\n");
break;
}
for(int i = 0; i < (BLOCK_SIZE); i++)
{
block[i] = buf1[i+(BLOCK_SIZE)];
}
for(int i = 0; i < (BLOCK_SIZE); i++)
{
block[i+(BLOCK_SIZE)] = buf2[i];
}
index = doBlock(block, readcount, index);
index = doBlock(buf2, readcount, index);
readcount = sf_readf_float(infile, buf1, BLOCK_SIZE);
if(readcount < 0)
{
break;
}
else if(readcount != BLOCK_SIZE)
{
fprintf(stderr, "Info: Dropped last block\n");
break;
}
for(int i = 0; i < (BLOCK_SIZE); i++)
{
block[i] = buf2[i+(BLOCK_SIZE)];
}
for(int i = 0; i < (BLOCK_SIZE); i++)
{
block[i+(BLOCK_SIZE)] = buf1[i];
}
index = doBlock(block, readcount, index);
index = doBlock(buf1, readcount, index);
} ;
/* Print out the output */
for(int i = 0; i < BLOCK_SIZE; i++)
{
fprintf(outfile, "%10d ", (int)(i*g_div));
for(int c = 0; c<g_channels; c++)
{
fprintf(outfile, "%10g ", peakhold[c][i]);
}
fprintf(outfile, "\n");
}
#define PEAK_RANGE 5
for(int c = 0; c < g_channels; c++)
{
int othercount = 0;
int peakIndex = g_peakIndex[c];
for(int i = (-PEAK_RANGE); i< PEAK_RANGE;i++)
{
peakEnergy[c] += peakhold[c][g_peakIndex[c]+i];
}
for(int i = 0; i < BLOCK_SIZE; i++)
{
if((i < (peakIndex-PEAK_RANGE)) || (i > (peakIndex+PEAK_RANGE)))
{
if(peakhold[c][i] > -200)
{
otherEnergy[c] -= peakhold[c][i];
othercount++;
}
}
}
peakEnergy[c] /= (PEAK_RANGE*2+1);
otherEnergy[c] /= (othercount-(PEAK_RANGE*2+1));
otherEnergy[c] *= -1;
/* Convert to freq */
g_peakIndex[c] = (int)((double)g_peakIndex[c]*g_div);
fprintf(stderr, "Channel %d : Peak found at approx %d Hz, Peak avg: %10g, Other Freqs: %10g\n", c, g_peakIndex[c], (peakEnergy[c]), otherEnergy[c]);
if(otherEnergy[c] > -85)
{
fprintf(stderr, "Suspect glitch on this channel!\n\n");
}
else
{
fprintf(stderr, "Looks okay - manual inspect to be sure!\n\n");
}
}
return ;
} /* convert_to_fft */
int
main (int argc, char * argv [])
{
SNDFILE *infile = NULL ;
FILE *outfile = NULL ;
SF_INFO sfinfo ;
int plot = 0;
progname = strrchr (argv [0], '/') ;
progname = progname ? progname + 1 : argv [0] ;
if (argc != 3 && argc != 4)
{
print_usage (progname) ;
return 1 ;
} ;
if(argc == 4)
{
if (strcmp(argv[3], "-p") == 0) /* Process optional arguments. */
{
plot = 1; /* This is used as a boolean value. */
}
else
{
print_usage (progname) ;
return 1 ;
}
}
infilename = argv [1] ;
outfilename = argv [2] ;
/* Some basic checking of command line.. */
if (strcmp (infilename, outfilename) == 0)
{
printf ("Error : Input and output filenames are the same.\n\n") ;
print_usage (progname) ;
return 1 ;
}
if ((infile = sf_open (infilename, SFM_READ, &sfinfo)) == NULL)
{
printf ("Not able to open input file %s.\n", infilename) ;
puts (sf_strerror (NULL)) ;
return 1 ;
}
/* Open the output file. */
if ((outfile = fopen (outfilename, "w")) == NULL)
{
printf ("Not able to open output file %s : %s\n", outfilename, sf_strerror (NULL)) ;
return 1 ;
}
fprintf (stdout, "Running from input file %s.\n", infilename) ;
fprintf (stdout, "Channels %d, Sample rate %d\n", sfinfo.channels, sfinfo.samplerate) ;
g_samplerate = sfinfo.samplerate;
g_channels = sfinfo.channels;
g_div = (double)g_samplerate/(double)(BLOCK_SIZE);
convert_to_fft(infile, outfile, sfinfo.channels) ;
if(plot)
{
FILE *pipe = popen("gnuplot ","w");
fprintf(pipe, "set xtic auto; set ytic auto; set title 'Spectrum (%s)'; set ylabel 'Relative Amp (dB)'; \
set xlabel 'Freq (Hz)'; set xr[0:20000]; set yr [-140:1];",infilename );
fprintf(pipe, "set arrow from %d,-140 to %d,-120 lc rgb 'red';", g_peakIndex[0], g_peakIndex[0]);
fprintf(pipe, "set label '%d Hz' at %d,-138;", g_peakIndex[0], g_peakIndex[0]);
fprintf(pipe, "set arrow from %d,-140 to %d,-120 lc rgb 'navy';", g_peakIndex[1], g_peakIndex[1]);
fprintf(pipe, "set label '%d Hz' at %d,-138;", g_peakIndex[1], g_peakIndex[1]);
fprintf(pipe, "set arrow from 1,%d to 20000, %d lc rgb 'red' nohead ;", (int)otherEnergy[0], (int)otherEnergy[0]);
fprintf(pipe, "set arrow from 1,%d to 20000, %d lc rgb 'navy' nohead ;", (int)otherEnergy[1], (int)otherEnergy[1]);
fprintf(pipe, "plot '%s' using 1:2 title 'Left (peak hold)' with lines lc rgb 'red', \
'%s' using 1:3 title 'Right (peak hold)' with lines lc rgb 'navy'", outfilename, outfilename);
pclose(pipe);
}
sf_close (infile) ;
fclose (outfile) ;
return 0 ;
} /* main */