Fast Fourier transform: Difference between revisions
Content added Content deleted
m (→Recursive) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 17: | Line 17: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F fft(x) |
||
V n = x.len |
V n = x.len |
||
I n <= 1 |
I n <= 1 |
||
Line 27: | Line 27: | ||
(0 .< n I/ 2).map(k -> @even[k] - @t[k]) |
(0 .< n I/ 2).map(k -> @even[k] - @t[k]) |
||
print(fft([Complex(1.0), 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]).map(f -> ‘#1.3’.format(abs(f))).join(‘ ’))</ |
print(fft([Complex(1.0), 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]).map(f -> ‘#1.3’.format(abs(f))).join(‘ ’))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 39: | Line 39: | ||
a user instance of Ada.Numerics.Generic_Complex_Arrays. |
a user instance of Ada.Numerics.Generic_Complex_Arrays. |
||
<syntaxhighlight lang="ada"> |
|||
<lang Ada> |
|||
with Ada.Numerics.Generic_Complex_Arrays; |
with Ada.Numerics.Generic_Complex_Arrays; |
||
Line 47: | Line 47: | ||
use Complex_Arrays; |
use Complex_Arrays; |
||
function Generic_FFT (X : Complex_Vector) return Complex_Vector; |
function Generic_FFT (X : Complex_Vector) return Complex_Vector; |
||
</ |
</syntaxhighlight> |
||
<syntaxhighlight lang="ada"> |
|||
<lang Ada> |
|||
with Ada.Numerics; |
with Ada.Numerics; |
||
with Ada.Numerics.Generic_Complex_Elementary_Functions; |
with Ada.Numerics.Generic_Complex_Elementary_Functions; |
||
Line 89: | Line 89: | ||
return FFT (X, X'Length, 1); |
return FFT (X, X'Length, 1); |
||
end Generic_FFT; |
end Generic_FFT; |
||
</syntaxhighlight> |
|||
</lang> |
|||
Example: |
Example: |
||
<syntaxhighlight lang="ada"> |
|||
<lang Ada> |
|||
with Ada.Numerics.Complex_Arrays; use Ada.Numerics.Complex_Arrays; |
with Ada.Numerics.Complex_Arrays; use Ada.Numerics.Complex_Arrays; |
||
with Ada.Complex_Text_IO; use Ada.Complex_Text_IO; |
with Ada.Complex_Text_IO; use Ada.Complex_Text_IO; |
||
Line 114: | Line 114: | ||
end loop; |
end loop; |
||
end; |
end; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 134: | Line 134: | ||
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.3.5 algol68g-2.3.5].}} |
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.3.5 algol68g-2.3.5].}} |
||
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}} |
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}} |
||
'''File: Template.Fast_Fourier_transform.a68'''< |
'''File: Template.Fast_Fourier_transform.a68'''<syntaxhighlight lang="algol68">PRIO DICE = 9; # ideally = 11 # |
||
OP DICE = ([]SCALAR in, INT step)[]SCALAR: ( |
OP DICE = ([]SCALAR in, INT step)[]SCALAR: ( |
||
Line 171: | Line 171: | ||
coef |
coef |
||
FI |
FI |
||
);</ |
);</syntaxhighlight>'''File: test.Fast_Fourier_transform.a68'''<syntaxhighlight lang="algol68">#!/usr/local/bin/a68g --script # |
||
# -*- coding: utf-8 -*- # |
# -*- coding: utf-8 -*- # |
||
Line 192: | Line 192: | ||
$"1¼ cycle wave: "$, compl array fmt, one and a quarter wave ft, $l$ |
$"1¼ cycle wave: "$, compl array fmt, one and a quarter wave ft, $l$ |
||
)) |
)) |
||
)</ |
)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 202: | Line 202: | ||
{{trans|Fortran}} |
{{trans|Fortran}} |
||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
< |
<syntaxhighlight lang="apl">fft←{ |
||
1>k←2÷⍨N←⍴⍵:⍵ |
1>k←2÷⍨N←⍴⍵:⍵ |
||
0≠1|2⍟N:'Argument must be a power of 2 in length' |
0≠1|2⍟N:'Argument must be a power of 2 in length' |
||
Line 209: | Line 209: | ||
T←even×*(0J¯2×(○1)×(¯1+⍳k)÷N) |
T←even×*(0J¯2×(○1)×(¯1+⍳k)÷N) |
||
(odd+T),odd-T |
(odd+T),odd-T |
||
}</ |
}</syntaxhighlight> |
||
'''Example:''' |
'''Example:''' |
||
< |
<syntaxhighlight lang="apl"> fft 1 1 1 1 0 0 0 0</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 220: | Line 220: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang="bbcbasic"> @% = &60A |
||
DIM Complex{r#, i#} |
DIM Complex{r#, i#} |
||
Line 264: | Line 264: | ||
c.i# = i# |
c.i# = i# |
||
ENDPROC |
ENDPROC |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>Input (real, imag): |
<pre>Input (real, imag): |
||
Line 289: | Line 289: | ||
Note: array size is assumed to be power of 2 and not checked by code; |
Note: array size is assumed to be power of 2 and not checked by code; |
||
you can just pad it with 0 otherwise. |
you can just pad it with 0 otherwise. |
||
Also, <code>complex</code> is C99 standard.< |
Also, <code>complex</code> is C99 standard.<syntaxhighlight lang="c"> |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 342: | Line 342: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>Data: 1 1 1 1 0 0 0 0 |
<pre>Data: 1 1 1 1 0 0 0 0 |
||
Line 349: | Line 349: | ||
===OS X / iOS=== |
===OS X / iOS=== |
||
OS X 10.7+ / iOS 4+ |
OS X 10.7+ / iOS 4+ |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <Accelerate/Accelerate.h> |
#include <Accelerate/Accelerate.h> |
||
Line 389: | Line 389: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Data: 1 1 1 1 0 0 0 0 |
<pre>Data: 1 1 1 1 0 0 0 0 |
||
Line 395: | Line 395: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Numerics; |
using System.Numerics; |
||
using System.Linq; |
using System.Linq; |
||
Line 497: | Line 497: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 512: | Line 512: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <complex> |
||
#include <iostream> |
#include <iostream> |
||
#include <valarray> |
#include <valarray> |
||
Line 636: | Line 636: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 668: | Line 668: | ||
and condenses also the inverse part, by a keyword. |
and condenses also the inverse part, by a keyword. |
||
< |
<syntaxhighlight lang="lisp"> |
||
(defun fft (a &key (inverse nil) &aux (n (length a))) |
(defun fft (a &key (inverse nil) &aux (n (length a))) |
||
"Perform the FFT recursively on input vector A. |
"Perform the FFT recursively on input vector A. |
||
Line 707: | Line 707: | ||
:do (setq ⍵ (* ⍵ ⍵_n)) |
:do (setq ⍵ (* ⍵ ⍵_n)) |
||
:finally (return â))))))) |
:finally (return â))))))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
From here on the old solution. |
From here on the old solution. |
||
< |
<syntaxhighlight lang="lisp"> |
||
;;; This is adapted from the Python sample; it uses lists for simplicity. |
;;; This is adapted from the Python sample; it uses lists for simplicity. |
||
;;; Production code would use complex arrays (for compiler optimization). |
;;; Production code would use complex arrays (for compiler optimization). |
||
Line 725: | Line 725: | ||
;; finally, concatenate the sum and difference of the lists |
;; finally, concatenate the sum and difference of the lists |
||
(return (mapcan #'mapcar '(+ -) `(,ffta ,ffta) `(,aux ,aux))))))) |
(return (mapcan #'mapcar '(+ -) `(,ffta ,ffta) `(,aux ,aux))))))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="lisp"> |
||
;;; Demonstrates printing an FFT in both rectangular and polar form: |
;;; Demonstrates printing an FFT in both rectangular and polar form: |
||
CL-USER> (mapc (lambda (c) (format t "~&~6F~6@Fi = ~6Fe^~6@Fipi" |
CL-USER> (mapc (lambda (c) (format t "~&~6F~6@Fi = ~6Fe^~6@Fipi" |
||
Line 746: | Line 746: | ||
#C(0.9999999999999999D0 0.4142135623730949D0) #C(0.0D0 0.0D0) |
#C(0.9999999999999999D0 0.4142135623730949D0) #C(0.0D0 0.0D0) |
||
#C(0.9999999999999997D0 2.414213562373095D0)) |
#C(0.9999999999999997D0 2.414213562373095D0)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="ruby">require "complex" |
||
def fft(x : Array(Int32 | Float64)) #: Array(Complex) |
def fft(x : Array(Int32 | Float64)) #: Array(Complex) |
||
Line 762: | Line 762: | ||
fft([1,1,1,1,0,0,0,0]).each{ |c| puts c } |
fft([1,1,1,1,0,0,0,0]).each{ |c| puts c } |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 777: | Line 777: | ||
=={{header|D}}== |
=={{header|D}}== |
||
===Standard Version=== |
===Standard Version=== |
||
< |
<syntaxhighlight lang="d">void main() { |
||
import std.stdio, std.numeric; |
import std.stdio, std.numeric; |
||
[1.0, 1, 1, 1, 0, 0, 0, 0].fft.writeln; |
[1.0, 1, 1, 1, 0, 0, 0, 0].fft.writeln; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[4+0i, 1-2.41421i, 0-0i, 1-0.414214i, 0+0i, 1+0.414214i, 0+0i, 1+2.41421i]</pre> |
<pre>[4+0i, 1-2.41421i, 0-0i, 1-0.414214i, 0+0i, 1+0.414214i, 0+0i, 1+2.41421i]</pre> |
||
Line 787: | Line 787: | ||
===creals Version=== |
===creals Version=== |
||
Built-in complex numbers will be deprecated. |
Built-in complex numbers will be deprecated. |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, std.math; |
||
const(creal)[] fft(in creal[] x) pure /*nothrow*/ @safe { |
const(creal)[] fft(in creal[] x) pure /*nothrow*/ @safe { |
||
Line 801: | Line 801: | ||
void main() @safe { |
void main() @safe { |
||
[1.0L+0i, 1, 1, 1, 0, 0, 0, 0].fft.writeln; |
[1.0L+0i, 1, 1, 1, 0, 0, 0, 0].fft.writeln; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[4+0i, 1+-2.41421i, 0+0i, 1+-0.414214i, 0+0i, 1+0.414214i, 0+0i, 1+2.41421i]</pre> |
<pre>[4+0i, 1+-2.41421i, 0+0i, 1+-0.414214i, 0+0i, 1+0.414214i, 0+0i, 1+2.41421i]</pre> |
||
===Phobos Complex Version=== |
===Phobos Complex Version=== |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, std.math, std.complex; |
||
auto fft(T)(in T[] x) pure /*nothrow @safe*/ { |
auto fft(T)(in T[] x) pure /*nothrow @safe*/ { |
||
Line 821: | Line 821: | ||
void main() { |
void main() { |
||
[1.0, 1, 1, 1, 0, 0, 0, 0].map!complex.array.fft.writeln; |
[1.0, 1, 1, 1, 0, 0, 0, 0].map!complex.array.fft.writeln; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[4+0i, 1-2.41421i, 0+0i, 1-0.414214i, 0+0i, 1+0.414214i, 0+0i, 1+2.41421i]</pre> |
<pre>[4+0i, 1-2.41421i, 0+0i, 1-0.414214i, 0+0i, 1+0.414214i, 0+0i, 1+2.41421i]</pre> |
||
Line 829: | Line 829: | ||
{{libheader| System.Math}} |
{{libheader| System.Math}} |
||
{{Trans|C#}} |
{{Trans|C#}} |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program Fast_Fourier_transform; |
program Fast_Fourier_transform; |
||
Line 917: | Line 917: | ||
writeln(c); |
writeln(c); |
||
readln; |
readln; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>4 + 0i |
<pre>4 + 0i |
||
Line 929: | Line 929: | ||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
(define -∏*2 (complex 0 (* -2 PI))) |
(define -∏*2 (complex 0 (* -2 PI))) |
||
Line 947: | Line 947: | ||
→ #( 4+0i 1-2.414213562373095i 0+0i 1-0.4142135623730949i |
→ #( 4+0i 1-2.414213562373095i 0+0i 1-0.4142135623730949i |
||
0+0i 1+0.4142135623730949i 0+0i 1+2.414213562373095i) |
0+0i 1+0.4142135623730949i 0+0i 1+2.414213562373095i) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
<syntaxhighlight lang="erre"> |
|||
<lang ERRE> |
|||
PROGRAM FFT |
PROGRAM FFT |
||
Line 1,050: | Line 1,050: | ||
END FOR |
END FOR |
||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,069: | Line 1,069: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
<syntaxhighlight lang="factor"> |
|||
<lang Factor> |
|||
IN: USE math.transforms.fft |
IN: USE math.transforms.fft |
||
IN: { 1 1 1 1 0 0 0 0 } fft . |
IN: { 1 1 1 1 0 0 0 0 } fft . |
||
Line 1,082: | Line 1,082: | ||
C{ 0.9999999999999997 2.414213562373095 } |
C{ 0.9999999999999997 2.414213562373095 } |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
<syntaxhighlight lang="fortran"> |
|||
<lang Fortran> |
|||
module fft_mod |
module fft_mod |
||
implicit none |
implicit none |
||
Line 1,143: | Line 1,143: | ||
end do |
end do |
||
end program test</ |
end program test</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,156: | Line 1,156: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">'Graphic fast Fourier transform demo, |
||
'press any key for the next image. |
'press any key for the next image. |
||
'131072 samples: the FFT is fast indeed. |
'131072 samples: the FFT is fast indeed. |
||
Line 1,374: | Line 1,374: | ||
cls |
cls |
||
next i |
next i |
||
end</ |
end</syntaxhighlight> |
||
<b>(Images only)</b> |
<b>(Images only)</b> |
||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
Frink has a built-in FFT function that can produce results based on different conventions. The following is not the default convention, but matches many of the other results in this page. |
Frink has a built-in FFT function that can produce results based on different conventions. The following is not the default convention, but matches many of the other results in this page. |
||
< |
<syntaxhighlight lang="frink">a = FFT[[1,1,1,1,0,0,0,0], 1, -1] |
||
println[joinln[format[a, 1, 5]]] |
println[joinln[format[a, 1, 5]]] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,395: | Line 1,395: | ||
=={{header|GAP}}== |
=={{header|GAP}}== |
||
< |
<syntaxhighlight lang="gap"># Here an implementation with no optimization (O(n^2)). |
||
# In GAP, E(n) = exp(2*i*pi/n), a primitive root of the unity. |
# In GAP, E(n) = exp(2*i*pi/n), a primitive root of the unity. |
||
Line 1,417: | Line 1,417: | ||
InverseFourier(last); |
InverseFourier(last); |
||
# [ 1, 1, 1, 1, 0, 0, 0, 0 ]</ |
# [ 1, 1, 1, 1, 0, 0, 0, 0 ]</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,448: | Line 1,448: | ||
fmt.Printf("%8.4f\n", c) |
fmt.Printf("%8.4f\n", c) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,462: | Line 1,462: | ||
=={{header|Golfscript}}== |
=={{header|Golfscript}}== |
||
< |
<syntaxhighlight lang="golfscript">#Cooley-Tukey |
||
{.,.({[\.2%fft\(;2%fft@-1?-1\?-2?:w;.,,{w\?}%[\]zip{{*}*}%]zip.{{+}*}%\{{-}*}%+}{;}if}:fft; |
{.,.({[\.2%fft\(;2%fft@-1?-1\?-2?:w;.,,{w\?}%[\]zip{{*}*}%]zip.{{+}*}%\{{-}*}%+}{;}if}:fft; |
||
[1 1 1 1 0 0 0 0]fft n* |
[1 1 1 1 0 0 0 0]fft n* |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,481: | Line 1,481: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.Complex |
||
-- Cooley-Tukey |
-- Cooley-Tukey |
||
Line 1,497: | Line 1,497: | ||
exp' k n = cis $ -2 * pi * (fromIntegral k) / (fromIntegral n) |
exp' k n = cis $ -2 * pi * (fromIntegral k) / (fromIntegral n) |
||
main = mapM_ print $ fft [1,1,1,1,0,0,0,0]</ |
main = mapM_ print $ fft [1,1,1,1,0,0,0,0]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,511: | Line 1,511: | ||
=={{header|Idris}}== |
=={{header|Idris}}== |
||
<lang>module Main |
<syntaxhighlight lang="text">module Main |
||
import Data.Complex |
import Data.Complex |
||
Line 1,541: | Line 1,541: | ||
main : IO() |
main : IO() |
||
main = traverse_ printLn $ fft [1,1,1,1,0,0,0,0]</ |
main = traverse_ printLn $ fft [1,1,1,1,0,0,0,0]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,558: | Line 1,558: | ||
Based on [[j:Essays/FFT]], with some simplifications -- sacrificing accuracy, optimizations and convenience which are not relevant to the task requirements, for clarity: |
Based on [[j:Essays/FFT]], with some simplifications -- sacrificing accuracy, optimizations and convenience which are not relevant to the task requirements, for clarity: |
||
< |
<syntaxhighlight lang="j">cube =: ($~ q:@#) :. , |
||
rou =: ^@j.@o.@(% #)@i.@-: NB. roots of unity |
rou =: ^@j.@o.@(% #)@i.@-: NB. roots of unity |
||
floop =: 4 : 'for_r. i.#$x do. (y=.{."1 y) ] x=.(+/x) ,&,:"r (-/x)*y end.' |
floop =: 4 : 'for_r. i.#$x do. (y=.{."1 y) ] x=.(+/x) ,&,:"r (-/x)*y end.' |
||
fft =: ] floop&.cube rou@#</ |
fft =: ] floop&.cube rou@#</syntaxhighlight> |
||
Example (first row of result is sine, second row of result is fft of the first row, (**+)&.+. cleans an irrelevant least significant bit of precision from the result so that it displays nicely): |
Example (first row of result is sine, second row of result is fft of the first row, (**+)&.+. cleans an irrelevant least significant bit of precision from the result so that it displays nicely): |
||
< |
<syntaxhighlight lang="j"> (**+)&.+. (,: fft) 1 o. 2p1*3r16 * i.16 |
||
0 0.92388 0.707107 0.382683 1 0.382683 0.707107 0.92388 0 0.92388 0.707107 0.382683 1 0.382683 0.707107 0.92388 |
0 0.92388 0.707107 0.382683 1 0.382683 0.707107 0.92388 0 0.92388 0.707107 0.382683 1 0.382683 0.707107 0.92388 |
||
0 0 0 0j8 0 0 0 0 0 0 0 0 0 0j8 0 0</ |
0 0 0 0j8 0 0 0 0 0 0 0 0 0 0j8 0 0</syntaxhighlight> |
||
Here is a representation of an example which appears in some of the other implementations, here: |
Here is a representation of an example which appears in some of the other implementations, here: |
||
< |
<syntaxhighlight lang="j"> Re=: {.@+.@fft |
||
Im=: {:@+.@fft |
Im=: {:@+.@fft |
||
M=: 4#1 0 |
M=: 4#1 0 |
||
Line 1,578: | Line 1,578: | ||
4 1 0 1 0 1 0 1 |
4 1 0 1 0 1 0 1 |
||
Im M |
Im M |
||
0 2.41421 0 0.414214 0 _0.414214 0 _2.41421</ |
0 2.41421 0 0.414214 0 _0.414214 0 _2.41421</syntaxhighlight> |
||
Note that Re and Im are not functions of 1 and 0 but are functions of the complete sequence. |
Note that Re and Im are not functions of 1 and 0 but are functions of the complete sequence. |
||
Line 1,586: | Line 1,586: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|C sharp}} |
{{trans|C sharp}} |
||
< |
<syntaxhighlight lang="java">import static java.lang.Math.*; |
||
public class FastFourierTransform { |
public class FastFourierTransform { |
||
Line 1,680: | Line 1,680: | ||
return String.format("(%f,%f)", re, im); |
return String.format("(%f,%f)", re, im); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<pre>Results: |
<pre>Results: |
||
Line 1,696: | Line 1,696: | ||
variants on this page. |
variants on this page. |
||
< |
<syntaxhighlight lang="javascript">/* |
||
complex fast fourier transform and inverse from |
complex fast fourier transform and inverse from |
||
http://rosettacode.org/wiki/Fast_Fourier_transform#C.2B.2B |
http://rosettacode.org/wiki/Fast_Fourier_transform#C.2B.2B |
||
Line 1,761: | Line 1,761: | ||
//test code |
//test code |
||
//console.log( cfft([1,1,1,1,0,0,0,0]) ); |
//console.log( cfft([1,1,1,1,0,0,0,0]) ); |
||
//console.log( icfft(cfft([1,1,1,1,0,0,0,0])) );</ |
//console.log( icfft(cfft([1,1,1,1,0,0,0,0])) );</syntaxhighlight> |
||
Very very basic Complex number that provides only the components |
Very very basic Complex number that provides only the components |
||
required by the code above. |
required by the code above. |
||
< |
<syntaxhighlight lang="javascript">/* |
||
basic complex number arithmetic from |
basic complex number arithmetic from |
||
http://rosettacode.org/wiki/Fast_Fourier_transform#Scala |
http://rosettacode.org/wiki/Fast_Fourier_transform#Scala |
||
Line 1,813: | Line 1,813: | ||
else |
else |
||
console.log(this.re.toString()+'+'+this.im.toString()+'j'); |
console.log(this.re.toString()+'+'+this.im.toString()+'j'); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
Currently jq has no support for complex numbers, so the following implementation uses [x,y] to represent the complex number x+iy. |
Currently jq has no support for complex numbers, so the following implementation uses [x,y] to represent the complex number x+iy. |
||
====Complex number arithmetic==== |
====Complex number arithmetic==== |
||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
# multiplication of real or complex numbers |
# multiplication of real or complex numbers |
||
def cmult(x; y): |
def cmult(x; y): |
||
Line 1,841: | Line 1,841: | ||
# e(ix) = cos(x) + i sin(x) |
# e(ix) = cos(x) + i sin(x) |
||
def expi(x): [ (x|cos), (x|sin) ];</ |
def expi(x): [ (x|cos), (x|sin) ];</syntaxhighlight> |
||
====FFT==== |
====FFT==== |
||
< |
<syntaxhighlight lang="jq">def fft: |
||
length as $N |
length as $N |
||
| if $N <= 1 then . |
| if $N <= 1 then . |
||
Line 1,851: | Line 1,851: | ||
| [ range(0; $N/2) | cplus($even[.]; cmult( expi(-2*$pi*./$N); $odd[.] )) ] + |
| [ range(0; $N/2) | cplus($even[.]; cmult( expi(-2*$pi*./$N); $odd[.] )) ] + |
||
[ range(0; $N/2) | cminus($even[.]; cmult( expi(-2*$pi*./$N); $odd[.] )) ] |
[ range(0; $N/2) | cminus($even[.]; cmult( expi(-2*$pi*./$N); $odd[.] )) ] |
||
end;</ |
end;</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<syntaxhighlight lang="jq">[1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0] | fft |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
[[4,-0],[1,-2.414213562373095], |
[[4,-0],[1,-2.414213562373095], |
||
Line 1,862: | Line 1,862: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">using FFTW # or using DSP |
||
fft([1,1,1,1,0,0,0,0])</ |
fft([1,1,1,1,0,0,0,0])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="julia">8-element Array{Complex{Float64},1}: |
||
4.0+0.0im |
4.0+0.0im |
||
1.0-2.41421im |
1.0-2.41421im |
||
Line 1,874: | Line 1,874: | ||
1.0+0.414214im |
1.0+0.414214im |
||
0.0+0.0im |
0.0+0.0im |
||
1.0+2.41421im</ |
1.0+2.41421im</syntaxhighlight> |
||
An implementation of the radix-2 algorithm, which works for any vector for length that is a power of 2: |
An implementation of the radix-2 algorithm, which works for any vector for length that is a power of 2: |
||
< |
<syntaxhighlight lang="julia"> |
||
function fft(a) |
function fft(a) |
||
y1 = Any[]; y2 = Any[] |
y1 = Any[]; y2 = Any[] |
||
Line 1,893: | Line 1,893: | ||
return vcat(y1,y2) |
return vcat(y1,y2) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Klong}}== |
=={{header|Klong}}== |
||
< |
<syntaxhighlight lang="k">fft::{ff2::{[n e o p t k];n::#x; |
||
f::{p::2:#x;e::ff2(*'p);o::ff2({x@1}'p);k::-1; |
f::{p::2:#x;e::ff2(*'p);o::ff2({x@1}'p);k::-1; |
||
t::{k::k+1;cmul(cexp(cdiv(cmul([0 -2];(k*pi),0);n,0));x)}'o; |
t::{k::k+1;cmul(cexp(cdiv(cmul([0 -2];(k*pi),0);n,0));x)}'o; |
||
(e cadd't),e csub't}; |
(e cadd't),e csub't}; |
||
:[n<2;x;f(x)]}; |
:[n<2;x;f(x)]}; |
||
n::#x;k::{(2^x)<n}{1+x}:~1;n#ff2({x,0}'x,&(2^k)-n)}</ |
n::#x;k::{(2^x)<n}{1+x}:~1;n#ff2({x,0}'x,&(2^k)-n)}</syntaxhighlight> |
||
Example (rounding to 4 decimal digits): |
Example (rounding to 4 decimal digits): |
||
< |
<syntaxhighlight lang="k"> all(rndn(;4);fft([1 1 1 1 0 0 0 0]))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[[4.0 0.0] |
<pre>[[4.0 0.0] |
||
Line 1,916: | Line 1,916: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
From Scala. |
From Scala. |
||
< |
<syntaxhighlight lang="scala">import java.lang.Math.* |
||
class Complex(val re: Double, val im: Double) { |
class Complex(val re: Double, val im: Double) { |
||
Line 1,935: | Line 1,935: | ||
private val a = "%1.3f".format(re) |
private val a = "%1.3f".format(re) |
||
private val b = "%1.3f".format(abs(im)) |
private val b = "%1.3f".format(abs(im)) |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="scala">object FFT { |
||
fun fft(a: Array<Complex>) = _fft(a, Complex(0.0, 2.0), 1.0) |
fun fft(a: Array<Complex>) = _fft(a, Complex(0.0, 2.0), 1.0) |
||
fun rfft(a: Array<Complex>) = _fft(a, Complex(0.0, -2.0), 2.0) |
fun rfft(a: Array<Complex>) = _fft(a, Complex(0.0, -2.0), 2.0) |
||
Line 1,964: | Line 1,964: | ||
left + right |
left + right |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="scala">fun Array<*>.println() = println(joinToString(prefix = "[", postfix = "]")) |
||
fun main(args: Array<String>) { |
fun main(args: Array<String>) { |
||
Line 1,975: | Line 1,975: | ||
a.println() |
a.println() |
||
FFT.rfft(a).println() |
FFT.rfft(a).println() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,982: | Line 1,982: | ||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
1) the function fft |
1) the function fft |
||
Line 2,093: | Line 2,093: | ||
A more usefull example can be seen in http://lambdaway.free.fr/lambdaspeech/?view=zorg |
A more usefull example can be seen in http://lambdaway.free.fr/lambdaspeech/?view=zorg |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
<syntaxhighlight lang="lb"> |
|||
<lang lb> |
|||
P =8 |
P =8 |
||
S =int( log( P) /log( 2) +0.9999) |
S =int( log( P) /log( 2) +0.9999) |
||
Line 2,202: | Line 2,202: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
M Re( M) Im( M) |
M Re( M) Im( M) |
||
Line 2,215: | Line 2,215: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">-- operations on complex number |
||
complex = {__mt={} } |
complex = {__mt={} } |
||
Line 2,286: | Line 2,286: | ||
-- Beginning with Lua 5.2 you have to write |
-- Beginning with Lua 5.2 you have to write |
||
print("orig:", table.unpack(data)) |
print("orig:", table.unpack(data)) |
||
print("fft:", table.unpack(fft(data)))</ |
print("fft:", table.unpack(fft(data)))</syntaxhighlight> |
||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
Maple has a built-in package DiscreteTransforms, and FourierTransform and InverseFourierTransform are in the commands available from that package. The FourierTransform command offers an FFT method by default. |
Maple has a built-in package DiscreteTransforms, and FourierTransform and InverseFourierTransform are in the commands available from that package. The FourierTransform command offers an FFT method by default. |
||
<syntaxhighlight lang="maple"> |
|||
<lang Maple> |
|||
with( DiscreteTransforms ): |
with( DiscreteTransforms ): |
||
FourierTransform( <1,1,1,1,0,0,0,0>, normalization=none ); |
FourierTransform( <1,1,1,1,0,0,0,0>, normalization=none ); |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
Line 2,315: | Line 2,315: | ||
</pre> |
</pre> |
||
Optionally, the FFT may be performed inplace on a Vector of hardware double-precision complex floats. |
Optionally, the FFT may be performed inplace on a Vector of hardware double-precision complex floats. |
||
<syntaxhighlight lang="maple"> |
|||
<lang Maple> |
|||
v := Vector( [1,1,1,1,0,0,0,0], datatype=complex[8] ): |
v := Vector( [1,1,1,1,0,0,0,0], datatype=complex[8] ): |
||
Line 2,321: | Line 2,321: | ||
v; |
v; |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
Line 2,340: | Line 2,340: | ||
[1. + 2.41421356237309 I ] |
[1. + 2.41421356237309 I ] |
||
</pre> |
</pre> |
||
<syntaxhighlight lang="maple"> |
|||
<lang Maple> |
|||
InverseFourierTransform( v, normalization=full, inplace ): |
InverseFourierTransform( v, normalization=full, inplace ): |
||
v; |
v; |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
Line 2,371: | Line 2,371: | ||
produce output that is consistent with most other FFT routines. |
produce output that is consistent with most other FFT routines. |
||
<syntaxhighlight lang="mathematica"> |
|||
<lang Mathematica> |
|||
Fourier[{1,1,1,1,0,0,0,0}, FourierParameters->{1,-1}] |
Fourier[{1,1,1,1,0,0,0,0}, FourierParameters->{1,-1}] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,380: | Line 2,380: | ||
Here is a user-space definition for good measure. |
Here is a user-space definition for good measure. |
||
< |
<syntaxhighlight lang="mathematica">fft[{x_}] := {N@x} |
||
fft[l__] := |
fft[l__] := |
||
Join[#, #] &@fft@l[[1 ;; ;; 2]] + |
Join[#, #] &@fft@l[[1 ;; ;; 2]] + |
||
Line 2,386: | Line 2,386: | ||
fft[l[[2 ;; ;; 2]]]) |
fft[l[[2 ;; ;; 2]]]) |
||
fft[{1, 1, 1, 1, 0, 0, 0, 0}] // Column</ |
fft[{1, 1, 1, 1, 0, 0, 0, 0}] // Column</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>4. |
<pre>4. |
||
Line 2,402: | Line 2,402: | ||
Matlab/Octave have a builtin FFT function. |
Matlab/Octave have a builtin FFT function. |
||
< |
<syntaxhighlight lang="matlab"> fft([1,1,1,1,0,0,0,0]') |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>ans = |
<pre>ans = |
||
Line 2,417: | Line 2,417: | ||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
< |
<syntaxhighlight lang="maxima">load(fft)$ |
||
fft([1, 2, 3, 4]); |
fft([1, 2, 3, 4]); |
||
[2.5, -0.5 * %i - 0.5, -0.5, 0.5 * %i - 0.5]</ |
[2.5, -0.5 * %i - 0.5, -0.5, 0.5 * %i - 0.5]</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="nim">import math, complex, strutils |
||
# Works with floats and complex numbers as input |
# Works with floats and complex numbers as input |
||
Line 2,450: | Line 2,450: | ||
for i in fft(@[1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]): |
for i in fft(@[1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]): |
||
echo formatFloat(abs(i), ffDecimal, 3)</ |
echo formatFloat(abs(i), ffDecimal, 3)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>4.000 |
<pre>4.000 |
||
Line 2,463: | Line 2,463: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
This is a simple implementation of the Cooley-Tukey pseudo-code |
This is a simple implementation of the Cooley-Tukey pseudo-code |
||
< |
<syntaxhighlight lang="ocaml">open Complex |
||
let fac k n = |
let fac k n = |
||
Line 2,487: | Line 2,487: | ||
let indata = [one;one;one;one;zero;zero;zero;zero] in |
let indata = [one;one;one;one;zero;zero;zero;zero] in |
||
show indata; |
show indata; |
||
show (fft indata)</ |
show (fft indata)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,497: | Line 2,497: | ||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |
||
{{trans|PL/I}} Output as shown in REXX |
{{trans|PL/I}} Output as shown in REXX |
||
< |
<syntaxhighlight lang="oorexx">Numeric Digits 16 |
||
list='1 1 1 1 0 0 0 0' |
list='1 1 1 1 0 0 0 0' |
||
n=words(list) |
n=words(list) |
||
Line 2,614: | Line 2,614: | ||
else return "-" value~abs |
else return "-" value~abs |
||
::requires rxMath library</ |
::requires rxMath library</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>---data--- num real-part imaginary-part |
<pre>---data--- num real-part imaginary-part |
||
Line 2,639: | Line 2,639: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
Naive implementation, using the same testcase as Ada: |
Naive implementation, using the same testcase as Ada: |
||
< |
<syntaxhighlight lang="parigp">FFT(v)=my(t=-2*Pi*I/#v,tt);vector(#v,k,tt=t*(k-1);sum(n=0,#v-1,v[n+1]*exp(tt*n))); |
||
FFT([1,1,1,1,0,0,0,0])</ |
FFT([1,1,1,1,0,0,0,0])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[4.0000000000000000000000000000000000000, 1.0000000000000000000000000000000000000 - 2.4142135623730950488016887242096980786*I, 0.E-37 + 0.E-38*I, 1.0000000000000000000000000000000000000 - 0.41421356237309504880168872420969807856*I, 0.E-38 + 0.E-37*I, 0.99999999999999999999999999999999999997 + 0.41421356237309504880168872420969807860*I, 4.701977403289150032 E-38 + 0.E-38*I, 0.99999999999999999999999999999999999991 + 2.4142135623730950488016887242096980785*I]</pre> |
<pre>[4.0000000000000000000000000000000000000, 1.0000000000000000000000000000000000000 - 2.4142135623730950488016887242096980786*I, 0.E-37 + 0.E-38*I, 1.0000000000000000000000000000000000000 - 0.41421356237309504880168872420969807856*I, 0.E-38 + 0.E-37*I, 0.99999999999999999999999999999999999997 + 0.41421356237309504880168872420969807860*I, 4.701977403289150032 E-38 + 0.E-38*I, 0.99999999999999999999999999999999999991 + 2.4142135623730950488016887242096980785*I]</pre> |
||
differently, and even with "graphics" |
differently, and even with "graphics" |
||
< |
<syntaxhighlight lang="parigp">install( FFTinit, Lp ); |
||
install( FFT, GG ); |
install( FFT, GG ); |
||
k = 7; N = 2 ^ k; |
k = 7; N = 2 ^ k; |
||
Line 2,654: | Line 2,654: | ||
\\plot( i = 1, N, v[ floor(i) ] ); |
\\plot( i = 1, N, v[ floor(i) ] ); |
||
print("Spectrum"); |
print("Spectrum"); |
||
plot( i = 1, N / 2 , abs( w[floor(i)] ) * 2 / N );</ |
plot( i = 1, N / 2 , abs( w[floor(i)] ) * 2 / N );</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Spectrum |
<pre>Spectrum |
||
Line 2,684: | Line 2,684: | ||
=== Recursive === |
=== Recursive === |
||
{{works with|Free Pascal|3.2.0 }} |
{{works with|Free Pascal|3.2.0 }} |
||
< |
<syntaxhighlight lang="pascal"> |
||
PROGRAM RDFT; |
PROGRAM RDFT; |
||
Line 2,872: | Line 2,872: | ||
</syntaxhighlight> |
|||
</lang> |
|||
JPD 2021/12/26 |
JPD 2021/12/26 |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use Math::Complex; |
use Math::Complex; |
||
Line 2,892: | Line 2,892: | ||
} |
} |
||
print "$_\n" for fft qw(1 1 1 1 0 0 0 0);</ |
print "$_\n" for fft qw(1 1 1 1 0 0 0 0);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>4 |
<pre>4 |
||
Line 2,904: | Line 2,904: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #000080;font-style:italic;">-- |
<span style="color: #000080;font-style:italic;">-- |
||
-- demo\rosetta\FastFourierTransform.exw |
-- demo\rosetta\FastFourierTransform.exw |
||
Line 3,035: | Line 3,035: | ||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"\nResults of %d-point inverse fft (rounded to 6 d.p.):\n\n"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">))</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"\nResults of %d-point inverse fft (rounded to 6 d.p.):\n\n"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">))</span> |
||
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ifft</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fft</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)))</span> |
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ifft</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fft</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)))</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,065: | Line 3,065: | ||
Complex Class File: |
Complex Class File: |
||
<syntaxhighlight lang="php"> |
|||
<lang PHP> |
|||
<?php |
<?php |
||
Line 3,107: | Line 3,107: | ||
} |
} |
||
</ |
</syntaxhighlight> |
||
Example: |
Example: |
||
<syntaxhighlight lang="php"> |
|||
<lang PHP> |
|||
<?php |
<?php |
||
Line 3,213: | Line 3,213: | ||
echo PHP_EOL; |
echo PHP_EOL; |
||
</syntaxhighlight> |
|||
</lang> |
|||
Line 3,254: | Line 3,254: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
{{works with|PicoLisp|3.1.0.3}} |
{{works with|PicoLisp|3.1.0.3}} |
||
< |
<syntaxhighlight lang="picolisp"># apt-get install libfftw3-dev |
||
(scl 4) |
(scl 4) |
||
Line 3,273: | Line 3,273: | ||
(native "libfftw3.so" "fftw_destroy_plan" NIL P) |
(native "libfftw3.so" "fftw_destroy_plan" NIL P) |
||
(native "libfftw3.so" "fftw_free" NIL Out) |
(native "libfftw3.so" "fftw_free" NIL Out) |
||
(native "libfftw3.so" "fftw_free" NIL In) ) ) )</ |
(native "libfftw3.so" "fftw_free" NIL In) ) ) )</syntaxhighlight> |
||
Test: |
Test: |
||
< |
<syntaxhighlight lang="picolisp">(for R (fft '((1.0 0) (1.0 0) (1.0 0) (1.0 0) (0 0) (0 0) (0 0) (0 0))) |
||
(tab (6 8) |
(tab (6 8) |
||
(round (car R)) |
(round (car R)) |
||
(round (cadr R)) ) )</ |
(round (cadr R)) ) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 4.000 0.000 |
<pre> 4.000 0.000 |
||
Line 3,290: | Line 3,290: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pl/i">test: PROCEDURE OPTIONS (MAIN, REORDER); /* Derived from Fortran Rosetta Code */ |
||
/* In-place Cooley-Tukey FFT */ |
/* In-place Cooley-Tukey FFT */ |
||
Line 3,338: | Line 3,338: | ||
end; |
end; |
||
END test;</ |
END test;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 4.000000000000+0.000000000000I |
<pre> 4.000000000000+0.000000000000I |
||
Line 3,350: | Line 3,350: | ||
=={{header|POV-Ray}}== |
=={{header|POV-Ray}}== |
||
<syntaxhighlight lang="pov-ray"> |
|||
<lang POV-Ray> |
|||
//cmd: +w0 +h0 -F -D |
//cmd: +w0 +h0 -F -D |
||
//Stockham algorithm |
//Stockham algorithm |
||
Line 3,448: | Line 3,448: | ||
CdebugArr(cdata) |
CdebugArr(cdata) |
||
#debug "\n" |
#debug "\n" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,495: | Line 3,495: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
< |
<syntaxhighlight lang="powershell">Function FFT($Arr){ |
||
$Len = $Arr.Count |
$Len = $Arr.Count |
||
Line 3,527: | Line 3,527: | ||
Return $Output |
Return $Output |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,546: | Line 3,546: | ||
{{trans|Python}}Note: Similar algorithmically to the python example. |
{{trans|Python}}Note: Similar algorithmically to the python example. |
||
{{works with|SWI Prolog|Version 6.2.6 by Jan Wielemaker, University of Amsterdam}} |
{{works with|SWI Prolog|Version 6.2.6 by Jan Wielemaker, University of Amsterdam}} |
||
< |
<syntaxhighlight lang="prolog">:- dynamic twiddles/2. |
||
%_______________________________________________________________ |
%_______________________________________________________________ |
||
% Arithemetic for complex numbers; only the needed rules |
% Arithemetic for complex numbers; only the needed rules |
||
Line 3,583: | Line 3,583: | ||
write(R), (I>=0, write('+'),fail;write(I)), write('j, '), |
write(R), (I>=0, write('+'),fail;write(I)), write('j, '), |
||
fail; write(']'), nl). |
fail; write(']'), nl). |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,593: | Line 3,593: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Python: Recursive=== |
===Python: Recursive=== |
||
< |
<syntaxhighlight lang="python">from cmath import exp, pi |
||
def fft(x): |
def fft(x): |
||
Line 3,605: | Line 3,605: | ||
print( ' '.join("%5.3f" % abs(f) |
print( ' '.join("%5.3f" % abs(f) |
||
for f in fft([1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0])) )</ |
for f in fft([1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0])) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,611: | Line 3,611: | ||
===Python: Using module [http://numpy.scipy.org/ numpy]=== |
===Python: Using module [http://numpy.scipy.org/ numpy]=== |
||
< |
<syntaxhighlight lang="python">>>> from numpy.fft import fft |
||
>>> from numpy import array |
>>> from numpy import array |
||
>>> a = array([1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]) |
>>> a = array([1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]) |
||
>>> print( ' '.join("%5.3f" % abs(f) for f in fft(a)) ) |
>>> print( ' '.join("%5.3f" % abs(f) for f in fft(a)) ) |
||
4.000 2.613 0.000 1.082 0.000 1.082 0.000 2.613</ |
4.000 2.613 0.000 1.082 0.000 1.082 0.000 2.613</syntaxhighlight> |
||
=={{header|R}}== |
=={{header|R}}== |
||
The function "fft" is readily available in R |
The function "fft" is readily available in R |
||
< |
<syntaxhighlight lang="r">fft(c(1,1,1,1,0,0,0,0))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>4+0.000000i 1-2.414214i 0+0.000000i 1-0.414214i 0+0.000000i 1+0.414214i 0+0.000000i 1+2.414214i</pre> |
<pre>4+0.000000i 1-2.414214i 0+0.000000i 1-0.414214i 0+0.000000i 1+0.414214i 0+0.000000i 1+2.414214i</pre> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
(require math) |
(require math) |
||
(array-fft (array #[1. 1. 1. 1. 0. 0. 0. 0.])) |
(array-fft (array #[1. 1. 1. 1. 0. 0. 0. 0.])) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,646: | Line 3,646: | ||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{Works with|rakudo 2015-12}} |
{{Works with|rakudo 2015-12}} |
||
<lang |
<syntaxhighlight lang="raku" line>sub fft { |
||
return @_ if @_ == 1; |
return @_ if @_ == 1; |
||
my @evn = fft( @_[0, 2 ... *] ); |
my @evn = fft( @_[0, 2 ... *] ); |
||
Line 3,654: | Line 3,654: | ||
} |
} |
||
.say for fft <1 1 1 1 0 0 0 0>;</ |
.say for fft <1 1 1 1 0 0 0 0>;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>4+0i |
<pre>4+0i |
||
Line 3,667: | Line 3,667: | ||
For the fun of it, here is a purely functional version: |
For the fun of it, here is a purely functional version: |
||
<lang |
<syntaxhighlight lang="raku" line>sub fft { |
||
@_ == 1 ?? @_ !! |
@_ == 1 ?? @_ !! |
||
fft(@_[0,2...*]) «+« |
fft(@_[0,2...*]) «+« |
||
fft(@_[1,3...*]) «*« map &cis, (0,-τ/@_...^-τ) |
fft(@_[1,3...*]) «*« map &cis, (0,-τ/@_...^-τ) |
||
}</ |
}</syntaxhighlight> |
||
This particular version is numerically inaccurate though, because of the pi approximation. It is possible to fix it with the 'cisPi' function available in the TrigPi module: |
This particular version is numerically inaccurate though, because of the pi approximation. It is possible to fix it with the 'cisPi' function available in the TrigPi module: |
||
<lang |
<syntaxhighlight lang="raku" line>sub fft { |
||
use TrigPi; |
use TrigPi; |
||
@_ == 1 ?? @_ !! |
@_ == 1 ?? @_ !! |
||
fft(@_[0,2...*]) «+« |
fft(@_[0,2...*]) «+« |
||
fft(@_[1,3...*]) «*« map &cisPi, (0,-2/@_...^-2) |
fft(@_[1,3...*]) «*« map &cisPi, (0,-2/@_...^-2) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 3,694: | Line 3,694: | ||
This REXX program also adds zero values if the number of data points in the list doesn't exactly equal to a |
This REXX program also adds zero values if the number of data points in the list doesn't exactly equal to a |
||
<br>power of two. This is known as ''zero-padding''. |
<br>power of two. This is known as ''zero-padding''. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program performs a fast Fourier transform (FFT) on a set of complex numbers. */ |
||
numeric digits length( pi() ) - length(.) /*limited by the PI function result. */ |
numeric digits length( pi() ) - length(.) /*limited by the PI function result. */ |
||
arg data /*ARG verb uppercases the DATA from CL.*/ |
arg data /*ARG verb uppercases the DATA from CL.*/ |
||
Line 3,761: | Line 3,761: | ||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
pi: return 3.1415926535897932384626433832795028841971693993751058209749445923078164062862 |
pi: return 3.1415926535897932384626433832795028841971693993751058209749445923078164062862 |
||
r2r: return arg(1) // ( pi() * 2 ) /*reduce the radians to a unit circle. */</ |
r2r: return arg(1) // ( pi() * 2 ) /*reduce the radians to a unit circle. */</syntaxhighlight> |
||
Programming note: the numeric precision (decimal digits) is only restricted by the number of decimal digits in the |
Programming note: the numeric precision (decimal digits) is only restricted by the number of decimal digits in the |
||
<br>'''pi''' variable (which is defined in the penultimate assignment statement in the REXX program. |
<br>'''pi''' variable (which is defined in the penultimate assignment statement in the REXX program. |
||
Line 3,793: | Line 3,793: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">def fft(vec) |
||
return vec if vec.size <= 1 |
return vec if vec.size <= 1 |
||
evens_odds = vec.partition.with_index{|_,i| i.even?} |
evens_odds = vec.partition.with_index{|_,i| i.even?} |
||
Line 3,802: | Line 3,802: | ||
end |
end |
||
fft([1,1,1,1,0,0,0,0]).each{|c| puts "%9.6f %+9.6fi" % c.rect}</ |
fft([1,1,1,1,0,0,0,0]).each{|c| puts "%9.6f %+9.6fi" % c.rect}</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 3,816: | Line 3,816: | ||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">cnt = 8 |
||
sig = int(log(cnt) /log(2) +0.9999) |
sig = int(log(cnt) /log(2) +0.9999) |
||
Line 3,915: | Line 3,915: | ||
print " "; i;" ";using("##.#",rel(i));" ";img(i) |
print " "; i;" ";using("##.#",rel(i));" ";img(i) |
||
next i |
next i |
||
end</ |
end</syntaxhighlight> |
||
<pre> Num real imag |
<pre> Num real imag |
||
0 4.0 0 |
0 4.0 0 |
||
Line 3,928: | Line 3,928: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="rust">extern crate num; |
||
use num::complex::Complex; |
use num::complex::Complex; |
||
use std::f64::consts::PI; |
use std::f64::consts::PI; |
||
Line 3,988: | Line 3,988: | ||
let output = fft(&input); |
let output = fft(&input); |
||
show("output:", &output); |
show("output:", &output); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,002: | Line 4,002: | ||
{{works with|Scala|2.10.4}} |
{{works with|Scala|2.10.4}} |
||
Imports and Complex arithmetic: |
Imports and Complex arithmetic: |
||
< |
<syntaxhighlight lang="scala">import scala.math.{ Pi, cos, sin, cosh, sinh, abs } |
||
case class Complex(re: Double, im: Double) { |
case class Complex(re: Double, im: Double) { |
||
Line 4,026: | Line 4,026: | ||
val r = (cosh(c.re) + sinh(c.re)) |
val r = (cosh(c.re) + sinh(c.re)) |
||
Complex(cos(c.im), sin(c.im)) * r |
Complex(cos(c.im), sin(c.im)) * r |
||
}</ |
}</syntaxhighlight> |
||
The FFT definition itself: |
The FFT definition itself: |
||
< |
<syntaxhighlight lang="scala">def _fft(cSeq: Seq[Complex], direction: Complex, scalar: Int): Seq[Complex] = { |
||
if (cSeq.length == 1) { |
if (cSeq.length == 1) { |
||
return cSeq |
return cSeq |
||
Line 4,053: | Line 4,053: | ||
def fft(cSeq: Seq[Complex]): Seq[Complex] = _fft(cSeq, Complex(0, 2), 1) |
def fft(cSeq: Seq[Complex]): Seq[Complex] = _fft(cSeq, Complex(0, 2), 1) |
||
def rfft(cSeq: Seq[Complex]): Seq[Complex] = _fft(cSeq, Complex(0, -2), 2)</ |
def rfft(cSeq: Seq[Complex]): Seq[Complex] = _fft(cSeq, Complex(0, -2), 2)</syntaxhighlight> |
||
Usage: |
Usage: |
||
< |
<syntaxhighlight lang="scala">val data = Seq(Complex(1,0), Complex(1,0), Complex(1,0), Complex(1,0), |
||
Complex(0,0), Complex(0,2), Complex(0,0), Complex(0,0)) |
Complex(0,0), Complex(0,2), Complex(0,0), Complex(0,0)) |
||
println(fft(data)) |
println(fft(data)) |
||
println(rfft(fft(data)))</ |
println(rfft(fft(data)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,067: | Line 4,067: | ||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
{{works with|Chez Scheme}} |
{{works with|Chez Scheme}} |
||
< |
<syntaxhighlight lang="scheme">; Compute and return the FFT of the given input vector using the Cooley-Tukey Radix-2 |
||
; Decimation-in-Time (DIT) algorithm. The input is assumed to be a vector of complex |
; Decimation-in-Time (DIT) algorithm. The input is assumed to be a vector of complex |
||
; numbers that is a power of two in length greater than zero. |
; numbers that is a power of two in length greater than zero. |
||
Line 4,099: | Line 4,099: | ||
(dft (fft-r2dit inp))) |
(dft (fft-r2dit inp))) |
||
(printf "In: ~a~%" inp) |
(printf "In: ~a~%" inp) |
||
(printf "DFT: ~a~%" dft))</ |
(printf "DFT: ~a~%" dft))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,110: | Line 4,110: | ||
Scilab has a builtin FFT function. |
Scilab has a builtin FFT function. |
||
< |
<syntaxhighlight lang="scilab">fft([1,1,1,1,0,0,0,0]')</syntaxhighlight> |
||
=={{header|SequenceL}}== |
=={{header|SequenceL}}== |
||
< |
<syntaxhighlight lang="sequencel">import <Utilities/Complex.sl>; |
||
import <Utilities/Math.sl>; |
import <Utilities/Math.sl>; |
||
import <Utilities/Sequence.sl>; |
import <Utilities/Sequence.sl>; |
||
Line 4,130: | Line 4,130: | ||
x when n <= 1 |
x when n <= 1 |
||
else |
else |
||
complexAdd(top,z) ++ complexSubtract(top,z);</ |
complexAdd(top,z) ++ complexSubtract(top,z);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,140: | Line 4,140: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="ruby">func fft(arr) { |
||
arr.len == 1 && return arr |
arr.len == 1 && return arr |
||
Line 4,155: | Line 4,155: | ||
var wave = sequence.map {|n| ::sin(n * Num.tau / sequence.len * cycles) } |
var wave = sequence.map {|n| ::sin(n * Num.tau / sequence.len * cycles) } |
||
say "wave:#{wave.map{|w| '%6.3f' % w }.join(' ')}" |
say "wave:#{wave.map{|w| '%6.3f' % w }.join(' ')}" |
||
say "fft: #{fft(wave).map { '%6.3f' % .abs }.join(' ')}"</ |
say "fft: #{fft(wave).map { '%6.3f' % .abs }.join(' ')}"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,167: | Line 4,167: | ||
See the '''[https://www.stata.com/help.cgi?mf_fft fft function]''' in Mata help, and in the FAQ: [https://www.stata.com/support/faqs/mata/discrete-fast-fourier-transform/ How can I calculate the Fourier coefficients of a discretely sampled function in Stata?]. |
See the '''[https://www.stata.com/help.cgi?mf_fft fft function]''' in Mata help, and in the FAQ: [https://www.stata.com/support/faqs/mata/discrete-fast-fourier-transform/ How can I calculate the Fourier coefficients of a discretely sampled function in Stata?]. |
||
<lang>. mata |
<syntaxhighlight lang="text">. mata |
||
: a=1,2,3,4 |
: a=1,2,3,4 |
||
: fft(a) |
: fft(a) |
||
Line 4,174: | Line 4,174: | ||
1 | 10 -2 - 2i -2 -2 + 2i | |
1 | 10 -2 - 2i -2 -2 + 2i | |
||
+-----------------------------------------+ |
+-----------------------------------------+ |
||
: end</ |
: end</syntaxhighlight> |
||
=== fft command === |
=== fft command === |
||
Stata can also compute FFT using the undocumented '''fft''' command. Here is an example showing its syntax. A time variable must have been set prior to calling this command. Notice that in order to get the same result as Mata's fft() function, in both the input and the output variables the imaginary part must be passed '''first'''. |
Stata can also compute FFT using the undocumented '''fft''' command. Here is an example showing its syntax. A time variable must have been set prior to calling this command. Notice that in order to get the same result as Mata's fft() function, in both the input and the output variables the imaginary part must be passed '''first'''. |
||
< |
<syntaxhighlight lang="stata">clear |
||
set obs 4 |
set obs 4 |
||
gen t=_n |
gen t=_n |
||
Line 4,186: | Line 4,186: | ||
tsset t |
tsset t |
||
fft y x, gen(v u) |
fft y x, gen(v u) |
||
list u v, noobs</ |
list u v, noobs</syntaxhighlight> |
||
'''Output''' |
'''Output''' |
||
Line 4,205: | Line 4,205: | ||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="swift">import Foundation |
||
import Numerics |
import Numerics |
||
Line 4,273: | Line 4,273: | ||
print(fft(dat).map({ $0.pretty })) |
print(fft(dat).map({ $0.pretty })) |
||
print(rfft(f).map({ $0.pretty }))</ |
print(rfft(f).map({ $0.pretty }))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,286: | Line 4,286: | ||
I could have written a more beautiful code by using non-blocking assignments in the bit_reverse_order function, but it could not be coded in a function, so FFT could not be implemented as a function as well. |
I could have written a more beautiful code by using non-blocking assignments in the bit_reverse_order function, but it could not be coded in a function, so FFT could not be implemented as a function as well. |
||
<syntaxhighlight lang="systemverilog"> |
|||
<lang SystemVerilog> |
|||
package math_pkg; |
package math_pkg; |
||
Line 4,374: | Line 4,374: | ||
endclass |
endclass |
||
</syntaxhighlight> |
|||
</lang> |
|||
Now let's perform the standard test |
Now let's perform the standard test |
||
<syntaxhighlight lang="systemverilog"> |
|||
<lang SystemVerilog> |
|||
/// @Author: Alexandre Felipe (o.alexandre.felipe@gmail.com) |
/// @Author: Alexandre Felipe (o.alexandre.felipe@gmail.com) |
||
/// @Date: 2018-Jan-25 |
/// @Date: 2018-Jan-25 |
||
Line 4,401: | Line 4,401: | ||
end |
end |
||
endmodule |
endmodule |
||
</syntaxhighlight> |
|||
</lang> |
|||
By running the sanity test it outputs the following |
By running the sanity test it outputs the following |
||
<pre> |
<pre> |
||
Line 4,427: | Line 4,427: | ||
A more reliable test is to implement the Discrete Fourier Transform by its definition and compare the results obtained by FFT and by definition evaluation. For that let's create a class with a random data vector, and each time the vector is randomized the FFT is calculated and the output is compared by the result obtained by the definition. |
A more reliable test is to implement the Discrete Fourier Transform by its definition and compare the results obtained by FFT and by definition evaluation. For that let's create a class with a random data vector, and each time the vector is randomized the FFT is calculated and the output is compared by the result obtained by the definition. |
||
<syntaxhighlight lang="systemverilog"> |
|||
<lang SystemVerilog> |
|||
/// @Author: Alexandre Felipe (o.alexandre.felipe@gmail.com) |
/// @Author: Alexandre Felipe (o.alexandre.felipe@gmail.com) |
||
/// @Date: 2018-Jan-25 |
/// @Date: 2018-Jan-25 |
||
Line 4,480: | Line 4,480: | ||
endfunction |
endfunction |
||
endclass |
endclass |
||
</syntaxhighlight> |
|||
</lang> |
|||
Now let's create a code that tests the FFT with random inputs for different sizes. |
Now let's create a code that tests the FFT with random inputs for different sizes. |
||
Uses a generate block since the number of samples is a parameter and must be defined at compile time. |
Uses a generate block since the number of samples is a parameter and must be defined at compile time. |
||
<syntaxhighlight lang="systemverilog"> |
|||
<lang SystemVerilog> |
|||
/// @Author: Alexandre Felipe (o.alexandre.felipe@gmail.com) |
/// @Author: Alexandre Felipe (o.alexandre.felipe@gmail.com) |
||
/// @Date: 2018-Jan-25 |
/// @Date: 2018-Jan-25 |
||
Line 4,499: | Line 4,499: | ||
endgenerate |
endgenerate |
||
endmodule |
endmodule |
||
</syntaxhighlight> |
|||
</lang> |
|||
Simulating the fft_test_by_definition we get the following output: |
Simulating the fft_test_by_definition we get the following output: |
||
Line 4,529: | Line 4,529: | ||
{{tcllib|math::constants}} |
{{tcllib|math::constants}} |
||
{{tcllib|math::fourier}} |
{{tcllib|math::fourier}} |
||
< |
<syntaxhighlight lang="tcl">package require math::constants |
||
package require math::fourier |
package require math::fourier |
||
Line 4,566: | Line 4,566: | ||
# Convert to magnitudes for printing |
# Convert to magnitudes for printing |
||
set fft2 [waveMagnitude $fft] |
set fft2 [waveMagnitude $fft] |
||
printwave fft2</ |
printwave fft2</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,575: | Line 4,575: | ||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
The [http://www.fftw.org <code>fftw</code> library] is callable from Ursala using the syntax <code>..u_fw_dft</code> for a one dimensional forward discrete Fourier transform operating on a list of complex numbers. Ordinarily the results are scaled so that the forward and reverse transforms are inverses of each other, but additional scaling can be performed as shown below to conform to convention. |
The [http://www.fftw.org <code>fftw</code> library] is callable from Ursala using the syntax <code>..u_fw_dft</code> for a one dimensional forward discrete Fourier transform operating on a list of complex numbers. Ordinarily the results are scaled so that the forward and reverse transforms are inverses of each other, but additional scaling can be performed as shown below to conform to convention. |
||
< |
<syntaxhighlight lang="ursala">#import nat |
||
#import flo |
#import flo |
||
Line 4,584: | Line 4,584: | ||
#cast %jLW |
#cast %jLW |
||
t = (f,g)</ |
t = (f,g)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>( |
<pre>( |
||
Line 4,608: | Line 4,608: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="vlang">import math.complex |
||
import math |
import math |
||
fn ditfft2(x []f64, mut y []Complex, n int, s int) { |
fn ditfft2(x []f64, mut y []Complex, n int, s int) { |
||
Line 4,630: | Line 4,630: | ||
println("${c:8.4f}") |
println("${c:8.4f}") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,653: | Line 4,653: | ||
{{libheader|Wren-complex}} |
{{libheader|Wren-complex}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/complex" for Complex |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 4,679: | Line 4,679: | ||
for (i in 0...y.count) y[i] = Complex.zero |
for (i in 0...y.count) y[i] = Complex.zero |
||
ditfft2.call(x, y, x.count, 1) |
ditfft2.call(x, y, x.count, 1) |
||
for (c in y) Fmt.print("$6.4z", c)</ |
for (c in y) Fmt.print("$6.4z", c)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,694: | Line 4,694: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">var [const] GSL=Import("zklGSL"); // libGSL (GNU Scientific Library) |
||
v:=GSL.ZVector(8).set(1,1,1,1); |
v:=GSL.ZVector(8).set(1,1,1,1); |
||
GSL.FFT(v).toList().concat("\n").println(); // in place</ |
GSL.FFT(v).toList().concat("\n").println(); // in place</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |