UNPKG

6.86 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2020 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 * as tf from '../../index';
18import { ALL_ENVS, describeWithFlags } from '../../jasmine_util';
19import { expectArraysClose } from '../../test_util';
20describeWithFlags('stft', ALL_ENVS, () => {
21 it('3 length with hann window', async () => {
22 const input = tf.tensor1d([1, 1, 1, 1, 1]);
23 const frameLength = 3;
24 const frameStep = 1;
25 const output = tf.signal.stft(input, frameLength, frameStep);
26 expect(output.shape).toEqual([3, 3]);
27 expectArraysClose(await output.data(), [
28 1.0,
29 0.0,
30 0.0,
31 -1.0,
32 -1.0,
33 0.0,
34 1.0,
35 0.0,
36 0.0,
37 -1.0,
38 -1.0,
39 0.0,
40 1.0,
41 0.0,
42 0.0,
43 -1.0,
44 -1.0,
45 0.0,
46 ]);
47 });
48 it('3 length with hann window (sequencial number)', async () => {
49 const input = tf.tensor1d([1, 2, 3, 4, 5]);
50 const frameLength = 3;
51 const frameStep = 1;
52 const output = tf.signal.stft(input, frameLength, frameStep);
53 expect(output.shape).toEqual([3, 3]);
54 expectArraysClose(await output.data(), [
55 2.0, 0.0, 0.0, -2.0, -2.0, 0.0, 3.0, 0.0, 0.0, -3.0, -3.0, 0.0, 4.0, 0.0,
56 0.0, -4.0, -4.0, 0.0
57 ]);
58 });
59 it('3 length, 2 step with hann window', async () => {
60 const input = tf.tensor1d([1, 1, 1, 1, 1]);
61 const frameLength = 3;
62 const frameStep = 2;
63 const output = tf.signal.stft(input, frameLength, frameStep);
64 expect(output.shape).toEqual([2, 3]);
65 expectArraysClose(await output.data(), [1.0, 0.0, 0.0, -1.0, -1.0, 0.0, 1.0, 0.0, 0.0, -1.0, -1.0, 0.0]);
66 });
67 it('3 fftLength, 5 frameLength, 2 step', async () => {
68 const input = tf.tensor1d([1, 1, 1, 1, 1, 1]);
69 const frameLength = 5;
70 const frameStep = 1;
71 const fftLength = 3;
72 const output = tf.signal.stft(input, frameLength, frameStep, fftLength);
73 expect(output.shape[0]).toEqual(2);
74 expectArraysClose(await output.data(), [1.5, 0.0, -0.749999, 0.433, 1.5, 0.0, -0.749999, 0.433]);
75 });
76 it('5 length with hann window', async () => {
77 const input = tf.tensor1d([1, 1, 1, 1, 1]);
78 const frameLength = 5;
79 const frameStep = 1;
80 const output = tf.signal.stft(input, frameLength, frameStep);
81 expect(output.shape).toEqual([1, 5]);
82 expectArraysClose(await output.data(), [2.0, 0.0, 0.0, -1.7071068, -1.0, 0.0, 0.0, 0.29289323, 0.0, 0.0]);
83 });
84 it('5 length with hann window (sequential)', async () => {
85 const input = tf.tensor1d([1, 2, 3, 4, 5]);
86 const frameLength = 5;
87 const frameStep = 1;
88 const output = tf.signal.stft(input, frameLength, frameStep);
89 expect(output.shape).toEqual([1, 5]);
90 expectArraysClose(await output.data(), [
91 6.0, 0.0, -0.70710677, -5.1213202, -3.0, 1.0, 0.70710677, 0.87867975, 0.0,
92 0.0
93 ]);
94 });
95 it('3 length with hamming window', async () => {
96 const input = tf.tensor1d([1, 1, 1, 1, 1]);
97 const frameLength = 3;
98 const frameStep = 1;
99 const fftLength = 3;
100 const output = tf.signal.stft(input, frameLength, frameStep, fftLength, (length) => tf.signal.hammingWindow(length));
101 expect(output.shape).toEqual([3, 2]);
102 expectArraysClose(await output.data(), [
103 1.16, 0.0, -0.46, -0.79674333, 1.16, 0.0, -0.46, -0.79674333, 1.16, 0.0,
104 -0.46, -0.79674333
105 ]);
106 });
107 it('3 length, 2 step with hamming window', async () => {
108 const input = tf.tensor1d([1, 1, 1, 1, 1]);
109 const frameLength = 3;
110 const frameStep = 2;
111 const fftLength = 3;
112 const output = tf.signal.stft(input, frameLength, frameStep, fftLength, (length) => tf.signal.hammingWindow(length));
113 expect(output.shape).toEqual([2, 2]);
114 expectArraysClose(await output.data(), [1.16, 0.0, -0.46, -0.79674333, 1.16, 0.0, -0.46, -0.79674333]);
115 });
116 it('3 fftLength, 5 frameLength, 2 step with hamming window', async () => {
117 const input = tf.tensor1d([1, 1, 1, 1, 1, 1]);
118 const frameLength = 5;
119 const frameStep = 1;
120 const fftLength = 3;
121 const output = tf.signal.stft(input, frameLength, frameStep, fftLength, (length) => tf.signal.hammingWindow(length));
122 expect(output.shape).toEqual([2, 2]);
123 expectArraysClose(await output.data(), [1.619999, 0.0, -0.69, 0.39837, 1.619999, 0.0, -0.69, 0.39837]);
124 });
125 it('5 length with hann window (sequential)', async () => {
126 const input = tf.tensor1d([1, 2, 3, 4, 5]);
127 const frameLength = 5;
128 const frameStep = 1;
129 const fftLength = 5;
130 const output = tf.signal.stft(input, frameLength, frameStep, fftLength, (length) => tf.signal.hammingWindow(length));
131 expect(output.shape).toEqual([1, 3]);
132 expectArraysClose(await output.data(), [6.72, 0.0, -3.6371822, -1.1404576, 0.4771822, 0.39919350]);
133 });
134 it('3 length without window function', async () => {
135 const input = tf.tensor1d([1, 1, 1, 1, 1]);
136 const frameLength = 3;
137 const frameStep = 1;
138 const fftLength = 3;
139 const ident = (length) => tf.ones([length]).as1D();
140 const output = tf.signal.stft(input, frameLength, frameStep, fftLength, ident);
141 expect(output.shape).toEqual([3, 2]);
142 expectArraysClose(await output.data(), [3.0, 0.0, 0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 3.0, 0.0, 0.0, 0.0]);
143 });
144 it('3 length, 2 step without window function', async () => {
145 const input = tf.tensor1d([1, 1, 1, 1, 1]);
146 const frameLength = 3;
147 const frameStep = 2;
148 const fftLength = 3;
149 const ident = (length) => tf.ones([length]).as1D();
150 const output = tf.signal.stft(input, frameLength, frameStep, fftLength, ident);
151 expect(output.shape).toEqual([2, 2]);
152 expectArraysClose(await output.data(), [3.0, 0.0, 0.0, 0.0, 3.0, 0.0, 0.0, 0.0]);
153 });
154});
155//# sourceMappingURL=stft_test.js.map
\No newline at end of file