Localizing sound in space: part 2 (algorithm)

In the previous part, we’ve been able to find a way to theoretically localize a sound in space: “the delay between the two mics gives a distance x, which is correlated to the angle the sound comes from, as x = d.sin(ß)”.OK. Now it’s time to get back to reality, and apply this. Several problems can occur:

  • the acquisition time may be too long
  • we cannot predict which microphone the sound will hit first
  • the delay between the mics may be too long… or too short
  • for some reason, one of the microphone may not record the sound

1. ADC acquisition time

We’re trying to measure (quite) precisely the delay which occur when a sound hits a microphone, then another. Since the speed of sound is ~ 343m/s @20°C, this delay can be very short. The distance between the microphone is obviously involved, but so is the analog-to-digital conversion acquisition time. We’ll name it tacq so it looks like a complicated variable, meaning we’re probably smart people.So, tacq corresponds to:

  •  the time the PIC uses to setup the ADC. With a 20MHz Xtal, that’s about 4.8µs (setup) + 10µs (delay) ~ 15µs
  • the ADC itself. We’re using low resolution ADC (8 bits) but let’s compute it for high resolution (10 bits), so we’ll get a max value. With a 20MHz Xtal, it costs 1.6µs per bit, so 10 * 1.6µs = 16µs. There’s also a need to wait a little time (Tad time, see specs) after the acquisition. It costs 1µs.
So the total is 15 + 16 + 1 = 32µs. Because we’re a little bit paranoid, we’re round this value to 40µs. So:
tacq ~ 40µs

2. Minimal distance between the microphones

In a “classic” scenario, a wave sound hits the first microphone, then the second. Neglecting the code managing this sequence, we need at least 2 * tacq = 80µs. That is,  the distance between the mics cannot be less than what gives this delay. But… in this scenario, we’re waiting the sound for hitting the first mic, but we actually don’t know what is the first, since the sound can come from anywhere. So:
  1. if a microphone detect a sound, we must check the second. The delay will give the distance, so the angle of the sound
  2. if a microphone didn’t detect any sound, we must switch the mics, and retry to step one.
From 1., this means, we’ll need 2 * tacq = 80µs. Using the speed of sound, this value corresponds to 80.10-6 * 343 = 0.02744m = 2.75cm. So the minimal distance between the microphones is:
dmin ~ 2.75cm
From 2., this means in the worst case, the witdh of sound wave must be at least:
Wmin ~ 80µs

3. Measuring the delay

Measuring the delay occuring when the sound hits the first microphone, then the second, can be done using PIC timers. The PIC 16F88 has several available timers. One interesting is timer1, which is a 16bits counter, the larger this PIC can offer. Using the internal clock as reference, the timer is incremented at Fosc/4 = 20MHz / 4 = 5MHz (every 2.10-7s = 0.2µs). Since it’s a 16bits timer, it can count 65536 * 0.2µs = 13107µs, which corresponds to ~ 4.5m.
dmax = 4.5m
2.75cm < d < 450cm

4. Borderline cases

Mostly due to the acquisition time, data and information can be loss. Particularly, beeing able to detect sound depends on the wave width:
  1. The wave’s width is large enough so acquisition can be done at least one time during the wave.
  2. The peak is too narrow, due to a too long acquisition time, we skip the information.
Note in case 1, the same information (“there is sound right now”) may be sent several times. Results may require an aggregation over a given period of time (need to define what is a peak).Assuming the width is large enough, there may also be problems while the sound hits the second microphone. Actually, the second one may even not receive a signal at all:

This case is closed the first one, and is related to the minimal width. Another case which could why the second mic didn’t receive a signal is related to the treshold. If mic1′s signal is closed to the threshold value, mic2 may not receive the signal as it could be considered as background noise:
So let’s summarize the whole:
  • the acquisition time is an important factor the get reliable results. Must use a max-speed Xtal, that is 20MHz.
  • the distance between the microphones can’t be randomly chosen. It’s between 3cm and 450cm. 10 or 15cm is good. If too long, the sound level may be to low when hitting the second mic.
  • there may be cases where information can be lost, due to the wave form (too narrow), to the fact we can’t know which mic the sound will hit first (this increase the required acquisition time since we may need to get 3 acquisition to get a result). Note, a possible optimization would say: “there’s a high probability that when a sound hits micA, the next sound may also hit the micA. So if micA is the first, keep it as the first for the next sound dectection”.
Those different points have to be tested and experienced in a “real-world” context. That’s what the next post will talk about…


Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>