tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
fftshift.cc
1 // -*- mode: c++; fill-column: 80; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 /*
3  Copyright (c) 2013 Juan Jose Garcia Ripoll
4 
5  Tensor is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published
7  by the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Library General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #include <tensor/fftw.h>
21 
22 namespace tensor {
23 
24  const CTensor
25  fftshift(const CTensor& input, int direction) {
26  // just forward to the simpler function
27  CTensor output(input);
28  for (index dim = 0; dim < input.rank(); dim++) {
29  output = fftshift(output, dim, direction);
30  }
31 
32  return output;
33  }
34 
35  const CTensor
36  fftshift(const CTensor& input, index dim, int direction) {
37  assert(dim >= 0 && dim < input.rank());
38 
39  const Indices& size = input.dimensions();
40  if (size[dim] == 1) {
41  // nothing to do
42  return input;
43  }
44 
45  index before = 1;
46  for (index i = 0; i < dim; i++) {
47  before *= size[i];
48  }
49 
50  index after = 1;
51  for (index i = dim+1; i < size.size(); i++) {
52  after *= size[i];
53  }
54 
55  // Transform the tensor into a 3-dimensional form, then use slicing.
56  // Not terribly efficient, but we can delay extensive memory handling to
57  // until someone needs the speed.
58  CTensor output(before, size[dim], after);
59  const CTensor reshape_input = reshape(input, output.dimensions());
60 
61  // even number of grid points as default => direction has no effect.
62  index minfreq = size[dim]/2;
63  index Nhalf = size[dim]/2;
64  index end = size[dim]-1;
65 
66  if (size[dim] % 2 == 1) {
67  minfreq = (size[dim]+1)/2;
68  Nhalf = (size[dim]-1)/2;
69  if (direction == FFTW_BACKWARD) {
70  // undo the effects of the forward shift, requires some creativity
71  minfreq--;
72  Nhalf++;
73  }
74  }
75 
76  output.at(range(), range(0,Nhalf-1), range()) = reshape_input(range(), range(minfreq,end), range());
77  output.at(range(), range(Nhalf, end), range()) = reshape_input(range(), range(0, minfreq-1), range());
78 
79  return reshape(output, size);
80  }
81 
82  const CTensor
83  fftshift(const CTensor& input, const Booleans& convert, int direction) {
84  assert(input.rank() == convert.size());
85 
86  CTensor output(input);
87  for (index dim = 0; dim < input.rank(); dim++) {
88  if (convert[dim]) {
89  output = fftshift(output, dim, direction);
90  }
91  }
92 
93  return output;
94  }
95 }
const RTensor reshape(const RTensor &t, const Indices &new_dims)
Return a RTensor with same data and given dimensions.
const Indices & dimensions() const
Return Tensor dimensions.
Definition: tensor.h:121