ROOT_Application  2.0
C++ Core modules and GUIStock
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Wavelet.cpp
Go to the documentation of this file.
1 
2 #include "Wavelet.h"
3 
4 WaveletBase::WaveletBase( double *values_in, unsigned int size_in )
5 {
6  std::cout << "WaveletBase constructor with size_in " << size_in << std::endl;
7  std::cout << "values_in[0]= " << values_in[0] << " " << values_in[size_in-1] << std::endl;
8 
9  size = getLevels( size_in ); //, nb_level );
10  std::cout << " size " << size << " max_level " << max_level << std::endl;
11 
12  unsigned int discard_data = size_in - size;
13  //need to make the copy of data
14  values = new double[ size ];
15  //copy discarding data and keeping chronological order
16  for ( unsigned int i=0; i< size; i++) {
17  // get chronological order ( from latest to new of only the first size data points.
18  // inversion of order done in GetCArray
19  values[i] = values_in [ i + discard_data ];
20  }
21  print_values( true ); //option see only first and last
22 }
23 
25 unsigned int WaveletBase::getLevels( unsigned int size_in ) //, unsigned int &nb_level )
26 {
27  unsigned int max_size = 0;
28 
29  unsigned int v; // compute the next highest power of 2 of 32-bit v, to check with 64-bit just add >> 64
30  v = size_in;
31  //v--; apply only if not power of 2
32 
33  //test first if is a power of 2
34  if ( this->isPowerOfTwo( size_in ) == false ) {
35  std::cout << "size_in is not a power of 2!" << std::endl;
36  v--;
37 
38  } else {
39  std::cout << " ok power of 2 " << std::endl;
40  }
41 
42  // http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
43  /*may work but very slow certainly
44  while ( isPowerOfTwo ( max_size ) == false ) {
45  max_size = max_size - 1
46  }*/
47 
48  //should be only if not power of 2
49  //unsigned int v; // compute the next highest power of 2 of 32-bit v, to check with 64-bit just add >> 64
50  //v = size_in;
51  //v--;
52  v |= v >> 1;
53  v |= v >> 2;
54  v |= v >> 4;
55  v |= v >> 8;
56  v |= v >> 16;
57  v++;
58 
59  std::cout << " size_in " << size_in << " v " << v << " " << v / 2 << std::endl;
60  max_size = v / 2;
61 
62  // currently signals up to a length of 2^20 supported
63  for( unsigned int i = 0; i < 20; ++i) {
64  if( max_size == (1 << i)) {
65  // here set member max_level
66  max_level = i;
67  break;
68  }
69  }
70  std::cout << "getLvels return max_size " << max_size << " max_level " << max_level << std::endl;
71  return max_size;
72 }
73 //
74 void WaveletBase::print_values( bool only_first_last )
75 {
76  std::cout << "WaveletBase::print_values option " << only_first_last << std::endl;
77 
78  if ( only_first_last ) {
79  std::cout << "first i=0 " << values[0] << std::endl;
80  std::cout << "first i=1 " << values[1] << std::endl;
81  std::cout << "last i=" << size-1 << " " << values[ GetSize()-2 ] << std::endl;
82  std::cout << "last i=" << size << " " << values[ GetSize()-1 ] << std::endl;
83  } else {
84  for ( unsigned int i = 0; i < size; i ++ )
85  std::cout << " i= " << i << values[i] << std::endl;
86  }
87  std::cout << "end print_values \n" << std::endl;
88 }
89 
92 {
93  std::cout << "\n\t Haar Transform" << std::endl;
94  //std::cout << " size " << size << std::endl;
95 
96  // last interface send size/2, retrun a[0]
97  haar_value = CalcTransform( values, size / 2, 1 );
98 
99  std::cout << "End Haar Transform nb_level " << nb_level << std::endl;
100  return 0;
101 }
102 
113 // need new size ??
115 {
116  // for return
117  double * new_values;
118  std::cout << "\n\t Haar Inverse Transform" << std::endl;
119 
120  // test entry
121  if ( map_coeff_filt.empty() == true ) {
122  std::cout << "default coeff use all" << std::endl;
124  } else
125  std::cout << "use modified coefficient " << std::endl;
126 
127  /*
128  if ( h_value == 0. ) {
129  std::cout << "default haar_value " << haar_value << std::endl;
130  h_value = haar_value;
131  }*/
132 
133  //std::cout << " size " << size << std::endl;
134 
135 
136  // last interface send size/2, retrun a[0]
137  double values[1]; // { haar_value; }
138  values[0] = haar_value;
139  // tmp_value, size, level to reconstruct
140  new_values = CalcInvTransform( values, 2, nb_level - 1 );
141 
142  std::cout << "End Haar InvTransform " << nb_level << std::endl;
143  return new_values;
144 }
145 
146 
148 // need tmp_level to fill the map, to add max_level if want to stop
149 // first written with tmp_values, finally exist in map average but keep if later want to not store average
150 double Haar::CalcTransform( double *tmp_values, unsigned int tmp_size, unsigned int tmp_level )
151 {
152  std::cout << "HaarCalc size " << size << " tmp_size " << tmp_size << " tmp_level " << tmp_level << std::endl;
153  double retValue;
154 
155  // temporary array, will be copy after in map
156  double a[tmp_size];
157  double c[tmp_size];
158 
159  //correct in c++ j?? compile !? ok !
160  for ( unsigned int i = 0, j = 0; i < tmp_size*2; i += 2, j++) {
161  a[j] = (tmp_values[i] + tmp_values[i+1])/2.;
162  c[j] = (tmp_values[i] - tmp_values[i+1])/2.;
163  }
164 
165  //insert new coefficient and average
166  std::vector<double> tmp_coeff, tmp_average;
167  tmp_coeff.reserve(tmp_size);
168  tmp_average.reserve(tmp_size);
169 
170  for ( unsigned int j = 0; j < tmp_size; j++ ) {
171  std::cout << c[j] << " ";
172  tmp_coeff.push_back( c[j] );
173  tmp_average.push_back( a[j] );
174  }
175  std::cout << " " << std::endl;
176  // copy to the map
177  map_coeff[tmp_level] = tmp_coeff;
178  map_average[tmp_level] = tmp_average;
179 
180  // or max_level reached
181  if ( tmp_size == 1 ) { //should be size / 2, not size, change pass tmp_size / 2 in argument
182  //std::cout << "last loop sizeof_coeff " << vec_coeff.size() << " a[0] " << a[0] << std::endl;
183  retValue = a[0];
184  // need to set nb_level data member
185  nb_level = tmp_level;
186 
187  } else
188  retValue = CalcTransform( a, tmp_size / 2, tmp_level+1 );
189 
190  return retValue;
191 }
192 
193 //
194 double * Haar::CalcInvTransform ( double *tmp_values, unsigned int tmp_size, unsigned int tmp_level )
195 {
196  double *retValue;
197  std::cout << "HaarInvTransform tmp_size:" << tmp_size << " tmp_level:" << tmp_level << std::endl;
198 
199  double new_values[tmp_size];
200 
201  for ( unsigned int i = 0, j = 0; i < tmp_size/2; i++, j= j+2 ) {
202  //A1 = a1 + c1
203  new_values[j] = tmp_values[i] + map_coeff_filt[tmp_level+1][i];
204  //A2 = a1 - c1
205  new_values[j+1] = tmp_values[i] - map_coeff_filt[tmp_level+1][i];
206  }
207 
208  for ( unsigned int i = 0; i < tmp_size; i++ ) {
209  std::cout << new_values[i] << " ";
210  }
211  std::cout << " " << std::endl;
212 
213  if ( tmp_level == 0 ) {//or tmp_size
214  std::cout << "return recursion " << std::endl;
215  retValue = new double[tmp_size];
216  // copy data to final array
217  for ( unsigned int i=0; i< tmp_size; i++ )
218  retValue[i] = new_values[i];
219  /*
220  for ( unsigned int i = 0; i < tmp_size; i++ ) {
221  std::cout << retValue[i] << " ";
222  }
223  */
224  return retValue;
225  //return 0.;
226  } else
227  retValue = CalcInvTransform ( new_values, tmp_size*2, tmp_level - 1);
228  //return 0.;
229 
230 
231  return retValue;
232 }
233 
234 //void Haar::Filter( std::string mode, int numb1, int numb2 )
235 void Haar::Filter( const char * mode, int numb1, int numb2 )
236 {
237  unsigned int tmp;
238  std::cout << "\nEntry Filter nb1:" << numb1 << " nb2:" << numb2 << std::endl;
239  std::cout << "mode " << mode << " strcmp " << strcmp( mode,"DEL LEVEL") << std::endl;
240  //always make copy of the initial coefficient
242 
243  // want to delete some level
244  //if ( mode == "DEL LEVEL") {
245  if ( strcmp( mode,"DEL LEVEL")==0 ) {
246  std::cout << "delete level from " << numb1 << " to " << numb2 << std::endl;
247  for ( unsigned int i = (unsigned int)numb1; i<= (unsigned int)numb2; i++ ) {
248  tmp = map_coeff_filt[i].size();
249  std::cout << "tmp " << tmp << std::endl;
250  for ( unsigned int j = 0; j<tmp; j++ )
251  map_coeff_filt[i][j] = 0.;
252  }
253  }
254 
255  return;
256 }
257 
259 {
260  std::cout << "Print Coefficient size " << size << std::endl;
261  std::cout << "Haar value " << haar_value << std::endl;
262  std::cout << "max_level " << max_level << std::endl;
263  std::cout << "nb_level " << nb_level << "\n" << std::endl;
264 
265  // key map begin at 1
266  for ( unsigned int i=1; i <= nb_level; i ++ ) {
267  std::cout << "Level " << i << std::endl;
268  std::cout << "nb_coeff " << map_coeff[i].size() << std::endl;
269  std::cout << "nb_average " << map_average[i].size() << std::endl;
270  std::cout << "coeff :";
271  for ( unsigned int j=0; j < map_coeff[i].size(); j++ ) {
272  std::cout << map_coeff[i][j] << " ";
273  }
274  std::cout << "\naverage :";
275  for ( unsigned int j=0; j < map_average[i].size(); j++ ) {
276  std::cout << map_average[i][j] << " ";
277  }
278  std::cout << " " << std::endl;
279  }
280 }
281 
282 // may add option, levels, 3d-2d...
284 {
285  std::cout << "Entry Haar::Draw " << std::endl;
286 }
287 
int Transform()
entry for haar transformation, will call CalcTransform
Definition: Wavelet.cpp:91
double * CalcInvTransform(double *values, unsigned int size, unsigned int level)
Definition: Wavelet.cpp:194
unsigned int nb_level
nb_level computed <= max_lvel
Definition: Wavelet.h:75
unsigned int size
initial size
Definition: Wavelet.h:32
WaveletBase()
Definition: Wavelet.h:39
std::map< unsigned int, std::vector< double > > map_average
save also intermediate average
Definition: Wavelet.h:72
double haar_value
Definition: Wavelet.h:66
virtual void print_values(bool only_first_last=false)
Definition: Wavelet.cpp:74
std::map< unsigned int, std::vector< double > > map_coeff_filt
Definition: Wavelet.h:80
double * values
initial values
Definition: Wavelet.h:30
double CalcTransform(double *values, unsigned int size, unsigned int level)
recursive function, need tmp argument, but tmp_values not modified (const ??)
Definition: Wavelet.cpp:150
unsigned int max_level
maximum nb_level: 2^0=1 (initial) to 2^nb_level-1 ?? loaded in derivative
Definition: Wavelet.h:34
virtual void Draw()
Definition: Wavelet.cpp:283
unsigned int getLevels(unsigned int size_in)
given a size ( max_level ?? ) set the number maximum of level,and return maximim size ...
Definition: Wavelet.cpp:25
double * InvTransform()
entry for inverse transformation.
Definition: Wavelet.cpp:114
unsigned int GetSize()
Definition: Wavelet.h:45
int isPowerOfTwo(unsigned int x)
Definition: Wavelet.h:57
virtual void print_coeff()
Definition: Wavelet.cpp:258
std::map< unsigned int, std::vector< double > > map_coeff
assigned last loop, should be the average
Definition: Wavelet.h:70
void Filter(const char *mode, int numb1, int numb2)
Definition: Wavelet.cpp:235