| 1 |
=head1 NAME |
| 2 |
|
| 3 |
PDL::FFTW3 - PDL interface to the Fastest Fourier Transform in the West v3 |
| 4 |
|
| 5 |
=head1 SYNOPSIS |
| 6 |
|
| 7 |
use PDL; |
| 8 |
use PDL::FFTW3; |
| 9 |
use PDL::Graphics::Gnuplot; |
| 10 |
|
| 11 |
# Basic functionality |
| 12 |
my $x = sin( sequence(100) * 2.0 ) + 2.0 * cos( sequence(100) / 3.0 ); |
| 13 |
my $F = rfft1( $x ); |
| 14 |
gplot( with => 'lines', $F->abs ); |
| 15 |
|
| 16 |
=====> |
| 17 |
|
| 18 |
8000 ++------------+-------------+------------+-------------+------------++ |
| 19 |
+ + + + + + |
| 20 |
| | |
| 21 |
| * | |
| 22 |
7000 ++ * ++ |
| 23 |
| * | |
| 24 |
| * | |
| 25 |
| * | |
| 26 |
| * | |
| 27 |
6000 ++ * ++ |
| 28 |
| * | |
| 29 |
| * | |
| 30 |
| * | |
| 31 |
5000 ++ * ++ |
| 32 |
| * | |
| 33 |
| * | |
| 34 |
| ** | |
| 35 |
4000 ++ ** ++ |
| 36 |
| ** | |
| 37 |
| * * | |
| 38 |
| * * | |
| 39 |
| * * | |
| 40 |
3000 ++ * * ++ |
| 41 |
| * * | |
| 42 |
| * * | |
| 43 |
| * * * | |
| 44 |
2000 ++ * * * ++ |
| 45 |
| * * * | |
| 46 |
| * * ** | |
| 47 |
| * * ** | |
| 48 |
| * * ** | |
| 49 |
1000 ++ * * * * ++ |
| 50 |
| * * * * | |
| 51 |
| ** * * * | |
| 52 |
+ * * + + + * * + + |
| 53 |
0 ****-------*********************************--************************ |
| 54 |
0 10 20 30 40 50 |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
# Correlation of two real signals |
| 59 |
|
| 60 |
# two signals offset by 30 units |
| 61 |
my $x = sequence(100); |
| 62 |
my $y1 = exp( 0.2*($x - 20.5) ** (-2.0) ); |
| 63 |
my $y2 = exp( 0.2*($x - 50.5) ** (-2.0) ); |
| 64 |
|
| 65 |
# compute the correlation |
| 66 |
my $F12 = rfft1( cat($y1,$y2) ); |
| 67 |
my $corr = irfft1( $F12(:,(1)) * $F12(:,(0))->conj ); |
| 68 |
# and find the peak |
| 69 |
say maximum_ind($corr); |
| 70 |
|
| 71 |
=====> 30 |
| 72 |
|
| 73 |
=head1 DESCRIPTION |
| 74 |
|
| 75 |
This is a PDL binding to version 3 of the FFTW library. Supported are complex |
| 76 |
<-> complex and real <-> complex FFTs. |
| 77 |
|
| 78 |
=head2 NB to install |
| 79 |
|
| 80 |
wget http://www.fftw.org/fftw-3.3.4.tar.gz |
| 81 |
tar xvf fftw-3.3.4.tar.gz |
| 82 |
cd fftw-3.3.4/ |
| 83 |
./configure --prefix=/usr --enable-threads --enable-float --enable-shared --with-pic |
| 84 |
make all install install-pkgconfigDATA |
| 85 |
make clean |
| 86 |
./configure --prefix=/usr --enable-threads --enable-shared --with-pic |
| 87 |
make all install install-pkgconfigDATA |
| 88 |
|
| 89 |
This will give you both fftw3f (first chunk) and fftw3 (second). |
| 90 |
|
| 91 |
=head2 Supported operations |
| 92 |
|
| 93 |
This module computes the Discrete Fourier Transform. In its most basic form, |
| 94 |
this transform converts a vector of complex numbers in the time domain into |
| 95 |
another vector of complex numbers in the frequency domain. These complex <-> |
| 96 |
complex transforms are supported with C<fftN> functions for a rank-C<N> |
| 97 |
transform. The opposite effect (transform data in the frequency domain back to |
| 98 |
the time domain) can be achieved with the C<ifftN> functions. |
| 99 |
|
| 100 |
A common use case is to transform purely-real data. This data has 0 for its |
| 101 |
complex component, and FFTW can take advantage of this to compute the FFT faster |
| 102 |
and using less memory. Since a Fourier Transform of a real signal has an even |
| 103 |
real part and an odd imaginary part, only 1/2 of the spectrum is needed. These |
| 104 |
forward real -> complex transforms are supported with the C<rfftN> functions. |
| 105 |
The backward version of this transform is complex -> real and is supported with |
| 106 |
the C<irfftN> functions. |
| 107 |
|
| 108 |
=head2 Basic usage details |
| 109 |
|
| 110 |
Arbitrary C<N>-dimensional transforms are supported. All functions exported by |
| 111 |
this module have the C<N> in their name, so for instance a complex <-> complex |
| 112 |
3D forward transform is computed with the C<fft3> function. The rank I<must |
| 113 |
always> be specified in this way; there is no function called simply C<fft>. |
| 114 |
|
| 115 |
In-place operation is supported for complex <-> complex functions, but not the |
| 116 |
real ones (real functions don't have matching dimensionality of the input and |
| 117 |
output). An in-place transform of C<$x> can be computed with |
| 118 |
|
| 119 |
fft1( $x->inplace ); |
| 120 |
|
| 121 |
All the functions in this module support PDL broadcasting. For instance, if we have |
| 122 |
4 different image ndarrays C<$a>, C<$b>, C<$c>, C<$d> and we want to compute |
| 123 |
their 2D FFTs at the same time, we can say |
| 124 |
|
| 125 |
my $ABCD_transformed = rfft2( PDL::cat( $a, $b, $c, $d) ); |
| 126 |
|
| 127 |
This takes advantage of PDL's automatic parallelization, if appropriate (See |
| 128 |
L<PDL::ParallelCPU>). |
| 129 |
|
| 130 |
=head2 Data formats |
| 131 |
|
| 132 |
FFTW supports single and double-precision floating point numbers directly. If |
| 133 |
possible, the PDL input will be used as-is. If not, a type conversion will be |
| 134 |
made to use the lowest-common type. So as an example, the following will perform |
| 135 |
a single-precision floating point transform (and return data of that type). |
| 136 |
|
| 137 |
fft1( $x->byte ) |
| 138 |
|
| 139 |
As of 0.20, this module expects complex numbers to be stored as "native |
| 140 |
complex" types (C<cfloat>, C<cdouble>). Complex outputs will also be native |
| 141 |
complex. |
| 142 |
|
| 143 |
Generally, the sizes of the input and the output must match. This is completely |
| 144 |
true for the complex <-> complex transforms: the output will have the same size |
| 145 |
and the input, and an error will result if this isn't possible for some reason. |
| 146 |
|
| 147 |
This is a little bit more involved for the real <-> complex transforms. If I'm |
| 148 |
transforming a real 3D vector of dimensions C<K,L,M>, I will get a complex output of |
| 149 |
dimensions C<int(K/2)+1,L,M>. |
| 150 |
The C<K/2> is there because the input was real. The first dimension is |
| 151 |
always the one that gets the C<K/2>. This is described in detail in section 2.4 |
| 152 |
of the FFTW manual. |
| 153 |
|
| 154 |
Note that given a real input, the dimensionality of the complex transformed |
| 155 |
output is unambiguous. However, this is I<not> true for the backward transform. |
| 156 |
For instance, a 1D inverse transform of a vector of 10 complex numbers can |
| 157 |
produce real output of either 18 or 19 elements (because C<int(18/2)+1 == 10> |
| 158 |
and C<int(19/2)+1 == 10>). |
| 159 |
|
| 160 |
I<Without any extra information this module assumes the even-sized input>. |
| 161 |
|
| 162 |
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: |
| 163 |
|
| 164 |
irfft1( sequence(cdouble,10), zeros(19) ) |
| 165 |
|
| 166 |
Here I create a new output ndarray with the C<zeros> function; C<irfft1> then |
| 167 |
fills in this ndarray with the result of the computation. This module validates |
| 168 |
all of its input, so only 18 and 19 are valid here. An error will be thrown if |
| 169 |
you try to pass in C<zeros(20)>. |
| 170 |
|
| 171 |
This all means that the following will produce surprising results if |
| 172 |
C<$x-E<gt>dim(0)> isn't even |
| 173 |
|
| 174 |
irfft1( rfft1( $x ) ) |
| 175 |
|
| 176 |
=head2 FFT normalization |
| 177 |
|
| 178 |
Following the widest-used convention for discrete Fourier transforms, |
| 179 |
this module normalizes the inverse transform (but not the forward |
| 180 |
transform) by dividing by the number of elements in the data set, so |
| 181 |
that |
| 182 |
|
| 183 |
ifft1( fft1( $x ) ) |
| 184 |
|
| 185 |
is a slow approximate no-op, if C<$x> is well-behaved. |
| 186 |
|
| 187 |
This is different from the behavior of the underlying FFTW3 library itself, |
| 188 |
but more consistent with other FFT packages for popular analysis languages |
| 189 |
including PDL. |
| 190 |
|
| 191 |
|
| 192 |
=head1 FUNCTIONS |
| 193 |
|
| 194 |
=head2 fft1, fft2, fft3, fft4, fft5, fft6, fft7, fft8, fft9, fft10, fftn |
| 195 |
|
| 196 |
=for ref |
| 197 |
|
| 198 |
Basic complex -> complex FFT. |
| 199 |
|
| 200 |
You can pass in the rank as a |
| 201 |
parameter with the C<fftn> form, or append the rank to the function |
| 202 |
name for ranks up to 10. These functions all take one input ndarray and |
| 203 |
one output ndarray. The dimensions of the input and the output are |
| 204 |
identical. The output parameter is optional and, if present, must be |
| 205 |
the last argument. If the output ndarray is passed in, the user I<must> |
| 206 |
make sure the dimensions match. |
| 207 |
|
| 208 |
As of 0.20, inputs must be "native complex" data. Any type other |
| 209 |
than C<cfloat> or C<cdouble> will be converted in the normal PP |
| 210 |
way. |
| 211 |
|
| 212 |
The fftn form takes a minimum of two arguments: the PDL to transform, |
| 213 |
and the number of dimensions to transform as a separate argument. |
| 214 |
|
| 215 |
The following are equivalent: |
| 216 |
|
| 217 |
$X = fftn( $x, 1 ); |
| 218 |
$X = fft1( $x ); |
| 219 |
fft1( $x, my $X = $x->zeros ); |
| 220 |
|
| 221 |
|
| 222 |
=head2 ifft1, ifft2, ifft3, ifft4, ifft5, ifft6, ifft7, ifft8, ifft9, ifft10, ifftn |
| 223 |
|
| 224 |
=for ref |
| 225 |
|
| 226 |
Basic, properly normalized, complex -> complex backward FFT. |
| 227 |
|
| 228 |
Everything is exactly like in the C<fftX> functions, except the |
| 229 |
inverse transform is computed and normalized, so that (for example) |
| 230 |
|
| 231 |
ifft1( fft1 ( $x ) ) |
| 232 |
|
| 233 |
is a good approximation of C<$x> itself. |
| 234 |
|
| 235 |
=head2 rfft1, rfft2, rfft3, rfft4, rfft5, rfft6, rfft7, rfft8, rfft9, rfft10, rfftn |
| 236 |
|
| 237 |
=for ref |
| 238 |
|
| 239 |
Real -> complex FFT. |
| 240 |
|
| 241 |
You can pass in the rank with the C<rfftn> |
| 242 |
form, or append the rank to the function name for ranks up to 9. |
| 243 |
These functions all take one input ndarray and one output ndarray. The |
| 244 |
dimensions of the input and the output are not identical, but are |
| 245 |
related as described in L<Data formats>. The output can be passed in |
| 246 |
as the last argument, if desired. If the output ndarray is passed in, |
| 247 |
the user I<must> make sure the dimensions match. |
| 248 |
|
| 249 |
In the C<rfftn> form, the rank is the second argument. |
| 250 |
|
| 251 |
The following are equivalent: |
| 252 |
|
| 253 |
$X = rfftn( $x, 1 ); |
| 254 |
$X = rfft1( $x ); |
| 255 |
rfft1( $x, my $X = $x->zeroes ); |
| 256 |
|
| 257 |
As of 0.20, only returns native-complex data. Support for PDL::Complex |
| 258 |
has been removed. |
| 259 |
|
| 260 |
=head2 rNfft1, rNfft2, rNfft3, rNfft4, rNfft5, rNfft6, rNfft7, rNfft8, rNfft9, rNfft10, rNfftn |
| 261 |
|
| 262 |
=for ref |
| 263 |
|
| 264 |
As of 0.20, just an alias for rfftX, etc. |
| 265 |
|
| 266 |
=head2 irfft1, irfft2, irfft3, irfft4, irfft5, irfft6, irfft7, irfft8, irfft9, irfft10, irfftn |
| 267 |
|
| 268 |
=for ref |
| 269 |
|
| 270 |
Complex -> real inverse FFT. |
| 271 |
|
| 272 |
You can pass in the rank with the |
| 273 |
C<irfftn> form, or append the rank to the function name for ranks up |
| 274 |
to 9. Argument passing and interpretation is as described in |
| 275 |
C<rfftX> above. Please read L<Data formats> for details about dimension |
| 276 |
interpretation. There's an ambiguity about the output dimensionality, |
| 277 |
which is described in that section. |
| 278 |
|
| 279 |
=head1 AUTHOR |
| 280 |
|
| 281 |
Dima Kogan, C<< <[email protected]> >>; contributions from Craig |
| 282 |
DeForest, C<< <[email protected]> >>. |
| 283 |
|
| 284 |
=head1 LICENSE AND COPYRIGHT |
| 285 |
|
| 286 |
Copyright 2013 Dima Kogan and Craig DeForest. |
| 287 |
|
| 288 |
This program is free software; you can redistribute it and/or modify it |
| 289 |
under the same terms as PDL. |
| 290 |
|
| 291 |
=cut |