I have a long music track (2 hours) which consists of many smaller tracks (1 min each in avg). How to programmatically or using a service to determine the time position of a particular small track in a long track? Thank you

  • 2
    $\begingroup$ Depends a bit on the nature of the tracks, how similar they are and what specific properties they have and what editing (if any) was involved. In most cases you can just cross correlate the first 5 seconds of each smaller track with the longer track using a running cross correlation. The start of each short track should generate a large peak that's easy to detect $\endgroup$
    – Hilmar
    Commented Dec 28, 2022 at 21:34
  • $\begingroup$ @Hilmar thank you. The larger track might have a bit modified version of the smaller tracks due to the mixing process or adding transition effects. But for human ears they are pretty similar. Have you seen ready to use code examples in Java or any other language? $\endgroup$
    – Ruslan
    Commented Dec 28, 2022 at 21:55
  • $\begingroup$ Turns out that what matters to the human ear and what machines can easily detect are often VERY different things. I'm not aware of any off-the-shelf software to do this. $\endgroup$
    – Hilmar
    Commented Dec 28, 2022 at 23:37

1 Answer 1


This is a job for a matched filter. Matched filtering the long track with the desired short track (or a sufficiently long segment of it, as Hilmar pointed out in the comment) will give you a peak at the short track location.

Matched filtering can be implemented using cross-correlation (e.g., xcorr in MATLAB). Another way to implement cross-correlation is with convolution (e.g., conv(flip(h), y), where h is the filter (short track)).

Because you have a lot of data, I would recommend breaking your long track into shorter segments, and implementing the cross-correlation with FFTs. See this answer, with the caveat that conjugation in the frequency domain actually does not flip a discrete signal in the time domain (as explained here).

  • 1
    $\begingroup$ Gillespie, thank you 👋 Will check it next week and follow up. Happy New Year! $\endgroup$
    – Ruslan
    Commented Dec 29, 2022 at 23:20
  • $\begingroup$ Is there any guidance on setting thresholds for a peak? A few experiments show matches peaking within 10db of where non-matches peak, which is kinda close for comfort. The main obvious difference is that a match makes a pretty sinc-shaped pulse, but that's hard to programmatically check for. $\endgroup$
    – Matt DiMeo
    Commented Aug 30, 2023 at 15:24
  • $\begingroup$ You can set the threshold based on the false alarm rate you're willing to accept, and the distribution of the signal. A given SNR has an associated ROC curve trading false alarms for true detection probability. This curve depends on the underlying signal statistics. To improve SNR, you could increase the length of your matched filter. $\endgroup$
    – Gillespie
    Commented Aug 31, 2023 at 0:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.