RosettaCodeData/Task/Fast-Fourier-transform/D/fast-fourier-transform-2.d

16 lines
495 B
D
Raw Permalink Normal View History

2013-06-05 21:47:54 +00:00
import std.stdio, std.algorithm, std.range, std.math;
2013-04-10 21:29:02 -07:00
2015-02-20 00:35:01 -05:00
const(creal)[] fft(in creal[] x) pure /*nothrow*/ @safe {
2013-06-05 21:47:54 +00:00
immutable N = x.length;
if (N <= 1) return x;
const ev = x.stride(2).array.fft;
const od = x[1 .. $].stride(2).array.fft;
2013-10-27 22:24:23 +00:00
auto l = iota(N / 2).map!(k => ev[k] + expi(-2*PI * k/N) * od[k]);
auto r = iota(N / 2).map!(k => ev[k] - expi(-2*PI * k/N) * od[k]);
2013-04-10 21:29:02 -07:00
return l.chain(r).array;
}
2015-02-20 00:35:01 -05:00
void main() @safe {
2013-06-05 21:47:54 +00:00
[1.0L+0i, 1, 1, 1, 0, 0, 0, 0].fft.writeln;
2013-04-10 21:29:02 -07:00
}