In [1]:
print arange(5)
In [2]:
spectral_size = 50
size = 1*spectral_size
In [3]:
f = []
g = []
for i in arange(size):
f.append(math.sin(2*pi*i/size))
g.append(math.cos(2*pi*i/size))
for j in arange(spectral_size):
f[len(f)-1] += random.random()*math.sin(2*pi*j*i/size)
g[len(g)-1] += random.random()*math.cos(2*pi*j*i/size)
We want to transform these functions to space, multiply them together and then transform them back
In [4]:
print f
In [5]:
pyplot.plot(arange(size)*1.0/size, f)
pyplot.plot(arange(size)*1.0/size, g)
pyplot.show()
In [6]:
npf = np.array(f)
In [7]:
print npf
In [9]:
npf_fft = numpy.fft.fft(npf)
In [10]:
print npf_fft
In [73]:
pyplot.plot(arange(size)*1.0/size, imag(npf_fft), 'b')
pyplot.plot(arange(size)*1.0/size, real(npf_fft), 'r')
pyplot.plot(arange(size)*1.0/size, abs(npf_fft), 'k')
pyplot.show()
In [75]:
npf_fft_ifft = numpy.fft.ifft(npf_fft)
In [77]:
pyplot.plot(arange(size)*1.0/size, npf, 'b')
pyplot.plot(arange(size)*1.0/size, real(npf_fft_ifft), 'r')
pyplot.show()
In [78]:
print npf-npf_fft_ifft
In [1]:
spectral_n = 10
spectral_size = spectral_n + 1
spatial_size = 2*spectral_size + 1
In [106]:
a = zeros(spectral_size)
b = zeros(spectral_size)
print a
In [107]:
a[0] = 1.0
a[1] = 2.0
a[6] = 1.0
b[1] = 0.5
b[2] = 3.0
print a, b
In [4]:
a_spatial = []
b_spatial = []
for i in arange(spatial_size):
a_spatial.append(0.0)
b_spatial.append(0.0)
for n in arange(spectral_size):
a_spatial[i] += a[n]*math.cos(2*pi*n*i/(spatial_size))
b_spatial[i] += b[n]*math.sin(2*pi*n*i/(spatial_size))
In [5]:
pyplot.plot(arange(spatial_size), a_spatial, 'b')
pyplot.plot(arange(spatial_size), b_spatial, 'r')
pyplot.show()
In [6]:
a_ifft = numpy.fft.ifft(np.array(a))
b_ifft = numpy.fft.ifft(np.array(b))
In [7]:
a_spatial_undersampled = []
b_spatial_undersampled = []
for i in arange(spectral_size):
a_spatial_undersampled.append(0.0)
b_spatial_undersampled.append(0.0)
for n in arange(spectral_size):
a_spatial_undersampled[i] += a[n]*math.cos(2*pi*n*i/(spectral_size))
b_spatial_undersampled[i] += b[n]*math.sin(2*pi*n*i/(spectral_size))
In [9]:
pyplot.plot(arange(spectral_size)*1.0/(spectral_size), (spectral_size)*real(a_ifft), 'b')
pyplot.plot(arange(spectral_size)*1.0/(spectral_size), (spectral_size)*imag(a_ifft), 'r')
pyplot.plot(arange(spectral_size)*1.0/(spectral_size), a_spatial_undersampled, 'k')
pyplot.show()
In [9]:
pyplot.plot(arange(spectral_size)*1.0/(spectral_size), (spectral_size)*real(b_ifft), 'b')
pyplot.plot(arange(spectral_size)*1.0/(spectral_size), (spectral_size)*imag(b_ifft), 'r')
pyplot.plot(arange(spectral_size)*1.0/(spectral_size), b_spatial_undersampled, 'k')
pyplot.show()
In [6]:
a_ifft_fft = numpy.fft.fft(a_ifft)
b_ifft_fft = numpy.fft.fft(b_ifft)
In [7]:
pyplot.plot(arange(spectral_size), real(a_ifft_fft), 'b')
pyplot.plot(arange(spectral_size), imag(a_ifft_fft), 'r')
pyplot.plot(arange(spectral_size), a, 'k')
pyplot.show()
In [8]:
pyplot.plot(arange(spectral_size), real(b_ifft_fft), 'b')
pyplot.plot(arange(spectral_size), imag(b_ifft_fft), 'r')
pyplot.plot(arange(spectral_size), b, 'k')
pyplot.show()
In [13]:
a_spatial_undersampled_inject = []
b_spatial_undersampled_inject = []
for i in arange(spectral_size):
a_spatial_undersampled_inject.append(0.0)
b_spatial_undersampled_inject.append(0.0)
for n in arange(spectral_size):
a_spatial_undersampled_inject[i] += a[n]*math.cos(2*pi*n*i/spectral_size)
b_spatial_undersampled_inject[i] += b[n]*math.sin(2*pi*n*i/spectral_size)
a_spatial_undersampled_inject[i] += 1.0*math.cos(2*pi*(spectral_size)*i/spectral_size)
b_spatial_undersampled_inject[i] += 1.0*math.sin(2*pi*(spectral_size)*i/spectral_size)
In [14]:
pyplot.plot(arange(spectral_size)*1.0/spectral_size, spectral_size*real(a_ifft), 'b')
pyplot.plot(arange(spectral_size)*1.0/spectral_size, spectral_size*imag(a_ifft), 'r')
pyplot.plot(arange(spectral_size)*1.0/spectral_size, a_spatial_undersampled_inject, 'k')
pyplot.show()
In [17]:
product_size = len(a_spatial)
ab_spatial = zeros(product_size)
for i in arange(len(a_spatial)):
ab_spatial[i] = a_spatial[i]*b_spatial[i]
pyplot.plot(arange(spatial_size), a_spatial, 'b')
pyplot.plot(arange(spatial_size), b_spatial, 'r')
pyplot.plot(arange(product_size), ab_spatial, 'k')
pyplot.show()
In [10]:
ab_spatial_fft = numpy.fft.fft(np.array(ab_spatial))
In [11]:
pyplot.plot(arange(product_size), imag(ab_spatial_fft)/product_size, 'b')
pyplot.plot(arange(product_size), real(ab_spatial_fft)/product_size, 'r')
pyplot.show()
In [108]:
#This if for the cos*sin products
product_spectral_full = zeros(product_size)
product_spectral_full[1] = (a[0]*b[1] + 0.5*a[1]*b[2])
product_spectral_full[2] = (a[0]*b[2] + 0.5*a[1]*b[1])
product_spectral_full[3] = 0.5*a[1]*b[2]
product_spectral_full[4] = -0.5*a[6]*b[2]
product_spectral_full[5] = -0.5*a[6]*b[1]
product_spectral_full[7] = 0.5*a[6]*b[1]
product_spectral_full[8] = 0.5*a[6]*b[2]
In [14]:
print spectral_size, product_size
pyplot.plot(arange(product_size), -2*imag(ab_spatial_fft)/(product_size), 'b')
pyplot.plot(arange(product_size), real(ab_spatial_fft)/(product_size), 'r')
pyplot.plot(arange(product_size), product_spectral_full, 'k')
pyplot.show()
In [15]:
a_spatial_fft = numpy.fft.fft(np.array(a_spatial))
b_spatial_fft = numpy.fft.fft(np.array(b_spatial))
In [27]:
pyplot.plot(arange(spatial_size), imag(a_spatial_fft), 'b')
pyplot.plot(arange(spatial_size), real(b_spatial_fft), 'r')
pyplot.plot(arange(spatial_size), zeros(spatial_size), 'k')
pyplot.show()
In [28]:
a_spatial = []
b_spatial = []
for i in arange(spatial_size):
a_spatial.append(0.0)
b_spatial.append(0.0)
for n in arange(spectral_size):
a_spatial[i] += a[n]*math.cos(2*pi*n*i/(spatial_size))
b_spatial[i] += b[n]*math.sin(2*pi*n*i/(spatial_size))
a_spatial_fft = numpy.fft.fft(np.array(a_spatial))
b_spatial_fft = numpy.fft.fft(np.array(b_spatial))
pyplot.plot(arange(spatial_size), real(a_spatial_fft)/(spatial_size), 'b')
pyplot.plot(arange(spatial_size), imag(a_spatial_fft)/(spatial_size), 'r')
pyplot.show()
pyplot.plot(arange(spatial_size), real(b_spatial_fft)/(spatial_size), 'b')
pyplot.plot(arange(spatial_size), imag(b_spatial_fft)/(spatial_size), 'r')
pyplot.show()
In [25]:
a_spatial = []
b_spatial = []
for i in arange(2*spatial_size):
a_spatial.append(0.0)
b_spatial.append(0.0)
for n in arange(spectral_size):
a_spatial[i] += a[n]*math.cos(2*pi*n*i/(2*spatial_size))
b_spatial[i] += b[n]*math.sin(2*pi*n*i/(2*spatial_size))
a_spatial_fft = numpy.fft.fft(np.array(a_spatial))
b_spatial_fft = numpy.fft.fft(np.array(b_spatial))
pyplot.plot(arange(2*spatial_size), real(a_spatial_fft)/(2*spatial_size), 'b')
pyplot.plot(arange(2*spatial_size), imag(a_spatial_fft)/(2*spatial_size), 'r')
pyplot.show()
pyplot.plot(arange(2*spatial_size), real(b_spatial_fft)/(2*spatial_size), 'b')
pyplot.plot(arange(2*spatial_size), imag(b_spatial_fft)/(2*spatial_size), 'r')
pyplot.show()
In [20]:
a_single = []
b_single = []
temp_spatial_size = spectral_size
for i in arange(temp_spatial_size):
a_single.append(0.0)
b_single.append(0.0)
a_single[i] = 3*math.cos(4*pi*i/(temp_spatial_size))
b_single[i] = 2*math.sin(6*pi*i/(temp_spatial_size))
a_single_fft = numpy.fft.fft(np.array(a_single))
b_single_fft = numpy.fft.fft(np.array(b_single))
pyplot.plot(arange(temp_spatial_size), real(a_single_fft)/(temp_spatial_size), 'b')
pyplot.plot(arange(temp_spatial_size), imag(a_single_fft)/(temp_spatial_size), 'r')
pyplot.show()
pyplot.plot(arange(temp_spatial_size), real(b_single_fft)/(temp_spatial_size), 'b')
pyplot.plot(arange(temp_spatial_size), imag(b_single_fft)/(temp_spatial_size), 'r')
pyplot.show()
In [21]:
print a
a_pad = numpy.pad(a,(0,spatial_size-len(a)),'constant', constant_values=(0))
b_pad = numpy.pad(b,(0,spatial_size-len(b)),'constant', constant_values=(0))
print a_pad
print b_pad
print len(a), len(a_pad), len(b_pad)
In [22]:
a_pad_ifft = numpy.fft.ifft(numpy.array(a_pad))
b_pad_ifft = numpy.fft.ifft(numpy.array(b_pad))
In [23]:
pyplot.plot(arange(spatial_size), real(a_pad_ifft)/(spatial_size), 'b')
pyplot.plot(arange(spatial_size), imag(a_pad_ifft)/(spatial_size), 'r')
pyplot.show()
In [24]:
pyplot.plot(arange(spatial_size), real(b_pad_ifft)/(spatial_size), 'b')
pyplot.plot(arange(spatial_size), imag(b_pad_ifft)/(spatial_size), 'r')
pyplot.show()
In [34]:
pyplot.plot(arange(spatial_size), spatial_size*real(a_pad_ifft), 'b')
pyplot.plot(arange(spatial_size), spatial_size*imag(a_pad_ifft), 'r')
pyplot.plot(arange(spatial_size), a_spatial, 'k')
pyplot.show()
In [35]:
pyplot.plot(arange(spatial_size), spatial_size*real(b_pad_ifft), 'b')
pyplot.plot(arange(spatial_size), spatial_size*imag(b_pad_ifft), 'r')
pyplot.plot(arange(spatial_size), numpy.array(b_spatial), 'k')
pyplot.show()
In [36]:
a_pad_ifft_fft = numpy.fft.fft(numpy.array(a_pad_ifft))
b_pad_ifft_fft = numpy.fft.fft(numpy.array(b_pad_ifft))
In [37]:
pyplot.plot(arange(spatial_size), real(a_pad_ifft_fft), 'b')
pyplot.plot(arange(spatial_size), imag(a_pad_ifft_fft), 'r')
pyplot.plot(arange(spatial_size), a_pad, 'k')
pyplot.show()
In [38]:
pyplot.plot(arange(spatial_size), real(b_pad_ifft_fft), 'b')
pyplot.plot(arange(spatial_size), imag(b_pad_ifft_fft), 'r')
pyplot.plot(arange(spatial_size), b_pad, 'k')
pyplot.show()
In [39]:
ab_ifft = spatial_size**2*a_pad_ifft*b_pad_ifft
pyplot.plot(arange(spatial_size), real(ab_ifft), 'b')
pyplot.plot(arange(spatial_size), imag(ab_ifft), 'r')
pyplot.plot(arange(spatial_size), ab_spatial, 'k')
pyplot.show()
In [40]:
#ab_ifft = spatial_size**2*real(a_pad_ifft)*imag(b_pad_ifft) #+ (spatial_size**2*imag(a_pad_ifft)*real(b_pad_ifft))*1j
ab_ifft = spatial_size**2*a_pad_ifft*b_pad_ifft #Product of complex arrays
pyplot.plot(arange(spatial_size), real(ab_ifft), 'b')
pyplot.plot(arange(spatial_size), imag(ab_ifft), 'r')
pyplot.plot(arange(spatial_size), ab_spatial, 'k')
pyplot.show()
In [41]:
ab_ifft_fft = numpy.fft.fft(numpy.array(ab_ifft))
pyplot.plot(arange(spatial_size), real(ab_ifft_fft)/(spatial_size), 'b')
pyplot.plot(arange(spatial_size), imag(ab_ifft_fft)/(spatial_size), 'r')
pyplot.plot(arange(spatial_size), product_spectral_full, 'k')
pyplot.show()
In [42]:
print a_pad
print b_pad
print real(ab_ifft_fft)/spatial_size
print imag(ab_ifft_fft)/spatial_size
print product_spectral_full
What if we make the b frequency components imaginary so that the transform is a real function? Then we don't have to worry about multiplying the real part of one function with the imaginary part of another.
In [49]:
a_pad_ifft = numpy.fft.ifft(numpy.array(a_pad))
b_pad_ifft = numpy.fft.ifft(numpy.array(b_pad*-1.0j))
In [63]:
pyplot.plot(arange(spatial_size), spatial_size*real(a_pad_ifft), 'b')
pyplot.plot(arange(spatial_size), spatial_size*imag(a_pad_ifft), 'r')
pyplot.plot(arange(spatial_size), a_spatial, 'k')
pyplot.show()
In [64]:
pyplot.plot(arange(spatial_size), spatial_size*real(b_pad_ifft), 'b')
pyplot.plot(arange(spatial_size), spatial_size*imag(b_pad_ifft), 'r')
pyplot.plot(arange(spatial_size), numpy.array(b_spatial), 'k')
pyplot.show()
In [57]:
a_pad_ifft_fft = numpy.fft.fft(numpy.array(a_pad_ifft))
b_pad_ifft_fft = numpy.fft.fft(numpy.array(b_pad_ifft*-1.0))
In [58]:
pyplot.plot(arange(spatial_size), real(a_pad_ifft_fft), 'b')
pyplot.plot(arange(spatial_size), imag(a_pad_ifft_fft), 'r')
pyplot.plot(arange(spatial_size), a_pad, 'k')
pyplot.show()
In [59]:
pyplot.plot(arange(spatial_size), real(b_pad_ifft_fft), 'b')
pyplot.plot(arange(spatial_size), imag(b_pad_ifft_fft), 'r')
pyplot.plot(arange(spatial_size), b_pad, 'k')
pyplot.show()
In [67]:
#ab_ifft = spatial_size**2*a_pad_ifft*b_pad_ifft
ab_ifft = spatial_size**2*real(a_pad_ifft)*real(b_pad_ifft)
pyplot.plot(arange(spatial_size), real(ab_ifft), 'b')
pyplot.plot(arange(spatial_size), imag(ab_ifft), 'r')
pyplot.plot(arange(spatial_size), ab_spatial, 'k')
pyplot.show()
In [69]:
ab_ifft_fft = numpy.fft.fft(numpy.array(ab_ifft))
pyplot.plot(arange(spatial_size), real(ab_ifft_fft)/(spatial_size), 'b')
pyplot.plot(arange(spatial_size), imag(ab_ifft_fft)/(spatial_size), 'r')
pyplot.plot(arange(spatial_size), product_spectral_full, 'k')
pyplot.show()
Ok, this doesn't seem to work either because a_pad_ifft and b_pad_ifft both have real and imag parts so real(ab_ifft) contains contributions from both real(a_ifft)real(b_ifft) and -imag(a_ifft)imag(b_ifft). What can we do about this? Let's see what happens when we iFFT the expected spectral terms in our product function.
In [72]:
product_spectral_full_ifft = numpy.fft.ifft(numpy.array(product_spectral_full))
pyplot.plot(arange(spatial_size), spatial_size*real(product_spectral_full_ifft), 'b')
pyplot.plot(arange(spatial_size), spatial_size*imag(product_spectral_full_ifft), 'r')
pyplot.plot(arange(spatial_size), ab_spatial, 'k')
pyplot.show()
So this works - the imagiary part of the iFFT of our expected spectral terms give the correct product function in space. Now, how do we get the real part from the pieces of the individual iFFTs?
In [5]:
import scipy.fftpack
In [6]:
a_spatial = []
b_spatial = []
for i in arange(spatial_size):
a_spatial.append(0.0)
b_spatial.append(0.0)
for n in arange(spectral_size):
a_spatial[i] += a[n]*math.cos(pi*n*i/(spatial_size-1))
b_spatial[i] += b[n]*math.sin(pi*n*i/(spatial_size-1))
pyplot.plot(arange(spatial_size), a_spatial, 'b')
pyplot.plot(arange(spatial_size), b_spatial, 'r')
pyplot.show()
In [12]:
a_pad_idct = scipy.fftpack.idct(numpy.array(a_pad), type=1)
b_pad_idst = scipy.fftpack.idst(numpy.array(b_pad), type=1)
In [13]:
pyplot.plot(arange(spatial_size), a_pad_idct, 'b')
pyplot.plot(arange(spatial_size), a_spatial, 'r')
pyplot.show()
In [11]:
pyplot.plot(arange(spatial_size), b_pad_idst, 'b')
pyplot.plot(arange(spatial_size), b_spatial, 'r')
pyplot.show()
Ok - so as we thought, we need to convert our initial real frequency components into complex values
In [10]:
a_complex = zeros(spatial_size)*1j
b_complex = zeros(spatial_size)*1j
a_complex[0] = a[0]
for index, val in enumerate(a[1:]):
a_complex[index+1] = val/2
a_complex[spatial_size-(index+1)] = val/2
for index, val in enumerate(b[1:]):
b_complex[index+1] = val/(2j)
b_complex[spatial_size-(index+1)] = -val/(2j)
print a
print a_complex
print b
print b_complex
In [11]:
a_complex_ifft = numpy.fft.ifft(numpy.array(a_complex))
b_complex_ifft = numpy.fft.ifft(numpy.array(b_complex))
In [129]:
pyplot.plot(arange(spatial_size), spatial_size*real(a_complex_ifft), 'b')
pyplot.plot(arange(spatial_size), spatial_size*imag(a_complex_ifft), 'r')
pyplot.plot(arange(spatial_size), a_spatial, 'k')
pyplot.show()
In [128]:
pyplot.plot(arange(spatial_size), spatial_size*real(b_complex_ifft), 'b')
pyplot.plot(arange(spatial_size), spatial_size*imag(b_complex_ifft), 'r')
pyplot.plot(arange(spatial_size), b_spatial, 'k')
pyplot.show()
Fuckin' A - finally! Now let's multiply this ish.
In [14]:
ab_complex_ifft_fft = numpy.fft.fft(numpy.array(spatial_size**2*a_complex_ifft*b_complex_ifft))
In [15]:
pyplot.plot(arange(spatial_size), real(ab_complex_ifft_fft)/spatial_size, 'b')
pyplot.plot(arange(spatial_size), imag(ab_complex_ifft_fft)/spatial_size, 'r')
pyplot.plot(arange(spatial_size), product_spectral_full, 'k')
pyplot.show()
Finally - negative frequencies are still there, but now I understand why. They must be for real spatial functions!
In [99]:
a = zeros(spectral_size)
b = zeros(spectral_size)
a[0] = 1.0
a[1] = 2.0
a[6] = 1.0
b[1] = 0.5
b[6] = 3.0
print a, b
In [69]:
a_spatial = []
b_spatial = []
ab_spatial = []
for i in arange(spatial_size):
a_spatial.append(0.0)
b_spatial.append(0.0)
ab_spatial.append(0.0)
for n in arange(spectral_size):
a_spatial[i] += a[n]*math.cos(2*pi*n*i/(spatial_size))
b_spatial[i] += b[n]*math.sin(2*pi*n*i/(spatial_size))
ab_spatial[i] += a_spatial[i]*b_spatial[i]
In [100]:
a_complex = zeros(spatial_size)*1j
b_complex = zeros(spatial_size)*1j
a_complex[0] = a[0]
for index, val in enumerate(a[1:]):
a_complex[index+1] = val/2
a_complex[spatial_size-(index+1)] = val/2
for index, val in enumerate(b[1:]):
b_complex[index+1] = val/(2j)
b_complex[spatial_size-(index+1)] = -val/(2j)
print a
print a_complex
print b
print b_complex
In [63]:
a_complex_ifft = numpy.fft.ifft(numpy.array(a_complex))
b_complex_ifft = numpy.fft.ifft(numpy.array(b_complex))
In [140]:
pyplot.plot(arange(spatial_size), spatial_size*real(a_complex_ifft), 'b')
pyplot.plot(arange(spatial_size), spatial_size*imag(a_complex_ifft), 'r')
pyplot.plot(arange(spatial_size), a_spatial, 'k')
pyplot.show()
In [139]:
pyplot.plot(arange(spatial_size), spatial_size*real(b_complex_ifft), 'b')
pyplot.plot(arange(spatial_size), spatial_size*imag(b_complex_ifft), 'r')
pyplot.plot(arange(spatial_size), b_spatial, 'k')
pyplot.show()
In [70]:
ab_complex_ifft_fft = numpy.fft.fft(numpy.array(spatial_size**2*a_complex_ifft*b_complex_ifft))
In [19]:
product_spectral_full = zeros(product_size)
product_spectral_full[1] = (a[0]*b[1] + 0.5*a[1]*b[2])
product_spectral_full[2] = (a[0]*b[2] + 0.5*a[1]*b[1])
product_spectral_full[3] = 0.5*a[1]*b[2]
product_spectral_full[4] = -0.5*a[6]*b[2]
product_spectral_full[5] = -0.5*a[6]*b[1] + 0.5*a[1]*b[6]
product_spectral_full[6] = a[0]*b[6]
product_spectral_full[7] = 0.5*a[6]*b[1] + 0.5*a[1]*b[6]
product_spectral_full[8] = 0.5*a[6]*b[2]
product_spectral_full[12] = 0.5*a[6]*b[6]
In [61]:
pyplot.plot(arange(spatial_size), real(ab_complex_ifft_fft)/spatial_size, 'b')
pyplot.plot(arange(spatial_size), imag(ab_complex_ifft_fft)/spatial_size, 'r')
pyplot.plot(arange(spatial_size), product_spectral_full, 'k')
pyplot.show()
In [84]:
print spectral_size, spatial_size
So here's how you multiply terms in the spectral transform method. You start out with Fourier terms representing sines/cosines. These need to be converted to coefficients in a complex exponetial expansion for use with the FFT. This is pretty straightforward. Here's how we do it for our a and b arrays defined at the beginning:
In [109]:
a_complex = zeros(spatial_size)*1j
b_complex = zeros(spatial_size)*1j
a_complex[0] = a[0]
for index, val in enumerate(a[1:]):
a_complex[index+1] = val/2
a_complex[spatial_size-(index+1)] = val/2
for index, val in enumerate(b[1:]):
b_complex[index+1] = val/(2j)
b_complex[spatial_size-(index+1)] = -val/(2j)
print a
print a_complex
print b
print b_complex
The next step is to multiply the functions together in spatial space via the iFFT (noting the normalization factor in the FFT definition used by SciPy - this is not in FFTW):
In [110]:
a_complex_ifft = numpy.fft.ifft(numpy.array(a_complex))
b_complex_ifft = numpy.fft.ifft(numpy.array(b_complex))
Finally, we transform the product back to coefficients in a complex exponential expansion, and then back to sine/cosine coefficients if that's what we started with:
In [111]:
ab_complex_ifft_fft = numpy.fft.fft(numpy.array(spatial_size**2*a_complex_ifft*b_complex_ifft))
ab = zeros(spectral_size)
ab = -2*imag(ab_complex_ifft_fft[0:spectral_size])/(spatial_size)
print ab
Check that everything worked as planned:
In [112]:
pyplot.plot(arange(spatial_size), real(ab_complex_ifft_fft)/spatial_size, 'b')
pyplot.plot(arange(spatial_size), imag(ab_complex_ifft_fft)/spatial_size, 'r')
pyplot.plot(arange(spatial_size), product_spectral_full, 'k')
pyplot.show()
In [152]:
a_spatial = []
b_spatial = []
ab_spatial = []
ab_analytic = []
ab_transform = []
for i in arange(spatial_size):
a_spatial.append(0.0)
b_spatial.append(0.0)
ab_spatial.append(0.0)
ab_analytic.append(0.0)
ab_transform.append(0.0)
for n in arange(spectral_size):
a_spatial[i] += a[n]*math.cos(2*pi*n*i/(spatial_size))
b_spatial[i] += b[n]*math.sin(2*pi*n*i/(spatial_size))
ab_analytic[i] += product_spectral_full[n]*math.sin(2*pi*n*i/(spatial_size))
ab_transform[i] += ab[n]*math.sin(2*pi*n*i/(spatial_size))
ab_spatial[i] += a_spatial[i]*b_spatial[i]
pyplot.plot(arange(spatial_size), ab_analytic, 'b')
pyplot.plot(arange(spatial_size), ab_transform, 'r')
pyplot.plot(arange(spatial_size), ab_spatial, 'k')
pyplot.plot(arange(spatial_size), numpy.array(spatial_size**2*real(a_complex_ifft)*real(b_complex_ifft)), 'o')
pyplot.show()
In [ ]: