gx
chenyc
2025-06-12 7b72ac13a83764a662159d4a49b7fffb90476ecb
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
"use strict";
/**
 * @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.
 * =============================================================================
 */
Object.defineProperty(exports, "__esModule", { value: true });
var engine_1 = require("../engine");
var complex_ops_1 = require("../ops/complex_ops");
var operation_1 = require("../ops/operation");
var util_1 = require("../util");
var tensor_ops_1 = require("./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) {
    util_1.assert(input.dtype === 'complex64', function () { return "The dtype for tf.spectral.fft() must be complex64 " +
        ("but got " + input.dtype + "."); });
    // Collapse all outer dimensions to a single batch dimension.
    var innerDimensionSize = input.shape[input.shape.length - 1];
    var batch = input.size / innerDimensionSize;
    var input2D = input.as2D(batch, innerDimensionSize);
    var ret = engine_1.ENGINE.runKernelFunc(function (backend) { return backend.fft(input2D); }, { input: 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) {
    util_1.assert(input.dtype === 'complex64', function () { return "The dtype for tf.spectral.ifft() must be complex64 " +
        ("but got " + input.dtype + "."); });
    // Collapse all outer dimensions to a single batch dimension.
    var innerDimensionSize = input.shape[input.shape.length - 1];
    var batch = input.size / innerDimensionSize;
    var input2D = input.as2D(batch, innerDimensionSize);
    var ret = engine_1.ENGINE.runKernelFunc(function (backend) { return backend.ifft(input2D); }, { input: 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, fftLength) {
    util_1.assert(input.dtype === 'float32', function () { return "The dtype for rfft() must be real value but got " + input.dtype; });
    var innerDimensionSize = input.shape[input.shape.length - 1];
    var batch = input.size / innerDimensionSize;
    var adjustedInput;
    if (fftLength != null && fftLength < innerDimensionSize) {
        // Need to crop
        var begin = input.shape.map(function (v) { return 0; });
        var size = input.shape.map(function (v) { return v; });
        size[input.shape.length - 1] = fftLength;
        adjustedInput = input.slice(begin, size);
        innerDimensionSize = fftLength;
    }
    else if (fftLength != null && fftLength > innerDimensionSize) {
        // Need to pad with zeros
        var zerosShape = input.shape.map(function (v) { return v; });
        zerosShape[input.shape.length - 1] = fftLength - innerDimensionSize;
        adjustedInput = input.concat(tensor_ops_1.zeros(zerosShape), input.shape.length - 1);
        innerDimensionSize = fftLength;
    }
    else {
        adjustedInput = input;
    }
    // Complement the input with zero imaginary numbers.
    var zerosInput = adjustedInput.zerosLike();
    var complexInput = complex_ops_1.complex(adjustedInput, zerosInput).as2D(batch, innerDimensionSize);
    var ret = exports.fft(complexInput);
    // Exclude complex conjugations. These conjugations are put symmetrically.
    var half = Math.floor(innerDimensionSize / 2) + 1;
    var realValues = complex_ops_1.real(ret);
    var imagValues = complex_ops_1.imag(ret);
    var realComplexConjugate = realValues.split([half, innerDimensionSize - half], realValues.shape.length - 1);
    var imagComplexConjugate = imagValues.split([half, innerDimensionSize - half], imagValues.shape.length - 1);
    var outputShape = adjustedInput.shape.slice();
    outputShape[adjustedInput.shape.length - 1] = half;
    return complex_ops_1.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]);
 * const imag = tf.tensor1d([0, 0, 0]);
 * const x = tf.complex(real, imag);
 *
 * x.irfft().print();
 * ```
 * @param input The real value input to compute an irfft over.
 */
/**
 * @doc {heading: 'Operations', subheading: 'Spectral', namespace: 'spectral'}
 */
function irfft_(input) {
    var innerDimensionSize = input.shape[input.shape.length - 1];
    var batch = input.size / innerDimensionSize;
    if (innerDimensionSize <= 2) {
        var complexInput = input.as2D(batch, innerDimensionSize);
        var ret = exports.ifft(complexInput);
        return complex_ops_1.real(ret);
    }
    else {
        // The length of unique components of the DFT of a real-valued signal
        // is 2 * (input_len - 1)
        var outputShape = [batch, 2 * (innerDimensionSize - 1)];
        var realInput = complex_ops_1.real(input).as2D(batch, innerDimensionSize);
        var imagInput = complex_ops_1.imag(input).as2D(batch, innerDimensionSize);
        var realConjugate = realInput.slice([0, 1], [batch, innerDimensionSize - 2]).reverse(1);
        var imagConjugate = imagInput.slice([0, 1], [batch, innerDimensionSize - 2])
            .reverse(1)
            .mul(tensor_ops_1.scalar(-1));
        var r = realInput.concat(realConjugate, 1);
        var i = imagInput.concat(imagConjugate, 1);
        var complexInput = complex_ops_1.complex(r, i).as2D(outputShape[0], outputShape[1]);
        var ret = exports.ifft(complexInput);
        return complex_ops_1.real(ret);
    }
}
exports.fft = operation_1.op({ fft_: fft_ });
exports.ifft = operation_1.op({ ifft_: ifft_ });
exports.rfft = operation_1.op({ rfft_: rfft_ });
exports.irfft = operation_1.op({ irfft_: irfft_ });
//# sourceMappingURL=spectral_ops.js.map