shahram kalantari
shahram kalantari

Reputation: 863

Using cross-correlation to detect an audio signal within another signal

I am trying to write a script in python to detect the existence of a simple alarm sound in any given input audio file. I explain my solution and I appreciate it if anyone can confirm it is a good solution. Any other solution implementable in python is appreciated.

The way I do this is calculating cross correlation of the two signals by calculating FFT of both signals (one is reversed), and multiplying them together and then calculating IFFT of the result. Then finding the peak of the result and comparing it with a pre-specified threshold would determine if the alarm sound is detected or not.

This is my code:

import scipy.fftpack as fftpack
def similarity(template, test):
    corr = fftpack.irfft(fftpack.rfft(test , 2 * test.size ) *    \
           fftpack.rfft(template[:-1] , 2 * template.size ))           

    return max(abs(corr))

template and test are the 1-D lists of signal data. The second argument to rfft is used to pad zeros for calculating FFT. however, I am not sure how many zeros should be added. Also, should I do any normalisation o the given signal before applying FFT? for example, normalizing it based on the peak of template signal?

Upvotes: 3

Views: 5591

Answers (1)

shahram kalantari
shahram kalantari

Reputation: 863

Solved! I just needed to use scipy.signal.fftconvolve which takes care of zero padding itself. No normalization was required. So the working code for me is:

   from scipy.signal import fftconvolve
   def similarity(template, test):
       corr = fftconvolve(template, test, mode='same')           

       return max(abs(corr))

Upvotes: 5

Related Questions