-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgradientDtt1D.m
518 lines (354 loc) · 13.2 KB
/
gradientDtt1D.m
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
function deriv = gradientDtt1D(f, dx, dtt_type, shift, align_output)
%GRADIENTDTT1D Calculate gradient using discrete trigonometric transforms.
%
% DESCRIPTION:
% gradientDtt1D computes the spectral gradient of a 1D input function
% using discrete trigonometric transforms (DTTs). The DTTs used to
% transform the input to and from the frequency domain are chosen based
% on the symmetry of the input f, defined using dtt_type. The gradient
% values can also be returned on a staggered grid.
%
% Note, the group I DTTs (DCT-I, DCT-II, DST-I, DST-II), have different
% length k-space vectors due to the implied symmetry of the input
% sequence (e.g., the Nyquist and/or DC components are zero, and do not
% need to be defined). This means that in some cases the basis
% functions weights are trimmed or appended after the forward
% transform. To ensure the output of gradientDTT1D is the same length
% as the input f, additional values can be appended or removed from the
% calculated gradient after the inverse transform is calculated by
% setting the optional input pad to true (the default). These values
% are known from the symmetry of the output sequence.
%
% For additional details on gradient calculation using DTTs, see [1].
%
% [1] E. Wise, J. Jaros, B. Cox, and B. Treeby, "Pseudospectral
% time-domain (PSTD) methods for the wave equation: Realising boundary
% conditions with discrete sine and cosine transforms", 2020.
%
% INPUTS:
% f - Vector to find the gradient of.
% dx - Grid point spacing.
% dtt_type - Type of discrete trigonometric transform. This should
% correspond to the assumed input symmetry of the input
% function, where:
%
% 1: DCT-I WSWS
% 2: DCT-II HSHS
% 3: DCT-III WSWA
% 4: DCT-IV HSHA
% 5: DST-I WAWA
% 6: DST-II HAHA
% 7: DST-III WAWS
% 8: DST-IV HAHS
%
% OPTIONAL INPUTS:
% shift - Integer controlling whether derivative is shifted to a
% staggered grid (default = 0), where
%
% 0: no shift
% 1: shift by + dx/2
% 2: shift by - dx/2
%
% align_output - Boolean controlling whether the returned values are
% padded and trimmed based on the implied symmetry so
% the output is the same length as the input (default =
% true). Note, if align_output is false, then the
% gradient calculated for shift = 1 and shift = 2 will
% be the same.
%
% OUTPUTS:
% dfdx - Gradient of the input function.
%
% ABOUT:
% author - Bradley Treeby
% date - 23 April 2013
% last update - 20 April 2020
%
% Copyright (C) 2013-2020 Bradley Treeby
%
% See also dtt1D
% This program is free software: you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by the
% Free Software Foundation, either version 3 of the License, or (at your
% option) any later version.
%
% This program is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
% Public License for more details.
%
% You should have received a copy of the GNU General Public License along
% with this program. If not, see <https://www.gnu.org/licenses/>.
% define literals for DTT types used by the function dtt1D
DCT1 = 1; % WSWS
DCT2 = 2; % HSHS
DCT3 = 3; % WSWA
DCT4 = 4; % HSHA
DST1 = 5; % WAWA
DST2 = 6; % HAHA
DST3 = 7; % WAWS
DST4 = 8; % HAHS
% check for shift input
if (nargin < 4) || isempty(shift)
shift = 0;
end
% check for pad input
if (nargin < 5) || isempty(align_output)
align_output = true;
end
% check inputs
validateattributes(f, {'numeric'}, {'real', 'vector', 'nonsparse'}, 'gradientDTT1D', 'f', 1);
validateattributes(dx, {'numeric'}, {'real', 'scalar', 'nonsparse'}, 'gradientDTT1D', 'dx', 2);
validateattributes(dtt_type, {'numeric'}, {'integer', 'scalar', 'nonsparse', '>=', 1, '<=', 8}, 'gradientDTT1D', 'dtt_type', 3);
validateattributes(shift, {'numeric'}, {'integer', 'scalar', 'nonsparse', '>=', 0, '<=', 2}, 'gradientDTT1D', 'shift', 4);
validateattributes(align_output, {'logical'}, {'scalar'}, 'gradientDTT1D', 'align_output', 5);
% reshape to be a row vector
f = reshape(f, 1, []);
% extract length of input function
Nx = length(f);
% compute the implied period of the input function
switch dtt_type
case 1
M = 2 .* (Nx - 1);
case 5
M = 2 .* (Nx + 1);
otherwise
M = 2 .* Nx;
end
% calculate the wavenumbers
switch dtt_type
case 1
% WSWS / DCT-I
n = (0:1:M/2);
kx = 2 .* pi .* n ./ (M .* dx);
case 2
% HSHS / DCT-II
n = (0:1:(M/2 - 1));
kx = 2 .* pi .* n ./ (M .* dx);
case 5
% WAWA / DST-I
n = (1:1:(M/2 - 1));
kx = 2 .* pi .* n ./ (M .* dx);
case 6
% HAHA / DST-II
n = (1:1:M/2);
kx = 2 .* pi .* n ./ (M .* dx);
case {3, 4, 7, 8}
% WSWA / DCT-III
% HSHA / DCT-IV
% WAWS / DST-III
% HAHS / DST-IV
n = (0:1:(M/2 - 1));
kx = 2 .* pi .* (n + 0.5) ./ (M .* dx);
end
% compute forward transform and multiply by the wavenumbers
if dtt_type < 5
% discrete cosine transform
deriv = -kx .* dtt1D(f, dtt_type);
else
% discrete sin transform
deriv = kx .* dtt1D(f, dtt_type);
end
% select appropriate function range for inverse transform
% (this is only required for the whole-wavenumber DTTs as the function
% range is the same for the half-wavenumber DTTs)
switch dtt_type
case 1
if shift
% WSWS -> HAHA, remove left endpoint
deriv = deriv(2:end);
else
% WSWS -> WAWA, remove both endpoints
deriv = deriv(2:end - 1);
end
case 2
if shift
% HSHS -> WAWA, remove left endpoint
deriv = deriv(2:end);
else
% HSHS -> HAHA, remove left endpoint and append a zero
deriv = [deriv(2:end), 0];
end
case 5
if shift
% WAWA -> HSHS, prepend zero
deriv = [0, deriv];
else
% WAWA -> WSWS, prepend and append zeros
deriv = [0, deriv, 0];
end
case 6
if shift
% HAHA -> WSWS, prepend zero
deriv = [0, deriv];
else
% HAHA -> HSHS, prepend zero, remove right endpoint
deriv = [0, deriv(1:end - 1)];
end
end
% compute inverse transform and normalise by the implied period of the
% input function
switch dtt_type
case 1
if shift
% WSWS -> HAHA, S2^-1 = S3
deriv = dtt1D(deriv, DST3) ./ M;
else
% WSWS -> WAWA, S1^-1 = S1
deriv = dtt1D(deriv, DST1) ./ M;
end
case 2
if shift
% HSHS -> WAWA, S1^-1 = S1
deriv = dtt1D(deriv, DST1) ./ M;
else
% HSHS -> HAHA, S2^-1 = S3
deriv = dtt1D(deriv, DST3) ./ M;
end
case 3
if shift
% WSWA -> HAHS, S4^-1 = S4
deriv = dtt1D(deriv, DST4) ./ M;
else
% WSWA -> WAWS, S3^-1 = S2
deriv = dtt1D(deriv, DST2) ./ M;
end
case 4
if shift
% HSHA -> WAWS, S3^-1 = S2
deriv = dtt1D(deriv, DST2) ./ M;
else
% HSHA -> HAHS, S4^-1 = S4
deriv = dtt1D(deriv, DST4) ./ M;
end
case 5
if shift
% WAWA -> HSHS, C2^-1 = C3
deriv = dtt1D(deriv, DCT3) ./ M;
else
% WAWA -> WSWS, C1^-1 = C1
deriv = dtt1D(deriv, DCT1) ./ M;
end
case 6
if shift
% HAHA -> WSWS, C1^-1 = C1
deriv = dtt1D(deriv, DCT1) ./ M;
else
% HAHA -> HSHS, C2^-1 = C3
deriv = dtt1D(deriv, DCT3) ./ M;
end
case 7
if shift
% WAWS -> HSHA, C4^-1 = C4
deriv = dtt1D(deriv, DCT4) ./ M;
else
% WAWS -> WSWA, C3^-1 = C2
deriv = dtt1D(deriv, DCT2) ./ M;
end
case 8
if shift
% HAHS ->WSWA, C3^-1 = C2
deriv = dtt1D(deriv, DCT2) ./ M;
else
% HAHS -> HSHA, C4^-1 = C4
deriv = dtt1D(deriv, DCT4) ./ M;
end
end
% add back in the implied values so the output is the same length as the
% input
if align_output
switch dtt_type
case 1
switch shift
case 0
% WSWS -> WAWA, add both endpoints
deriv = [0, deriv, 0];
case 1
% WSWS -> HAHA, shift right, mirror right endpoint
deriv = [deriv, -deriv(end)];
case 2
% WSWS -> HAHA, shift left, mirror left endpoint
deriv = [-deriv(1), deriv];
end
case 2
switch shift
case 0
% HSHS -> HAHA, no change
case 1
% HSHS -> WAWA, shift right, append zero
deriv = [deriv, 0];
case 2
% HSHS -> WAWA, shift left, prepend zero
deriv = [0, deriv];
end
case 3
switch shift
case 0
% WSWA -> WAWS, prepend zero, remove right endpoint
deriv = [0, deriv(1:end - 1)];
case 1
% WSWA -> HAHS, shift right, no change
case 2
% WSWA -> HAHS, shift left, mirror left endpoint, remove
% right endpoint
deriv = [-deriv(1), deriv(1:end - 1)];
end
case 4
switch shift
case 0
% HSHA -> HAHS, no change
case 1
% HSHA -> WAWS, shift right, no change
case 2
% HSHA -> WAWS, shift left, prepend zero, remove right
% endpoint
deriv = [0, deriv(1:end - 1)];
end
case 5
switch shift
case 0
% WAWA -> WSWS, remove both endpoints
deriv = deriv(2:end - 1);
case 1
% WAWA -> HSHS, shift right, remove left endpoint
deriv = deriv(2:end);
case 2
% WAWA -> HSHS, shift left, remove right endpoint
deriv = deriv(1:end - 1);
end
case 6
switch shift
case 0
% HAHA -> HSHS, no change
case 1
% HAHA -> WSWS, shift right, remove left endpoint
deriv = deriv(2:end);
case 2
% HAHA -> WSWS, shift left, remove right endpoint
deriv = deriv(1:end - 1);
end
case 7
switch shift
case 0
% WAWS -> WSWA, remove left endpoint, append zero
deriv = [deriv(2:end), 0];
case 1
% WAWS -> HSHA, shift right, remove left endpoint, mirror
% right endpoint
deriv = [deriv(2:end), -deriv(end)];
case 2
% WAWS -> HSHA, shift left, no change
end
case 8
switch shift
case 0
% HAHS -> HSHA, no change
case 1
% HAHS -> WSWA, shift right, remove left endpoint, append
% zero
deriv = [deriv(2:end), 0];
case 2
% HAHS -> WSWA, shift left, no change
end
end
end