# Locating reference audio in longer audio

I'm trying to solve the following problem:

Let's say I have a reference sound and a recording that I know contains at least one occurrence of the reference sound. How can I determine the positions in the audio where the sound was played? It's possible that the longer recording could have noise, and I cannot assume perfect time alignment with the sound that's playing in the recording (meaning, if I knew the content of the sound played in the recording other than the reference sound, I cannot just subtract that from the recording itself).

The method I'm experimenting with is as follows:

1. Grab windows the size of the reference sound from the recorded sound
2. For each window, compute the N dominant frequencies (by finding peak magnitudes in the FFT results)
3. Assign points to the dominant frequencies of the reference sound (where the most dominant gets a score of N, and the least gets a score of 1)
4. Compare the window's dominant frequencies to these and score it

The next few parts are just experimental, and used to narrow down results.

1. Do some filtering on the matches (for instance, a match with a score lower than half the possible score gets filtered out)
2. Look at overlapping matches and compute the cross correlation with the reference sound (using every possible time shift, prefer the maximum result). Prefer the match which requires the smaller shift
3. Perform that shift on the match's endpoints

The problem is that I want to also detect if part of the reference sound was played, and step 6 would not allow that if the reference sound was played two times in quick succession where one was cut off.

It seems like there has to be a solution that's not as ad-hoc as this. I'm very new to the field of DSP, so there may be something simple that I don't know about.

Any ideas?

Thanks!

• Given this level of problem definition, you want a suggestion rather than a solution. And the most probable suggestion will be a matched filtering if you performa simple waveform based detection. If you are ok with parametric methods than more sophisticated algorithms can also be used. Commented Oct 6, 2017 at 23:18
• step 6. is the key step. do you know how to compute cross-correlation? Commented Oct 6, 2017 at 23:18
• @robertbristow-johnson Yes, I know how to compute cross-correlation. What do you have in mind? Commented Oct 9, 2017 at 23:41
• @Fat32 would you mind giving me some resources on the more sophisticated methods that you've alluded to? The matched filter (very cool btw, thanks for that!) might work for me, but I'm just interested in exploring several options. Commented Oct 9, 2017 at 23:41
• matched filter is the same as cross-correlation in this case. so @user2437378, have you tried simply cross-correlating the shorter snippet of reference audio against the recorded audio with various lags? for each lag, it's like computing a simple dot product and you get a number. try it for all possible lags and see where (at what lag) your dot-product results in a maximum value. Commented Oct 10, 2017 at 3:57