how can i apply convolution to a stereo audio signal?

Viewed 324

here's my code, but i don't know how to apply convolution to a stereo audio signal, i could only apply it to one channel instead of both, so i want to know if is possible to apply convolution between an array 1d to an aray 2d (stereo audio signal)

import  numpy as np
import soundfile as sf
import  sounddevice as sd

delay=22050

x, FS = sf.read('acdc-stereo.wav', dtype='float')
print(x)
zeros = np.zeros(delay-1,dtype='float')
h = np.concatenate(([1],zeros,[0.3]))

print('Processing..')
y = np.convolve(x[:,0],h, mode="full")
newaudio=(y,FS)
print(y)
print('Playing original signal')
sd.play(x,FS)
sd.wait()
print("playing output signal")
sd.play(newaudio)
sd.wait()
2 Answers

The numpy array is a frames x channel array. And in this line:

y = np.convolve(x[:,0],h, mode="full")

You did convolution only to channel 0. You should apply it to other channels as well if you mean to. I think for most realistic cases, convolution should be applied separately to different channels:

y = np.array([np.convolve(x[:,0], h, mode="full"),
              np.convolve(x[:,1], h, mode="full")]).T

Maybe more elegant:

stereoOutput = np.array(
    [ np.convolve(monoFoo, ch) for ch in stereoBar ]
)

Only works if you're using 2 cols for your stereo signal (instead of 2 rows).

Related