Author |
Message |
JesterN
Joined: May 23, 2014 Posts: 2 Location: Amsterdam, The Netherlands
|
Posted: Thu Jan 08, 2015 9:24 am Post subject:
xcorrelation help for a noobie Subject description: xcorrelation across blocks |
 |
|
Hi there I just started using Chuck because I need some sample-accurate onset difference detector between channel left and right. I'm trying to identify the distance of an object on a line with two mikes put on the extremes. I want to use cross correlation and see the max in the function to detect delay of percussive events in the two mikes. I'm kind of clumpsy, as I'm learning, and don't know how to calculate cross correlation taking care of the block size. Anyone can help? |
|
Back to top
|
|
 |
blue hell
Site Admin

Joined: Apr 03, 2004 Posts: 24422 Location: The Netherlands, Enschede
Audio files: 297
G2 patch files: 320
|
Posted: Thu Jan 08, 2015 2:33 pm Post subject:
|
 |
|
I'm no expert by any means .. just some tinkering along ...
Basically the brute force method for real valued (non-complex) arrays is:
Having two arrays with samples and then for each sample of the first multiply it with all samples of the second and add those values to obtain each sample of the output. For real valued data it is the same as convolution, except time runs in the opposite direction.
Then find the maximum in the output array and the cell it is in gives you the sample delay between the signals (or keep a running max during the computation).
You can easily test your algorithm by setting known and equal values into the arrays and time shifting one of them by a known amount of samples.
This can be sped up by using FFT, but that is a bit more complicated, and also need to pad the data then to avoid circular calculations.
Some practical stuff .. 300 m/s as the speed of sound, a sample rate of 96 ks/s meaning you have about 300 m of space in 96ks, or about 3 mm for one sample, hmm not as bad as I had expected.
Then say the mics are 3 m apart which maps to about 10 ms of sound or 96 samples. Ok .. brute force will do fine then I guess, you'll probably have some more samples but that's fine, hundreds is no problem I'd think.
You'd start your correlation at about -48 and end it at about 48 samples difference, non existing data can be assumed to be zero. A peak in the middle would then mean that the sound source was midway between the mics, and then about 3mm off for each sample the peak shifts.
These are rough estimates of course. _________________ Jan
also .. could someone please turn down the thermostat a bit.
 |
|
Back to top
|
|
 |
Antimon
Joined: Jan 18, 2005 Posts: 4145 Location: Sweden
Audio files: 371
G2 patch files: 100
|
Posted: Thu Jan 08, 2015 11:39 pm Post subject:
|
 |
|
Without knowing anything the theory about xcorrelation and stuff, here's some untested, pseudo-ish code that just updates the maximum difference between left and right on standard input - dunno if this helps or if you had got this down already:
Code: | adc.chan(0) => Gain channel0 => blackhole;
adc.chan(1) => Gain channel1 => blackhole;
0 => int maxDifference;
while (true) {
Math.abs(channel0.next() - channel1.next()) => int difference;
if (difference > maxDifference) {
difference => maxDifference;
}
1::samp => now;
}
|
_________________ Antimon's Window
@soundcloud @Flattr home - you can't explain music |
|
Back to top
|
|
 |
|