import ( "fft.lib.dsp" ) ;
import ( "complex.lib" ) ;

GREY_MAX = 256;

//fft2d(x,y) = vectorize(x) : vectorize(y) <: picks(n) : real2complex(n) : fft(n) : stops(n/2), pcplx_moduls(n/2) : modules_vector(n/2);
//fft2d(x,y) = vectorize(x) : vectorize(y) : fft(x) : fftc(y) : pcplx_moduls(y) : modules_vector(y);
//fft2d(x,y) = vectorize(x) : fft(x) : fftc(y) : pcplx_moduls(y) : modules_vector(y);
 
matricize(x, y) = vectorize(y) : vectorize(x);

ffts(n, m) = par(i, n, (fft(m) : nconcat(m)));

fftcs(n, m) = par(i, n, (fftc(m) : nconcat(m)));

lines_fft(n, m) = _ <: picks(n) : ffts(n, m) : nconcat(n);

lines_fftc(n, m) = _ <: picks(n) : fftcs(n, m) : nconcat(n);

take_column(n, m) = par(i, n, [m]) : nconcat(n);

matrix_transpose(n, m) = _ <: picks(n) <: par(i, m, take_column(n, i)) : nconcat(m);

matrix_pcplx2modul(n, m) = _ <: picks(n) : par(i, n, ( _ <: picks(m) : pcplx_moduls(m) : nconcat(m))) : nconcat(n);

matrix_real2pcplx(n, m) = _ <: picks(n) : par(i, n, ( _ <: picks(m) : real2pcplx(m) : nconcat(m))) : nconcat(n);

normalize(n) = _ , n : /;

unnormalize(n) = par(i, n, ( _ , GREY_MAX : *));

norm_out(n, m) = _ <: picks(n) : par(i, n, ( _ <: picks(m) : unnormalize(m) : nconcat(m))) : nconcat(n) : serialize;

output(n, m) = serialize;

//fft2d(x, y) = matricize(x, y) : lines_fft(x, y) : matrix_transpose(x, y) : lines_fftc(y, x) : matrix_transpose(y, x)  : output(x, y);
//fft2d(x, y) = normalize(GREY_MAX) : matricize(x, y) : lines_fft(x, y) : matrix_transpose(x, y) : lines_fftc(y, x) : matrix_transpose(y, x) : norm_out(x, y);
//fft2d(x, y) = matricize(x, y) : matrix_real2pcplx(x, y) : matrix_pcplx2modul(x, y) : output(x, y);
//fft2d(x, y) = matricize(x, y) : output(x, y);
fft2d(x, y) = normalize(GREY_MAX) : matricize(x, y) : lines_fft(x, y) : matrix_transpose(x, y) : lines_fftc(y, x) : matrix_transpose(y, x) : norm_out(x, y);

//safer with x=y because tests and zero padding are still to implement
process = fft2d(64,64);