-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpuInversion.py
executable file
·145 lines (114 loc) · 4.22 KB
/
gpuInversion.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import cupy as cp
import numpy as np
from cupyx.scipy import signal
import scipy
def da_gpu(slcStack):
amp=cp.abs(slcStack)
std=cp.std(amp,axis=0)
ave=cp.average(amp,axis=0)
da=std/ave
return da
def da(slcStack,axis=0):
amp=np.abs(slcStack)
std=np.std(amp,axis=axis)
ave=np.average(amp,axis=axis)
da=std/ave
return da
def gpu_moving_average_2d(arr_cpu, window_size):
"""
input: cpu arr
output: gpu arr
"""
arr_gpu=cp.asarray(arr_cpu)
patch=arr_cpu.shape[0]
# window = scipy.signal.gaussian(window_size,1).reshape(-1,1)
# window = window*window.T
# window_ave = cp.asanyarray(window,dtype=cp.complex64)
window_ave = cp.ones((window_size, window_size), dtype=cp.complex64) / (window_size**2)
# Perform 2D convolution to calculate the moving average
result_gpu=cp.empty(shape=arr_gpu.shape,dtype=cp.complex64)
for i in range(patch):
# result_gpu[i,...]=signal.convolve2d(arr_gpu[i,...], window, mode='same')
result_gpu[i,...]=signal.convolve2d(arr_gpu[i,...], window_ave, mode='same')
return result_gpu
def gpuBFinversion(cpxGpu,steering):
"""
input: gpu array
output: cpu array
"""
steeringGpu=cp.asarray(steering)
patch,ydim,xdim=cpxGpu.shape
level=steering.shape[1]
# cpxGpu=cp.asarray(cpxCpu)
cpxGpu=cpxGpu.reshape(patch,ydim*xdim)
# start calculation
norm=cp.max(cp.abs(cpxGpu),axis=0)
cpxGpu=cpxGpu/norm
cpxGpu=cpxGpu.T
cpxGpu=cpxGpu[:,:,cp.newaxis]
cov=cp.einsum('ijk,ilk->ijl',cpxGpu,cp.conj(cpxGpu))
cov+=cp.eye(patch)*0.01
covI=cp.linalg.inv(cov)
power=cp.einsum('jl,ijk->ilk',cp.conj(steeringGpu),covI)
power=cp.einsum('ijk,kl->ijl',power,steeringGpu)
power=1/cp.diagonal(power,0,1,2)
power=power.reshape(ydim,xdim,level)
return cp.abs(power)
def BFinversion(cpxArray,steering):
patch,ydim,xdim=cpxArray.shape
level=steering.shape[1]
cpx=cpxArray.reshape(patch,ydim*xdim)
# start calculation
norm=np.max(np.abs(cpx),axis=0)
cpx=cpx/norm
cpx=cpx.T
cov=np.einsum('ij,il->ijl',cpx,np.conj(cpx))/patch
load=0.01*np.eye(patch)
cov+=load
covI=np.linalg.inv(cov)
power=np.einsum('jl,ijk->ilk',np.conj(steering),covI)
power=np.einsum('ijk,kl->ijl',power,steering)
power=1/np.diagonal(power,0,1,2)
power=power.reshape(ydim,xdim,level)
return np.abs(power)
def gpuCalcCoh(tomography,steering,gaussian):
k=cp.argmax(tomography,axis=2)
s_gpu=cp.asarray(steering)
sig=cp.exp(1j*cp.angle(gaussian))
L_g=cp.einsum('ij,jkl->ikl',s_gpu.conj().T,sig)
L_k=cp.einsum('ij,jkl->ikl',s_gpu.conj().T,s_gpu[:,k])
coh=cp.sum(L_g*L_k.conj(),axis=0)/cp.sum(L_k*L_k.conj(),axis=0)**0.5/cp.sum(L_g*L_g.conj(),axis=0)**0.5
return cp.abs(coh)
def CalcCoh(tomography,steering,gaussian):
k=np.argmax(tomography,axis=2)
sig=np.exp(1j*np.angle(gaussian))
L_g=np.einsum('ij,jkl->ikl',steering.conj().T,sig)
L_k=np.einsum('ij,jkl->ikl',steering.conj().T,steering[:,k])
coh=np.sum(L_g*L_k.conj(),axis=0)/np.sum(L_k*L_k.conj(),axis=0)**0.5/np.sum(L_g*L_g.conj(),axis=0)**0.5
return np.abs(coh)
#### original code
# for j in range(lns):
# for i in range(width):
# j0=max(0,int(j-win/2))
# j1=min(lns,int(j+win/2))
# i0=max(0,int(i-win/2))
# i1=min(width,int(i+win/2))
# # multilook and normalization
# cpx=real[:,j0:j1,i0:i1]+1j*imag[:,j0:j1,i0:i1]
# cpx=np.average(np.average(cpx,axis=1),axis=1)
# norm=np.max(np.abs(cpx))
# cpx=cpx/norm
# cpx=cpx.reshape(-1,1)
# da[j,i]=utils.da(cpx)
# # calculate covariance matrix
# cpxH=np.conj(cpx.T)
# cov=np.dot(cpx,cpxH)/nslc
# load_factor=0.025
# cov=cov+np.eye(nslc)*load_factor
# invCov=inv(cov)
# denominator=np.dot(np.dot(steeringH,invCov),steering)
# # denominator=np.array([np.diag(denominator)]*nslc,dtype=np.complex64)
# # weight=np.dot(invCov,steering)/denominator
# # wH=np.conj(weight.T)
# # power=np.dot(np.dot(wH,cov),weight)
# tomography[j,i,:]=np.log10(np.diag(1/denominator.real))