Fast Fourier transform: Difference between revisions
m (→{{header|J}}) |
m (→{{header|J}}) |
||
Line 17: | Line 17: | ||
<lang j> require'printf' |
<lang j> require'printf' |
||
fmt =: [:, sprintf~&'%7.3f'@| |
fmt =: [:, sprintf~&'%7.3f'@| |
||
fmt"1 (,:fft) 1 o. 3r8p1 * i.16 |
(>'wave:';'fft:'),.fmt"1 (,:fft) 1 o. 3r8p1 * i.16 |
||
0.000 0.924 0.707 0.383 1.000 0.383 0.707 0.924 0.000 0.924 0.707 0.383 1.000 0.383 0.707 0.924 |
wave: 0.000 0.924 0.707 0.383 1.000 0.383 0.707 0.924 0.000 0.924 0.707 0.383 1.000 0.383 0.707 0.924 |
||
0.000 0.000 0.000 8.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 8.000 0.000 0.000</lang> |
fft: 0.000 0.000 0.000 8.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 8.000 0.000 0.000</lang> |
||
Note that fmt only displays the magnitude of the result, because sprintf does not support complex arguments (and shows only 3 decimals of the fractional part). |
Note that fmt only displays the magnitude of the result, because sprintf does not support complex arguments (and shows only 3 decimals of the fractional part). |
Revision as of 16:41, 8 February 2011
The purpose of this task is to calculate the FFT (Fast Fourier Transform) of an input sequence. The most general case allows for complex numbers at the input and results in a sequence of equal length, again of complex numbers. If you need to restrict yourself to real numbers the output should be the magnitude (i.e. sqrt(re²+im²)) of the complex result. The classic version is the recursive Cooley–Tukey FFT. Wikipedia has pseudocode for that. Further optimizations are possible but not required.
J
From j:Essays/FFT:
<lang j>cube =: ($~ q:@#) :. , roots =: +@]^:(0>[) (_1^2%]) ^ i.@-: NB. The elegant version, but less accurate rou =: [: (, j.) [: (, (j.~%:0.5) , |."1&.:+.@|.@}.) [: ^@o.@:j. i.@(%&8) % -: NB. roots of unity floop =: 4 : 'for_r. i.#$x do. (y=.{."1 y) ] x=.(+/x) ,&,:"r (-/x)*y end.' fft =: ( ] floop&.cube rou@#) f. :. ifft ifft =: (# %~ ] floop&.cube +@rou@#) f. :. fft</lang>
Example:
<lang j> require'printf'
fmt =: [:, sprintf~&'%7.3f'@| (>'wave:';'fft:'),.fmt"1 (,:fft) 1 o. 3r8p1 * i.16
wave: 0.000 0.924 0.707 0.383 1.000 0.383 0.707 0.924 0.000 0.924 0.707 0.383 1.000 0.383 0.707 0.924 fft: 0.000 0.000 0.000 8.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 8.000 0.000 0.000</lang>
Note that fmt only displays the magnitude of the result, because sprintf does not support complex arguments (and shows only 3 decimals of the fractional part).
Perl 6
<lang perl6>sub fft {
return @_ if @_ == 1; my @evn = fft( @_[0,2...^* >= @_] ); my @odd = fft( @_[1,3...^* >= @_] ); my $twd = 2i * pi / @_; # twiddle factor @odd »*=« (^@odd).map(* * $twd)».exp; return @evn »+« @odd, @evn »-« @odd;
}
my @seq = ^16; my $cycles = 3; my @wave = (@seq »*» (2*pi / @seq * $cycles))».sin; say "wave: ", @wave.fmt("%7.3f");
say "fft: ", fft(@wave)».abs.fmt("%7.3f");</lang>
Output:
wave: 0.000 0.924 0.707 -0.383 -1.000 -0.383 0.707 0.924 0.000 -0.924 -0.707 0.383 1.000 0.383 -0.707 -0.924 fft: 0.000 0.000 0.000 8.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 8.000 0.000 0.000