7758d044b81a639e9f9e4165687d09564d795025
1 /******************************************************************
2 * FFT
3 * Implementation contributed by Remy Muller
4 *****************************************************************/
6 // bus(n) : n parallel cables
7 bus(2) = _,_; // avoids a lot of "bus(1)" labels in block diagrams
8 bus(n) = par(i, n, _);
10 // twiddle_mult(n) : n parallel cables
12 W(k, n) = 1, (0, ( k, ((2 * PI) / n) : *) : -) : polar_cplx;
14 twiddle_mult(k, n) = _, W(k, n) : pcplx_mul;
16 // selector(i,n) : select ith cable among n
17 selector(i,n) = par(j, n, S(i, j)) with { S(i,i) = _; S(i,j) = !; };
19 // interleave(m,n) : interleave m*n cables : x(0), x(m), x(2m), ..., x(1),x(1+m), x(1+2m)...
20 //interleave(m,n) = bus(m*n) <: par(i, m, par(j, n, selector(i+j*m,m*n)));
22 // interleave(row,col) : interleave row*col cables from column order to row order.
23 // input : x(0), x(1), x(2) ..., x(row*col-1)
24 // output: x(0+0*row), x(0+1*row), x(0+2*row), ..., x(1+0*row), x(1+1*row), x(1+2*row), ...
25 interleave(row,col) = bus(row*col) <: par(r, row, par(c, col, selector(r+c*row,row*col)));
27 // butterfly(n) : addition then substraction of interleaved signals :
28 xbutterfly(n) = (bus(n/2), par(k, n/2, twiddle_mult(k, n))) <: interleave(n/2,2), interleave(n/2,2) : par(i, n/2, pcplx_add), par(i, n/2, pcplx_sub);
30 //btf_downside(n) = bus(n) : interleave(n/2,2);
32 // fft(n) : fft matrix function of size n = 2^k
33 //fft(2) = butterfly(2);
34 //fft(n) = butterfly(n) : (fft(n/2) , fft(n/2));
36 xbutterflies(2) = xbutterfly(2);
37 xbutterflies(n) = (xbutterflies(n/2) , xbutterflies(n/2)) : xbutterfly(n);
40 evens = case {
41 (2) => _ , ! ;
42 (n) => _ , ! , evens(n - 2);
43 };
44 odds = case {
45 (2) => ! , _ ;
46 (n) => ! , _ , odds(n - 2);
47 };
49 eo(n) = evens(n), odds(n);
51 shuffling = case {
52 (2) => eo(2);
53 (n) => (evens(n) <: shuffling(n/2)), (odds(n) <: shuffling(n/2));
54 };
56 shuffle(n) = bus(n) <: shuffling(n);
58 real2pcplx(n) = par(i, n, (sca2pcplx));
60 //fft(n) = shuffle(n) : xbutterflies(n);
61 fft(n) = _ <: picks(n) : real2pcplx(n) : shuffle(n) : xbutterflies(n);
62 fftc(n) = _ <: picks(n) : shuffle(n) : xbutterflies(n) : pcplx_moduls(n); // already complex input
65 picks(n) = par(i, n, [i]);
67 concats = case {
68 (1) => vectorize(1);
69 (n) => concats(n-1) # vectorize(1);
70 };
72 nconcat(n) = concats(n); //fake name for svg block encapsulation
74 pack = case {
75 (1) => _;
76 (n) => pack(n-1), _ : #;
77 };
79 delays(m) = _ <: par(i, m, @(i));
81 overlap(n,m) = vectorize(n/m) : delays(m) : pack(m);
83 stops(n) = par(i, n, !);