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
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.
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
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
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 (
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...