Anonymous user
Fast Fourier transform: Difference between revisions
→{{header|ALGOL 68}}: replace array SLICE operator with array DICE operator... simpler.
m (→{{header|ALGOL 68}}: misc code refactor) |
(→{{header|ALGOL 68}}: replace array SLICE operator with array DICE operator... simpler.) |
||
Line 97:
=={{header|ALGOL 68}}==
{{trans|Python}}Note: This specimen retains the original [[#Python|Python]] coding style.
{{works with|ALGOL 68|Revision 1 -
{{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''.}}
'''File: Template.Fast_Fourier_transform.a68'''<lang algol68>PRIO DICE = 9; #
# -*- coding: utf-8 -*- #▼
MODE VALUE = COMPL;▼
FORMAT slice fmt = $"["g(0)":"g(0)"/"g(0)"] "$;▼
OP DICE = ([]SCALAR in, INT step)[]SCALAR: (
### Dice the array, extract array values a "step" apart ###
IF step = 1 THEN
in
ELSE
[(upb-lwb)%step+1]VALUE out;▼
INT upb out := 0;
FOR index FROM
out[upb out+:=1] := in[index] OD;
out[@LWB in]
FI
);
PROC fft = ([]
###
IF LWB in t >= UPB in t THEN
in t[@0]
ELSE
[LWB t:UPB t]SCALAR coef;
COMPL exp = complex exp(-(0I2)*pi*k/n)*odd[i];▼
COMPL
out[mid in + i] := even[i] - exp▼
REAL w = 2*pi / n;
FOR k FROM LWB t TO half n-1 DO
coef[k] := even[k] + cis t;
OD;
FI
);</lang>'''File: test.Fast_Fourier_transform.a68'''<lang algol68>#!/usr/local/bin/a68g --script #
▲# -*- coding: utf-8 -*- #
PROC (COMPL)COMPL scalar exp = complex exp;
PR READ "Template.Fast_Fourier_transform.a68" PR
FORMAT real fmt := $g(0,
FORMAT
FORMAT
test:(
printf((▼
[]COMPL
one and a quarter wave ft = fft((0, 0.924, 0.707,-0.383,-1,-0.383, 0.707, 0.924,
0,-0.924,-0.707, 0.383, 1, 0.383,-0.707,-0.924))
▲ printf((
))</lang>'''Output:'''▼
$"Tooth wave: "$,compl array fmt, tooth wave ft, $l$,
$"1¼ cycle wave: "$, compl array fmt, one and a quarter wave ft, $l$
))
<pre>
Tooth wave: 4.000⊥.000, 1.000⊥-2.414, .000⊥.000, 1.000⊥-.414, .000⊥.000, 1.000⊥.414, .000⊥.000, 1.000⊥2.414,
1¼ cycle wave: .000⊥.000, .000⊥.001, .000⊥.000, .000⊥-8.001, .000⊥.000, -.000⊥-.001, .000⊥.000, .000⊥.001, .000⊥.000, .000⊥-.001, .000⊥.000, -.000⊥.001, .000⊥.000, -.000⊥8.001, .000⊥.000, -.000⊥-.001,
</pre>
|