4 years, 6 months ago.

cross correlation

i am using sparkfun microphone to calculate my cross correlation.need some help on this making John C. Murray cross correlation.here is a link and what i have done.can help add on is a great help.thank you

https://www2.informatik.uni-hamburg.de/wtm/ps/murray_ulm04.pdf

for(j=0;j<sample;j++)
{
     float correlation[j]=0
     delay=;//not sure
for(int k=0;k<(sample-1);k++)
{
     float a=j+k;
     correlation[j]+=(leftresult[a]+rightmic[k];
}

1 Answer

4 years, 6 months ago.

If you were sure of the value of delay in your code then you wouldn't need to do the calculation, that is what you want to find. ;-)

I think you want something like this:

#define sampleWidth 1024
#define windowSize 128

float left[sampleWidth];
float right[sampleWidth];

// take a span of windowSize samples in the left data and find the point in right data that matches it best.
// returns the offset in samples between the two.
int correlate() {

  float peakCorrelation = 0;
  int peakOffset = 0;
  float correlation;
  
// the first sample in the left data such that our window will be centered in the middle.
  int firstLeftSample = sampleWidth/ 2 - windowSize/2; 

// the minimum time offset we can test for. 
  int timeOffset = -firstLeftSample ; 
 
  // loop through all possible timeOffset values
  while ( (timeOffset + firstLeftSample  + windowSize ) < sampleWidth) {
    correlation = 0;
    for (int i = 0; i<windowSize ; i++) {
        correlation += left[firstLeftSample  + i] * right[firstLeftSample + timeOffset +i];
    }
    if (correlation  > peakCorrelation) { // look for the peak
      peakCorrelation = correlation;
      peakOffset = timeOffset;
    }
    timeOffset++;
  }
  return peakOffset;
}

That should take a window of 128 samples from the middle of the left data and try find the time offset between that and the right data that gives the best match. e.g. a return value of -5 would indicate that the right channel was 5 samples ahead of the left.

Increasing that window size would give a better quality result, the bigger the window the lower the chance of a false positive, but it would also decrease the maximum time difference you could detect since you need the full window to be in both data sets. You could double the maximum time difference covered by also moving the point used in the left data rather than just using the middle, that would allow a larger window without loss of range. If you know the microphone separation and the sample rate you can work out what the maximum possible time difference and adjust the technique and window size to match that.

The alternative approach would be to calculate over all of the data points that overlap for each time offset but then you would need to control for the varying sizes of the comparison, by using a fixed window size it makes the maths a lot easier.

Warning, I just made this code up and haven't tested it or even put it into a compiler. I don't think I'm running past the ends of any arrays but you should double check for that or the results will be very random.

Accepted Answer