3
$\begingroup$

I want to calculate delay between an input and an output audio signal of my audio processing system. The input and output signals are available as signed 16 bit integers. To try out, I tried the autocorrelation of the input signal with the following numpy commands:

import numpy as np
import wave
wfp = wave.open('test.wav', 'rb')
samples = wfp.readframes(wfp.getnframes())
signal = np.frombuffer(samples, np.int16)
corr = np.correlate(signal, signal, "full")

I assumed that the peak of the autocorrelation is always at lag 0, which is at the index corr.size / 2 in the corr array. However I get different values for the index, when I calculate np.argmax(corr).

When I normalize the signal first to get values between -1.0 and +1.0, the peak is always at corr.size / 2 as expected, at least in my tests with different signals.

For normalization, I used the following steps:

signal = signal / float(0xFFFF)

Can someone please explain to me, why I have to normalize the signal.

$\endgroup$
  • 1
    $\begingroup$ If you want to measure time delay, shouldn't you be looking at two signals? Also, you aren't required to normalize; you indicated that you see similar results with and without normalization. $\endgroup$ – Jason R Dec 17 '13 at 16:59
  • 1
    $\begingroup$ Are you having an integer overflow problem? $\endgroup$ – Aaron Dec 17 '13 at 17:03
  • $\begingroup$ Thanks for the suggestion of integer overflow. This is plausible. I am not used to care for integer overflow in python, but here I specified int16 as dtype of the array. $\endgroup$ – Emil Dec 20 '13 at 20:42
  • $\begingroup$ Your signal becomes float dtype after normalizing, right? So it won't have integer overflow if you do that before the correlation. Incidentally scipy.signal.fftconvolve(a, b[::-1]) is equivalent to numpy.correlate(a, b), but much faster. $\endgroup$ – endolith Jun 6 '14 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.