aboutsummaryrefslogtreecommitdiff
path: root/rfft.h
blob: 7cb3c509084c9ae5736a37db7ede143b5cefeb62 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* Windowed real FFT for the short-time transform stage.
 *
 * The model's stored transform is a dense [2*bins][n_fft] basis whose rows are
 * w[n]cos(2*pi*f*n/n_fft) and -w[n]sin(2*pi*f*n/n_fft) with w the periodic Hann
 * window (tools/export_weights.py verifies this on every export). The stage
 * therefore computes exactly the magnitude spectrum of the windowed frame, and
 * an FFT computes the same quantity in O(n log n) instead of O(n * bins):
 * 264K multiply-accumulates per 16 kHz chunk become about 24K floating point
 * operations. It is the same transform, not an approximation of it, so only
 * rounding differs.
 *
 * A real transform of length n_fft is evaluated as a complex transform of
 * length n_fft/2 over the even/odd split, followed by the standard untangle
 * step. Only magnitudes are returned, so the sign convention of the imaginary
 * part is irrelevant. */

#ifndef SVAD_RFFT_H
#define SVAD_RFFT_H

#include <stdint.h>

#define SVAD_RFFT_MAX_N 128 /* complex length, = max n_fft / 2 */

/* Scratch floats svad_rfft_mag needs; sized for the batched SIMD path. */
#define SVAD_RFFT_SCRATCH (8 * SVAD_RFFT_MAX_N)

typedef struct {
    int n;                                 /* complex transform length, n_fft / 2 */
    int simd;                              /* use the batched SIMD path when available */
    uint16_t rev[SVAD_RFFT_MAX_N];         /* bit-reversal permutation */
    float tw[2 * SVAD_RFFT_MAX_N];         /* per-stage butterfly twiddles */
    float post[2 * (SVAD_RFFT_MAX_N + 1)]; /* untangle twiddles */
} svad_rfft;

void svad_rfft_init(svad_rfft *f, int n_fft, int simd);

/* Magnitude spectra of `frames` windows taken from x at the given hop, each
 * multiplied by `window` first. Writes frames * (n_fft/2 + 1) magnitudes as
 * out[t * out_stride + bin]. */
void svad_rfft_mag(const svad_rfft *f,
                   const float *x,
                   int hop,
                   int frames,
                   const float *window,
                   float *out,
                   int out_stride,
                   float *scratch);

#endif /* SVAD_RFFT_H */