0

Here is the scenario:

I'm generated a signal which is: 200ms @ 2kHz 1000ms of zeros 200ms @ 2kHz

and i want to calculate the time delay between them, not between the two synthetic audio part. but by playing the signal on a speaker and recording it using a microphone (adds noise)

Fs = 44100

i tried: 1. cross correlation 2. calculation the diff between two maximas of RMS window at the size of 8820 samples. (we get the maxima when the window is on the sound part.

the distance between the speaker and the mic is around 30cm. i cant get a steady result. why?

Ran
  • 165
  • 1
  • 4

1 Answers1

1

If you want to do this accurately and consistently then one method I have used in the past is to loop back one channel (e.g. the left channel) from the output to the input and then use the other (i.e. right) channel for the timing test. You can then cross correlate between the left (loopback) and right (actual audio) channels. This eliminates many potential sources of error (buffer delays, hardware latency, software issues, etc), since the left and right channels will always be "in sync" and you should be able to make measurements accurate to +/- 1 sample period (+/- 12 µs at 44.1 kHz).

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 10x for the answer but, looping one channel in and one through audio would give me the delay between the both channels. i want to know the delay between the two audio parts. and because they are all in the same synthetic signal that i created, i can know for shore the time delay between them, no? i don't think that hardware latency is a problem here no? – Ran Jun 03 '14 at 08:23
  • OK - it wasn't clear from the question what you were trying to measure and I assumed that you were wanting to measure the time for the physical audio part (time between speaker and microphone). If you want want the end-to-end delay, including all the software and hardware factors in your computer system and sound card, then life gets a lot trickier, and it may be subject to significant amounts of jitter, depending on what OS you are using, and the aforementioned other factors. – Paul R Jun 03 '14 at 09:17
  • why am i subjected to all the above? i have 1 signal that i created that include sound silence and sound. if i play it at 44100 i would expect it to be has i planed no? then i recored all together, and only off line i try to solve it. – Ran Jun 03 '14 at 09:20
  • If you think about it there are 3 delays in your loop: (i) software to speaker, (ii) speaker to microphone and (iii) microphone to software. (i) and (iii) are both subject to delays due to (multiple levels of) buffering, task scheduling, interrupt latency, hardware latency and various other issues. These delays may also be subject to jitter, depending on how and when you initiate playback and recording. FWIW, audio conferencing software has to deal with these unpredictable latencies when performing echo cancellation, and it's a non-trivial problem. – Paul R Jun 03 '14 at 09:25
  • speaker to microphone is a steady delay, the time it takes the sound to travel is the same for both sound parts, so it's not an issue. The first and third delay are happening again for both sound parts. we need to remember that it is one long signal, i would expect that the gap of 1 sec, will remain 1 sec. i'm not waiting 1 sec in the code. but letrely just playing quiet for 1 sec between both sound parts. – Ran Jun 03 '14 at 09:32
  • also when i used a headset for a headphones and a mic set i got a perfect match, but when i changed the headphone into a speaker (that is not part of the headset) it started getting crazy – Ran Jun 03 '14 at 09:36
  • It sounds like you have more problems than just the above-mentioned delays - maybe your delay estimation method is not very robust in the presence of background noise? You should probably try to eliminate one problem at a time. – Paul R Jun 03 '14 at 09:42
  • Is my assumption that if i play a single signal with sound pause sound. and i record it with a mic, arent the pause will remain the same length. i'm claiming that non of the above problem exists. (again thank you so much for the answers) – Ran Jun 03 '14 at 09:45
  • Yes, the pause between the two sound bursts will be the same, of course, assuming it's all contained within in a single playback buffer. But surely that is not what you are trying to measure ? Maybe you need to explain what is is exactly that you are trying to *achieve*, otherwise we may be trapped in an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Paul R Jun 03 '14 at 09:48
  • this is step 1, this is what i'm trying to achieve :) i know it's basic that's the annoying part. in simulation its works, it fails when i use a real speaker and a mic – Ran Jun 03 '14 at 09:51
  • OK - so that sounds like your delay estimation method is not robust in the presence of noise. If you want to ask a question about the method then I suggest you ask a new question on http://dsp.stackoverflow.com, as this seems to be a DSP theory question now, rather than a MATLAB programming problem. – Paul R Jun 03 '14 at 10:03