TBTK
Need a break? Support the development by playing Polarity Puzzles
Convolver.h
Go to the documentation of this file.
1 /* Copyright 2018 Kristofer Björnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
23 #ifndef COM_DAFER45_TBTK_CONVOLVER
24 #define COM_DAFER45_TBTK_CONVOLVER
25 
26 #include "TBTK/Array.h"
27 #include "TBTK/FourierTransform.h"
28 
29 namespace TBTK{
30 
31 class Convolver{
32 public:
40  template<typename DataType>
42  const Array<DataType> &array0,
43  const Array<DataType> &array1
44  );
45 
53  template<typename DataType>
55  const Array<DataType> &array0,
56  const Array<DataType> &array1
57  );
58 private:
59 };
60 
61 template<typename DataType>
63  const Array<DataType> &array0,
64  const Array<DataType> &array1
65 ){
66  const std::vector<unsigned int> &ranges0 = array0.getRanges();
67  const std::vector<unsigned int> &ranges1 = array1.getRanges();
68  TBTKAssert(
69  ranges0.size() == ranges1.size(),
70  "Convolver::convolve()",
71  "Incompatible ranges. The ranges must be equal, but array0 has"
72  << " '" << ranges0.size() << "' range parameters, while array1"
73  << " has '" << ranges1.size() << "' range parameters.",
74  ""
75  );
76 
77  for(unsigned int n = 0; n < ranges0.size(); n++){
78  TBTKAssert(
79  ranges0[n] == ranges1[n],
80  "Convolver::convolve()",
81  "Incompatible ranges. The ranges must be equal, but"
82  << " range '" << n << "' is '" << ranges0[n] << "' for"
83  << " array0, while it is '" << ranges1[n] << "' for"
84  << " array1.",
85  ""
86  );
87  }
88 
89  Array<DataType> array0Out
91  Array<DataType> array1Out
93 
95  array0.getData(),
96  array0Out.getData(),
97  array0.getRanges()
98  );
99  plan0.setNormalizationFactor(1);
101  array1.getData(),
102  array1Out.getData(),
103  array1.getRanges()
104  );
105  plan1.setNormalizationFactor(1);
106 
109 
111  for(unsigned int n = 0; n < result.getSize(); n++)
112  result[n] = array0Out[n]*array1Out[n];
113 
115  result.getData(),
116  result.getData(),
117  result.getRanges()
118  );
119  planResult.setNormalizationFactor(1);
120 
121  FourierTransform::transform(planResult);
122 
123  for(unsigned int n = 0; n < result.getSize(); n++)
124  result[n] /= result.getSize();
125 
126  return result;
127 }
128 
129 template<typename DataType>
131  const Array<DataType> &array0,
132  const Array<DataType> &array1
133 ){
134  const std::vector<unsigned int> &ranges0 = array0.getRanges();
135  const std::vector<unsigned int> &ranges1 = array1.getRanges();
136  TBTKAssert(
137  ranges0.size() == ranges1.size(),
138  "Convolver::crossCorrelate()",
139  "Incompatible ranges. The ranges must be equal, but array0 has"
140  << " '" << ranges0.size() << "' range parameters, while array1"
141  << " has '" << ranges1.size() << "' range parameters.",
142  ""
143  );
144 
145  for(unsigned int n = 0; n < ranges0.size(); n++){
146  TBTKAssert(
147  ranges0[n] == ranges1[n],
148  "Convolver::crossCorrelate()",
149  "Incompatible ranges. The ranges must be equal, but"
150  << " range '" << n << "' is '" << ranges0[n] << "' for"
151  << " array0, while it is '" << ranges1[n] << "' for"
152  << " array1.",
153  ""
154  );
155  }
156 
157  Array<DataType> array0Out
159  Array<DataType> array1Out
161 
163  array0.getData(),
164  array0Out.getData(),
165  array0.getRanges()
166  );
167  plan0.setNormalizationFactor(1);
169  array1.getData(),
170  array1Out.getData(),
171  array1.getRanges()
172  );
173  plan1.setNormalizationFactor(1);
174 
177 
179  for(unsigned int n = 0; n < result.getSize(); n++)
180  result[n] = conj(array0Out[n])*array1Out[n];
181 
183  result.getData(),
184  result.getData(),
185  result.getRanges()
186  );
187  planResult.setNormalizationFactor(1);
188 
189  FourierTransform::transform(planResult);
190 
191  for(unsigned int n = 0; n < result.getSize(); n++)
192  result[n] /= result.getSize();
193 
194  return result;
195 }
196 
197 }; //End of namespace TBTK
198 
199 #endif
TBTK::Array
Multi-dimensional array.
Definition: Array.h:98
TBTK::FourierTransform::Plan::setNormalizationFactor
void setNormalizationFactor(double normalizationFactor)
Definition: FourierTransform.h:255
TBTK::Array::getData
CArray< DataType > & getData()
Definition: Array.h:1281
Array.h
Multi-dimensional array.
TBTK::Convolver
Definition: Convolver.h:31
TBTK::Convolver::crossCorrelate
static Array< DataType > crossCorrelate(const Array< DataType > &array0, const Array< DataType > &array1)
Definition: Convolver.h:130
TBTK::FourierTransform::transform
static void transform(const CArray< std::complex< double >> &in, CArray< std::complex< double >> &out, const std::vector< unsigned int > &ranges, int sign)
TBTK::FourierTransform::ForwardPlan
Definition: FourierTransform.h:113
FourierTransform.h
Fourier transform.
TBTK::FourierTransform::InversePlan
Definition: FourierTransform.h:125
TBTK::Array::getSize
unsigned int getSize() const
Definition: Array.h:1291
TBTK::Array::getRanges
const std::vector< unsigned int > & getRanges() const
Definition: Array.h:1228
TBTK::Array::create
static Array create(const std::vector< unsigned int > &ranges)
Definition: Array.h:585
TBTK::Convolver::convolve
static Array< DataType > convolve(const Array< DataType > &array0, const Array< DataType > &array1)
Definition: Convolver.h:62