1 //! FFT and RDFT implementation.
2 use std::f32::{self, consts};
3 use std::ops::{Not, Neg, Add, AddAssign, Sub, SubAssign, Mul, MulAssign, Div};
8 #[derive(Debug,Clone,Copy,PartialEq)]
9 pub struct FFTComplex {
10 /// Real part of the numner.
12 /// Complex part of the number.
17 /// Calculates `exp(i * val)`.
18 pub fn exp(val: f32) -> Self {
19 FFTComplex { re: val.cos(), im: val.sin() }
21 /// Returns `-Im + i * Re`.
22 pub fn rotate(self) -> Self {
23 FFTComplex { re: -self.im, im: self.re }
25 /// Multiplies complex number by scalar.
26 pub fn scale(self, scale: f32) -> Self {
27 FFTComplex { re: self.re * scale, im: self.im * scale }
29 /// Returns squared modulus value of the complex number.
30 pub fn sq_modulus(self) -> f32 {
31 self.re * self.re + self.im * self.im
33 /// Returns reciprocal of the complex number.
34 pub fn reciprocal(self) -> Self {
35 !self.scale(self.sq_modulus())
39 impl Neg for FFTComplex {
40 type Output = FFTComplex;
41 fn neg(self) -> Self::Output {
42 FFTComplex { re: -self.re, im: -self.im }
46 impl Not for FFTComplex {
47 type Output = FFTComplex;
48 fn not(self) -> Self::Output {
49 FFTComplex { re: self.re, im: -self.im }
53 impl Add for FFTComplex {
54 type Output = FFTComplex;
55 fn add(self, other: Self) -> Self::Output {
56 FFTComplex { re: self.re + other.re, im: self.im + other.im }
60 impl AddAssign for FFTComplex {
61 fn add_assign(&mut self, other: Self) {
67 impl Sub for FFTComplex {
68 type Output = FFTComplex;
69 fn sub(self, other: Self) -> Self::Output {
70 FFTComplex { re: self.re - other.re, im: self.im - other.im }
74 impl SubAssign for FFTComplex {
75 fn sub_assign(&mut self, other: Self) {
81 impl Mul for FFTComplex {
82 type Output = FFTComplex;
83 fn mul(self, other: Self) -> Self::Output {
84 FFTComplex { re: self.re * other.re - self.im * other.im,
85 im: self.im * other.re + self.re * other.im }
89 impl MulAssign for FFTComplex {
90 fn mul_assign(&mut self, other: Self) {
91 let re = self.re * other.re - self.im * other.im;
92 let im = self.im * other.re + self.re * other.im;
98 impl Div for FFTComplex {
99 type Output = FFTComplex;
100 fn div(self, other: Self) -> Self::Output {
101 self * other.reciprocal()
105 impl fmt::Display for FFTComplex {
106 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
107 write!(f, "({}, {})", self.re, self.im)
111 /// Complex number with zero value.
112 pub const FFTC_ZERO: FFTComplex = FFTComplex { re: 0.0, im: 0.0 };
114 /// Calculates forward or inverse FFT in the straightforward way.
115 pub fn generic_fft(data: &mut [FFTComplex], forward: bool) {
116 let mut tmp = Vec::with_capacity(data.len());
117 tmp.resize(data.len(), FFTC_ZERO);
118 let base = if forward { -consts::PI * 2.0 / (data.len() as f32) }
119 else { consts::PI * 2.0 / (data.len() as f32) };
120 for k in 0..data.len() {
121 let mut sum = FFTC_ZERO;
122 for n in 0..data.len() {
123 let w = FFTComplex::exp(base * ((n * k) as f32));
128 for k in 0..data.len() {
134 table: Vec<FFTComplex>,
135 tmp: Vec<FFTComplex>,
136 twiddle: Vec<FFTComplex>,
144 const FFT3_CONST: f32 = 0.86602540378443864677;
145 const FFT5_CONST1: FFTComplex = FFTComplex { re: 0.80901699437494742410, im: 0.58778525229247312915 };
146 const FFT5_CONST2: FFTComplex = FFTComplex { re: 0.30901699437494742411, im: 0.95105651629515357211 };
148 fn twiddle5(a: FFTComplex, b: FFTComplex, c: FFTComplex) -> (FFTComplex, FFTComplex) {
149 let re = a.re * c.re;
150 let im = a.im * c.re;
151 let diffre = b.im * c.im;
152 let diffim = b.re * c.im;
154 (FFTComplex { re: re - diffre, im: im + diffim }, FFTComplex { re: re + diffre, im: im - diffim })
158 fn new_data(size: usize, forward: bool) -> FFTData {
159 let mut table: Vec<FFTComplex> = Vec::with_capacity(size * size);
160 table.resize(size * size, FFTC_ZERO);
161 let base = consts::PI * 2.0 / (size as f32);
165 table[n * size + k] = FFTComplex::exp(-base * ((n * k) as f32));
171 table[n * size + k] = FFTComplex::exp( base * ((n * k) as f32));
175 let mut tmp = Vec::with_capacity(size);
176 tmp.resize(size, FFTC_ZERO);
177 FFTData { table, tmp, twiddle: Vec::new(), size, step: 0, div: 0 }
179 fn fft(tbl: &mut FFTData, size: usize, data: &mut [FFTComplex], step: usize) {
181 let s0 = data[step * 0];
182 let s1 = data[step * 1];
183 let s2 = data[step * 2];
185 data[step * 0] += t0;
186 let t1 = s0 - t0.scale(0.5);
187 let t2 = (s2 - s1).rotate().scale(FFT3_CONST);
188 data[step * 1] = t1 + t2;
189 data[step * 2] = t1 - t2;
193 let s0 = data[step * 0];
194 let s1 = data[step * 1];
195 let s2 = data[step * 2];
196 let s3 = data[step * 3];
197 let s4 = data[step * 4];
203 let (t4, t5) = twiddle5(t0, t1, FFT5_CONST2);
204 let (t6, t7) = twiddle5(t2, t3, FFT5_CONST1);
205 let (t8, t9) = twiddle5(t0, t1, FFT5_CONST1);
206 let (ta, tb) = twiddle5(t2, t3, FFT5_CONST2);
208 data[step * 0] = s0 + t0 + t2;
209 data[step * 1] = s0 + t5 - t6;
210 data[step * 2] = s0 - t8 + ta;
211 data[step * 3] = s0 - t9 + tb;
212 data[step * 4] = s0 + t4 - t7;
215 for k in 0..tbl.size {
216 tbl.tmp[k] = FFTC_ZERO;
217 for n in 0..tbl.size {
218 tbl.tmp[k] += data[n * step] * tbl.table[k * tbl.size + n];
221 for n in 0..tbl.size {
222 data[n * step] = tbl.tmp[n];
225 fn ifft(tbl: &mut FFTData, size: usize, data: &mut [FFTComplex], step: usize) {
227 let s0 = data[step * 0];
228 let s1 = data[step * 1];
229 let s2 = data[step * 2];
231 data[step * 0] += t0;
232 let t1 = s0 - t0.scale(0.5);
233 let t2 = (s2 - s1).rotate().scale(FFT3_CONST);
234 data[step * 1] = t1 - t2;
235 data[step * 2] = t1 + t2;
239 let s0 = data[step * 0];
240 let s1 = data[step * 1];
241 let s2 = data[step * 2];
242 let s3 = data[step * 3];
243 let s4 = data[step * 4];
249 let (t4, t5) = twiddle5(t0, t1, FFT5_CONST2);
250 let (t6, t7) = twiddle5(t2, t3, FFT5_CONST1);
251 let (t8, t9) = twiddle5(t0, t1, FFT5_CONST1);
252 let (ta, tb) = twiddle5(t2, t3, FFT5_CONST2);
254 data[step * 0] = s0 + t0 + t2;
255 data[step * 1] = s0 + t4 - t7;
256 data[step * 2] = s0 - t9 + tb;
257 data[step * 3] = s0 - t8 + ta;
258 data[step * 4] = s0 + t5 - t6;
261 Self::fft(tbl, size, data, step);
265 struct FFTSplitRadix {}
268 fn new_data(bits: u8, _forward: bool) -> FFTData {
269 let size = 1 << bits;
270 let mut table = Vec::with_capacity(size);
271 for _ in 0..4 { table.push(FFTC_ZERO); }
273 let qsize = (1 << (b - 2)) as usize;
274 let base = -consts::PI / ((qsize * 2) as f32);
276 table.push(FFTComplex::exp(base * ((k * 1) as f32)));
277 table.push(FFTComplex::exp(base * ((k * 3) as f32)));
280 FFTData { table, tmp: Vec::new(), twiddle: Vec::new(), size, step: 0, div: 0 }
282 fn fft(fftdata: &mut FFTData, bits: u8, data: &mut [FFTComplex]) {
283 if bits == 0 { return; }
285 let sum01 = data[0] + data[1];
286 let dif01 = data[0] - data[1];
292 let sum01 = data[0] + data[2];
293 let dif01 = data[0] - data[2];
294 let sum23 = data[1] + data[3];
295 let dif23 = data[1] - data[3];
296 data[0] = sum01 + sum23;
297 data[1] = dif01 - dif23.rotate();
298 data[2] = sum01 - sum23;
299 data[3] = dif01 + dif23.rotate();
302 let qsize = (1 << (bits - 2)) as usize;
303 let hsize = (1 << (bits - 1)) as usize;
304 let q3size = qsize + hsize;
306 Self::fft(fftdata, bits - 1, &mut data[0 ..hsize]);
307 Self::fft(fftdata, bits - 2, &mut data[hsize ..q3size]);
308 Self::fft(fftdata, bits - 2, &mut data[q3size..]);
311 let t3 = data[0 + hsize] + data[0 + q3size];
312 let t4 = (data[0 + hsize] - data[0 + q3size]).rotate();
314 let e2 = data[0 + qsize];
316 data[0 + qsize] = e2 - t4;
317 data[0 + hsize] = e1 - t3;
318 data[0 + q3size] = e2 + t4;
321 let t1 = fftdata.table[off + k * 2 + 0] * data[k + hsize];
322 let t2 = fftdata.table[off + k * 2 + 1] * data[k + q3size];
324 let t4 = (t1 - t2).rotate();
326 let e2 = data[k + qsize];
328 data[k + qsize] = e2 - t4;
329 data[k + hsize] = e1 - t3;
330 data[k + qsize * 3] = e2 + t4;
333 fn ifft(fftdata: &mut FFTData, bits: u8, data: &mut [FFTComplex]) {
334 if bits == 0 { return; }
336 let sum01 = data[0] + data[1];
337 let dif01 = data[0] - data[1];
343 let sum01 = data[0] + data[2];
344 let dif01 = data[0] - data[2];
345 let sum23 = data[1] + data[3];
346 let dif23 = data[1] - data[3];
347 data[0] = sum01 + sum23;
348 data[1] = dif01 + dif23.rotate();
349 data[2] = sum01 - sum23;
350 data[3] = dif01 - dif23.rotate();
353 let qsize = (1 << (bits - 2)) as usize;
354 let hsize = (1 << (bits - 1)) as usize;
355 let q3size = qsize + hsize;
357 Self::ifft(fftdata, bits - 1, &mut data[0 ..hsize]);
358 Self::ifft(fftdata, bits - 2, &mut data[hsize ..q3size]);
359 Self::ifft(fftdata, bits - 2, &mut data[q3size..]);
362 let t3 = data[0 + hsize] + data[0 + q3size];
363 let t4 = (data[0 + hsize] - data[0 + q3size]).rotate();
365 let e2 = data[0 + qsize];
367 data[0 + qsize] = e2 + t4;
368 data[0 + hsize] = e1 - t3;
369 data[0 + q3size] = e2 - t4;
372 let t1 = !fftdata.table[off + k * 2 + 0] * data[k + hsize];
373 let t2 = !fftdata.table[off + k * 2 + 1] * data[k + q3size];
375 let t4 = (t1 - t2).rotate();
377 let e2 = data[k + qsize];
379 data[k + qsize] = e2 + t4;
380 data[k + hsize] = e1 - t3;
381 data[k + qsize * 3] = e2 - t4;
388 const FFT15_INSWAP: [usize; 20] = [ 0, 5, 10, 42, 3, 8, 13, 42, 6, 11, 1, 42, 9, 14, 4, 42, 12, 2, 7, 42 ];
389 const FFT15_OUTSWAP: [usize; 20] = [ 0, 10, 5, 42, 6, 1, 11, 42, 12, 7, 2, 42, 3, 13, 8, 42, 9, 4, 14, 42 ];
392 fn new_data(size: usize, _forward: bool) -> FFTData {
393 FFTData { table: Vec::new(), tmp: Vec::new(), twiddle: Vec::new(), size, step: 0, div: 0 }
395 fn fft3(dst: &mut [FFTComplex], src: &[FFTComplex], step: usize, n: usize) {
401 let t1 = s0 - t0.scale(0.5);
402 let t2 = (s2 - s1).rotate().scale(FFT3_CONST);
404 dst[FFT15_OUTSWAP[n * 4 + 0] * step] = s0 + t0;
405 dst[FFT15_OUTSWAP[n * 4 + 1] * step] = t1 + t2;
406 dst[FFT15_OUTSWAP[n * 4 + 2] * step] = t1 - t2;
408 fn ifft3(dst: &mut [FFTComplex], src: &[FFTComplex], step: usize, n: usize) {
414 let t1 = s0 - t0.scale(0.5);
415 let t2 = (s2 - s1).rotate().scale(FFT3_CONST);
417 dst[FFT15_OUTSWAP[n * 4 + 0] * step] = s0 + t0;
418 dst[FFT15_OUTSWAP[n * 4 + 1] * step] = t1 - t2;
419 dst[FFT15_OUTSWAP[n * 4 + 2] * step] = t1 + t2;
421 fn fft5(dst: &mut [FFTComplex], src: &[FFTComplex], step: usize, n: usize) {
422 let s0 = src[FFT15_INSWAP[n + 0 * 4] * step];
423 let s1 = src[FFT15_INSWAP[n + 1 * 4] * step];
424 let s2 = src[FFT15_INSWAP[n + 2 * 4] * step];
425 let s3 = src[FFT15_INSWAP[n + 3 * 4] * step];
426 let s4 = src[FFT15_INSWAP[n + 4 * 4] * step];
432 let (t4, t5) = twiddle5(t0, t1, FFT5_CONST2);
433 let (t6, t7) = twiddle5(t2, t3, FFT5_CONST1);
434 let (t8, t9) = twiddle5(t0, t1, FFT5_CONST1);
435 let (ta, tb) = twiddle5(t2, t3, FFT5_CONST2);
437 dst[0 * 3] = s0 + t0 + t2;
438 dst[1 * 3] = s0 + t5 - t6;
439 dst[2 * 3] = s0 - t8 + ta;
440 dst[3 * 3] = s0 - t9 + tb;
441 dst[4 * 3] = s0 + t4 - t7;
443 fn ifft5(dst: &mut [FFTComplex], src: &[FFTComplex], step: usize, n: usize) {
444 let s0 = src[FFT15_INSWAP[n + 0 * 4] * step];
445 let s1 = src[FFT15_INSWAP[n + 1 * 4] * step];
446 let s2 = src[FFT15_INSWAP[n + 2 * 4] * step];
447 let s3 = src[FFT15_INSWAP[n + 3 * 4] * step];
448 let s4 = src[FFT15_INSWAP[n + 4 * 4] * step];
454 let (t4, t5) = twiddle5(t0, t1, FFT5_CONST2);
455 let (t6, t7) = twiddle5(t2, t3, FFT5_CONST1);
456 let (t8, t9) = twiddle5(t0, t1, FFT5_CONST1);
457 let (ta, tb) = twiddle5(t2, t3, FFT5_CONST2);
459 dst[0 * 3] = s0 + t0 + t2;
460 dst[1 * 3] = s0 + t4 - t7;
461 dst[2 * 3] = s0 - t9 + tb;
462 dst[3 * 3] = s0 - t8 + ta;
463 dst[4 * 3] = s0 + t5 - t6;
465 fn fft(_fftdata: &mut FFTData, data: &mut [FFTComplex], step: usize) {
466 let mut tmp = [FFTC_ZERO; 15];
468 Self::fft5(&mut tmp[n..], data, step, n);
471 Self::fft3(data, &tmp[n * 3..][..3], step, n);
474 fn ifft(_fftdata: &mut FFTData, data: &mut [FFTComplex], step: usize) {
475 let mut tmp = [FFTC_ZERO; 15];
477 Self::ifft5(&mut tmp[n..], data, step, n);
480 Self::ifft3(data, &tmp[n * 3..][..3], step, n);
493 fn permute(&self, perms: &mut [usize]) {
495 FFTMode::Generic(_) => {},
496 FFTMode::SplitRadix(bits) => {
497 let div = perms.len() >> bits;
498 gen_sr_perms(perms, 1 << bits);
500 for i in 0..(1 << bits) {
504 for j in 0..(1 << bits) {
505 perms[(i << bits) + j] = perms[j] + i;
510 FFTMode::Prime15 => {},
513 fn do_fft(&self, fftdata: &mut FFTData, data: &mut [FFTComplex]) {
515 FFTMode::Generic(size) => FFTGeneric::fft(fftdata, size, data, 1),
516 FFTMode::SplitRadix(bits) => FFTSplitRadix::fft(fftdata, bits, data),
517 FFTMode::Prime15 => FFT15::fft(fftdata, data, 1),
520 fn do_fft2(&self, fftdata: &mut FFTData, data: &mut [FFTComplex], step: usize) {
522 FFTMode::Generic(size) => FFTGeneric::fft(fftdata, size, data, step),
523 FFTMode::SplitRadix(_) => unreachable!(),
524 FFTMode::Prime15 => FFT15::fft(fftdata, data, step),
527 fn do_ifft(&self, fftdata: &mut FFTData, data: &mut [FFTComplex]) {
529 FFTMode::Generic(size) => FFTGeneric::ifft(fftdata, size, data, 1),
530 FFTMode::SplitRadix(bits) => FFTSplitRadix::ifft(fftdata, bits, data),
531 FFTMode::Prime15 => FFT15::ifft(fftdata, data, 1),
534 fn do_ifft2(&self, fftdata: &mut FFTData, data: &mut [FFTComplex], step: usize) {
536 FFTMode::Generic(size) => FFTGeneric::ifft(fftdata, size, data, step),
537 FFTMode::SplitRadix(_) => unreachable!(),
538 FFTMode::Prime15 => FFT15::ifft(fftdata, data, step),
541 fn get_size(&self) -> usize {
543 FFTMode::Generic(size) => size,
544 FFTMode::SplitRadix(bits) => 1 << bits,
545 FFTMode::Prime15 => 15,
550 /// FFT working context.
554 ffts: Vec<(FFTMode, FFTData)>,
558 /// Calculates Fourier transform.
559 pub fn do_fft(&mut self, src: &[FFTComplex], dst: &mut [FFTComplex]) {
560 for k in 0..src.len() { dst[k] = src[self.perms[k]]; }
561 self.do_fft_core(dst);
563 /// Calculates inverse Fourier transform.
564 pub fn do_ifft(&mut self, src: &[FFTComplex], dst: &mut [FFTComplex]) {
565 for k in 0..src.len() { dst[k] = src[self.perms[k]]; }
566 self.do_ifft_core(dst);
568 /// Performs inplace FFT.
569 pub fn do_fft_inplace(&mut self, data: &mut [FFTComplex]) {
570 for idx in 0..self.swaps.len() {
571 let nidx = self.swaps[idx];
573 data.swap(nidx, idx);
576 self.do_fft_core(data);
578 /// Performs inplace inverse FFT.
579 pub fn do_ifft_inplace(&mut self, data: &mut [FFTComplex]) {
580 for idx in 0..self.swaps.len() {
581 let nidx = self.swaps[idx];
583 data.swap(nidx, idx);
586 self.do_ifft_core(data);
588 fn do_fft_core(&mut self, data: &mut [FFTComplex]) {
589 for el in self.ffts.iter_mut() {
590 let (mode, ref mut fftdata) = el;
591 let bsize = mode.get_size();
592 let div = fftdata.div;
593 let step = fftdata.step;
595 mode.do_fft(fftdata, data);
597 mode.do_fft(fftdata, &mut data[i * bsize..]);
600 mode.do_fft2(fftdata, data, div);
601 let mut toff = bsize;
604 data[i + j * div] *= fftdata.twiddle[toff + j];
606 mode.do_fft2(fftdata, &mut data[i..], div);
612 fn do_ifft_core(&mut self, data: &mut [FFTComplex]) {
613 for el in self.ffts.iter_mut() {
614 let (mode, ref mut fftdata) = el;
615 let bsize = mode.get_size();
616 let div = fftdata.div;
617 let step = fftdata.step;
619 mode.do_ifft(fftdata, data);
621 mode.do_ifft(fftdata, &mut data[i * bsize..]);
624 mode.do_ifft2(fftdata, data, div);
625 let mut toff = bsize;
628 data[i + j * div] *= fftdata.twiddle[toff + j];
630 mode.do_ifft2(fftdata, &mut data[i..], div);
638 /// [`FFT`] context creator.
640 /// [`FFT`]: ./struct.FFT.html
641 pub struct FFTBuilder {
644 /*fn reverse_bits(inval: u32) -> u32 {
645 const REV_TAB: [u8; 16] = [
646 0b0000, 0b1000, 0b0100, 0b1100, 0b0010, 0b1010, 0b0110, 0b1110,
647 0b0001, 0b1001, 0b0101, 0b1101, 0b0011, 0b1011, 0b0111, 0b1111,
653 ret = (ret << 4) | (REV_TAB[(val & 0xF) as usize] as u32);
659 fn swp_idx(idx: usize, bits: u32) -> usize {
660 let s = reverse_bits(idx as u32) as usize;
664 fn gen_sr_perms(swaps: &mut [usize], size: usize) {
665 if size <= 4 { return; }
666 let mut evec: Vec<usize> = Vec::with_capacity(size / 2);
667 let mut ovec1: Vec<usize> = Vec::with_capacity(size / 4);
668 let mut ovec2: Vec<usize> = Vec::with_capacity(size / 4);
670 evec.push (swaps[k * 4 + 0]);
671 ovec1.push(swaps[k * 4 + 1]);
672 evec.push (swaps[k * 4 + 2]);
673 ovec2.push(swaps[k * 4 + 3]);
675 for k in 0..size/2 { swaps[k] = evec[k]; }
676 for k in 0..size/4 { swaps[k + size/2] = ovec1[k]; }
677 for k in 0..size/4 { swaps[k + 3*size/4] = ovec2[k]; }
678 gen_sr_perms(&mut swaps[0..size/2], size/2);
679 gen_sr_perms(&mut swaps[size/2..3*size/4], size/4);
680 gen_sr_perms(&mut swaps[3*size/4..], size/4);
683 fn gen_swaps_for_perm(swaps: &mut Vec<usize>, perms: &[usize]) {
684 let mut idx_arr: Vec<usize> = Vec::with_capacity(perms.len());
685 for i in 0..perms.len() { idx_arr.push(i); }
686 let mut run_size = 0;
688 for idx in 0..perms.len() {
689 if perms[idx] == idx_arr[idx] {
690 if run_size == 0 { run_pos = idx; }
693 for i in 0..run_size {
694 swaps.push(run_pos + i);
697 let mut spos = idx + 1;
698 while idx_arr[spos] != perms[idx] { spos += 1; }
699 idx_arr[spos] = idx_arr[idx];
700 idx_arr[idx] = perms[idx];
707 fn generate_twiddle(data: &mut FFTData, size: usize, cur_size: usize, forward: bool) {
708 if size == cur_size { return; }
709 data.twiddle = Vec::with_capacity(size);
710 let div = size / cur_size;
711 let base = if forward { -2.0 * consts::PI / (size as f32) } else { 2.0 * consts::PI / (size as f32) };
713 for k in 0..cur_size {
714 data.twiddle.push(FFTComplex::exp(base * ((k * n) as f32)));
718 /// Constructs a new `FFT` context.
719 pub fn new_fft(size: usize, forward: bool) -> FFT {
720 let mut ffts: Vec<(FFTMode, FFTData)> = Vec::with_capacity(1);
721 let mut perms: Vec<usize> = Vec::with_capacity(size);
722 let mut swaps: Vec<usize> = Vec::with_capacity(size);
723 let mut rem_size = size;
724 if rem_size.trailing_zeros() > 0 {
725 let bits = rem_size.trailing_zeros() as u8;
726 let mut data = FFTSplitRadix::new_data(bits, forward);
727 Self::generate_twiddle(&mut data, size, 1 << bits, forward);
729 data.div = rem_size >> bits;
730 ffts.push((FFTMode::SplitRadix(bits), data));
733 if (rem_size % 15) == 0 {
734 let mut data = FFT15::new_data(size, forward);
735 Self::generate_twiddle(&mut data, size, 15, forward);
736 data.step = size / rem_size;
737 data.div = size / rem_size;
738 ffts.push((FFTMode::Prime15, data));
742 let mut data = FFTGeneric::new_data(rem_size, forward);
743 Self::generate_twiddle(&mut data, size, rem_size, forward);
744 data.step = size / rem_size;
745 data.div = size / rem_size;
746 ffts.push((FFTMode::Generic(rem_size), data));
752 for (mode, _) in ffts.iter().rev() {
753 mode.permute(&mut perms);
755 gen_swaps_for_perm(&mut swaps, perms.as_slice());
757 FFT { perms, swaps, ffts }
761 /// RDFT working context.
763 table: Vec<FFTComplex>,
770 fn crossadd(a: FFTComplex, b: FFTComplex) -> FFTComplex {
771 FFTComplex { re: a.re + b.re, im: a.im - b.im }
776 pub fn do_rdft(&mut self, src: &[FFTComplex], dst: &mut [FFTComplex]) {
777 dst.copy_from_slice(src);
778 self.do_rdft_inplace(dst);
780 /// Calculates inplace RDFT.
781 pub fn do_rdft_inplace(&mut self, buf: &mut [FFTComplex]) {
783 for n in 0..self.size/2 {
784 let in0 = buf[n + 1];
785 let in1 = buf[self.size - n - 1];
787 let t0 = crossadd(in0, in1);
788 let t1 = FFTComplex { re: in1.im + in0.im, im: in1.re - in0.re };
789 let tab = self.table[n];
790 let t2 = FFTComplex { re: t1.im * tab.im + t1.re * tab.re, im: t1.im * tab.re - t1.re * tab.im };
792 buf[n + 1] = FFTComplex { re: t0.im - t2.im, im: t0.re - t2.re }; // (t0 - t2).conj().rotate()
793 buf[self.size - n - 1] = (t0 + t2).rotate();
801 self.fft.do_fft_inplace(buf);
803 self.fft.do_ifft_inplace(buf);
806 for n in 0..self.size/2 {
807 let in0 = buf[n + 1];
808 let in1 = buf[self.size - n - 1];
810 let t0 = crossadd(in0, in1).scale(0.5);
811 let t1 = FFTComplex { re: in0.im + in1.im, im: in0.re - in1.re };
812 let t2 = t1 * self.table[n];
814 buf[n + 1] = crossadd(t0, t2);
815 buf[self.size - n - 1] = FFTComplex { re: t0.re - t2.re, im: -(t0.im + t2.im) };
822 for n in 0..self.size {
823 buf[n] = FFTComplex{ re: buf[n].im, im: buf[n].re };
829 /// [`RDFT`] context creator.
831 /// [`RDFT`]: ./struct.FFT.html
832 pub struct RDFTBuilder {
836 /// Constructs a new `RDFT` context.
837 pub fn new_rdft(size: usize, forward: bool, forward_fft: bool) -> RDFT {
838 let mut table: Vec<FFTComplex> = Vec::with_capacity(size / 4);
839 let (base, scale) = if forward { (consts::PI / (size as f32), 0.5) } else { (-consts::PI / (size as f32), 1.0) };
841 table.push(FFTComplex::exp(base * ((i + 1) as f32)).scale(scale));
843 let fft = FFTBuilder::new_fft(size, forward_fft);
844 RDFT { table, fft, size, fwd: forward, fwd_fft: forward_fft }
853 fn test_fft(size: usize) {
854 println!("testing FFT {}", size);
855 let mut fin: Vec<FFTComplex> = Vec::with_capacity(size);
856 let mut fout1: Vec<FFTComplex> = Vec::with_capacity(size);
857 let mut fout2: Vec<FFTComplex> = Vec::with_capacity(size);
858 fin.resize(size, FFTC_ZERO);
859 fout1.resize(size, FFTC_ZERO);
860 fout2.resize(size, FFTC_ZERO);
861 let mut fft = FFTBuilder::new_fft(size, true);
862 let mut seed: u32 = 42;
863 for i in 0..fin.len() {
864 seed = seed.wrapping_mul(1664525).wrapping_add(1013904223);
865 let val = (seed >> 16) as i16;
866 fin[i].re = (val as f32) / 256.0;
867 seed = seed.wrapping_mul(1664525).wrapping_add(1013904223);
868 let val = (seed >> 16) as i16;
869 fin[i].im = (val as f32) / 256.0;
871 fft.do_fft(&fin, &mut fout1);
872 fout2.copy_from_slice(&fin);
873 generic_fft(&mut fout2, true);
875 for i in 0..fin.len() {
876 assert!((fout1[i].re - fout2[i].re).abs() < 1.0);
877 assert!((fout1[i].im - fout2[i].im).abs() < 1.0);
879 let mut ifft = FFTBuilder::new_fft(size, false);
880 ifft.do_ifft_inplace(&mut fout1);
881 generic_fft(&mut fout2, false);
883 let sc = 1.0 / (size as f32);
884 for i in 0..fin.len() {
885 assert!((fin[i].re - fout1[i].re * sc).abs() < 1.0);
886 assert!((fin[i].im - fout1[i].im * sc).abs() < 1.0);
887 assert!((fout1[i].re - fout2[i].re).abs() * sc < 1.0);
888 assert!((fout1[i].im - fout2[i].im).abs() * sc < 1.0);
905 let mut fin: [FFTComplex; 128] = [FFTC_ZERO; 128];
906 let mut fout1: [FFTComplex; 128] = [FFTC_ZERO; 128];
907 let mut rdft = RDFTBuilder::new_rdft(fin.len(), true, true);
908 let mut seed: u32 = 42;
909 for i in 0..fin.len() {
910 seed = seed.wrapping_mul(1664525).wrapping_add(1013904223);
911 let val = (seed >> 16) as i16;
912 fin[i].re = (val as f32) / 256.0;
913 seed = seed.wrapping_mul(1664525).wrapping_add(1013904223);
914 let val = (seed >> 16) as i16;
915 fin[i].im = (val as f32) / 256.0;
917 rdft.do_rdft(&fin, &mut fout1);
918 let mut irdft = RDFTBuilder::new_rdft(fin.len(), false, true);
919 irdft.do_rdft_inplace(&mut fout1);
921 for i in 0..fin.len() {
922 let tst = fout1[i].scale(0.5/(fout1.len() as f32));
923 assert!((tst.re - fin[i].re).abs() < 1.0);
924 assert!((tst.im - fin[i].im).abs() < 1.0);