I am working on a final project called sound source localization using Arduino Uno. I want to use two sound sensor to capture a sound. The nature of the sound is clap or speech sound. I want to use the cross correlation method to estimate the time delay between the microphones then find the position and angle. My supervisor said to me you have to store the values on analogRead in two arrays then cross correlation the element of two arrays. But i am struggle the cross correlation part. I will appreciate any help from you guys.

#define LED_NORTH 2
#define LED_SOUTH 4

#define MIC_NORTH A0
#define MIC_SOUTH A2

/* let's said we want to read 100 samples */ 
const unsigned int numReadings = 100;
unsigned int analogVals[numReadings];
unsigned int i = 0;

void setup() {
  digitalWrite(LED_NORTH, LOW);
  digitalWrite(LED_SOUTH, LOW);

void loop() {
  int north = analogRead(MIC_NORTH); 
  int south = analogRead(MIC_SOUTH); 
  /* take numReadings # of readings and store into array */
  for(unsigned int i = 0; i < numReadings; i++)
    analogVals[i] = analogRead(A0);
  • 1
    What have you tried so far? Show us your code. What is its expected behavior? What is the actual behavior? Jun 19, 2019 at 14:25
  • 1
    The code you posted is incomplete. It cannot be compiled nor tested. Jun 19, 2019 at 14:59
  • 1
    İ change a little bit the code.now i assume i can compile
    – yaya
    Jun 19, 2019 at 15:36
  • 1
    Don't assume. Try. Jun 19, 2019 at 16:01
  • 2
    Once you know how cross correlation works (en.m.wikipedia.org/wiki/Cross-correlation), the answer in this question might help you: arduino.stackexchange.com/q/11754/38721
    – Sim Son
    Jun 19, 2019 at 16:45

1 Answer 1


I am not going to directly answer the question, as I think it is ill-posed. Before posting a question here, you are supposed to have done your research, and come with very specific problems you cannot solve by your own. I may be mistaken, but it looks to me like you are at a very preliminary stage where you barely started to think about the project.

So, instead of trying to answer an overly broad question, I will try to provide some guidance by asking questions back to you. Below is a list of some problems and challenges you will have to consider for advancing your project. It may not be complete: it's just what I can think off the top of my head. This will hopefully help you tackle the right problems.

Non-stereo sampling

Your Arduino is incapable of stereo sampling. Since it has only one ADC, it can only sample one channel at a time. This means you will have to alternatingly sample the left channel, then the right channel, then the left channel... Since the left and right samples are not taken at the same time, this will have an implication on how you interpret the results from the cross-correlation. Did you think about how you will handle this? Is this acceptable? Will you have to instead use external ADC chips in order to have real stereo sampling?

Slow sampling rate

If you analogRead() in a tight loop, you are taking at most one sample every 112 µs. And since you are alternating between the channels, on each channel you get one sample every 224 µs. This is a sampling rate of 4.46 kHz, and a Nyquist frequency of 2.23 kHz. Is this fast enough for your application?

In 224 µs, the sound travels about 76 mm, which is thus the spatial resolution of your cross-correlation. How does this translate into angular resolution? Is this resolution satisfactory? Do you plan to use interpolation methods to try to improve it?

If the time resolution is not good enough, you can speed up the ADC by a factor 2 or 4 at the cost of lowered accuracy. You will have to dig on the MCU datasheet (or the Internet...) in order to learn how to do that.

Inconsistent sampling rate

When you use analogRead(), the time between consecutive samples can depend on the code you run between the consecutive calls to analogRead(). It can also depend on how your code gets interrupted by, say, the timer interrupt, or the UART interrupt if you are sending data out. An inconsistent sampling rate is something you want to avoid when doing cross-correlations.

You could try to mitigate this problem by running the acquisition with interrupts disabled. However, the only way to guarantee periodic sampling is by configuring the ADC to be triggered automatically, either by a timer or by itself (free running mode). Walking this path will require you to forego the comfort of the Arduino library (analogRead() is just so easy to use...), and carefully study the datasheet, which would be a very significant time investment.

Do you have the time and energy for this? If you end up learning all this low-level details on the configuration of the ADC, will this learning be relevant to the course for which this is a project?

Finite data arrays

Mathematically, the cross-correlation is typically defined for infinite series of samples. In your experiment, you will have to deal with finite arrays, and this makes a subtle difference. Did you think about how you will deal with the finiteness of the arrays? Are you going to zero-pad? Remove the average before zero-padding? Apply a windowing function?

I hope it is clear by now that the “cross correlation part” is only one of the many issues you will have to face. I suggest you discuss all these questions with your supervisor. He might be able to advise you on how to tackle them, whether it's worth it, where you may cut corners...

Not the answer you're looking for? Browse other questions tagged or ask your own question.