# Initialize two signals with arbitrary length n, m = 5, 7 convolution_length = n+m-1 u = np.random.randint(0, 255, n) v = np.random.randint(0, 255, m) # Convolve the two signals cv = sp.signal.convolve(u, v) assert cv.shape[0] == convolution_length # Now, apply the Convolution Theorem. ## Copy and pad signal. Padding is necessary to match the convolution length. vv = v.copy() vv.resize(n+m-1, refcheck=False) uu = u.copy() uu.resize(n+m-1, refcheck=False) # Compute FFT fu = fft(uu) fv = fft(vv) # Inverse FFT of frequency signal fcv = ifft(fu * fv) # Does the inverse transform of the product of the Fourier transforms of the signals equal the convolution of the two signals? assert np.allclose(fcv, cv)