Skip to content

Commit 71fc813

Browse files
committed
test(stft): add return_complex=false and win_length < n_fft alignment tests
- write_stft_result_to_file: support kFloat/kDouble (real tensor from return_complex=false) - Add ReturnComplexFalse test: validates [batch, freq, frames, 2] shape output - Add WinLengthSmallerThanNFFT test: validates zero-pad window behavior
1 parent c5236d0 commit 71fc813

1 file changed

Lines changed: 47 additions & 1 deletion

File tree

test/ATen/ops/StftTest.cpp

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <ATen/ATen.h>
22
#include <ATen/core/Tensor.h>
33
#include <ATen/ops/ones.h>
4-
#include <ATen/ops/stft.h>
54
#include <gtest/gtest.h>
65

76
#include <cmath>
@@ -46,6 +45,16 @@ static void write_stft_result_to_file(FileManerger* file,
4645
std::complex<double>* data =
4746
reinterpret_cast<std::complex<double>*>(contig.data_ptr());
4847
*file << std::to_string(data[f * freq_bins].real()) << " ";
48+
} else if (contig.scalar_type() == at::kFloat) {
49+
// return_complex=false: shape [..., freq, frames, 2], last dim = [real,
50+
// imag]
51+
float* data = reinterpret_cast<float*>(contig.data_ptr());
52+
int64_t stride = freq_bins * 2;
53+
*file << std::to_string(data[f * stride]) << " ";
54+
} else if (contig.scalar_type() == at::kDouble) {
55+
double* data = reinterpret_cast<double*>(contig.data_ptr());
56+
int64_t stride = freq_bins * 2;
57+
*file << std::to_string(data[f * stride]) << " ";
4958
} else {
5059
*file << "unsupported_dtype ";
5160
}
@@ -214,5 +223,42 @@ TEST_F(StftTest, DifferentHopLength) {
214223
file.saveFile();
215224
}
216225

226+
TEST_F(StftTest, ReturnComplexFalse) {
227+
auto file_name = g_custom_param.get();
228+
FileManerger file(file_name);
229+
file.openAppend();
230+
file << "ReturnComplexFalse ";
231+
at::Tensor result = test_tensor.stft(
232+
/*n_fft=*/8,
233+
/*hop_length=*/4,
234+
/*win_length=*/8,
235+
/*window=*/::std::nullopt,
236+
/*normalized=*/false,
237+
/*onesided=*/true,
238+
/*return_complex=*/false);
239+
write_stft_result_to_file(&file, result);
240+
file << "\n";
241+
file.saveFile();
242+
}
243+
244+
TEST_F(StftTest, WinLengthSmallerThanNFFT) {
245+
auto file_name = g_custom_param.get();
246+
FileManerger file(file_name);
247+
file.openAppend();
248+
file << "WinLengthSmallerThanNFFT ";
249+
at::Tensor window = at::ones({4}, at::kFloat);
250+
at::Tensor result = test_tensor.stft(
251+
/*n_fft=*/8,
252+
/*hop_length=*/4,
253+
/*win_length=*/4,
254+
/*window=*/window,
255+
/*normalized=*/false,
256+
/*onesided=*/true,
257+
/*return_complex=*/true);
258+
write_stft_result_to_file(&file, result);
259+
file << "\n";
260+
file.saveFile();
261+
}
262+
217263
} // namespace test
218264
} // namespace at

0 commit comments

Comments
 (0)