This repository has been archived by the owner on Aug 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 950
/
spectral_ops.ts
188 lines (167 loc) · 6.05 KB
/
spectral_ops.ts
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
/**
* @license
* Copyright 2018 Google Inc. All Rights Reserved.
* 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.
* =============================================================================
*/
import {ENV} from '../environment';
import {complex, imag, real} from '../ops/complex_ops';
import {op} from '../ops/operation';
import {Tensor, Tensor2D} from '../tensor';
import {assert} from '../util';
import {scalar} from './tensor_ops';
/**
* Fast Fourier transform.
*
* Computes the 1-dimensional discrete Fourier transform over the inner-most
* dimension of input.
*
* ```js
* const real = tf.tensor1d([1, 2, 3]);
* const imag = tf.tensor1d([1, 2, 3]);
* const x = tf.complex(real, imag);
*
* x.fft().print(); // tf.spectral.fft(x).print();
* ```
* @param input The complex input to compute an fft over.
*/
/**
* @doc {heading: 'Operations', subheading: 'Spectral', namespace: 'spectral'}
*/
function fft_(input: Tensor): Tensor {
assert(
input.dtype === 'complex64',
() => `The dtype for tf.spectral.fft() must be complex64 ` +
`but got ${input.dtype}.`);
// Collapse all outer dimensions to a single batch dimension.
const innerDimensionSize = input.shape[input.shape.length - 1];
const batch = input.size / innerDimensionSize;
const input2D = input.as2D(batch, innerDimensionSize);
const ret = ENV.engine.runKernel(backend => backend.fft(input2D), {input});
return ret.reshape(input.shape);
}
/**
* Inverse fast Fourier transform.
*
* Computes the inverse 1-dimensional discrete Fourier transform over the
* inner-most dimension of input.
*
* ```js
* const real = tf.tensor1d([1, 2, 3]);
* const imag = tf.tensor1d([1, 2, 3]);
* const x = tf.complex(real, imag);
*
* x.ifft().print(); // tf.spectral.ifft(x).print();
* ```
* @param input The complex input to compute an ifft over.
*/
/**
* @doc {heading: 'Operations', subheading: 'Spectral', namespace: 'spectral'}
*/
function ifft_(input: Tensor): Tensor {
assert(
input.dtype === 'complex64',
() => `The dtype for tf.spectral.ifft() must be complex64 ` +
`but got ${input.dtype}.`);
// Collapse all outer dimensions to a single batch dimension.
const innerDimensionSize = input.shape[input.shape.length - 1];
const batch = input.size / innerDimensionSize;
const input2D = input.as2D(batch, innerDimensionSize);
const ret = ENV.engine.runKernel(backend => backend.ifft(input2D), {input});
return ret.reshape(input.shape);
}
/**
* Real value input fast Fourier transform.
*
* Computes the 1-dimensional discrete Fourier transform over the
* inner-most dimension of the real input.
*
* ```js
* const real = tf.tensor1d([1, 2, 3]);
*
* real.rfft().print();
* ```
* @param input The real value input to compute an rfft over.
*/
/**
* @doc {heading: 'Operations', subheading: 'Spectral', namespace: 'spectral'}
*/
function rfft_(input: Tensor): Tensor {
assert(
input.dtype === 'float32',
() => `The dtype for rfft() must be real value but got ${input.dtype}`);
const innerDimensionSize = input.shape[input.shape.length - 1];
const batch = input.size / innerDimensionSize;
// Complement the input with zero imaginary numbers.
const zeros = input.zerosLike();
const complexInput = complex(input, zeros).as2D(batch, innerDimensionSize);
const ret = fft(complexInput);
// Exclude complex conjugations. These conjugations are put symmetrically.
const half = Math.floor(innerDimensionSize / 2) + 1;
const realValues = real(ret);
const imagValues = imag(ret);
const realComplexConjugate = realValues.split(
[half, innerDimensionSize - half], realValues.shape.length - 1);
const imagComplexConjugate = imagValues.split(
[half, innerDimensionSize - half], imagValues.shape.length - 1);
const outputShape = input.shape.slice();
outputShape[input.shape.length - 1] = half;
return complex(realComplexConjugate[0], imagComplexConjugate[0])
.reshape(outputShape);
}
/**
* Inversed real value input fast Fourier transform.
*
* Computes the 1-dimensional inversed discrete Fourier transform over the
* inner-most dimension of the real input.
*
* ```js
* const real = tf.tensor1d([1, 2, 3]);
*
* real.irfft().print();
* ```
* @param input The real value input to compute an irfft over.
*/
/**
* @doc {heading: 'Operations', subheading: 'Spectral', namespace: 'spectral'}
*/
function irfft_(input: Tensor): Tensor {
const innerDimensionSize = input.shape[input.shape.length - 1];
const batch = input.size / innerDimensionSize;
if (innerDimensionSize <= 2) {
const complexInput = input.as2D(batch, innerDimensionSize);
const ret = ifft(complexInput);
return real(ret);
} else {
// The length of unique components of the DFT of a real-valued signal
// is 2 * (input_len - 1)
const outputShape = [batch, 2 * (innerDimensionSize - 1)];
const realInput = real(input).as2D(batch, innerDimensionSize);
const imagInput = imag(input).as2D(batch, innerDimensionSize);
const realConjugate =
realInput.slice([0, 1], [batch, innerDimensionSize - 2]).reverse(1);
const imagConjugate =
imagInput.slice([0, 1], [batch, innerDimensionSize - 2])
.reverse(1)
.mul(scalar(-1)) as Tensor2D;
const r = realInput.concat(realConjugate, 1);
const i = imagInput.concat(imagConjugate, 1);
const complexInput = complex(r, i).as2D(outputShape[0], outputShape[1]);
const ret = ifft(complexInput);
return real(ret);
}
}
export const fft = op({fft_});
export const ifft = op({ifft_});
export const rfft = op({rfft_});
export const irfft = op({irfft_});