UNPKG

1.96 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2019 Google LLC. All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 * =============================================================================
16 */
17import { mul } from '../mul';
18import { op } from '../operation';
19import { enclosingPowerOfTwo } from '../signal_ops_util';
20import { rfft } from '../spectral/rfft';
21import { frame } from './frame';
22import { hannWindow } from './hann_window';
23/**
24 * Computes the Short-time Fourier Transform of signals
25 * See: https://en.wikipedia.org/wiki/Short-time_Fourier_transform
26 *
27 * ```js
28 * const input = tf.tensor1d([1, 1, 1, 1, 1])
29 * tf.signal.stft(input, 3, 1).print();
30 * ```
31 * @param signal 1-dimensional real value tensor.
32 * @param frameLength The window length of samples.
33 * @param frameStep The number of samples to step.
34 * @param fftLength The size of the FFT to apply.
35 * @param windowFn A callable that takes a window length and returns 1-d tensor.
36 *
37 * @doc {heading: 'Operations', subheading: 'Signal', namespace: 'signal'}
38 */
39function stft_(signal, frameLength, frameStep, fftLength, windowFn = hannWindow) {
40 if (fftLength == null) {
41 fftLength = enclosingPowerOfTwo(frameLength);
42 }
43 const framedSignal = frame(signal, frameLength, frameStep);
44 const windowedSignal = mul(framedSignal, windowFn(frameLength));
45 return rfft(windowedSignal, fftLength);
46}
47export const stft = op({ stft_ });
48//# sourceMappingURL=stft.js.map
\No newline at end of file