Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

compresstest.cpp

Go to the documentation of this file.
00001 
00036 
00037 
00038 #include <stdio.h>
00039 
00040 #include "invpacktree_int.h"
00041 #include "packtree_int.h"
00042 #include "support.h"
00043 #include "delta.h"
00044 #include "haar_int.h"
00045 #include "ts_trans_int.h"
00046 #include "line_int.h"
00047 #include "costwidth.h"
00048 #include "yahooTS.h"
00049 
00050 
00056 int *copy( int *intVec, const size_t N )
00057 {
00058   int *newVec = new int[ N ];
00059 
00060   for (size_t i = 0; i < N; i++)
00061   {
00062     newVec[i] = intVec[i];
00063   }
00064 
00065   return newVec;
00066 } // copy
00067 
00068 
00073 bool compare( const int *v1, const int *v2, const size_t N )
00074 {
00075   bool rslt = true;
00076 
00077   for (size_t i = 0; i < N; i++) 
00078   {
00079     if (v1[i] != v2[i])
00080     {
00081       rslt = false;
00082       break;
00083     }
00084   }
00085 
00086   return rslt;
00087 } // compare
00088 
00089 
00090 
00104 size_t calcPacketWidth( packdata_list<int> &bestBasisList )
00105 {
00106   packdata_list<int>::handle h;
00107   size_t totalWidth = 0;
00108 
00109   for (h = bestBasisList.first(); h != 0; h = bestBasisList.next( h )) {
00110     packdata<int> *node = bestBasisList.get_item( h );
00111     const size_t len = node->length();
00112     const int *vec = node->getData();
00113     int nodeWidth = support::vecWidth( vec, len );
00114     totalWidth += nodeWidth;
00115   }
00116 
00117   return totalWidth;
00118 } // calcPacketWidth
00119 
00120 
00121 
00130 size_t packet_calc( const int *intVec, 
00131                     const int *copyVec, 
00132                     const int N )
00133 {
00134   // The "line" wavelet transform (e.g., line with slope)
00135   line_int<packcontainer_int> line;
00136 
00137   // calculate the wavelet packet tree, using the line wavelet transform
00138   packtree_int tree( intVec, N, &line );
00139 
00140   // get the root of the wavelet packet transform tree
00141   packnode<int> *treeRoot = tree.getRoot();
00142 
00143   // Assign a cost on the basis of bit width
00144   costwidth cost( treeRoot );
00145 
00146   // Calculate the "best basis" function from the tree
00147   tree.bestBasis();
00148 
00149   // Check that the best basis function succeeded.  That is,
00150   // that the best basis function does not include the 
00151   // original data.
00152   
00153   if (! tree.bestBasisOK()) {
00154     printf("Best basis calculation failed\n");
00155   }
00156 
00157   // Get the best basis list.  This will be a list of
00158   // nodes consisting of the best basis set.  This set is
00159   // obtained by traversing the tree, top down, left to
00160   // right.
00161   packdata_list<int> bestBasis = tree.getBestBasisList();
00162 
00163   // Sum the cost values (width for each node in the best basis
00164   // list
00165   size_t width = calcPacketWidth( bestBasis );
00166 
00167   invpacktree_int invtree( bestBasis, &line );
00168 
00169   const int *invRslt = invtree.getData();
00170 
00171   bool isEqual = compare( invRslt, copyVec, N );
00172   if (! isEqual)
00173     printf("Wavelet packet inverse is wrong\n");
00174 
00175   return width;
00176 } // packet_calc
00177 
00178 
00186 size_t delta_calc( int *intVec, 
00187                    const int *copyVec, 
00188                    const int N )
00189 {
00190   delta_trans<int> delta;
00191 
00192   delta.forward( intVec, N );
00193 
00194   const size_t deltaWidth = support::vecWidth( intVec, N );
00195 
00196   delta.inverse( intVec, N);
00197 
00198   bool isEqual = compare( intVec, copyVec, N);
00199   if (! isEqual)
00200     printf("Delta compression inverse failed\n");
00201 
00202   return deltaWidth;
00203 } // delta_calc
00204 
00205 
00221 size_t wave_calc( int *intVec, 
00222                   const int *copyVec, 
00223                   const int N,
00224                   liftbase<int *, int> *w )
00225 {
00226   w->forwardTrans( intVec, N );
00227   
00228   const size_t waveWidth = support::vecWidth( intVec, N );
00229 
00230   w->inverseTrans( intVec, N );
00231 
00232   bool isEqual = compare( intVec, copyVec, N);
00233   if (! isEqual)
00234     printf("Line wavelet inverse is wrong\n");
00235 
00236   return waveWidth;
00237 } // wave_calc
00238 
00239 
00240 
00247 int main()
00248 {
00249   const char *files[] = { "aa",    // Alcoa Aluminium
00250                           "amat",  // Applied Materials
00251                           "ba",    // Boeing
00252                           "cof",   // Capital One
00253                           "ge",    // General Electric
00254                           "ibm",   // IBM Corp.
00255                           "intc",  // Intel
00256                           "mmm",   // 3M
00257                           "mrk",   // Merck
00258                           "wmt",   // Wal-Mart
00259                           0        // The null pointer
00260                         };
00261 
00262   const size_t N = 512;
00263   double realVec[ N ];
00264   int intVec[ N ];
00265 
00266   // an instance of yahooTS with the path to the data directory
00267   const char *dataDirPath = "..\\data\\equities\\";
00268   yahooTS ts( dataDirPath );
00269 
00270   printf("Equity Uncompressed  delta  Haar  line  TS    wavelet packet (line)\n");
00271 
00272 
00273   for (size_t i = 0; files[i] != 0; i++) {
00274     
00275     size_t n = N;
00276     if (! ts.getTS( files[i], realVec, n, yahooTS::Close )) {
00277       break;
00278     }
00279 
00280     if (n != N) {
00281       printf("Error: %d out of %d data elements read\n", n, N );
00282       break;
00283     }
00284     
00285     support::decimalToInt( intVec, realVec, N );
00286     
00287     int *copyVec = copy( intVec, N );
00288     
00289     const size_t beforeWidth = support::vecWidth( intVec, N );
00290     
00291     const size_t deltaWidth = delta_calc( intVec, copyVec, N );
00292     
00293     haar_int haar;
00294     const size_t haarWidth = wave_calc( intVec, copyVec, N, &haar );
00295     
00296     line_int<int *> line;
00297     const size_t lineWidth = wave_calc( intVec, copyVec, N, &line );
00298     
00299     ts_trans_int ts_trans;
00300     const size_t tsTransWidth = wave_calc( intVec, 
00301                                            copyVec, 
00302                                            N, 
00303                                            &ts_trans);
00304     
00305     const size_t packetWidth = packet_calc( intVec, copyVec, N );
00306     
00307     printf("  %4s       %4d    %4d   %4d  %4d  %d      %4d\n", 
00308            files[i], beforeWidth, deltaWidth, haarWidth, lineWidth, tsTransWidth, packetWidth );
00309     
00310   }
00311   return 0;
00312 }

Generated at Sat Aug 10 13:23:34 2002 for Wavelet Packet Transform and Lossless Compression by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001