← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
audio_istft_complex_window_checked.c File Reference
#include "checked_float_buffer.h"
#include "bf16_utils.h"
#include <string.h>

Go to the source code of this file.

Functions

int audio_istft_complex_window_bf16_envelope_f32_checked (const float *real, const float *imag, size_t spectral_elements, const float *window, size_t window_elements, size_t frames, size_t n_fft, size_t hop, size_t left_crop, size_t right_crop, float *output, size_t output_elements, double *scratch, size_t scratch_elements)
int audio_istft_complex_window_f32_checked (const float *real, const float *imag, size_t spectral_elements, const float *window, size_t window_elements, size_t frames, size_t n_fft, size_t hop, size_t left_crop, size_t right_crop, float *output, size_t output_elements, double *scratch, size_t scratch_elements)
int audio_istft_complex_window_plan (size_t frames, size_t n_fft, size_t hop, size_t left_crop, size_t right_crop, size_t *spectrum, size_t *samples, size_t *scratch)
static int inverse_transform (const float *real, const float *imag, size_t spectral_elements, const float *window, size_t window_elements, size_t frames, size_t n_fft, size_t hop, size_t left_crop, size_t right_crop, float *output, size_t output_elements, double *scratch, size_t scratch_elements, int bf16_envelope)

Function Documentation

◆ audio_istft_complex_window_bf16_envelope_f32_checked()

int audio_istft_complex_window_bf16_envelope_f32_checked ( const float * real,
const float * imag,
size_t spectral_elements,
const float * window,
size_t window_elements,
size_t frames,
size_t n_fft,
size_t hop,
size_t left_crop,
size_t right_crop,
float * output,
size_t output_elements,
double * scratch,
size_t scratch_elements )

Definition at line 114 of file audio_istft_complex_window_checked.c.

119{
120 return inverse_transform(real, imag, spectral_elements, window, window_elements,
121 frames, n_fft, hop, left_crop, right_crop, output, output_elements,
122 scratch, scratch_elements, 1);
123}
static int inverse_transform(const float *real, const float *imag, size_t spectral_elements, const float *window, size_t window_elements, size_t frames, size_t n_fft, size_t hop, size_t left_crop, size_t right_crop, float *output, size_t output_elements, double *scratch, size_t scratch_elements, int bf16_envelope)

References inverse_transform().

◆ audio_istft_complex_window_f32_checked()

int audio_istft_complex_window_f32_checked ( const float * real,
const float * imag,
size_t spectral_elements,
const float * window,
size_t window_elements,
size_t frames,
size_t n_fft,
size_t hop,
size_t left_crop,
size_t right_crop,
float * output,
size_t output_elements,
double * scratch,
size_t scratch_elements )

Definition at line 103 of file audio_istft_complex_window_checked.c.

108{
109 return inverse_transform(real, imag, spectral_elements, window, window_elements,
110 frames, n_fft, hop, left_crop, right_crop, output, output_elements,
111 scratch, scratch_elements, 0);
112}

References inverse_transform().

◆ audio_istft_complex_window_plan()

int audio_istft_complex_window_plan ( size_t frames,
size_t n_fft,
size_t hop,
size_t left_crop,
size_t right_crop,
size_t * spectrum,
size_t * samples,
size_t * scratch )

Definition at line 9 of file audio_istft_complex_window_checked.c.

12{
13 if (!spectrum || !samples || !scratch || !frames || n_fft < 2 ||
14 n_fft > 8192 || (n_fft & 1) || !hop || hop > n_fft ||
15 frames - 1 > (SIZE_MAX - n_fft) / hop) return -2;
16 size_t padded = (frames - 1) * hop + n_fft;
17 size_t bins = n_fft / 2 + 1;
18 if (left_crop >= padded || right_crop >= padded - left_crop ||
19 frames > SIZE_MAX / bins || padded > SIZE_MAX / 2 ||
20 n_fft > SIZE_MAX - 2 * padded) return -2;
21 size_t ns = frames * bins, nw = 2 * padded + n_fft;
22 if (ns > SIZE_MAX / sizeof(float) || nw > SIZE_MAX / sizeof(double)) return -2;
23 *spectrum = ns; *samples = padded - left_crop - right_crop; *scratch = nw;
24 return 0;
25}

Referenced by inverse_transform().

◆ inverse_transform()

int inverse_transform ( const float * real,
const float * imag,
size_t spectral_elements,
const float * window,
size_t window_elements,
size_t frames,
size_t n_fft,
size_t hop,
size_t left_crop,
size_t right_crop,
float * output,
size_t output_elements,
double * scratch,
size_t scratch_elements,
int bf16_envelope )
static

Definition at line 27 of file audio_istft_complex_window_checked.c.

