Discrete Fourier transform: Difference between revisions

(Correcting Latex)
Line 108:
clean ifourier 2 + fourier 2 3 5 7 11
4 3 5 7 11</lang>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
This entry uses a "complex" module, such as is available at [[Arithmetic/Complex#jq]].
<lang jq>include "complex"; # a reminder
def dft:
. as $x
| length as $N
| reduce range (0; $N) as $k ([]; # y
.[$k] = [0,0] # Complex.zero
| reduce range( 0; $N) as $n (.;
([[0, -1], [2,0], [pi,0], $k, $n, invert($N) ] | multiply) as $t
| .[$k] = plus(.[$k]; multiply($x[$n]; exp($t))) ) ) ;
 
# Input: an array of Complex
def idft:
. as $y
| length as $N
| reduce range(0; $N) as $n ([];
.[$n] = [0,0]
| reduce range(0; $N) as $k (.;
( [ 2, pi, [0,1], $k, $n, invert($N)] | multiply) as $t
| .[$n] = plus(.[$n]; multiply($y[$k]; exp($t))) )
| .[$n] = divide(.[$n]; $N) );
 
 
def task:
def abs: if . < 0 then -. else . end;
# round, and remove very small imaginary values altogether
def tidy:
(.[0] | round) as $round
| if (.[0]| (. - $round) | abs < 1e-14) then .[0] = $round else . end
| if .[1]|abs < 1e-14 then .[0] else . end;
 
[2, 3, 5, 7, 11] as $x
| "Original sequence: \($x)",
(reduce range(0; $x|length) as $i ( []; .[$i] = [$x[$i], 0])
| dft
| "\nAfter applying the Discrete Fourier Transform:",
.,
"\nAfter applying the Inverse Discrete Fourier Transform to this value: \(
idft | map(tidy))" )
;
 
task</lang>
{{out}}
<pre>
Original sequence: [2,3,5,7,11]
 
After applying the Discrete Fourier Transform:
[[28,0],[-3.3819660112501078,8.784022634946174],[-5.618033988749895,2.8001689857494747],[-5.618033988749892,-2.8001689857494823],[-3.381966011250096,-8.784022634946178]]
 
After applying the Inverse Discrete Fourier Transform to this value: [2,3,5,7,11]
</pre>
 
 
 
=={{header|Julia}}==
2,460

edits