Last active
May 27, 2019 07:52
-
-
Save fengye/d69ad0aaeec913b517bb29c7f8eb1929 to your computer and use it in GitHub Desktop.
Using cross correlation to find similarity in two audios. Useful in echo cancellation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import audio_io | |
import numpy | |
audio1, _ = audio_io.ReadWavFile('voice_nodelay.wav') | |
audio1 = audio1.ravel() | |
audio2, _ = audio_io.ReadWavFile('voice_mic_blend3.wav') | |
audio2 = audio2.ravel() | |
# Truncate all signals same length, then pad to avoid boundary effects. | |
n = min( | |
audio1.shape[0], | |
audio2.shape[0]) | |
audio1_padded = numpy.zeros((2 * n, )) | |
audio2_padded = numpy.zeros((2 * n, )) | |
audio1_padded[:n] = audio1[:n] | |
audio2_padded[:n] = audio2[:n] | |
# Use cross-correlation to estimate the impulse response of the room | |
# and speakers. | |
print( 'FFT(A)') | |
A = numpy.fft.fft(audio1_padded) | |
print( 'FFT(B)') | |
B = numpy.fft.fft(audio2_padded) | |
print( 'conj(A)') | |
Bconj = numpy.conj(B) | |
print( 'IFFT(A*B^)') | |
xcorr = numpy.fft.ifft( numpy.multiply(A, Bconj) ) | |
audio_io.WriteWavFile(xcorr[:2*n], 44100, 'xcorr2.wav') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment