| 1 | # |
| 2 | # GENERATED WITH PDL::PP from fftw3.pd! Don't modify! |
| 3 | # |
| 4 | package PDL::FFTW3; |
| 5 | |
| 6 | our @EXPORT_OK = qw( fft1 ifft1 rfft1 rNfft1 irfft1 fft2 ifft2 rfft2 rNfft2 irfft2 fft3 ifft3 rfft3 rNfft3 irfft3 fft4 ifft4 rfft4 rNfft4 irfft4 fft5 ifft5 rfft5 rNfft5 irfft5 fft6 ifft6 rfft6 rNfft6 irfft6 fft7 ifft7 rfft7 rNfft7 irfft7 fft8 ifft8 rfft8 rNfft8 irfft8 fft9 ifft9 rfft9 rNfft9 irfft9 fft10 ifft10 rfft10 rNfft10 irfft10 fftn ifftn rfftn irfftn ); |
| 7 | our %EXPORT_TAGS = (Func=>\@EXPORT_OK); |
| 8 | |
| 9 | use PDL::Core; |
| 10 | use PDL::Exporter; |
| 11 | use DynaLoader; |
| 12 | |
| 13 | |
| 14 | our $VERSION = '0.204'; |
| 15 | our @ISA = ( 'PDL::Exporter','DynaLoader' ); |
| 16 | push @PDL::Core::PP, __PACKAGE__; |
| 17 | bootstrap PDL::FFTW3 $VERSION; |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | #line 25 "fftw3.pd" |
| 27 | |
| 28 | #line 0 "README.pod" |
| 29 | |
| 30 | =head1 NAME |
| 31 | |
| 32 | PDL::FFTW3 - PDL interface to the Fastest Fourier Transform in the West v3 |
| 33 | |
| 34 | =head1 SYNOPSIS |
| 35 | |
| 36 | use PDL; |
| 37 | use PDL::FFTW3; |
| 38 | use PDL::Graphics::Gnuplot; |
| 39 | |
| 40 | # Basic functionality |
| 41 | my $x = sin( sequence(100) * 2.0 ) + 2.0 * cos( sequence(100) / 3.0 ); |
| 42 | my $F = rfft1( $x ); |
| 43 | gplot( with => 'lines', $F->abs ); |
| 44 | |
| 45 | =====> |
| 46 | |
| 47 | 8000 ++------------+-------------+------------+-------------+------------++ |
| 48 | + + + + + + |
| 49 | | | |
| 50 | | * | |
| 51 | 7000 ++ * ++ |
| 52 | | * | |
| 53 | | * | |
| 54 | | * | |
| 55 | | * | |
| 56 | 6000 ++ * ++ |
| 57 | | * | |
| 58 | | * | |
| 59 | | * | |
| 60 | 5000 ++ * ++ |
| 61 | | * | |
| 62 | | * | |
| 63 | | ** | |
| 64 | 4000 ++ ** ++ |
| 65 | | ** | |
| 66 | | * * | |
| 67 | | * * | |
| 68 | | * * | |
| 69 | 3000 ++ * * ++ |
| 70 | | * * | |
| 71 | | * * | |
| 72 | | * * * | |
| 73 | 2000 ++ * * * ++ |
| 74 | | * * * | |
| 75 | | * * ** | |
| 76 | | * * ** | |
| 77 | | * * ** | |
| 78 | 1000 ++ * * * * ++ |
| 79 | | * * * * | |
| 80 | | ** * * * | |
| 81 | + * * + + + * * + + |
| 82 | 0 ****-------*********************************--************************ |
| 83 | 0 10 20 30 40 50 |
| 84 | |
| 85 | # Correlation of two real signals |
| 86 | |
| 87 | # two signals offset by 30 units |
| 88 | my $x = sequence(100); |
| 89 | my $y1 = exp( 0.2*($x - 20.5) ** (-2.0) ); |
| 90 | my $y2 = exp( 0.2*($x - 50.5) ** (-2.0) ); |
| 91 | |
| 92 | # compute the correlation |
| 93 | my $F12 = rfft1( cat($y1,$y2) ); |
| 94 | my $corr = irfft1( $F12(:,(1)) * $F12(:,(0))->conj ); |
| 95 | # and find the peak |
| 96 | say maximum_ind($corr); |
| 97 | |
| 98 | =====> 30 |
| 99 | |
| 100 | =head1 DESCRIPTION |
| 101 | |
| 102 | This is a PDL binding to version 3 of the FFTW library. Supported are complex |
| 103 | <-> complex and real <-> complex FFTs. |
| 104 | |
| 105 | =head2 NB to install |
| 106 | |
| 107 | wget http://www.fftw.org/fftw-3.3.4.tar.gz |
| 108 | tar xvf fftw-3.3.4.tar.gz |
| 109 | cd fftw-3.3.4/ |
| 110 | ./configure --prefix=/usr --enable-threads --enable-float --enable-shared --with-pic |
| 111 | make all install install-pkgconfigDATA |
| 112 | make clean |
| 113 | ./configure --prefix=/usr --enable-threads --enable-shared --with-pic |
| 114 | make all install install-pkgconfigDATA |
| 115 | |
| 116 | This will give you both fftw3f (first chunk) and fftw3 (second). |
| 117 | |
| 118 | =head2 Supported operations |
| 119 | |
| 120 | This module computes the Discrete Fourier Transform. In its most basic form, |
| 121 | this transform converts a vector of complex numbers in the time domain into |
| 122 | another vector of complex numbers in the frequency domain. These complex <-> |
| 123 | complex transforms are supported with C<fftN> functions for a rank-C<N> |
| 124 | transform. The opposite effect (transform data in the frequency domain back to |
| 125 | the time domain) can be achieved with the C<ifftN> functions. |
| 126 | |
| 127 | A common use case is to transform purely-real data. This data has 0 for its |
| 128 | complex component, and FFTW can take advantage of this to compute the FFT faster |
| 129 | and using less memory. Since a Fourier Transform of a real signal has an even |
| 130 | real part and an odd imaginary part, only 1/2 of the spectrum is needed. These |
| 131 | forward real -> complex transforms are supported with the C<rfftN> functions. |
| 132 | The backward version of this transform is complex -> real and is supported with |
| 133 | the C<irfftN> functions. |
| 134 | |
| 135 | =head2 Basic usage details |
| 136 | |
| 137 | Arbitrary C<N>-dimensional transforms are supported. All functions exported by |
| 138 | this module have the C<N> in their name, so for instance a complex <-> complex |
| 139 | 3D forward transform is computed with the C<fft3> function. The rank I<must |
| 140 | always> be specified in this way; there is no function called simply C<fft>. |
| 141 | |
| 142 | In-place operation is supported for complex <-> complex functions, but not the |
| 143 | real ones (real functions don't have matching dimensionality of the input and |
| 144 | output). An in-place transform of C<$x> can be computed with |
| 145 | |
| 146 | fft1( $x->inplace ); |
| 147 | |
| 148 | All the functions in this module support PDL broadcasting. For instance, if we have |
| 149 | 4 different image ndarrays C<$a>, C<$b>, C<$c>, C<$d> and we want to compute |
| 150 | their 2D FFTs at the same time, we can say |
| 151 | |
| 152 | my $ABCD_transformed = rfft2( PDL::cat( $a, $b, $c, $d) ); |
| 153 | |
| 154 | This takes advantage of PDL's automatic parallelization, if appropriate (See |
| 155 | L<PDL::ParallelCPU>). |
| 156 | |
| 157 | =head2 Data formats |
| 158 | |
| 159 | FFTW supports single and double-precision floating point numbers directly. If |
| 160 | possible, the PDL input will be used as-is. If not, a type conversion will be |
| 161 | made to use the lowest-common type. So as an example, the following will perform |
| 162 | a single-precision floating point transform (and return data of that type). |
| 163 | |
| 164 | fft1( $x->byte ) |
| 165 | |
| 166 | As of 0.20, this module expects complex numbers to be stored as "native |
| 167 | complex" types (C<cfloat>, C<cdouble>). Complex outputs will also be native |
| 168 | complex. |
| 169 | |
| 170 | Generally, the sizes of the input and the output must match. This is completely |
| 171 | true for the complex <-> complex transforms: the output will have the same size |
| 172 | and the input, and an error will result if this isn't possible for some reason. |
| 173 | |
| 174 | This is a little bit more involved for the real <-> complex transforms. If I'm |
| 175 | transforming a real 3D vector of dimensions C<K,L,M>, I will get a complex output of |
| 176 | dimensions C<int(K/2)+1,L,M>. |
| 177 | The C<K/2> is there because the input was real. The first dimension is |
| 178 | always the one that gets the C<K/2>. This is described in detail in section 2.4 |
| 179 | of the FFTW manual. |
| 180 | |
| 181 | Note that given a real input, the dimensionality of the complex transformed |
| 182 | output is unambiguous. However, this is I<not> true for the backward transform. |
| 183 | For instance, a 1D inverse transform of a vector of 10 complex numbers can |
| 184 | produce real output of either 18 or 19 elements (because C<int(18/2)+1 == 10> |
| 185 | and C<int(19/2)+1 == 10>). |
| 186 | |
| 187 | I<Without any extra information this module assumes the even-sized input>. |
| 188 | |
| 189 | Thus C<irfft1( sequence(cdouble,10) )-E<gt>dim(0) == 18> is true. If we want the odd-sized output, we have to explicitly pass this into the function like this: |
| 190 | |
| 191 | irfft1( sequence(cdouble,10), zeros(19) ) |
| 192 | |
| 193 | Here I create a new output ndarray with the C<zeros> function; C<irfft1> then |
| 194 | fills in this ndarray with the result of the computation. This module validates |
| 195 | all of its input, so only 18 and 19 are valid here. An error will be thrown if |
| 196 | you try to pass in C<zeros(20)>. |
| 197 | |
| 198 | This all means that the following will produce surprising results if |
| 199 | C<$x-E<gt>dim(0)> isn't even |
| 200 | |
| 201 | irfft1( rfft1( $x ) ) |
| 202 | |
| 203 | =head2 FFT normalization |
| 204 | |
| 205 | Following the widest-used convention for discrete Fourier transforms, |
| 206 | this module normalizes the inverse transform (but not the forward |
| 207 | transform) by dividing by the number of elements in the data set, so |
| 208 | that |
| 209 | |
| 210 | ifft1( fft1( $x ) ) |
| 211 | |
| 212 | is a slow approximate no-op, if C<$x> is well-behaved. |
| 213 | |
| 214 | This is different from the behavior of the underlying FFTW3 library itself, |
| 215 | but more consistent with other FFT packages for popular analysis languages |
| 216 | including PDL. |
| 217 | |
| 218 | =head1 FUNCTIONS |
| 219 | |
| 220 | =head2 fft1, fft2, fft3, fft4, fft5, fft6, fft7, fft8, fft9, fft10, fftn |
| 221 | |
| 222 | =for ref |
| 223 | |
| 224 | Basic complex -> complex FFT. |
| 225 | |
| 226 | You can pass in the rank as a |
| 227 | parameter with the C<fftn> form, or append the rank to the function |
| 228 | name for ranks up to 10. These functions all take one input ndarray and |
| 229 | one output ndarray. The dimensions of the input and the output are |
| 230 | identical. The output parameter is optional and, if present, must be |
| 231 | the last argument. If the output ndarray is passed in, the user I<must> |
| 232 | make sure the dimensions match. |
| 233 | |
| 234 | As of 0.20, inputs must be "native complex" data. Any type other |
| 235 | than C<cfloat> or C<cdouble> will be converted in the normal PP |
| 236 | way. |
| 237 | |
| 238 | The fftn form takes a minimum of two arguments: the PDL to transform, |
| 239 | and the number of dimensions to transform as a separate argument. |
| 240 | |
| 241 | The following are equivalent: |
| 242 | |
| 243 | $X = fftn( $x, 1 ); |
| 244 | $X = fft1( $x ); |
| 245 | fft1( $x, my $X = $x->zeros ); |
| 246 | |
| 247 | =head2 ifft1, ifft2, ifft3, ifft4, ifft5, ifft6, ifft7, ifft8, ifft9, ifft10, ifftn |
| 248 | |
| 249 | =for ref |
| 250 | |
| 251 | Basic, properly normalized, complex -> complex backward FFT. |
| 252 | |
| 253 | Everything is exactly like in the C<fftX> functions, except the |
| 254 | inverse transform is computed and normalized, so that (for example) |
| 255 | |
| 256 | ifft1( fft1 ( $x ) ) |
| 257 | |
| 258 | is a good approximation of C<$x> itself. |
| 259 | |
| 260 | =head2 rfft1, rfft2, rfft3, rfft4, rfft5, rfft6, rfft7, rfft8, rfft9, rfft10, rfftn |
| 261 | |
| 262 | =for ref |
| 263 | |
| 264 | Real -> complex FFT. |
| 265 | |
| 266 | You can pass in the rank with the C<rfftn> |
| 267 | form, or append the rank to the function name for ranks up to 9. |
| 268 | These functions all take one input ndarray and one output ndarray. The |
| 269 | dimensions of the input and the output are not identical, but are |
| 270 | related as described in L<Data formats>. The output can be passed in |
| 271 | as the last argument, if desired. If the output ndarray is passed in, |
| 272 | the user I<must> make sure the dimensions match. |
| 273 | |
| 274 | In the C<rfftn> form, the rank is the second argument. |
| 275 | |
| 276 | The following are equivalent: |
| 277 | |
| 278 | $X = rfftn( $x, 1 ); |
| 279 | $X = rfft1( $x ); |
| 280 | rfft1( $x, my $X = $x->zeroes ); |
| 281 | |
| 282 | As of 0.20, only returns native-complex data. Support for PDL::Complex |
| 283 | has been removed. |
| 284 | |
| 285 | =head2 rNfft1, rNfft2, rNfft3, rNfft4, rNfft5, rNfft6, rNfft7, rNfft8, rNfft9, rNfft10, rNfftn |
| 286 | |
| 287 | =for ref |
| 288 | |
| 289 | As of 0.20, just an alias for rfftX, etc. |
| 290 | |
| 291 | =head2 irfft1, irfft2, irfft3, irfft4, irfft5, irfft6, irfft7, irfft8, irfft9, irfft10, irfftn |
| 292 | |
| 293 | =for ref |
| 294 | |
| 295 | Complex -> real inverse FFT. |
| 296 | |
| 297 | You can pass in the rank with the |
| 298 | C<irfftn> form, or append the rank to the function name for ranks up |
| 299 | to 9. Argument passing and interpretation is as described in |
| 300 | C<rfftX> above. Please read L<Data formats> for details about dimension |
| 301 | interpretation. There's an ambiguity about the output dimensionality, |
| 302 | which is described in that section. |
| 303 | |
| 304 | =head1 AUTHOR |
| 305 | |
| 306 | Dima Kogan, C<< <[email protected]> >>; contributions from Craig |
| 307 | DeForest, C<< <[email protected]> >>. |
| 308 | |
| 309 | =head1 LICENSE AND COPYRIGHT |
| 310 | |
| 311 | Copyright 2013 Dima Kogan and Craig DeForest. |
| 312 | |
| 313 | This program is free software; you can redistribute it and/or modify it |
| 314 | under the same terms as PDL. |
| 315 | |
| 316 | =cut |
| 317 | |
| 318 | use strict; |
| 319 | use warnings; |
| 320 | |
| 321 | #line 226 "fftw3.pd" |
| 322 | use PDL::Types; |
| 323 | use List::Util 'reduce'; |
| 324 | use threads::shared; |
| 325 | |
| 326 | # When I compute an FFTW plan, it goes here. |
| 327 | # This is :shared so that it can be used with Perl threads. |
| 328 | my %existingPlans :shared; |
| 329 | |
| 330 | # these are for the unit tests |
| 331 | our $_Nplans = 0; |
| 332 | our $_last_do_double_precision; |
| 333 | |
| 334 | # This is a function that sits between the user's call into this module and the |
| 335 | # PP-generated internals. Specifically, this function is called BEFORE any PDL |
| 336 | # broadcasting happens. Here I make sure the FFTW plan exists, or if it doesn't, I |
| 337 | # make it. Thus the PP-based internals can safely assume that the plan exists |
| 338 | sub __fft_internal { |
| 339 | my $thisfunction = shift; |
| 340 | |
| 341 | my ($do_inverse_fft, $is_real_fft, $rank) = $thisfunction =~ /^(i?)(r?)N?.*fft([0-9]+)/; |
| 342 | |
| 343 | # first I parse the variables. This is a very direct translation of what PP |
| 344 | # does normally. Plan-creation has to be outside of PP, so I must re-do this |
| 345 | # here |
| 346 | my $Nargs = scalar @_; |
| 347 | |
| 348 | my ($in, $out); |
| 349 | if ( $Nargs == 2 ) { |
| 350 | # all variables on stack, read in output and temp vars |
| 351 | ($in, $out) = map {defined $_ ? PDL::Core::topdl($_) : $_} @_; |
| 352 | } elsif ( $Nargs == 1 ) { |
| 353 | $in = PDL::Core::topdl $_[0]; |
| 354 | if ( $in->is_inplace ) { |
| 355 | barf <<EOF if $is_real_fft; |
| 356 | $thisfunction: in-place real FFTs are not supported since the input/output types and data sizes differ. |
| 357 | Giving up. |
| 358 | EOF |
| 359 | $out = $in; |
| 360 | $in->set_inplace(0); |
| 361 | } else { |
| 362 | $out = PDL::null(); |
| 363 | } |
| 364 | } else { |
| 365 | barf( <<EOF ); |
| 366 | $thisfunction must be given the input or the input and output as args. |
| 367 | Exactly 1 or 2 arguments are required. Instead I got $Nargs args. Giving up. |
| 368 | EOF |
| 369 | } |
| 370 | |
| 371 | # make sure the in/out types match. Convert $in if needed. This needs to |
| 372 | # happen before we instantiate $out (if it's null) to make sure we know the |
| 373 | # type |
| 374 | processTypes( $thisfunction, \$in, \$out ); |
| 375 | |
| 376 | # I now create an ndarray for the null output. Normally PP does this, but I need |
| 377 | # to have the ndarray made to create plans. If I don't, the alignment may |
| 378 | # differ between plan-time and run-time |
| 379 | if ( $out->isnull ) { |
| 380 | my ($type, @dims) = getOutArgs($in, $is_real_fft, $do_inverse_fft); |
| 381 | $out->set_datatype($type->enum); $out->setdims(\@dims); $out->make_physical; |
| 382 | } |
| 383 | |
| 384 | validateArguments( $rank, $is_real_fft, $do_inverse_fft, $thisfunction, $in, $out ); |
| 385 | |
| 386 | # I need to physical-ize the ndarrays before I make a plan. Again, normally PP |
| 387 | # does this, but to make sure alignments match, I need to do this myself, now |
| 388 | $in->make_physical; |
| 389 | $out->make_physical; |
| 390 | |
| 391 | my $plan = getPlan( $thisfunction, $rank, $is_real_fft, $do_inverse_fft, $in, $out ); |
| 392 | barf "$thisfunction couldn't make a plan. Giving up\n" unless defined $plan; |
| 393 | |
| 394 | my $is_native = !$in->type->real; # native complex |
| 395 | # I now have the arguments and the plan. Go! |
| 396 | my $internal_function = 'PDL::__'; |
| 397 | $internal_function .= |
| 398 | !$is_real_fft ? 'N' : |
| 399 | ($is_native && $do_inverse_fft) ? 'irN' : |
| 400 | $do_inverse_fft ? barf("irfft no longer supports PDL::Complex") : |
| 401 | 'rN'; |
| 402 | $internal_function .= "fft$rank"; |
| 403 | eval { no strict 'refs'; $internal_function->( $in, $out, $plan ) }; |
| 404 | barf $@ if $@; |
| 405 | $out; |
| 406 | } |
| 407 | |
| 408 | sub getOutArgs { |
| 409 | my ($in, $is_real_fft, $do_inverse_fft) = @_; |
| 410 | |
| 411 | my @dims = $in->dims; |
| 412 | my $is_native = !$in->type->real; |
| 413 | my $out_type = $in->type; |
| 414 | |
| 415 | if ( !$is_real_fft ) { |
| 416 | # complex fft. Output is the same size as the input. |
| 417 | } elsif ( !$do_inverse_fft ) { |
| 418 | # forward real fft |
| 419 | $dims[0] = int($dims[0]/2)+1; |
| 420 | $out_type = typeWithComplexity(getPrecision($out_type), 1); |
| 421 | } else { |
| 422 | # backward real fft |
| 423 | # |
| 424 | # there's an ambiguity here. I want int($out->dim(0)/2) + 1 == $in->dim(1), |
| 425 | # however this could mean that |
| 426 | # $out->dim(0) = 2*$in->dim(1) - 2 |
| 427 | # or |
| 428 | # $out->dim(0) = 2*$in->dim(1) - 1 |
| 429 | # |
| 430 | # WITHOUT ANY OTHER INFORMATION, I ASSUME EVEN INPUT SIZES, SO I ASSUME |
| 431 | # $out->dim(0) = 2*$in->dim(1) - 2 |
| 432 | if ($is_native) { |
| 433 | $out_type = ($out_type == cfloat) ? float : double; |
| 434 | } else { |
| 435 | shift @dims; |
| 436 | } |
| 437 | $dims[0] = 2*($dims[0]-1); |
| 438 | } |
| 439 | ($out_type, @dims); |
| 440 | } |
| 441 | |
| 442 | sub validateArguments |
| 443 | { |
| 444 | my ($rank, $is_real_fft, $do_inverse_fft, $thisfunction, $in, $out) = @_; |
| 445 | |
| 446 | for my $arg ( $in, $out ) |
| 447 | { |
| 448 | barf <<EOF unless defined $arg; |
| 449 | $thisfunction arguments must all be defined. If you want an auto-growing ndarray, use 'null' such as |
| 450 | $thisfunction( \$in, \$out = null ) |
| 451 | Giving up. |
| 452 | EOF |
| 453 | |
| 454 | my $type = ref $arg; |
| 455 | $type = 'scalar' unless defined $arg; |
| 456 | barf <<EOF unless ref $arg && $arg->isa('PDL'); |
| 457 | $thisfunction arguments must be of type 'PDL'. |
| 458 | Instead I got an arg of type '$type'. Giving up. |
| 459 | EOF |
| 460 | } |
| 461 | |
| 462 | # validate dimensionality of the ndarrays |
| 463 | my @inout = ($in, $out); |
| 464 | |
| 465 | for my $iarg ( 0..1 ) |
| 466 | { |
| 467 | my $arg = $inout[$iarg]; |
| 468 | |
| 469 | if( $arg->isnull ) |
| 470 | { |
| 471 | barf "$thisfunction: don't know what to do with a null input. Giving up"; |
| 472 | } |
| 473 | |
| 474 | if( !$is_real_fft ) |
| 475 | { validateArgumentDimensions_complex( $rank, $thisfunction, $arg); } |
| 476 | else |
| 477 | { validateArgumentDimensions_real( $rank, $do_inverse_fft, $thisfunction, $iarg, $arg); } |
| 478 | } |
| 479 | |
| 480 | # we have an explicit output ndarray we're filling in. Make sure the |
| 481 | # input/output dimensions match up |
| 482 | if ( !$is_real_fft ) |
| 483 | { matchDimensions_complex($thisfunction, $rank, $in, $out); } |
| 484 | else |
| 485 | { matchDimensions_real($thisfunction, $rank, $do_inverse_fft, $in, $out); } |
| 486 | } |
| 487 | |
| 488 | sub validateArgumentDimensions_complex |
| 489 | { |
| 490 | my ( $rank, $thisfunction, $arg ) = @_; |
| 491 | barf "Tried to compute a complex FFT, but non-native-complex argument given" |
| 492 | if $arg->type->real; |
| 493 | my $dims_cmp = $arg->ndims; |
| 494 | barf <<EOF if $dims_cmp < $rank; |
| 495 | Tried to compute a $rank-dimensional FFT, but an array has fewer than $rank dimensions. |
| 496 | Giving up. |
| 497 | EOF |
| 498 | } |
| 499 | |
| 500 | sub validateArgumentDimensions_real { |
| 501 | my ( $rank, $do_inverse_fft, $thisfunction, $iarg, $arg ) = @_; |
| 502 | my $is_native = !$arg->type->real; # native complex |
| 503 | |
| 504 | # real FFT. Forward transform takes in real and spits out complex; |
| 505 | # backward transform does the reverse |
| 506 | if (!!$do_inverse_fft == !!($iarg == 0)) { # need complex for this |
| 507 | my ($verb, $var, $reason) = ($iarg == 0) ? qw(takes input) : qw(produces output); |
| 508 | if ( ($iarg == 1 && !$is_native) || |
| 509 | ($iarg == 0 && !$is_native) |
| 510 | ) { |
| 511 | $reason = "\$$var should be native-complex"; |
| 512 | } elsif (!$is_native && $arg->dim(0) != 2) { |
| 513 | $reason = "\$$var->dim(0) == 2 should be true"; |
| 514 | } |
| 515 | barf <<EOF if $reason; |
| 516 | $thisfunction $verb complex $var, so $reason, |
| 517 | but it's not (in @{[$arg->info]}: $arg). Giving up. |
| 518 | EOF |
| 519 | } |
| 520 | |
| 521 | my ($min_dimensionality, $var) = ($rank, $iarg == 0 ? 'input' : 'output'); |
| 522 | if ( $arg->ndims < $min_dimensionality ) { |
| 523 | barf <<EOF; |
| 524 | $thisfunction: The $var needs at least $min_dimensionality dimensions, but |
| 525 | it has fewer. Giving up. |
| 526 | EOF |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | sub matchDimensions_complex { |
| 531 | my ($thisfunction, $rank, $in, $out) = @_; |
| 532 | for my $idim (0..$rank) { |
| 533 | if ( $in->dim($idim) != $out->dim($idim) ) { |
| 534 | barf <<EOF; |
| 535 | $thisfunction was given input/output matrices of non-matching sizes. |
| 536 | Giving up. |
| 537 | EOF |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | sub matchDimensions_real { |
| 543 | my ($thisfunction, $rank, $do_inverse_fft, $in, $out) = @_; |
| 544 | my ($varname1, $varname2, $var1, $var2); |
| 545 | if ( !$do_inverse_fft ) { |
| 546 | # Forward FFT. The input is real, the output is complex. |
| 547 | # $output->dim(1) should be int($input->dim(0)/2) + 1 (Section 2.4 of |
| 548 | # the FFTW3 documentation) |
| 549 | ($varname1, $varname2, $var1, $var2) = (qw(input output), $in, $out); |
| 550 | } else { |
| 551 | # Backward FFT. The input is complex, the output is real. |
| 552 | ($varname1, $varname2, $var1, $var2) = (qw(output input), $out, $in); |
| 553 | } |
| 554 | barf <<EOF if int($var1->dim(0)/2) + 1 != $var2->dim(0); |
| 555 | $thisfunction: mismatched first dimension: |
| 556 | \$$varname2->dim(0) == int(\$$varname1->dim(0)/2) + 1 wasn't true. |
| 557 | $varname1: @{[$var1->info]} |
| 558 | $varname2: @{[$var2->info]} |
| 559 | Giving up. |
| 560 | EOF |
| 561 | for my $idim (1..$rank-1) { |
| 562 | if ( $var1->dim($idim) != $var2->dim($idim) ) { |
| 563 | barf <<EOF; |
| 564 | $thisfunction was given input/output matrices of non-matching sizes. |
| 565 | Giving up. |
| 566 | EOF |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | sub processTypes |
| 572 | { |
| 573 | my ($thisfunction, $in, $out) = @_; |
| 574 | |
| 575 | # types: |
| 576 | # |
| 577 | # Input and output types must match, and I can only really deal with float and |
| 578 | # double. If given an output, I refuse to tweak the type of the output, |
| 579 | # otherwise, I upgrade to float and then to double |
| 580 | if( $$out->isnull ) { |
| 581 | if( $$in->type < float ) { |
| 582 | forceType( $in, (float) ); |
| 583 | } |
| 584 | } else { |
| 585 | # I'm given an output. Make sure this is of a type I can work with, |
| 586 | # otherwise give up |
| 587 | my $out_type = $$out->type; |
| 588 | barf <<EOF if $out_type < float; |
| 589 | $thisfunction can only generate 'float' or 'double' output. You gave an output |
| 590 | of type '$out_type'. I can't change this so I give up |
| 591 | EOF |
| 592 | my $in_type = $$in->type; |
| 593 | my $in_precision = getPrecision($in_type); |
| 594 | my $out_precision = getPrecision($out_type); |
| 595 | return if $in_precision == $out_precision; |
| 596 | forceType( $in, typeWithComplexity($out_precision, !$in_type->real) ); |
| 597 | forceType( $out, typeWithComplexity($out_precision, !$out_type->real) ); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | sub typeWithComplexity { |
| 602 | my ($precision, $complex) = @_; |
| 603 | $complex ? ($precision == 1 ? cfloat : cdouble) : |
| 604 | $precision == 1 ? float : double; |
| 605 | } |
| 606 | |
| 607 | sub getPrecision { |
| 608 | my ($type) = @_; |
| 609 | ($type <= float || $type == cfloat) ? 1 : # float |
| 610 | 2; # double |
| 611 | } |
| 612 | |
| 613 | sub forceType |
| 614 | { |
| 615 | my ($x, $type) = @_; |
| 616 | $$x = convert( $$x, $type ) unless $$x->type == $type; |
| 617 | } |
| 618 | |
| 619 | sub getPlan |
| 620 | { |
| 621 | my ($thisfunction, $rank, $is_real_fft, $do_inverse_fft, $in, $out) = @_; |
| 622 | |
| 623 | # I get the plan ID, check if I already have a plan, and make a new plan if I |
| 624 | # don't already have one |
| 625 | |
| 626 | my @dims = ((!$is_real_fft || !$do_inverse_fft) ? $in : $out)->dims; # FFT dimensionality |
| 627 | |
| 628 | my $Nslices = reduce {$a*$b} 1, splice(@dims, $rank); |
| 629 | |
| 630 | my $do_double_precision = ($in->get_datatype == $PDL_F || $in->get_datatype == $PDL_CF) |
| 631 | ? 0 : 1; |
| 632 | $_last_do_double_precision = $do_double_precision; |
| 633 | |
| 634 | my $do_inplace = is_same_data( $in, $out ); |
| 635 | |
| 636 | # I compute a single plan for the whole set of broadcast slices. I make a |
| 637 | # worst-case plan, so I find the worst-aligned broadcast slice and plan off of |
| 638 | # it. So if $Nslices>1 then the worst-case alignment is the worse of (1st, |
| 639 | # 2nd) slices |
| 640 | my $in_alignment = get_data_alignment_pdl( $in ); |
| 641 | my $out_alignment = get_data_alignment_pdl( $out ); |
| 642 | my $stride_bytes = ($do_double_precision ? 8 : 4) * reduce {$a*$b} @dims; |
| 643 | if( $Nslices > 1 ) |
| 644 | { |
| 645 | my $in_alignment_2nd = get_data_alignment_int($in_alignment + $stride_bytes); |
| 646 | my $out_alignment_2nd = get_data_alignment_int($out_alignment + $stride_bytes); |
| 647 | $in_alignment = $in_alignment_2nd if $in_alignment_2nd < $in_alignment; |
| 648 | $out_alignment = $out_alignment_2nd if $out_alignment_2nd < $out_alignment; |
| 649 | } |
| 650 | |
| 651 | my $planID = join('_', |
| 652 | $thisfunction, |
| 653 | $do_double_precision, |
| 654 | $do_inplace, |
| 655 | $in_alignment, |
| 656 | $out_alignment, |
| 657 | @dims); |
| 658 | if ( !exists $existingPlans{$planID} ) |
| 659 | { |
| 660 | lock(%existingPlans); |
| 661 | $existingPlans{$planID} = compute_plan( \@dims, $do_double_precision, $is_real_fft, $do_inverse_fft, |
| 662 | $in, $out, $in_alignment, $out_alignment ); |
| 663 | $_Nplans++; |
| 664 | } |
| 665 | |
| 666 | return $existingPlans{$planID}; |
| 667 | } |
| 668 | |
| 669 | #line 580 "fftw3.pd" |
| 670 | sub fft1 { __fft_internal( "fft1",@_ ); } |
| 671 | *PDL::fft1 = \&fft1; |
| 672 | |
| 673 | sub ifft1 { |
| 674 | my $a = __fft_internal( "ifft1", @_ ); |
| 675 | $a /= $a->shape->slice('0:0')->prodover; |
| 676 | $a; |
| 677 | } |
| 678 | *PDL::ifft1 = \&ifft1; |
| 679 | |
| 680 | sub rfft1 { __fft_internal( "rfft1", @_ ); } |
| 681 | *PDL::rfft1 = \&rfft1; |
| 682 | |
| 683 | sub rNfft1 { __fft_internal( "rNfft1", @_ ); } |
| 684 | *PDL::rNfft1 = \&rNfft1; |
| 685 | |
| 686 | sub irfft1 { my $a = __fft_internal( "irfft1", @_ ); $a /= $a->shape->slice('0:0')->prodover; $a; } |
| 687 | *PDL::irfft1 = \&irfft1; |
| 688 | #line 580 "fftw3.pd" |
| 689 | |
| 690 | sub fft2 { __fft_internal( "fft2",@_ ); } |
| 691 | *PDL::fft2 = \&fft2; |
| 692 | |
| 693 | sub ifft2 { |
| 694 | my $a = __fft_internal( "ifft2", @_ ); |
| 695 | $a /= $a->shape->slice('0:1')->prodover; |
| 696 | $a; |
| 697 | } |
| 698 | *PDL::ifft2 = \&ifft2; |
| 699 | |
| 700 | sub rfft2 { __fft_internal( "rfft2", @_ ); } |
| 701 | *PDL::rfft2 = \&rfft2; |
| 702 | |
| 703 | sub rNfft2 { __fft_internal( "rNfft2", @_ ); } |
| 704 | *PDL::rNfft2 = \&rNfft2; |
| 705 | |
| 706 | sub irfft2 { my $a = __fft_internal( "irfft2", @_ ); $a /= $a->shape->slice('0:1')->prodover; $a; } |
| 707 | *PDL::irfft2 = \&irfft2; |
| 708 | #line 580 "fftw3.pd" |
| 709 | |
| 710 | sub fft3 { __fft_internal( "fft3",@_ ); } |
| 711 | *PDL::fft3 = \&fft3; |
| 712 | |
| 713 | sub ifft3 { |
| 714 | my $a = __fft_internal( "ifft3", @_ ); |
| 715 | $a /= $a->shape->slice('0:2')->prodover; |
| 716 | $a; |
| 717 | } |
| 718 | *PDL::ifft3 = \&ifft3; |
| 719 | |
| 720 | sub rfft3 { __fft_internal( "rfft3", @_ ); } |
| 721 | *PDL::rfft3 = \&rfft3; |
| 722 | |
| 723 | sub rNfft3 { __fft_internal( "rNfft3", @_ ); } |
| 724 | *PDL::rNfft3 = \&rNfft3; |
| 725 | |
| 726 | sub irfft3 { my $a = __fft_internal( "irfft3", @_ ); $a /= $a->shape->slice('0:2')->prodover; $a; } |
| 727 | *PDL::irfft3 = \&irfft3; |
| 728 | #line 580 "fftw3.pd" |
| 729 | |
| 730 | sub fft4 { __fft_internal( "fft4",@_ ); } |
| 731 | *PDL::fft4 = \&fft4; |
| 732 | |
| 733 | sub ifft4 { |
| 734 | my $a = __fft_internal( "ifft4", @_ ); |
| 735 | $a /= $a->shape->slice('0:3')->prodover; |
| 736 | $a; |
| 737 | } |
| 738 | *PDL::ifft4 = \&ifft4; |
| 739 | |
| 740 | sub rfft4 { __fft_internal( "rfft4", @_ ); } |
| 741 | *PDL::rfft4 = \&rfft4; |
| 742 | |
| 743 | sub rNfft4 { __fft_internal( "rNfft4", @_ ); } |
| 744 | *PDL::rNfft4 = \&rNfft4; |
| 745 | |
| 746 | sub irfft4 { my $a = __fft_internal( "irfft4", @_ ); $a /= $a->shape->slice('0:3')->prodover; $a; } |
| 747 | *PDL::irfft4 = \&irfft4; |
| 748 | #line 580 "fftw3.pd" |
| 749 | |
| 750 | sub fft5 { __fft_internal( "fft5",@_ ); } |
| 751 | *PDL::fft5 = \&fft5; |
| 752 | |
| 753 | sub ifft5 { |
| 754 | my $a = __fft_internal( "ifft5", @_ ); |
| 755 | $a /= $a->shape->slice('0:4')->prodover; |
| 756 | $a; |
| 757 | } |
| 758 | *PDL::ifft5 = \&ifft5; |
| 759 | |
| 760 | sub rfft5 { __fft_internal( "rfft5", @_ ); } |
| 761 | *PDL::rfft5 = \&rfft5; |
| 762 | |
| 763 | sub rNfft5 { __fft_internal( "rNfft5", @_ ); } |
| 764 | *PDL::rNfft5 = \&rNfft5; |
| 765 | |
| 766 | sub irfft5 { my $a = __fft_internal( "irfft5", @_ ); $a /= $a->shape->slice('0:4')->prodover; $a; } |
| 767 | *PDL::irfft5 = \&irfft5; |
| 768 | #line 580 "fftw3.pd" |
| 769 | |
| 770 | sub fft6 { __fft_internal( "fft6",@_ ); } |
| 771 | *PDL::fft6 = \&fft6; |
| 772 | |
| 773 | sub ifft6 { |
| 774 | my $a = __fft_internal( "ifft6", @_ ); |
| 775 | $a /= $a->shape->slice('0:5')->prodover; |
| 776 | $a; |
| 777 | } |
| 778 | *PDL::ifft6 = \&ifft6; |
| 779 | |
| 780 | sub rfft6 { __fft_internal( "rfft6", @_ ); } |
| 781 | *PDL::rfft6 = \&rfft6; |
| 782 | |
| 783 | sub rNfft6 { __fft_internal( "rNfft6", @_ ); } |
| 784 | *PDL::rNfft6 = \&rNfft6; |
| 785 | |
| 786 | sub irfft6 { my $a = __fft_internal( "irfft6", @_ ); $a /= $a->shape->slice('0:5')->prodover; $a; } |
| 787 | *PDL::irfft6 = \&irfft6; |
| 788 | #line 580 "fftw3.pd" |
| 789 | |
| 790 | sub fft7 { __fft_internal( "fft7",@_ ); } |
| 791 | *PDL::fft7 = \&fft7; |
| 792 | |
| 793 | sub ifft7 { |
| 794 | my $a = __fft_internal( "ifft7", @_ ); |
| 795 | $a /= $a->shape->slice('0:6')->prodover; |
| 796 | $a; |
| 797 | } |
| 798 | *PDL::ifft7 = \&ifft7; |
| 799 | |
| 800 | sub rfft7 { __fft_internal( "rfft7", @_ ); } |
| 801 | *PDL::rfft7 = \&rfft7; |
| 802 | |
| 803 | sub rNfft7 { __fft_internal( "rNfft7", @_ ); } |
| 804 | *PDL::rNfft7 = \&rNfft7; |
| 805 | |
| 806 | sub irfft7 { my $a = __fft_internal( "irfft7", @_ ); $a /= $a->shape->slice('0:6')->prodover; $a; } |
| 807 | *PDL::irfft7 = \&irfft7; |
| 808 | #line 580 "fftw3.pd" |
| 809 | |
| 810 | sub fft8 { __fft_internal( "fft8",@_ ); } |
| 811 | *PDL::fft8 = \&fft8; |
| 812 | |
| 813 | sub ifft8 { |
| 814 | my $a = __fft_internal( "ifft8", @_ ); |
| 815 | $a /= $a->shape->slice('0:7')->prodover; |
| 816 | $a; |
| 817 | } |
| 818 | *PDL::ifft8 = \&ifft8; |
| 819 | |
| 820 | sub rfft8 { __fft_internal( "rfft8", @_ ); } |
| 821 | *PDL::rfft8 = \&rfft8; |
| 822 | |
| 823 | sub rNfft8 { __fft_internal( "rNfft8", @_ ); } |
| 824 | *PDL::rNfft8 = \&rNfft8; |
| 825 | |
| 826 | sub irfft8 { my $a = __fft_internal( "irfft8", @_ ); $a /= $a->shape->slice('0:7')->prodover; $a; } |
| 827 | *PDL::irfft8 = \&irfft8; |
| 828 | #line 580 "fftw3.pd" |
| 829 | |
| 830 | sub fft9 { __fft_internal( "fft9",@_ ); } |
| 831 | *PDL::fft9 = \&fft9; |
| 832 | |
| 833 | sub ifft9 { |
| 834 | my $a = __fft_internal( "ifft9", @_ ); |
| 835 | $a /= $a->shape->slice('0:8')->prodover; |
| 836 | $a; |
| 837 | } |
| 838 | *PDL::ifft9 = \&ifft9; |
| 839 | |
| 840 | sub rfft9 { __fft_internal( "rfft9", @_ ); } |
| 841 | *PDL::rfft9 = \&rfft9; |
| 842 | |
| 843 | sub rNfft9 { __fft_internal( "rNfft9", @_ ); } |
| 844 | *PDL::rNfft9 = \&rNfft9; |
| 845 | |
| 846 | sub irfft9 { my $a = __fft_internal( "irfft9", @_ ); $a /= $a->shape->slice('0:8')->prodover; $a; } |
| 847 | *PDL::irfft9 = \&irfft9; |
| 848 | #line 580 "fftw3.pd" |
| 849 | |
| 850 | sub fft10 { __fft_internal( "fft10",@_ ); } |
| 851 | *PDL::fft10 = \&fft10; |
| 852 | |
| 853 | sub ifft10 { |
| 854 | my $a = __fft_internal( "ifft10", @_ ); |
| 855 | $a /= $a->shape->slice('0:9')->prodover; |
| 856 | $a; |
| 857 | } |
| 858 | *PDL::ifft10 = \&ifft10; |
| 859 | |
| 860 | sub rfft10 { __fft_internal( "rfft10", @_ ); } |
| 861 | *PDL::rfft10 = \&rfft10; |
| 862 | |
| 863 | sub rNfft10 { __fft_internal( "rNfft10", @_ ); } |
| 864 | *PDL::rNfft10 = \&rNfft10; |
| 865 | |
| 866 | sub irfft10 { my $a = __fft_internal( "irfft10", @_ ); $a /= $a->shape->slice('0:9')->prodover; $a; } |
| 867 | *PDL::irfft10 = \&irfft10; |
| 868 | #line 608 "fftw3.pd" |
| 869 | |
| 870 | sub _rank_springboard { |
| 871 | my ($name, $source, $rank, @rest) = @_; |
| 872 | my $inverse = ($name =~ m/^i/); |
| 873 | |
| 874 | unless(defined $rank) { |
| 875 | die "${name}n: second argument must be the rank of the transform you want"; |
| 876 | } |
| 877 | $rank = 0+$rank; # force numeric context |
| 878 | unless($rank>=1 ) { |
| 879 | die "${name}n: second argument (rank) must be between 1 and 10"; |
| 880 | } |
| 881 | |
| 882 | my $active_lo = 0; |
| 883 | my $active_hi = $rank-1; |
| 884 | |
| 885 | unless($source->ndims > $active_hi) { |
| 886 | die "${name}n: rank is $rank but input has only ".($active_hi-$active_lo)." active dims!"; |
| 887 | } |
| 888 | |
| 889 | my $out = __fft_internal( $name.$rank, $source, @rest ); |
| 890 | |
| 891 | if($inverse) { |
| 892 | $out /= $out->shape->slice("$active_lo:$active_hi")->prodover; |
| 893 | } |
| 894 | return $out; |
| 895 | } |
| 896 | |
| 897 | sub fftn { _rank_springboard( "fft", @_ ) } |
| 898 | sub ifftn { _rank_springboard( "ifft", @_ ) } |
| 899 | sub rfftn { _rank_springboard( "rfft", @_ ) } |
| 900 | sub irfftn { _rank_springboard( "irfft", @_ ) } |
| 901 | |
| 902 | *PDL::fftn = \&fftn; |
| 903 | *PDL::ifftn = \&ifftn; |
| 904 | *PDL::rfftn = \&rfftn; |
| 905 | *PDL::irfftn = \&irfftn; |
| 906 | #line 907 "FFTW3.pm" |
| 907 | |
| 908 | # Exit with OK status |
| 909 | |
| 910 | 1; |