electro-music.com   Dedicated to experimental electro-acoustic
and electronic music
 
    Front Page  |  Articles  |  Radio
 |  Media  |  Forum  |  Wiki  |  Links  |  Store
Forum with support of Syndicator RSS
 FAQFAQ   CalendarCalendar   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   LinksLinks
 RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in  Chat RoomChat Room 
Live streaming at radio.electro-music.com

  host / artist show at your time

poster

Please visit the chat
 Forum index » DIY Hardware and Software » ChucK programming language
xcorrelation help for a noobie
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [3 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
JesterN



Joined: May 23, 2014
Posts: 2
Location: Amsterdam, The Netherlands

PostPosted: Thu Jan 08, 2015 9:24 am    Post subject: xcorrelation help for a noobie
Subject description: xcorrelation across blocks
Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message
Blue Hell
Site Admin


Joined: Apr 03, 2004
Posts: 22224
Location: The Netherlands, Enschede
Audio files: 208
G2 patch files: 319

PostPosted: Thu Jan 08, 2015 2:33 pm    Post subject: Reply with quote  Mark this post and the followings unread

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
Back to top
View user's profile Send private message Visit poster's website
Antimon



Joined: Jan 18, 2005
Posts: 4086
Location: Sweden
Audio files: 350
G2 patch files: 100

PostPosted: Thu Jan 08, 2015 11:39 pm    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [3 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
 Forum index » DIY Hardware and Software » ChucK programming language
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum
e-m mkii

Please support our site. If you click through and buy from
our affiliate partners, we earn a small commission.


Forum with support of Syndicator RSS
Powered by phpBB © 2001, 2005 phpBB Group
Copyright © 2003 through 2009 by electro-music.com - Conditions Of Use