VCV Rack API v2
Loading...
Searching...
No Matches
fft.hpp
Go to the documentation of this file.
1#pragma once
2#include <pffft.h>
3
4#include <dsp/common.hpp>
5
6
7namespace rack {
8namespace dsp {
9
10
16struct RealFFT {
17 PFFFT_Setup* setup;
18 int length;
19
20 RealFFT(size_t length) {
21 this->length = length;
22 setup = pffft_new_setup(length, PFFFT_REAL);
23 }
24
26 pffft_destroy_setup(setup);
27 }
28
36 void rfftUnordered(const float* input, float* output) {
37 pffft_transform(setup, input, output, NULL, PFFFT_FORWARD);
38 }
39
44 void irfftUnordered(const float* input, float* output) {
45 pffft_transform(setup, input, output, NULL, PFFFT_BACKWARD);
46 }
47
59 void rfft(const float* input, float* output) {
60 pffft_transform_ordered(setup, input, output, NULL, PFFFT_FORWARD);
61 }
62
63 void irfft(const float* input, float* output) {
64 pffft_transform_ordered(setup, input, output, NULL, PFFFT_BACKWARD);
65 }
66
69 void scale(float* x) {
70 float a = 1.f / length;
71 for (int i = 0; i < length; i++) {
72 x[i] *= a;
73 }
74 }
75};
76
77
81struct ComplexFFT {
82 PFFFT_Setup* setup;
83 int length;
84
86 this->length = length;
87 setup = pffft_new_setup(length, PFFFT_COMPLEX);
88 }
89
91 pffft_destroy_setup(setup);
92 }
93
98 void fftUnordered(const float* input, float* output) {
99 pffft_transform(setup, input, output, NULL, PFFFT_FORWARD);
100 }
101
106 void ifftUnordered(const float* input, float* output) {
107 pffft_transform(setup, input, output, NULL, PFFFT_BACKWARD);
108 }
109
110 void fft(const float* input, float* output) {
111 pffft_transform_ordered(setup, input, output, NULL, PFFFT_FORWARD);
112 }
113
114 void ifft(const float* input, float* output) {
115 pffft_transform_ordered(setup, input, output, NULL, PFFFT_BACKWARD);
116 }
117
118 void scale(float* x) {
119 float a = 1.f / length;
120 for (int i = 0; i < length; i++) {
121 x[2 * i + 0] *= a;
122 x[2 * i + 1] *= a;
123 }
124 }
125};
126
127
128} // namespace dsp
129} // namespace rack
Root namespace for the Rack API.
Definition AudioDisplay.hpp:9
Complex-valued FFT context.
Definition fft.hpp:81
~ComplexFFT()
Definition fft.hpp:90
void fftUnordered(const float *input, float *output)
Performs the complex FFT.
Definition fft.hpp:98
void ifftUnordered(const float *input, float *output)
Performs the inverse complex FFT.
Definition fft.hpp:106
void fft(const float *input, float *output)
Definition fft.hpp:110
PFFFT_Setup * setup
Definition fft.hpp:82
ComplexFFT(size_t length)
Definition fft.hpp:85
int length
Definition fft.hpp:83
void ifft(const float *input, float *output)
Definition fft.hpp:114
void scale(float *x)
Definition fft.hpp:118
Real-valued FFT context.
Definition fft.hpp:16
void rfftUnordered(const float *input, float *output)
Performs the real FFT.
Definition fft.hpp:36
~RealFFT()
Definition fft.hpp:25
int length
Definition fft.hpp:18
RealFFT(size_t length)
Definition fft.hpp:20
void rfft(const float *input, float *output)
Slower than the above methods, but returns results in the "canonical" FFT order as follows.
Definition fft.hpp:59
void scale(float *x)
Scales the RFFT so that scale(IFFT(FFT(x))) = x.
Definition fft.hpp:69
PFFFT_Setup * setup
Definition fft.hpp:17
void irfftUnordered(const float *input, float *output)
Performs the inverse real FFT.
Definition fft.hpp:44
void irfft(const float *input, float *output)
Definition fft.hpp:63