-
Notifications
You must be signed in to change notification settings - Fork 0
/
xcorr.cpp
82 lines (51 loc) · 1.56 KB
/
xcorr.cpp
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
#include <cmath>
#include <iostream>
#include "Buffer.h"
#include "types.h"
#include "wav.h"
//#include "extra.h"
#include <string.h> // memcpy
#include <limits.h>
#include <omp.h>
using std::cout;
using std::endl;
#include <complex>
int main(int argc, char **argv)
{
if(argc < 3)
{
puts("\nMissing program options:\n \txcorr <input> <output> [max_t(ms)] [max_corr_t(ms)]\n");
return 1;
}
SndfileHandle input_wav1(argv[1]);
SndfileHandle input_wav2(argv[1]);
uint sample_rate_Hz = input_wav1.samplerate();
real Tsampling = 1/(real)sample_rate_Hz;
Guarantee(input_wav1.samplerate() == input_wav2.samplerate(), "Sampling rates must match.");
Guarantee(wav::ok(input_wav1) && wav::ok(input_wav2), "File doesn't exist.");
Guarantee(wav::mono(input_wav1) && wav::mono(input_wav2), "Inputs must be mono.");
Buffer<real> I1(input_wav1.frames()), I2(input_wav2.frames());
input_wav1.read(I1(), input_wav1.frames());
input_wav2.read(I2(), input_wav2.frames());
long int tmax, tcorrmax, N1 = I1.size(), N2 = I2.size(), N = std::min(N1,N2);
if (argc >=4)
tmax = strtof(argv[3],NULL) / (1000.0 * Tsampling);
else
tmax = N;
if (argc >=5)
tcorrmax = strtof(argv[4],NULL) / (1000.0 * Tsampling);
else
tcorrmax = N;
if (tmax > N)
tmax = N;
if (tcorrmax > N)
tcorrmax = N;
Buffer<real> O(tmax);
for (long int t = 0; t < tmax; ++t)
{
for(long int i=0; t+i < N && i < tcorrmax; ++i)
O[t] += I1[t+i] * I2[i];
}
wav::write_mono (argv[2], O(), O.size(), sample_rate_Hz);
return 0;
}