blib/lib/PDL/FFT.pm

filter: Statementstmt 82.9% 141 / 170 Branchbran 52.7% 19 / 36 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 94.7% 18 / 19 Podpod 0.0% 0 / 9 totaltotal 76.5% 183 / 239 CC 22 SCAR 12.6
CC
22
Cov
78%
fftconvolve_inplace
12.0
fft
4.1
ifft
4.1
CRAP 3.5
« blib/lib/PDL/Filter/Linear.pm ↑ Summary blib/lib/PDL/Ops.pm »
1  
2 #
3 # GENERATED WITH PDL::PP! Don't modify!
4 #
5 package PDL::FFT;
6  
7 3 @EXPORT_OK = qw( PDL::PP _fft PDL::PP _ifft fft ifft fftnd ifftnd fftconvolve realfft realifft kernctr PDL::PP convmath PDL::PP cmul PDL::PP cdiv );
8 3 %EXPORT_TAGS = (Func=>[@EXPORT_OK]);
9  
10 3 ▶use PDL::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use PDL::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use DynaLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15  
16
17 3 @ISA = ( 'PDL::Exporter','DynaLoader' );
18 3 push @PDL::Core::PP, __PACKAGE__;
19 3 bootstrap PDL::FFT ;
20  
21  
22  
23  
24 =head1 NAME
25  
26 PDL::FFT - FFTs for PDL
27  
28 =head1 DESCRIPTION
29  
30 !!!!!!!!!!!!!!!!!!!!!!!!!!WARNING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
31 As of PDL-2.006_04, the direction of the FFT/IFFT has been
32 reversed to match the usage in the FFTW library and the convention
33 in use generally.
34 !!!!!!!!!!!!!!!!!!!!!!!!!!WARNING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
35  
36 FFTs for PDL. These work for arrays of any dimension, although ones
37 with small prime factors are likely to be the quickest. The forward
38 FFT is unnormalized while the inverse FFT is normalized so that the
39 IFFT of the FFT returns the original values.
40  
41 For historical reasons, these routines work in-place and do not recognize
42 the in-place flag. That should be fixed.
43  
44 =head1 SYNOPSIS
45  
46 use PDL::FFT qw/:Func/;
47  
48 fft($real, $imag);
49 ifft($real, $imag);
50 realfft($real);
51 realifft($real);
52  
53 fftnd($real,$imag);
54 ifftnd($real,$imag);
55  
56 $kernel = kernctr($image,$smallk);
57 fftconvolve($image,$kernel);
58  
59 =head1 DATA TYPES
60  
61 The underlying C library upon which this module is based performs FFTs
62 on both single precision and double precision floating point piddles.
63 Performing FFTs on integer data types is not reliable. Consider the
64 following FFT on piddles of type 'double':
65  
66 $r = pdl(0,1,0,1);
67 $i = zeroes($r);
68 fft($r,$i);
69 print $r,$i;
70 [2 0 -2 0] [0 0 0 0]
71  
72 But if $r and $i are unsigned short integers (ushorts):
73  
74 $r = pdl(ushort,0,1,0,1);
75 $i = zeroes($r);
76 fft($r,$i);
77 print $r,$i;
78 [2 0 65534 0] [0 0 0 0]
79  
80 This used to occur because L<PDL::PP|PDL::PP> converts the ushort
81 piddles to floats or doubles, performs the FFT on them, and then
82 converts them back to ushort, causing the overflow where the amplitude
83 of the frequency should be -2.
84  
85 Therefore, if you pass in a piddle of integer datatype (byte, short,
86 ushort, long) to any of the routines in PDL::FFT, your data will be
87 promoted to a double-precision piddle. If you pass in a float, the
88 single-precision FFT will be performed.
89  
90 =head1 FREQUENCIES
91  
92 For even-sized input arrays, the frequencies are packed like normal
93 for FFTs (where N is the size of the array and D is the physical step
94 size between elements):
95  
96 0, 1/ND, 2/ND, ..., (N/2-1)/ND, 1/2D, -(N/2-1)/ND, ..., -1/ND.
97  
98 which can easily be obtained (taking the Nyquist frequency to be
99 positive) using
100  
101 C<< $kx = $real->xlinvals(-($N/2-1)/$N/$D,1/2/$D)->rotate(-($N/2 -1)); >>
102  
103 For odd-sized input arrays the Nyquist frequency is not directly
104 acessible, and the frequencies are
105  
106 0, 1/ND, 2/ND, ..., (N/2-0.5)/ND, -(N/2-0.5)/ND, ..., -1/ND.
107  
108 which can easily be obtained using
109  
110 C<< $kx = $real->xlinvals(-($N/2-0.5)/$N/$D,($N/2-0.5)/$N/$D)->rotate(-($N-1)/2); >>
111  
112  
113 =head1 ALTERNATIVE FFT PACKAGES
114  
115 Various other modules - such as
116 L<PDL::FFTW|PDL::FFTW> and L<PDL::Slatec|PDL::Slatec> -
117 contain FFT routines.
118 However, unlike PDL::FFT, these modules are optional,
119 and so may not be installed.
120  
121 =cut
122  
123  
124  
125  
126  
127  
128  
129 =head1 FUNCTIONS
130  
131  
132  
133 =cut
134  
135  
136  
137  
138  
139  
140 3 *_fft = \&PDL::_fft;
141  
142  
143  
144  
145  
146 3 *_ifft = \&PDL::_ifft;
147  
148  
149  
150  
151 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
152 3 ▶use PDL::Core qw/:Func/;
Subroutine BEGIN: called CC=1 SCAR=0.0
153 3 ▶use PDL::Basic qw/:Func/;
Subroutine BEGIN: called CC=1 SCAR=0.0
154 3 ▶use PDL::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
155 3 ▶use PDL::ImageND qw/kernctr/; # moved to ImageND since FFTW uses it too
Subroutine BEGIN: called CC=1 SCAR=0.0
156  
157 END {
158 # tidying up required after using fftn
159 3 ▶ print "Freeing FFT space\n" if $PDL::verbose;
Subroutine END: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 3 3
160 3 fft_free();
161 }
162  
163 sub todecimal {
164 144 ▶ my ($arg) = @_;
Subroutine todecimal: called CC=3 SCAR=11.0
Pod undocumented
165 144 ▶ $arg = $arg->double if (($arg->get_datatype != $PDL_F) &&
Branch: 1 branch
BranchTrueFalseTotal
if $arg->get_datatype != $PDL_F and $arg->get_datatype != $PDL_D 12 130 142
Condition100%3/3
$arg->get_datatype != $PDL_F and $arg->get_datatype != $PDL_D
!ll&&!rl&&r
412612
MC/DC100%2/2
$arg->get_datatype != $PDL_F and $arg->get_datatype != $PDL_D
$arg->get_datatype != $PDL_F $arg->get_datatype != $PDL_D
Truth table100%3/3
A and B
A= $arg->get_datatype != $PDL_FB= $arg->get_datatype != $PDL_D
ABresult
0X0
100
111
166 ($arg->get_datatype != $PDL_D));
167 142 $_[0] = $arg;
168 142 1;}
169  
170 =head2 fft()
171  
172 =for ref
173  
174 Complex 1-D FFT of the "real" and "imag" arrays [inplace].
175  
176 =for sig
177  
178 Signature: ([o,nc]real(n); [o,nc]imag(n))
179  
180 =for usage
181  
182 fft($real,$imag);
183  
184 =cut
185  
186 3 *fft = \&PDL::fft;
187  
188 sub PDL::fft {
189 # Convert the first argument to decimal and check for trouble.
190 32 ▶ eval { todecimal($_[0]); };
Subroutine fft: called CC=4 SCAR=14.2
Pod undocumented
191 32 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 32 32
192 0 $@ =~ s/ at .*//s;
193 0 barf("Error in FFT with first argument: $@");
194 }
195 # Convert the second argument to decimal and check for trouble.
196 32 eval { todecimal($_[1]); };
197 32 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 31 32
198 1 $@ =~ s/ at .*//s;
199 1 my $message = "Error in FFT with second argument: $@";
200 1 ▶ $message .= '. Did you forget to supply the second (imaginary) piddle?'
Branch: 1 branch
BranchTrueFalseTotal
if $message =~ /undefined value/ 1 0 1
201 if ($message =~ /undefined value/);
202 1 barf($message);
203 }
204 31 _fft($_[0],$_[1]);
205 }
206  
207  
208 =head2 ifft()
209  
210 =for ref
211  
212 Complex inverse 1-D FFT of the "real" and "imag" arrays [inplace].
213  
214 =for sig
215  
216 Signature: ([o,nc]real(n); [o,nc]imag(n))
217  
218 =for usage
219  
220 ifft($real,$imag);
221  
222 =cut
223  
224 3 *ifft = \&PDL::ifft;
225  
226 sub PDL::ifft {
227 # Convert the first argument to decimal and check for trouble.
228 19 ▶ eval { todecimal($_[0]); };
Subroutine ifft: called CC=4 SCAR=14.2
Pod undocumented
229 19 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 19 19
230 0 $@ =~ s/ at .*//s;
231 0 barf("Error in FFT with first argument: $@");
232 }
233 # Convert the second argument to decimal and check for trouble.
234 19 eval { todecimal($_[1]); };
235 19 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 18 19
236 1 $@ =~ s/ at .*//s;
237 1 my $message = "Error in FFT with second argument: $@";
238 1 ▶ $message .= '. Did you forget to supply the second (imaginary) piddle?'
Branch: 1 branch
BranchTrueFalseTotal
if $message =~ /undefined value/ 1 0 1
239 if ($message =~ /undefined value/);
240 1 barf($message);
241 }
242 18 _ifft($_[0],$_[1]);
243 }
244  
245 =head2 realfft()
246  
247 =for ref
248  
249 One-dimensional FFT of real function [inplace].
250  
251 The real part of the transform ends up in the first half of the array
252 and the imaginary part of the transform ends up in the second half of
253 the array.
254  
255 =for usage
256  
257 realfft($real);
258  
259 =cut
260  
261 3 *realfft = \&PDL::realfft;
262  
263 sub PDL::realfft {
264 1 ▶ barf("Usage: realfft(real(*)") if $#_ != 0;
Subroutine realfft: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $#_ != 0 0 1 1
265 1 my ($a) = @_;
266 1 todecimal($a);
267 # FIX: could eliminate $b
268 1 my ($b) = 0*$a;
269 1 fft($a,$b);
270 1 my ($n) = int((($a->dims)[0]-1)/2); my($t);
271 1 ($t=$a->slice("-$n:-1")) .= $b->slice("1:$n");
272 1 undef;
273 }
274  
275 =head2 realifft()
276  
277 =for ref
278  
279 Inverse of one-dimensional realfft routine [inplace].
280  
281 =for usage
282  
283 realifft($real);
284  
285 =cut
286  
287 3 *realifft = \&PDL::realifft;
288  
289 sub PDL::realifft {
290 3 ▶ use PDL::Ufunc 'max';
Subroutine BEGIN: called CC=1 SCAR=0.0
291 1 ▶ barf("Usage: realifft(xfm(*)") if $#_ != 0;
Subroutine realifft: called CC=3 SCAR=11.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $#_ != 0 0 1 1
292 1 my ($a) = @_;
293 1 todecimal($a);
294 1 my ($n) = int((($a->dims)[0]-1)/2); my($t);
295 # FIX: could eliminate $b
296 1 my ($b) = 0*$a;
297 1 ($t=$b->slice("1:$n")) .= $a->slice("-$n:-1");
298 1 ($t=$a->slice("-$n:-1")) .= $a->slice("$n:1");
299 1 ($t=$b->slice("-$n:-1")) .= -$b->slice("$n:1");
300 1 ifft($a,$b);
301 # Sanity check -- shouldn't happen
302 1 ▶ carp "Bad inverse transform in realifft" if max(abs($b)) > 1e-6*max(abs($a));
Branch: 1 branch
BranchTrueFalseTotal
if max(abs $b) > "1e-06" * max(abs $a) 0 1 1
303 1 undef;
304 }
305  
306 =head2 fftnd()
307  
308 =for ref
309  
310 N-dimensional FFT over all pdl dims of input (inplace)
311  
312 =for example
313  
314 fftnd($real,$imag);
315  
316 =cut
317  
318 3 *fftnd = \&PDL::fftnd;
319  
320 sub PDL::fftnd {
321 13 ▶ barf "Must have real and imaginary parts for fftnd" if $#_ != 1;
Subroutine fftnd: called CC=4 SCAR=13.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $#_ != 1 0 13 13
322 13 my ($r,$i) = @_;
323 13 my ($n) = $r->getndims;
324 13 ▶ barf "Dimensions of real and imag must be the same for fft"
Branch: 1 branch
BranchTrueFalseTotal
if $n != $i->getndims 0 13 13
325 if ($n != $i->getndims);
326 13 $n--;
327 13 todecimal($r);
328 13 todecimal($i);
329 # need the copy in case $r and $i point to same memory
330 13 $i = $i->copy;
331 13 foreach (0..$n) {
332 27 fft($r,$i);
333 27 $r = $r->mv(0,$n);
334 27 $i = $i->mv(0,$n);
335 }
336 13 $_[0] = $r; $_[1] = $i;
337 13 undef;
338 }
339  
340 =head2 ifftnd()
341  
342 =for ref
343  
344 N-dimensional inverse FFT over all pdl dims of input (inplace)
345  
346 =for example
347  
348 ifftnd($real,$imag);
349  
350 =cut
351  
352 3 *ifftnd = \&PDL::ifftnd;
353  
354 sub PDL::ifftnd {
355 7 ▶ barf "Must have real and imaginary parts for ifftnd" if $#_ != 1;
Subroutine ifftnd: called CC=4 SCAR=13.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $#_ != 1 0 7 7
356 7 my ($r,$i) = @_;
357 7 my ($n) = $r->getndims;
358 7 ▶ barf "Dimensions of real and imag must be the same for ifft"
Branch: 1 branch
BranchTrueFalseTotal
if $n != $i->getndims 0 7 7
359 if ($n != $i->getndims);
360 7 todecimal($r);
361 7 todecimal($i);
362 # need the copy in case $r and $i point to same memory
363 7 $i = $i->copy;
364 7 $n--;
365 7 foreach (0..$n) {
366 14 ifft($r,$i);
367 14 $r = $r->mv(0,$n);
368 14 $i = $i->mv(0,$n);
369 }
370 7 $_[0] = $r; $_[1] = $i;
371 7 undef;
372 }
373  
374  
375  
376  
377 =head2 fftconvolve()
378  
379 =for ref
380  
381 N-dimensional convolution with periodic boundaries (FFT method)
382  
383 =for usage
384  
385 $kernel = kernctr($image,$smallk);
386 fftconvolve($image,$kernel);
387  
388 fftconvolve works inplace, and returns an error array in kernel as an
389 accuracy check -- all the values in it should be negligible.
390  
391 See also L<PDL::ImageND::convolveND|PDL::ImageND/convolveND>, which
392 performs speed-optimized convolution with a variety of boundary conditions.
393  
394 The sizes of the image and the kernel must be the same.
395 L<kernctr|PDL::ImageND/kernctr> centres a small kernel to emulate the
396 behaviour of the direct convolution routines.
397  
398 The speed cross-over between using straight convolution
399 (L<PDL::Image2D::conv2d()|PDL::Image2D/conv2d>) and
400 these fft routines is for kernel sizes roughly 7x7.
401  
402 =cut
403  
404 3 *fftconvolve = \&PDL::fftconvolve;
405  
406 sub PDL::fftconvolve {
407 2 ▶ barf "Must have image & kernel for fftconvolve" if $#_ != 1;
Subroutine fftconvolve: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $#_ != 1 0 2 2
408 2 my ($a, $k) = @_;
409  
410 2 my ($ar,$ai,$kr,$ki,$cr,$ci);
411  
412 2 $ar = $a->copy;
413 2 $ai = $ar->zeros;
414 2 fftnd($ar, $ai);
415  
416 2 $kr = $k->copy;
417 2 $ki = $kr->zeroes;
418 2 fftnd($kr,$ki);
419  
420 2 $cr = $ar->zeroes;
421 2 $ci = $ai->zeroes;
422 2 cmul($ar,$ai,$kr,$ki,$cr,$ci);
423  
424 2 ifftnd($cr,$ci);
425 2 $_[0] = $cr;
426 2 $_[1] = $ci;
427  
428 2 ($cr,$ci);
429 }
430  
431 sub PDL::fftconvolve_inplace {
432 0 ▶ barf "Must have image & kernel for fftconvolve" if $#_ != 1;
Subroutine fftconvolve_inplace: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $#_ != 1 0 0 0
433 0 my ($hr, $hi) = @_;
434 0 my ($n) = $hr->getndims;
435 0 todecimal($hr); # Convert to double unless already float or double
436 0 todecimal($hi); # Convert to double unless already float or double
437 # need the copy in case $r and $i point to same memory
438 0 $hi = $hi->copy;
439 0 $hr = $hr->copy;
440 0 fftnd($hr,$hi);
441 0 convmath($hr->clump(-1),$hi->clump(-1));
442 0 my ($str1, $str2, $tmp, $i);
443 0 chop($str1 = '-1:1,' x $n);
444 0 chop($str2 = '1:-1,' x $n);
445  
446 # FIX: do these inplace -- cuts the arithmetic by a factor 2 as well.
447  
448 0 ($tmp = $hr->slice($str2)) += $hr->slice($str1)->copy;
449 0 ($tmp = $hi->slice($str2)) -= $hi->slice($str1)->copy;
450 0 ▶ for ($i = 0; $i<$n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
451 0 chop ($str1 = ('(0),' x $i).'-1:1,'.('(0),'x($n-$i-1)));
452 0 chop ($str2 = ('(0),' x $i).'1:-1,'.('(0),'x($n-$i-1)));
453 0 ($tmp = $hr->slice($str2)) += $hr->slice($str1)->copy;
454 0 ($tmp = $hi->slice($str2)) -= $hi->slice($str1)->copy;
455 }
456 0 $hr->clump(-1)->set(0,$hr->clump(-1)->at(0)*2);
457 0 $hi->clump(-1)->set(0,0.);
458 0 ifftnd($hr,$hi);
459 0 $_[0] = $hr; $_[1] = $hi;
460 0 ($hr,$hi);
461 }
462  
463  
464  
465  
466  
467 =head2 convmath
468  
469 =for sig
470  
471 Signature: ([o,nc]a(m); [o,nc]b(m))
472  
473 =for ref
474  
475 Internal routine doing maths for convolution
476  
477 =for bad
478  
479 convmath does not process bad values.
480 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
481  
482  
483 =cut
484  
485  
486  
487  
488  
489  
490 3 *convmath = \&PDL::convmath;
491  
492  
493  
494  
495  
496 =head2 cmul
497  
498 =for sig
499  
500 Signature: (ar(); ai(); br(); bi(); [o]cr(); [o]ci())
501  
502 =for ref
503  
504 Complex multiplication
505  
506 =for bad
507  
508 cmul does not process bad values.
509 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
510  
511  
512 =cut
513  
514  
515  
516  
517  
518  
519 3 *cmul = \&PDL::cmul;
520  
521  
522  
523  
524  
525 =head2 cdiv
526  
527 =for sig
528  
529 Signature: (ar(); ai(); br(); bi(); [o]cr(); [o]ci())
530  
531 =for ref
532  
533 Complex division
534  
535 =for bad
536  
537 cdiv does not process bad values.
538 It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.
539  
540  
541 =cut
542  
543  
544  
545  
546  
547  
548 3 *cdiv = \&PDL::cdiv;
549  
550  
551  
552  
553 3 1; # OK
554  
555  
556  
557  
558 =head1 BUGS
559  
560 Where the source is marked `FIX', could re-implement using phase-shift
561 factors on the transforms and some real-space bookkeeping, to save
562 some temporary space and redundant transforms.
563  
564 =head1 AUTHOR
565  
566 This file copyright (C) 1997, 1998 R.J.R. Williams
567 (rjrw@ast.leeds.ac.uk), Karl Glazebrook (kgb@aaoepp.aao.gov.au),
568 Tuomas J. Lukka, (lukka@husc.harvard.edu). All rights reserved. There
569 is no warranty. You are allowed to redistribute this software /
570 documentation under certain conditions. For details, see the file
571 COPYING in the PDL distribution. If this file is separated from the
572 PDL distribution, the copyright notice should be included in the file.
573  
574  
575 =cut
576  
577  
578  
579 ;
580  
581  
582  
583 # Exit with OK status
584  
585 3 1;
586  
587
« blib/lib/PDL/Filter/Linear.pm ↑ Summary blib/lib/PDL/Ops.pm »