-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfft_poisson.py
65 lines (55 loc) · 1.75 KB
/
fft_poisson.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from __future__ import division
import matplotlib.pyplot as plt
import numpy as np
from numpy.fft import fft,fft2,ifft2,ifft,irfft2,rfft2
import random as random
from mpl_toolkits.mplot3d import Axes3D
def dst(x,axis=-1):
"""Discrete Sine Transform (DST-I)
Implemented using 2(N+1)-point FFT
xsym = r_[0,x,0,-x[::-1]]
DST = (-imag(fft(xsym))/2)[1:(N+1)]
adjusted to work over an arbitrary axis for entire n-dim array
"""
n = len(x.shape)
N = x.shape[axis]
slices = [None]*3
for k in range(3):
slices[k] = []
for j in range(n):
slices[k].append(slice(None))
newshape = list(x.shape)
newshape[axis] = 2*(N+1)
xsym = np.zeros(newshape,np.float)
slices[0][axis] = slice(1,N+1)
slices[1][axis] = slice(N+2,None)
slices[2][axis] = slice(None,None,-1)
for k in range(3):
slices[k] = tuple(slices[k])
xsym[slices[0]] = x
xsym[slices[1]] = -x[slices[2]]
DST = fft(xsym,axis=axis)
#print xtilde
return (-(DST.imag)/2)[slices[0]]
def dst2(x,axes=(-1,-2)):
return dst(dst(x,axis=axes[0]),axis=axes[1])
def idst2(x,axes=(-1,-2)):
return dst(dst(x,axis=axes[0]),axis=axes[1])
def fft_poisson(f,h):
m,n=f.shape
f_bar=np.zeros([n,n])
u_bar = f_bar # make of the same shape
u = u_bar
f_bar=idst2(f) # f_bar= fourier transform of f
f_bar = f_bar * (2/n+1)**2 #Normalize
#u_bar =np.zeros([n,n])
pi=np.pi
lam = np.arange(1,n+1)
lam = -4/h**2 * (np.sin((lam*pi) / (2*(n + 1))))**2 #$compute $\lambda_x$
#for rectangular domain add $lambda_y$
for i in range(0,n):
for j in range(0,n):
u_bar[i,j] = (f_bar[i,j]) / (lam[i] + lam[j])
u=dst2(u_bar) #sine transform back
u= u * (2/(n+1))**2 #normalize ,change for rectangular domain
return u