# 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

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…