MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ apply_spectral()

std::vector< double > MayaFlux::Kinesis::Discrete::apply_spectral ( std::span< const double >  src,
uint32_t  window_size,
uint32_t  hop_size,
const SpectrumProcessor processor 
)

Apply a per-frame spectrum processor via WOLA analysis-synthesis.

Frames the input with a Hann window, calls processor on each frame's one-sided spectrum, reconstructs via IFFT, and accumulates with WOLA normalisation. Output length equals input length.

Parameters
srcInput samples
window_sizeFFT frame size (power of 2, >= 64)
hop_sizeHop between analysis frames
processorPer-frame spectrum callback
Returns
Processed output, same length as src

Definition at line 59 of file Spectral.cpp.

64{
65 if (src.empty())
66 return {};
67
68 const uint32_t N = window_size;
69 const uint32_t bins = N / 2 + 1;
70 const std::vector<double> win = make_hann(N);
71
72 const size_t n_frames = (src.size() >= N)
73 ? (src.size() - N) / hop_size + 1
74 : 1;
75
76 std::vector<double> output(src.size(), 0.0);
77 std::vector<double> norm(src.size(), 0.0);
78
79 Eigen::FFT<double> fft;
80 Eigen::VectorXd frame(N);
81 Eigen::VectorXcd spectrum;
82 Eigen::VectorXd ifft_out;
83
84 for (size_t f = 0; f < n_frames; ++f) {
85 const size_t pos = f * hop_size;
86
87 for (uint32_t k = 0; k < N; ++k) {
88 const size_t idx = pos + k;
89 frame(static_cast<Eigen::Index>(k)) = (idx < src.size()) ? src[idx] * win[k] : 0.0;
90 }
91
92 fft.fwd(spectrum, frame);
93
94 std::vector<std::complex<double>> onesided(bins);
95 for (uint32_t b = 0; b < bins; ++b)
96 onesided[b] = spectrum(static_cast<Eigen::Index>(b));
97
98 processor(onesided, f);
99
100 Eigen::VectorXcd full = to_full_spectrum(onesided, N);
101 fft.inv(ifft_out, full);
102
103 const double inv_N = 1.0 / static_cast<double>(N);
104 for (uint32_t k = 0; k < N && pos + k < src.size(); ++k) {
105 const double w = win[k];
106 output[pos + k] += ifft_out(static_cast<Eigen::Index>(k)) * inv_N * w;
107 norm[pos + k] += w * w;
108 }
109 }
110
111 for (size_t i = 0; i < src.size(); ++i) {
112 if (norm[i] > 1e-10)
113 output[i] /= norm[i];
114 }
115
116 return output;
117}
#define N(method_name, full_type_name)
Definition Creator.hpp:183
size_t b

References b, and N.

Referenced by harmonic_enhance(), spectral_filter(), spectral_gate(), and spectral_invert().

+ Here is the caller graph for this function: