LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
comp_funcs.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2014, Sam Schetterer, Nathan Kutz, University of Washington
3 Authors: Sam Schetterer
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 
10 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11 
12 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 
16 */
17 #pragma once
18 #ifndef COMP_FUNCS_H
19 #define COMP_FUNCS_H
20 #include "defs.hpp"
21 
22 //Although I usually want to remove redundnant headers, this header is going to be included by
23 //99% of the files that include comp_funcs.hpp"
24 //If that stops being true in the future, I'll change it
25 #include "item_heads.hpp"
26 //These are over twice as fast as using the standard library functions,
27 //or even using intrinsics. They allow vectorizing
28 using std::abs;
29 inline comp _conj(comp inval){
30  ((double* restr)&inval)[1] *= -1;
31  return inval;
32 }
33 inline double _real(comp inval){
34  return ((double* restr)&inval)[0];
35 }
36 inline double _imag(comp inval){
37  return ((double* restr)&inval)[1];
38 }
39 inline double _sqabs(comp inval){
40  double r, i;
41  r = ((double* restr)&inval)[0];
42  i = ((double* restr)&inval)[1];
43  return r*r + i*i;
44 }
45 inline double energy(comp* v, size_t s){
46  double sum = _sqabs(v[0]);
47  for(size_t i = 1; i < s-1; i++){
48  sum += 2*_sqabs(v[i]);
49  }
50  sum += _sqabs(v[s-1]);
51  return sqrt(sum/2);
52 }
53 inline double energy(double* v, size_t s){
54  double sum = v[0]*v[0];
55  for(size_t i = 1; i < s-1; i++){
56  sum += 2*v[i]*v[i];
57  }
58  sum += v[s-1]*v[s-1];
59  return sqrt(sum/2);
60 }
61 template<class T>
62 T trap(T* restr v, size_t s){
63  ALIGNED(v);
64  T sum = T();
65  for(size_t i=1; i < s-1; i++){
66  sum += v[i];
67  }
68  sum += (v[0] + v[s-1])/2.0;
69  return sum;
70 }
71 
72 //These fourier transforms have to do a dirty pointer transform from std::complex
73 //to fftw_complex. Since std::complex<double> is bitwise the same as the C complex, and fftw_complex is
74 //the same as the C complex, this works. simply using fftw_complex works in c++03, but c++11 has some
75 //very nice template features for dealing with runtime type construction and inheritance validation
76 //
77 
79 void fft(comp* _in, comp* _out, const size_t len);
81 void ifft(comp* _in, comp* _out, const size_t len);
82 void fft_cleanup();
83 template<typename T>
84 inline std::vector<T> appendvec(std::vector<T> a, const std::vector<T>& b){
85  a.insert(a.begin(), b.begin(), b.end());
86  return a;
87 }
88 template<class T, size_t N>
89 inline std::vector<T> makevec(const T(& array)[N]){
90  return std::vector<T>(array, array+N);
91 }
92 template<class T, size_t N>
93 inline std::vector<T> make_append(const T (&array)[N], const std::vector<T>& b){
94  return appendvec(makevec(array), b);
95 }
96 
97 #endif