33{
34 size_t ns, no, nw;
35 if (audio_istft_complex_window_plan(frames, n_fft, hop, left_crop, right_crop,
36 &ns, &no, &nw) || spectral_elements < ns || window_elements < n_fft ||
37 output_elements < no || scratch_elements < nw) return -2;
38 if (!ck_checked_region(real, ns) || !ck_checked_region(imag, ns) ||
39 !ck_checked_region(window, n_fft) || !ck_checked_region(output, no) ||
40 !scratch || (uintptr_t)scratch % _Alignof(double) ||
41 (uintptr_t)scratch > UINTPTR_MAX - nw * sizeof(double)) return -1;
42 const float *buffers[] = {real, imag, window, output};
43 const size_t lengths[] = {ns, ns, n_fft, no};
44 uintptr_t begin = (uintptr_t)scratch, end = begin + nw * sizeof(double);
45 for (size_t i = 0; i < 4; ++i) {
46 uintptr_t b = (uintptr_t)buffers[i], e = b + lengths[i] * sizeof(float);
47 if (begin < e && b < end) return -1;
48 if (i < 3 && ck_checked_overlap(buffers[i], lengths[i], output, no)) return -1;
49 }
50 if (!ck_checked_finite(real, ns) || !ck_checked_finite(imag, ns) ||
51 !ck_checked_finite(window, n_fft)) return -3;
52 if (bf16_envelope)
53 for (size_t n = 0; n < n_fft; ++n)
54 if (bf16_to_float(float_to_bf16(window[n])) != window[n]) return -3;
55 const size_t padded = no + left_crop + right_crop;
56 double *sum = scratch, *envelope = scratch + padded, *frame = envelope + padded;
57 memset(scratch, 0, 2 * padded * sizeof(double));
58 const double tau = 6.283185307179586476925286766559;
59 const size_t bins = n_fft / 2 + 1;
60 for (size_t t = 0; t < frames; ++t) {
61 size_t base = t * bins;
62 for (size_t n = 0; n < n_fft; ++n)
63 frame[n] = (double)real[base] + ((n & 1) ? -(double)real[base + bins - 1] : (double)real[base + bins - 1]);
64 for (size_t k = 1; k + 1 < bins; ++k) {
65 double angle = tau * (double)k / (double)n_fft;
66 double step_c = cos(angle), step_s = sin(angle), c = 1.0, s = 0.0;
67 for (size_t n = 0; n < n_fft; ++n) {
68 frame[n] += 2.0 * ((double)real[base + k] * c - (double)imag[base + k] * s);
69 double next_c = c * step_c - s * step_s;
70 s = s * step_c + c * step_s; c = next_c;
71 }
72 }
73 for (size_t n = 0; n < n_fft; ++n) {
74 double w = window[n]; size_t at = t * hop + n;
75 sum[at] += frame[n] / (double)n_fft * w;
76 if (!bf16_envelope) envelope[at] += w * w;
77 }
78 }
79 /* PyTorch CPU fold walks window offsets ascending. For one destination
80 * sample that is descending contributing frame order; BF16 stores make
81 * this distinction observable. Preserve square and each add rounding. */
82 if (bf16_envelope) {
83 for (size_t t = frames; t-- > 0;) {
84 for (size_t n = 0; n < n_fft; ++n) {
85 size_t at = t * hop + n;
86 float squared = bf16_to_float(float_to_bf16(window[n] * window[n]));
87 envelope[at] = bf16_to_float(float_to_bf16((float)envelope[at] + squared));
88 }
89 }
90 }
91 for (size_t n = 0; n < no; ++n) {
92 size_t at = n + left_crop;
93 if (!(envelope[at] > 1e-11) || !isfinite(envelope[at]) ||
94 !isfinite((float)(sum[at] / envelope[at]))) return -3;
95 }
96 for (size_t n = 0; n < no; ++n) {
97 size_t at = n + left_crop;
98 output[n] = (float)(sum[at] / envelope[at]);
99 }
100 return 0;
101}
int audio_istft_complex_window_plan(size_t frames, size_t n_fft, size_t hop, size_t left_crop, size_t right_crop, size_t *spectrum, size_t *samples, size_t *scratch)
static uint16_t float_to_bf16(float f)
Definition bf16_utils.h:90
static float bf16_to_float(uint16_t v)
Definition bf16_utils.h:38
static int ck_checked_finite(const float *ptr, size_t n)
static int ck_checked_region(const float *ptr, size_t n)
static int ck_checked_overlap(const float *a, size_t na, const float *b, size_t nb)
static int buffers(const void **inputs, const size_t *sizes, size_t count, float *out, size_t on, float *scratch, size_t sn, size_t needed)
uint32_t end
Definition utf8.c:215

References audio_istft_complex_window_plan(), bf16_to_float(), buffers(), ck_checked_finite(), ck_checked_overlap(), ck_checked_region(), end, and float_to_bf16().

Referenced by audio_istft_complex_window_bf16_envelope_f32_checked(), and audio_istft_complex_window_f32_checked().