Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[ISTFT][Op][CPU][Ref] Audio results noise reduction (#29475)

Katarzyna Mitrus5e12f91a6cbe
diff --git a/src/core/reference/src/op/istft.cpp b/src/core/reference/src/op/istft.cpp
index fe4eaf0bf93..d22ab97668e 100644
--- a/src/core/reference/src/op/istft.cpp
+++ b/src/core/reference/src/op/istft.cpp
@@ -51,6 +51,10 @@ void istft(const float* in_data,
const auto window_length = window_shape[0] < frame_size_dim ? window_shape[0] : frame_size_dim;
std::vector<float> pad_window(frame_size, 0);
std::copy(window, window + window_shape[0], pad_window.begin() + (frame_size_dim - window_length) / 2);
+ std::vector<float> pow_window(frame_size, 0);
+ std::transform(pad_window.begin(), pad_window.end(), pow_window.begin(), [](float win_val) {
+ return win_val * win_val;
+ });
std::vector<float> data_t(in_data, in_data + shape_size(data_shape));
const auto stft_transp_out_shape = Shape{batch_size, num_frames, fft_out_shape[0], fft_out_shape[1]};
@@ -94,9 +98,7 @@ void istft(const float* in_data,
for (size_t frame_idx = 0; frame_idx < num_frames; ++frame_idx) {
const auto in_frame_start = batch_in_start + frame_idx * fft_out_shape_size;
const auto in_frame_end = in_frame_start + fft_out_shape_size;
-
const auto out_frame_start = batch_out_start + frame_idx * frame_step;
- const auto out_frame_end = out_frame_start + frame_size;
std::vector<float> frame_data(data_t.data() + in_frame_start, data_t.data() + in_frame_end);
reference::irdft(frame_data,
@@ -107,17 +109,13 @@ void istft(const float* in_data,
frame_size_dim_shape,
frame_size);
- std::transform(frame_signal.begin(),
- frame_signal.end(),
- mid_result.begin() + out_frame_start,
- mid_result.begin() + out_frame_start,
- func::add<float>);
-
- std::transform(window_sum.begin() + out_frame_start,
- window_sum.begin() + out_frame_end,
- pad_window.begin(),
- window_sum.begin() + out_frame_start,
- func::add<float>);
+ // Overlap Add
+ float* mid_result_sum = mid_result.data() + out_frame_start;
+ float* window_frame_sum = window_sum.data() + out_frame_start;
+ for (size_t i = 0; i < frame_signal.size(); ++i) {
+ mid_result_sum[i] += frame_signal[i] * pad_window[i];
+ window_frame_sum[i] += pow_window[i];
+ }
}
std::transform(result, result + signal_length, window_sum.begin() + batch_out_start, result, postprocess_func);
diff --git a/src/plugins/intel_cpu/src/nodes/istft.cpp b/src/plugins/intel_cpu/src/nodes/istft.cpp
index 0014bc11a06..cdac83afb4f 100644
--- a/src/plugins/intel_cpu/src/nodes/istft.cpp
+++ b/src/plugins/intel_cpu/src/nodes/istft.cpp
@@ -136,13 +136,15 @@ void istft_impl(const float* in_data,
OPENVINO_ASSERT(fft_results_dim == static_cast<size_t>((frame_size / 2) + 1));
const auto frame_size_dim = static_cast<size_t>(frame_size);
- const auto frame_size_dim_shape = ov::Shape{frame_size_dim};
- const auto frame_size_dim_shape_out = ov::Shape{frame_size_dim, 2};
const auto fft_out_shape = ov::Shape{fft_results_dim, 2};
const auto window_length = window_shape[0] < frame_size_dim ? window_shape[0] : frame_size_dim;
std::vector<float> pad_window(frame_size, 0);
std::copy(window, window + window_shape[0], pad_window.begin() + (frame_size_dim - window_length) / 2);
+ std::vector<float> pow_window(frame_size, 0);
+ std::transform(pad_window.begin(), pad_window.end(), pow_window.begin(), [](float win_val) {
+ return win_val * win_val;
+ });
std::vector<float> data_t(shape_size(data_shape));
const auto stft_transp_out_shape = ov::Shape{batch_size, num_frames, fft_out_shape[0], fft_out_shape[1]};
@@ -187,9 +189,7 @@ void istft_impl(const float* in_data,
size_t batch_out_start = batch * signal_length;
const auto in_frame_start = batch_in_start + frame_idx * fft_out_shape_size;
-
const auto out_frame_start = batch_out_start + frame_idx * frame_step;
- const auto out_frame_end = out_frame_start + frame_size;
std::vector<float> frame_signal(frame_size);
rdft_executor->execute(data_t.data() + in_frame_start,
@@ -203,17 +203,13 @@ void istft_impl(const float* in_data,
{1},
{1});
- std::transform(frame_signal.begin(),
- frame_signal.end(),
- mid_result.begin() + out_frame_start,
- mid_result.begin() + out_frame_start,
- std::plus<>());
-
- std::transform(window_sum.begin() + out_frame_start,
- window_sum.begin() + out_frame_end,
- pad_window.begin(),
- window_sum.begin() + out_frame_start,
- std::plus<>());
+ // Overlap Add
+ float* mid_result_sum = mid_result.data() + out_frame_start;
+ float* window_frame_sum = window_sum.data() + out_frame_start;
+ for (size_t i = 0; i < frame_signal.size(); ++i) {
+ mid_result_sum[i] += frame_signal[i] * pad_window[i];
+ window_frame_sum[i] += pow_window[i];
+ }
}
float* result = mid_result.data() + (batch * signal_length);
std::transform(result,
@@ -221,7 +217,6 @@ void istft_impl(const float* in_data,
window_sum.begin() + batch * signal_length,
result,
postprocess_func);
-
const auto result_start = result + margin;
std::copy(result_start, result_start + copy_end, final_result + batch * final_signal_length);
});