TransWikia.com

How to take convolution of two arrays in Python by using NumPy?

Computational Science Asked on June 27, 2021

Generally, we know that if we have this relation between Fourier transforms of three functions in frequency domain as:

$$mathfrak{F}{mathsf{P}(t)} = mathfrak{F}{mathsf{Z}(t)}mathfrak{F}{mathsf{Q}(t)}$$

We should have this relationship in the time domain as:

$$mathsf{P}(t) = int mathsf{Z}(tau) mathsf{Q}(t-tau) dtau$$

Now I have this in Python:

import numpy as np

time = np.genfromtxt('time-data.txt',delimiter=',').T

fft = np.genfromtxt('fft-data.txt',delimiter=',',dtype=np.complex_).T

Z_t = time[0]
Q_t = time[1]

Z_fft = fft[0]
Q_fft = fft[1]
P_fft = fft[2]

#Shows that indeed Z_fft is the Fourier transform of Z_t
assert (np.fft.irfft(Z_fft,n=len(Q_t)) == Z_t).all()

#Shows that indeed Q_fft is the Fourier transform of Q_t
assert (np.fft.irfft(Q_fft,n=len(Q_t)) == Q_t).all()

#Shows that indeed Z_fft = P_fft / Q_fft
assert (np.divide(P_fft,Q_fft) == Z_fft).all()

P_t = np.fft.irfft(P_fft,n=len(Q_t))

P_t_from_convolve = np.convolve(Z_t,Q_t,mode='same')

#Expect to see that P_t = P_t_convolve but it's not the case!
assert (P_t == P_t_from_convolve).all()

Is there any reason that $int mathsf{Z}(tau) mathsf{Q}(t-tau) dtau neq mathfrak{F^{-1}}{mathfrak{F} { mathsf{P}(t) } }$?

These are the data files here:

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP