SigmaTransform
The SigmaTransform unifies various known signal processing transforms, like the STFT and the wavelet transform, differing only by a specific diffeomorphism.
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
SigmaTransform_util.cpp
Go to the documentation of this file.
1 
2 #define DEBUG
3 
4 #ifdef DEBUG
5 
6 #include <chrono>
7 #include <iomanip>
8 
9 #endif
10 
11 #include "SigmaTransform_util.h"
12 
13 namespace SigmaTransform {
14 
15  #ifdef DEBUG
16 
17  Chronometer::Chronometer() : _tic( std::chrono::steady_clock::now() ) { }
18  void Chronometer::tic() { _tic = std::chrono::steady_clock::now(); }
19  Chronometer& Chronometer::toc(std::string const& str ) {
20  // get time
21  std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
22  double diff = std::chrono::duration_cast<std::chrono::microseconds>(end-_tic).count();
23  std::cout << std::fixed << std::showpoint << std::setw(60) << std::setfill('.') << std::setprecision(2)
24  << (diff?diff:1000.0)/1000.0 << " ms elapsed \r["<<str<<"]\n"<<std::flush;
25 
26  return *this;
27  }
28 
29  #endif
30 
31  // loads an ascii file, containing 1d data
32  cxVec loadAscii1D( std::string const& filename ) {
33  double tmp;
34  cxVec out;
35  // input filestream
36  std::ifstream is(filename);
37  if( !is )
38  throw std::runtime_error("Error opening File.");
39  // read
40  while( is >> tmp )
41  out.push_back( tmp );
42  // return the read data
43  return std::move( out );
44  }
45 
46  // loads an ascii file, containing 2d data
47  cxVec loadAscii2D( std::string const& filename , int &x , int &y ) {
48  // tmp
49  double tmp;
50  cxVec out;
51  // string for a line
52  std::string line;
53  // input filestream
54  std::ifstream is(filename);
55  if( !is )
56  throw std::runtime_error("Error opening File.");
57  // read file
58  y=0;
59  while( std::getline(is, line,'\n') ) {
60  // read line
61  x = 0;
62  std::stringstream ss(line);
63  while( ss >> tmp ) {
64  out.push_back( tmp );
65  ++x;
66  }
67  ++y;
68  }
69  // return the read data
70  return std::move( out );
71  }
72 
73  // saves "out" as a binary file "filename"
74  void save2file_bin( std::string const& filename , const cxVec &out ) {
75  std::ofstream( filename , std::ios::binary ).write((char*)out.data(),sizeof(double)*2*out.size());
76  }
77 
78  // returns a linearly spaced vector between L and R in N steps
79  std::vector<double> linspace( const double &L , const double &R , const int &N ) {
80  std::vector<double> out(N);
81  double delta = (R-L) / (N-1);
82  double Lr = L;
83  for( auto &val : out ) {
84  val = Lr;
85  Lr += delta;
86  }
87  return out;
88  }
89 
90  // returns a Fourier axis of length "len", with sampling frequency "fs"
91  std::vector<double> FourierAxis( const double &fs , const unsigned &len ) {
92  std::vector<double> domain = linspace( 0 , fs , len );
93  double offset = fs + domain[1];
94  std::for_each( domain.begin() + ceil( len / 2 ) , domain.end() , [&](double& x){ x -= offset; } );
95  return domain;
96  }
97 
98  // Starts a recursive loop and applies "toDo" for each point between (0,...,0) and "(max_1,...,max_n)"
99  void StartRecursiveLoop( const std::vector<int>& max , std::function<void(const std::vector<int>&)> toDo ) {
100  std::vector<int> index; index.resize( max.size() , 0 );
101  recursiveLoop( index , max , max.size()-1 , toDo );
102  }
103 
104  // Applies a recursive loop, which replaces an arbitrary number of nested loops
105  void recursiveLoop( std::vector<int> &index, const std::vector<int>& max , const int& curr_ind ,
106  std::function<void(const std::vector<int>&)> toDo ) {
107  for( index[curr_ind] = 0 ; index[curr_ind] < max[curr_ind] ; ++index[curr_ind] ) {
108  if( curr_ind )
109  recursiveLoop( index , max , curr_ind-1 , toDo );
110  else
111  toDo( index );
112  }
113  }
114 
115 } // namespace SigmaTransform