-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinterpolation.f90
executable file
·313 lines (244 loc) · 10.1 KB
/
interpolation.f90
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
!
! Copyright 2011 Francesco Pacchiani & Sebastian Heimann
!
! Licensed under the Apache License, Version 2.0 (the "License");
! you may not use this file except in compliance with the License.
! You may obtain a copy of the License at
!
! http://www.apache.org/licenses/LICENSE-2.0
!
! Unless required by applicable law or agreed to in writing, software
! distributed under the License is distributed on an "AS IS" BASIS,
! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
! See the License for the specific language governing permissions and
! limitations under the License.
!
module interpolation
use constants
use util
implicit none
include 'fftw3.f'
public gulunay2d, gulunay3d
contains
subroutine gulunay2d(A,Inter, ntmargin, nxmargin)
! Interpolate Green's function using the Generalized f-k interpolation method,
! also known as Gulunay's [2003] method.
real, intent(inout), dimension(:,:) :: A ! (t,s)
real, intent(out), dimension(:,:) :: Inter ! (t,kk)
integer, intent(in) :: ntmargin, nxmargin
integer :: t,s, kk, ff, fny
integer :: x
integer*8 :: plan
real :: m
integer :: l, il
real, dimension(size(A,1), size(Inter,2)) :: B
real, dimension(size(A,1)*(size(Inter,2)/size(A,2)), size(Inter,2)) :: C,D
complex, dimension(size(A,1)/2+1, size(Inter,2)) :: fB, fInter, Operator ! (t/2+1,kk)
complex, dimension(size(C,1)/2+1, size(Inter,2)) :: fC, fD ! (ff/2+1,kk)
! check sizes
if (size(Inter,1) /= size(A,1)) then
call die( "gulunay2d(): time length of input and output arrays do not match")
end if
if (mod(size(Inter,2),size(A,2)) /= 0) then
call die( "gulunay2d(): number of traces in ouput array is not a multiple of size of input array")
end if
l = size(Inter,2)/size(A,2)
t=size(A,1)
s=size(A,2)
kk=size(Inter,2)
ff=l*t
! --- Taper ---
!
do x=1,nxmargin/l
A(:,x) = A(:,x) * (1. - cos(2.*pi*((x-1)/(2.*nxmargin/l)))) / 2.
end do
do x=s-nxmargin/l+1,s
A(:,x) = A(:,x) * (1. - cos(2.*pi*((s-x)/(2.*nxmargin/l)))) / 2.
end do
do x=1,ntmargin/l
A(x,:) = A(x,:) * (1. - cos(2.*pi*((x-1)/(2.*ntmargin/l)))) / 2.
end do
do x=t-ntmargin/l+1,t
A(x,:) = A(x,:) * (1. - cos(2.*pi*((t-x)/(2.*ntmargin/l)))) / 2.
end do
!
! --- Insert zero traces in original data + 2-D FFT ---
!
B(:,1:kk:l) = A(:,:)
do il=2,l
B(:,il:kk:l) = 0.
end do
call sfftw_plan_dft_r2c_2d(plan,t,kk,B,fB,FFTW_ESTIMATE)
call sfftw_execute(plan)
call sfftw_destroy_plan(plan)
!
! --- Zero pad the original data in all dimensions + 2-D FFT ---
!
C(:,:) = 0.
C(1:t,1:s) = A(:,:)
call sfftw_plan_dft_r2c_2d(plan,ff,kk,C,fC,FFTW_ESTIMATE)
call sfftw_execute(plan)
call sfftw_destroy_plan(plan)
!
! --- Replace even traces in C with zeros + 2-D FFT ---
!
D(:,1:s:l) = C(:,1:s:l)
do il=2,l
D(:,il:s:l) = 0.
end do
call sfftw_plan_dft_r2c_2d(plan,ff,kk,D,fD,FFTW_ESTIMATE)
call sfftw_execute(plan)
call sfftw_destroy_plan(plan)
!
! --- Add white noise to spectrum ---
!
fny = t/2+1
m = 0.01*maxval( abs(fD(fny,:)) )
where (abs(fD(1:fny,:)) .lt. m/1000.)
fD(1:fny,:) = cmplx(m,aimag(fD(1:fny,:)))
end where
where (abs(fD(1:fny,:)) .lt. m)
fD(1:fny,:) = m/abs(fD(1:fny,:)) * fD(1:fny,:)
end where
!
! --- Calculate the interpolator ---
!
Operator(:,:) = fC(1:fny,:) / fD(1:fny,:)
!
! --- Clip the iterpolation operator ---
!
where (abs(Operator) .gt. l)
Operator = l/abs(Operator) * Operator ! cmplx(l,aimag(Operator))
end where
where (abs(Operator) .lt. l*0.5)
Operator = 0. ! cmplx(0.,aimag(Operator))
end where
!
! --- Compute interpolated traces ---
!
fInter(:,:) = fB(:,:) * Operator(:,:) / (t*kk)
!
! --- Return in the time domain ---
!
call sfftw_plan_dft_c2r_2d(plan,t,kk,fInter,Inter,FFTW_ESTIMATE)
call sfftw_execute(plan)
call sfftw_destroy_plan(plan)
end subroutine
subroutine gulunay3d(A,Inter, ntmargin, nxmargin, nzmargin)
! Interpolate Green's function using the Generalized f-k interpolation method,
! also known as Gulunay's [2003] method.
real, intent(inout), dimension(:,:,:) :: A ! (t,sz,sx)
real, intent(out), dimension(:,:,:) :: Inter ! (t,kkz,kkx)
integer, intent(in) :: ntmargin, nxmargin, nzmargin
integer :: t,sx,sz, kkx,kkz, ff, fny
integer :: x
integer*8 :: plan
integer :: l, il
real :: m, ls, lowcut
real, dimension(size(A,1), size(Inter,2), size(Inter,3)) :: B
real, dimension(size(A,1)*(size(Inter,2)/size(A,2)), size(Inter,2), size(Inter,3)) :: C,D
complex, dimension(size(A,1)/2+1, size(Inter,2), size(Inter,3)) :: fB, fInter, Operator ! (t/2+1,kk)
complex, dimension(size(C,1)/2+1, size(Inter,2), size(Inter,3)) :: fC, fD ! (ff/2+1,kk)
if (size(Inter,1) /= size(A,1)) then
call die( "gulunay3d(): time length of input and output arrays do not match")
end if
if (size(Inter,2)/size(A,2) /= size(Inter,3)/size(A,3)) then
call die( "gulunay3d(): proportions of dimensions 2 and 3 between input and output arrays do not match")
end if
if (mod(size(Inter,2),size(A,2)) /= 0 .or. mod(size(Inter,3),size(A,3)) /= 0 ) then
call die( "gulunay3d(): number of traces in ouput array is not a multiple of size of input array")
end if
l = size(Inter,2)/size(A,2)
t=size(A,1)
sz=size(A,2)
sx=size(A,3)
kkx=l*sx
kkz=l*sz
ff=l*t
!
! --- Taper ---
!
do x=1,nxmargin/l
A(:,:,x) = A(:,:,x) * (1. - cos(2.*pi*((x-1)/(2.*nxmargin/l)))) / 2.
end do
do x=sx-nxmargin/l+1,sx
A(:,:,x) = A(:,:,x) * (1. - cos(2.*pi*((sx-x)/(2.*nxmargin/l)))) / 2.
end do
do x=1,nzmargin/l
A(:,x,:) = A(:,x,:) * (1. - cos(2.*pi*((x-1)/(2.*nzmargin/l)))) / 2.
end do
do x=sz-nzmargin/l+1,sz
A(:,x,:) = A(:,x,:) * (1. - cos(2.*pi*((sz-x)/(2.*nzmargin/l)))) / 2.
end do
do x=1,ntmargin/l
A(x,:,:) = A(x,:,:) * (1. - cos(2.*pi*((x-1)/(2.*ntmargin/l)))) / 2.
end do
do x=t-ntmargin/l+1,t
A(x,:,:) = A(x,:,:) * (1. - cos(2.*pi*((t-x)/(2.*ntmargin/l)))) / 2.
end do
!
! --- Insert zero traces in original data + 2-D FFT ---
!
B(:,1:kkz:l,1:kkx:l) = A(:,:,:)
do il=2,l
B(:,il:kkz:l,il:kkx:l) = 0.
end do
call sfftw_plan_dft_r2c_3d(plan,t,kkz,kkx,B,fB,FFTW_ESTIMATE)
call sfftw_execute(plan)
call sfftw_destroy_plan(plan)
!
! --- Zero pad the original data in all dimensions + 2-D FFT ---
!
C(:,:,:) = 0.
C(1:t,1:sz,1:sx) = A(:,:,:)
call sfftw_plan_dft_r2c_3d(plan,ff,kkz,kkx,C,fC,FFTW_ESTIMATE)
call sfftw_execute(plan)
call sfftw_destroy_plan(plan)
!
! --- Replace even traces in C with zeros + 2-D FFT ---
!
D(:,1:sz:l,1:sx:l) = C(:,1:sz:l,1:sx:l)
do il=2,l
D(:,il:sz:l,il:sx:l) = 0.
end do
call sfftw_plan_dft_r2c_3d(plan,ff,kkz,kkx,D,fD,FFTW_ESTIMATE)
call sfftw_execute(plan)
call sfftw_destroy_plan(plan)
!
! --- Add white noise to spectrum ---
!
fny = t/2+1
m = 0.01*maxval( abs(fD(fny,:,:)) )
where (abs(fD(1:fny,:,:)) .lt. m/1000.)
fD(1:fny,:,:) = cmplx(m,aimag(fD(1:fny,:,:)))
end where
where (abs(fD(1:fny,:,:)) .lt. m)
fD(1:fny,:,:) = m/abs(fD(1:fny,:,:)) * fD(1:fny,:,:)
end where
!
! --- Calculate the interpolator ---
!
Operator(:,:,:) = fC(1:fny,:,:) / fD(1:fny,:,:)
!
! --- Clip the iterpolation operator ---
!
lowcut = 0.5*l**2
ls = l**2
where (abs(Operator) .gt. ls)
Operator = ls/abs(Operator) * Operator
end where
where (abs(Operator) .lt. lowcut)
Operator = 0.
end where
!
! --- Compute interpolated traces ---
!
fInter(:,:,:) = fB(:,:,:) * Operator(:,:,:) / (t*kkx*kkz)
!
! --- Return in the time domain ---
!
call sfftw_plan_dft_c2r_3d(plan,t,kkz,kkx,fInter,Inter,FFTW_ESTIMATE)
call sfftw_execute(plan)
call sfftw_destroy_plan(plan)
end subroutine
end module