Logo Coherent WaveBurst  
Reference Guide
Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
waverdc.cc
Go to the documentation of this file.
1 /*-------------------------------------------------------
2  * Package: Wavelet Analysis Tool
3  * File name: waverdc.cc :: Random Data Compression
4  *-------------------------------------------------------
5 */
6 
7 #include "waverdc.hh"
8 
9 ClassImp(WaveRDC)
10 
11 // Default constructor
12 WaveRDC::WaveRDC() : wavearray<unsigned int>()
13 {
14  nLayer = 0;
15  nSample = 0;
16  optz = 0;
17 }
18 
19 // Destructor
21 
22 
23 // Lists layers stored in compressed array,
24 // "v" - is verbosity level, =0 by default.
25 
26 void WaveRDC::Dir(int v)
27 {
28 
29  if (size() <= 0) {
30  cout <<" Compressed array is empty.\n";
31  return;
32  }
33 
34  if (v > 0) {
35 
36  cout <<"\n Number of layers :"<< nLayer <<"\n";
37 
38  int ll=78;
39  size_t ncd = 0;
40  double r;
41  size_t n32 = 0;
42  size_t n16 = 0;
43 
44  for (int i=0; i<ll; i++) printf("-"); printf("\n");
45 
46  printf("%s%s\n",
47  "|layer|compressed |uncompressed| ratio |",
48  "kBSW|sh,lo| shift | scale | opt |");
49 
50  for (int i=0; i<ll; i++) printf("-"); printf("\n");
51 
52  for (int i = 0; i < nLayer; i++) {
53 
54  if ((ncd + 2) > size()) break;
55 
56  optz = data[ncd] & 0xFFFF;
57 
58  kShort = (data[ncd] >> 26) & 0x1F;
59  kLong = (data[ncd] >> 21) & 0x1F;
60  kBSW = (data[ncd] >> 16) & 0x1F;
61  Bias = 0;
62 
63  if(optz & 0x2 ){ // 4 bytes was used to store number of words
64  n32 = data[++ncd]; // # of 32b words occupied by compressed data
65  n16 = data[++ncd]; // # of 16b words occupied by original data
66  }
67  else if(optz & 0x1){ // 2 bytes was used to store number of words
68  n32 = data[++ncd] & 0xFFFF;
69  n16 = (data[ncd]>>16) & 0xFFFF;
70  }
71  else{ // 1 byte was used to store number of words
72  n32 = data[++ncd] & 0xFF;
73  n16 = (data[ncd]>>8) & 0xFF;
74  Bias = short((data[ncd]>>16) & 0xFFFF);
75  }
76 
77  if (n32 < getLSW(optz)) {
78  cout <<" unCompress() error: invalid layer number "<< i+1 <<"\n";
79  return;
80  }
81 
82  if(!n16) break;
83 
84  Zero = (optz & 0x4) ? 1<<(kShort-1) : 0;
85  Bias = ((optz & 0x8) && (optz & 0x3)) ? data[++ncd] & 0xFFFF : Bias;
86  Scale = (optz & 0x10) ? *((float *)&data[++ncd]) : 1.;
87 
88  ncd += n32 - getLSW(optz) + 1;
89 
90  n32 *= sizeof(*data);
91  n16 *= sizeof(short);
92  r = double(n16)/double(n32);
93 
94  printf("|%4d |%10d |%11d |%9.3f |%3d |%2d,%2d|%8d",
95  i+1, (int)n32, (int)n16, r, (int)kBSW, (int)kShort, (int)kLong, (int)Bias);
96  printf("|%6.1f |%5o |\n", Scale, short(optz));
97 
98  }
99  for (int i=0; i<ll; i++) printf("-"); printf("\n");
100  }
101 
102  cout <<" Total compressed length : "
103  << size()*sizeof(*data) <<" bytes.\n";
104  cout <<" Total uncompressed length : "
105  << nSample*2 <<" bytes.\n";
106 
107  cout <<" Overall compression ratio : "
108  << double(nSample*2)/double(size()*sizeof(*data))<<"\n\n";
109 }
110 
112  size_t n = a.size();
113  if (this != &a && n > 0) {
114  if (size() != n) resize(n);
115  for (size_t i=0; i < n; i++) data[i] = a.data[i];
116  nSample = a.nSample;
117  nLayer = a.nLayer;
118  optz = a.optz;
119  }
120  return *this;
121 }
122 
123 // Concatenation of comressed data
125 {
126  size_t nZ = size();
127  size_t nA = a.size();
128  size_t i;
129  unsigned int *pnew;
130 
131  if (a.size() > 0) {
132  pnew = new unsigned int[size() + a.size()];
133  for (i = 0; i < nZ; i++) pnew[i] = data[i];
134  pnew += nZ;
135  for (i = 0; i < nA; i++) pnew[i] = a.data[i];
136 
137  cout<<nZ<<" "<<nA<<"\n";
138  delete [] data;
139  cout<<nZ<<" "<<nA<<"\n";
140  data = pnew-nZ;
141  nSample += a.nSample;
142  nLayer += a.nLayer;
143  }
144  return *this;
145 }
146 
147 // Dumps data array to file *fname in binary format and type "short".
148 // Variable 'app' select write mode, app=0 - write new file,
149 // app=1 - append to existent file.
150 int WaveRDC::DumpRDC(const char *fname, int app)
151 {
152  const char *mode = "wb";
153  if (app == 1) mode = (char*)"ab";
154 
155  FILE *fp;
156  if ( (fp = fopen(fname, mode)) == NULL ) {
157  cout << " DumpRDC() error: cannot open file " << fname <<". \n";
158  return -1;
159  }
160 
161  int n = size() * sizeof(*data);
162  fwrite(data, n, 1, fp);
163  fclose(fp);
164  return n;
165 }
166 
167 // Check if data will fit into short, return scale factor
168  float WaveRDC::getScale(const waveDouble &F, double loss)
169 {
170  if(!F.size()) return 0.;
171 
172  double mean, bias, rms;
173  float scale;
174  size_t N = F.size()-1;
175  register double *pF = F.data;
176  register double x = 0.;
177  register double y = pF[0];
178  register size_t i;
179 
180  loss = sqrt(0.12*loss);
181  optz = 0;
182 
183 // calculate the minimal rms and scale factor sf1
184  x = F.getStatistics(mean,rms);
185  if(rms > rmsLimit) rms = rmsLimit;
186  scale = rms*loss;
187 
188  if(fabs(x) < 0.5){
189  scale *= 2*fabs(x);
190  if(x<0) optz |= 0x80; // differentiation
191  if(x>0) optz |= 0x100; // heterodine & differentiation
192  }
193 
194  if(scale <=0.) scale = 1.;
195  bias = mean;
196 
197  if(optz & 0x180){
198  bias = (pF[N]-pF[0])/(N+1);
199 
200  if(optz & 0x100){ // heterodine + differentiation
201  for(i=N; i>0; i--){
202  x = pF[i]+pF[i-1];
203  if(fabs(x) > fabs(y)) y = x;
204  }
205  bias -= (N&1) ? 2*pF[N]/(N+1) : 0.;
206  }
207  else // differentiation
208  for(i=N; i>0; i--){
209  x = pF[i]-pF[i-1];
210  if(fabs(x) > fabs(y)) y = x;
211  }
212 
213  }
214  else{ // no handling
215  for(i=0; i<=N; i++)
216  if(fabs(pF[i]) > fabs(y)) y = pF[i];
217  }
218 
219  y -= bias;
220  x = (optz & 0x180) ? pF[0] : bias;
221  y = (fabs(x)>fabs(y)) ? x : y;
222  y /= (scale>0.) ? scale : 1;
223 
224  bias = double(1<<(sizeof(short)*8-1))-1.;
225  scale *= (fabs(y) > bias) ? fabs(y)/bias : 1.;
226  if(scale <=0.) scale = 1.;
227  Bias = wint(mean/scale);
228  Scale = scale;
229 
230 // cout<<rms<<" scale="<<Scale<<" bias="<<Bias<<" ";
231 // printf("%o \n", short(optz));
232 
233  return scale;
234 }
235 
236 // convert double to int/short
237 // Scale and Bias should be set before this function is executed
239 {
240  if(!F.size()) return;
241 
242  if(F.size() != S.size()) S.resize(F.size());
243 
244  size_t n16 = F.size();
245  size_t N = n16 - 1;
246  register size_t i;
247  register int x;
248  register double *pF = F.data;
249  register short *pS = S.data;
250  register double s = (Scale>0.) ? 1./Scale : 1.;
251 
252  int mean = wint(s*(F.data[N]-F.data[0])/n16);
253  if(n16&1 && optz&0x100)
254  mean = -wint(s*(F.data[N]+F.data[0])/n16);
255 
256  if(optz & 0x180){ // differentiation
257 
258  if(optz & 0x100){ // heterodine & differentiation
259  for(i=1; i<N; i+=2){
260  x = wint(F.data[i]*s);
261  pS[i] = -(x + wint(s*pF[i-1]) - mean);
262  pS[i+1] = (wint(s*pF[i+1]) + x + mean);
263  }
264  if(!(n16&1)) pS[N] = -wint(s*pF[N])-wint(s*pF[N-1])+mean;
265  }
266  else{ // differentiation
267  for(i=1; i<N; i+=2){
268  x = wint(F.data[i]*s);
269  pS[i] = x - wint(s*pF[i-1]) - mean;
270  pS[i+1] = wint(s*pF[i+1]) - x - mean;
271  }
272  if(!(n16&1)) pS[N] = wint(s*pF[N])-wint(s*pF[N-1])-mean;
273  }
274 
275  Bias = wint(pF[0]*s);
276  pS[0] = (optz & 0x100) ? -mean : mean;
277  }
278 
279  else
280  for(i=0; i<n16; i++){
281  pS[i] = short(wint(s*pF[i]) - Bias);
282  }
283 }
284 
285 // convert sign statistics
287 {
288  if(!F.size()) return;
289 
290  if(F.size() != S.size()) S.resize(F.size());
291 
292  size_t N = F.size();
293  register size_t i;
294  register double *pF = F.data;
295  register short *pS = S.data;
296 
297  optz = 0x20;
298 
299  for(i=0; i<N; i++)
300  pS[i] = (pF[i]>=0.) ? 1 : -1;
301 
302 }
303 
304 // Compress packs data into layer which is series of blocks each
305 // consisting of bricks of two different length 'kLong' and 'kShort'. Each
306 // brick represents one element of original unpacked data
307 // (16-bit integer) which is shortened to exclude redundant bits.
308 // Original data are taken from object wd. Packed data are
309 // placed into array 'data'. Returns number of bytes
310 // occupied by compressed data.
311 int WaveRDC::Compress(const waveDouble &F, double loss)
312 {
313  if(F.size()<1) return 0;
314  wavearray<short> S(F.size());
315  optz = 0;
316 
317  Scale = getScale(F, loss); // calculate the scale factor
318  getShort(F, S); // convert to short array
319 
320  if(Bias |= 0) optz |= 0x8;
321  if(Scale != 1.) optz |= 0x10;
322 
323  return Compress(S);
324 }
325 
326 
328 {
329  int cdLSW = 5; // length of LSW buffer
330  int n16 = S.size(); // number of uncompressed samples
331  short *dt = S.data; // pointer to input data
332  register int i;
333  register int x;
334  register int y;
335 
336  freebits = 8*sizeof(unsigned int);
337 
338 // calculate histogram
339 // "x" falls in histogram bin "k" if it needs k bits for encoding;
340 // "k" is the minimum number of bits to encode x
341 
342  int h[17], g[17];
343  int k,m;
344 
345  for (i = 0; i < 17; i++) {h[i]=0; g[i]=0;}
346 
347  for (i = 0; i < n16; i++) {
348  x = dt[i];
349  y = (x>0) ? x : -x;
350  for(k=0; (1<<k) < y; k++);
351  m = 1<<k; // number of "short zeroes"
352  if(y > 0) k++; // add bit for sign
353  h[k] += 1; // histogram for number of bits
354  if(m == x) g[k] += 1; // histogram for "short zeroes"
355  }
356 
357 //printf(" h[0..8] =");
358 //for (i = 0; i < 9; i++) printf("%7d",h[i]);
359 //printf("\n h[9..16]=");
360 //for (i = 9; i < 17; i++) printf("%7d",h[i]);
361 //printf("\n");
362 
363 // ****************************************************************
364 // * estimate compressed size using histogram h[]
365 // * and find optimal kShort to get minimal compressed length
366 // ****************************************************************
367 
368 // kShort/kLong - number of bits to pack short/long data samples
369  kBSW = kShort = kLong = 16;
370 
371 // "compressed" length in bytes if kShort = kLong = 16
372  int Lmin = 2*n16+2;
373 
374  int m0,ksw;
375  int L = 0;
376 
377  m = 0;
378  for (i = 16; i > 0; i--) {
379  m += h[i]; // # of long bricks
380  if (m == 0) kLong = i-1; // # of bits for long brick
381  m0 = g[i-1]<h[0] ? g[i-1] : h[0]; // # of zeroes
382 
383  ksw = 16;
384  if(m+m0 > 0) for(ksw=0; (1<<ksw) < (n16/(m+m0)+2); ksw++);
385  ksw = ksw<15 ? ksw+2 : 16; // # of bits in BSW
386 
387  L = ((m+m0)*ksw + (n16-m-m0)*(i-1) + m*kLong)/8 + 1;
388  if (L <= Lmin) {
389  Lmin = L;
390  kShort = i - 1;
391  kBSW = ksw;
392  Zero = (kShort==0 || g[kShort]>h[0]) ? 0 : 1<<(kShort-1); // zero in int domain
393  }
394 
395 // printf(" h[%2d] = %7d g[%2d] = %7d L=%7d Zero=%7d m0=%7d h[0]=%7d\n",
396 // i, h[i], i, g[i], L, Zero, m0, h[0]);
397 
398  }
399 
400 // cout <<" kBSW="<<kBSW<<" kShort="<<kShort<<" kLong="<<kLong<<" Lmin="<<Lmin<<"\n";
401 
402 // ****************************************************************
403 // * pack data using kShort bits for small numbers and kLong for large: *
404 // ****************************************************************
405 // encoding example if kShort = 3
406 // -4 -3 -2 -1 4 1 2 3 0
407 // 110 100 10 0 1 11 101 111
408 // |___ represented by 0 in long word
409 //
410 // 0 - coded by 111,
411 // 4 - max number coded with kShort bits; will be coded by 0 in long word
412 
413 
414  int ncd = cdLSW; // ncd counts elements of compressed array "cd"
415  int j, jj = 0;
416  unsigned int bsw = 0; // Block Service Word
417  int ns, maxns; // ns - # of short words in block
418  maxns = (1<<(kBSW-1)) - 1; // maxns - max # of short bricks in block
419 
420  int lcd = 6 + Lmin + n16/maxns/2 + 2; // length of the cd array
421  unsigned int *cd = new unsigned int[lcd];
422 
423  for(i=0; i<lcd; i++) cd[i]=0; // Sazonov 01/29/2001
424 
425 // cout<<"lcd="<<lcd<<" ncd="<<ncd<<" shift="<<Bias<<endl;
426 
427 
428  if(kShort > 0) m = 1<<(kShort-1); // m - max # encoded with kShort bits
429  int jmax = 0; // current limit on array index
430  while (jj < n16) {
431  j = jj;
432  jmax = ((j+maxns) < n16) ? j+maxns : n16;
433 
434  if(kShort == 0) // count # of real 0
435  while (j<jmax && dt[j]==0) j++;
436  else // count ns for |td|<=m, excluding zero
437  while (j<jmax && wabs(dt[j])<=m && dt[j] != Zero) j++;
438 
439  ns = j - jj;
440  if(ns > maxns) ns = maxns; // maxns - max number of short words
441  j = jj + ns;
442 
443 // fill BSW
444  bsw = ns << 1;
445  if(j<n16){
446  if(kShort == 0 && dt[j] != 0) bsw += 1;
447  if(kShort > 0 && dt[j] != Zero) bsw += 1;
448  }
449 
450 // ***************************************************************
451 // generate compressed data skiping zero word that is always long
452 // ***************************************************************
453 
454  Push(bsw, cd, ncd, kBSW); // push in BSW
455 
456  if(kShort != 0) Push(dt, jj, cd, ncd, kShort, ns); // push in a short words
457  jj += ns;
458 
459  if(j<n16 && dt[j] != Zero) Push(dt,j,cd,ncd,kLong,1); // push in a long word
460  jj++;
461  }
462 
463 // cout<<"**0** ncd="<<ncd<<" bsw="<<bsw<<" j="<<jj<<" fb="<<freebits<<"\n";
464 // printf(" data[%6d] = %o data[%6d] = %o\n", ncd, cd[ncd], ncd-1, cd[ncd-1]);
465 
466  ncd++;
467 
468 // cout<<"lcd="<<lcd<<" ncd="<<ncd<<" bias="<<Bias<<" scale="<<Scale<<endl;
469 // cout<<"zero="<<Zero<<" bias="<<Bias<<" scale="<<Scale<<endl;
470 
471 // compression options defined by first byte.
472 // bit if set # of bytes
473 // 1 number of compressed words < 64k 2
474 // 2 number of compressed words > 64k 4
475 // if(!optz&3) 1
476 // 3 Zero != 0 0-2
477 // 4 Bias != 0 0-2
478 // 5 Scale != 0 0-4
479 // 6 sign bit
480 // 7 ----
481 // 8 dif-
482 // 9 dif+
483 
484  if(ncd >= (1<<sizeof(short)*4)) optz |= 1;
485  if(n16 >= (1<<sizeof(short)*4)) optz |= 1;
486  if(ncd >= (1<<sizeof(short)*8)) optz |= 2;
487  if(n16 >= (1<<sizeof(short)*8)) optz |= 2;
488  if(Zero) optz |= 4;
489 
490  size_t n32 = ncd-cdLSW+getLSW(optz); // actual length
491  size_t nLSW = 2;
492 
493  cd[0] |= ((kShort & 0x1F) << 26); // length of short word
494  cd[0] |= ((kLong & 0x1F) << 21); // length of long word
495  cd[0] |= ((kBSW & 0x1F) << 16); // length of the block service word
496  cd[0] |= ((optz & 0xFFFF) ); // compression option
497 
498  if(optz & 0x2 ){ // use 4 bytes to store number of words
499  cd[1] = n32; // # of 32b words occupied by compressed data
500  cd[2] = n16; // # of 16b words occupied by original data
501  nLSW++;
502  }
503  else if(optz & 0x1){ // use 2 bytes to store number of words
504  cd[1] |= (n16 & 0xFFFF) << 16;
505  cd[1] |= (n32 & 0xFFFF);
506  }
507  else{ // use 1 byte to store number of words
508  cd[1] |= (*((unsigned short *)&Bias) & 0xFFFF) << 16;
509  cd[1] |= (n16 & 0xFF) << 8;
510  cd[1] |= (n32 & 0xFF);
511  }
512 
513  if((optz & 0x8) && (optz & 0x3)) // use 4 bytes to store Bias
514  cd[nLSW++] |= (*((unsigned short *)&Bias) & 0xFFFF);
515 
516  if(optz & 0x10 ) // use 4 bytes to store Scale
517  cd[nLSW++] |= (*((unsigned int *)&Scale) & 0xFFFF);
518 
519 // concatenate *cd with *this object
520 
521  if (n32 > nLSW) {
522  int N = size();
523  resize(N+n32); // resize this to a new size
524  unsigned int *p = data + N + nLSW - cdLSW;
525 
526  for(i=0; i<(int)nLSW; i++) data[i+N] = cd[i]; // copy LSW
527  for(i=cdLSW; i<ncd; i++) p[i] = cd[i]; // copy compressed data
528 
529  nSample += n16;
530  nLayer += 1;
531  }
532 
533  delete [] cd;
534  return size()*sizeof(*data);
535 }
536 
537 int WaveRDC::Push(short *dt, int j, unsigned int *cd, int &ncd,
538  int k, int n)
539 {
540 // Push takes "n" elements of data from "dt" and packs it as a series
541 // of "n" words of k-bit length to the compressed data array "cd".
542 // lcd counts the # of unsigned int words filled with compressed data.
543 // This 'Push' makes data conversion:
544 // x<0: x -> 2*(|x|-1)
545 // x>0: x -> 2*(|x|-1) + 1
546 // x=0: x -> 2*(Zero-1) + 1
547 
548  int x, jj;
549  unsigned int u;
550 
551  if(n == 0 || k == 0) return 0;
552  jj = j;
553 
554  for (int i = 0; i < n; i++) {
555  x = (int)dt[jj++];
556  u = x == 0 ? Zero-1 : wabs(x)-1; // unsigned number
557  u = x >= 0 ? (u<<1)+1 : u<<1; // add sign (first bit); 0/1 = -/+
558  Push(u, cd, ncd, k);
559  }
560 
561  return jj-j;
562 }
563 
564 void WaveRDC::Push(unsigned int &u, unsigned int *cd, int &ncd, int k)
565 {
566 // Push takes unsigned int u and packs it as a word k-bit leng
567 // into the compressed data array "cd".
568 // ncd counts the # of unsigned int words filled with compressed data.
569 
570  if(k == 0) return;
571 
572  if (k <= freebits) {
573  freebits -= k;
574  cd[ncd] |= u << freebits; // add whole word to cd[k]
575  }
576  else {
577  cd[ncd++] |= u >> (k - freebits); // add upper bits to cd[k]
578  freebits += 8*sizeof(u) - k; // expected free space in cd[k+1]
579  cd[ncd] = u << freebits; // save lower bits in cd[k+1]
580  }
581  return;
582 }
583 
584 
585 // Unpacks data from the layer 'm' which is series of blocks each
586 // consisting from bricks of two different length 'kLong' and 'kShort'.
587 // Each brick represents one element of original unpacked data
588 // (16-bit integer) which is shortened to exclude redundant bits.
589 // Returns number of bytes occupied by unpacked data.
591 {
593  int n = unCompress(in, m);
594  waveAssign(w,in);
595  if(Scale != 1.) w *= Scale;
596  return n;
597 }
598 
600 {
602  int n = unCompress(in, m);
603  waveAssign(w,in);
604  if(Scale != 1.) w *= Scale;
605  return n;
606 }
607 
608 
610 {
611  if (m <=0 || m > nLayer) {
612  cout << " UnCompress() error: layer "<< m<<" is unavailable.\n";
613  return 0;
614  }
615 
616  int ncd = 0;
617  int mm = 0;
618  size_t n32 = 0;
619  size_t n16 = 0;
620 
621 // find layer number 'm'
622 
623  while (mm < m) {
624  ncd += n32; // find beginning of the next layer
625 
626  if ((ncd + 2) > (int)size()) {
627  cout <<" unCompress() error: invalid layer number "<< mm <<"\n";
628  return 0;
629  }
630 
631  optz = data[ncd] & 0xFFFF;
632  Bias =0;
633 
634  if(optz & 0x2 ){ // 4 bytes was used to store number of words
635  n32 = data[ncd+1]; // # of 32b words occupied by compressed data
636  n16 = data[ncd+2]; // # of 16b words occupied by original data
637  }
638  else if(optz & 0x1){ // 2 bytes was used to store number of words
639  n32 = data[ncd+1] & 0xFFFF;
640  n16 = (data[ncd+1]>>16) & 0xFFFF;
641  }
642  else{ // 1 byte was used to store number of words
643  n32 = data[ncd+1] & 0xFF;
644  n16 = (data[ncd+1]>>8) & 0xFF;
645  Bias = short((data[ncd+1]>>16) & 0xFFFF);
646  }
647 
648 // printf(" n16[%6d] n32[%6d] %o\n", n16, n32, optz);
649 
650 
651  if (n32 < getLSW(optz) || !n16) {
652  cout <<" unCompress() error: invalid layer number "<< mm+1 <<"\n";
653  return 0;
654  }
655  mm++;
656  }
657 
658  if(!n16) return 0;
659 
660  kShort = (data[ncd] >> 26) & 0x1F;
661  kLong = (data[ncd] >> 21) & 0x1F;
662  kBSW = (data[ncd] >> 16) & 0x1F;
663 
664  ncd++;
665  if(optz & 0x2) ncd++;
666 
667  Zero = (optz & 0x4 ) ? 1<<(kShort-1) : 0;
668  Bias = ((optz & 0x8) && (optz & 0x3)) ? short(data[++ncd] & 0xFFFF) : Bias;
669  Scale = (optz & 0x10) ? *((float *)&data[++ncd]) : 1.;
670 
671 // cout<<"kShort="<<kShort<<" kLong="<<kLong<<" kBSW="<<kBSW<<"\n";
672 // cout<<"Bias ="<< Bias<<" Zero="<<Zero<<" ncd="<<ncd<<"\n";
673 
674 // if ((optz & 64) != 0) {kLong = 16; kShort = 16;} // no compression
675 // if (kLong == 0) kLong = 16;
676 
677  if (w.size() != n16) w.resize(n16);
678 
679  register int *pw = w.data;
680  int ns;
681  unsigned int i;
682  unsigned int j=0;
683  unsigned int bsw=0;
684 
685  freebits = 8*sizeof(unsigned int);
686 
687  if (!pw) {
688  cout <<"WaveRDC:unCompress - memory allocation error.\n";
689  return -1;
690  }
691 
692  w = 0; // Sazonov 01/29/2001
693 
694  ncd++; // ncd is counter of elements of compressed array "data"
695 // printf(" data[0] = %o\n", data[ncd]);
696 
697 // ***************************
698 // start unpack block of data
699 // ***************************
700 
701  while (j < n16) {
702 
703  Pop(bsw, ncd, kBSW); // read block service word
704  ns = (bsw >> 1);
705 
706  if (ns < 0) {
707  cout <<" unCompress() error: invalid number of short words "<< ns <<"\n";
708  return -1;
709  }
710 
711 // cout<<"**0** ncd="<<ncd<<" bsw="<<bsw<<" j="<<j<<" fb="<<freebits<<"\n";
712 // printf(" data[%6d] = %o\n", ncd, data[ncd]);
713 
714  if (kShort > 0)
715  Pop(pw, j, ncd, kShort, ns);
716  else
717  for (int i = 0; i < ns; i++) pw[j+i] = 0;
718 
719  j += ns;
720 
721 // cout<<"**1** ncd="<< ncd<<" fb="<<freebits<<" j="<<j<<"\n";
722 
723  if(bsw & 1) {
724  Pop(pw, j, ncd, kLong, 1);
725 // cout<<"**2** ncd="<< ncd<<" fb="<<freebits<<" j="<<j<<" pw="<<pw[j]<<"\n";
726  }
727  else
728  if(j<n16) pw[j] = Zero;
729 
730  j++;
731  }
732 
733 // undifferentiate
734  if(optz & 0x180){
735  int mean = pw[0];
736  pw[0] = Bias;
737  for(i=1; i<n16; i++) pw[i] += pw[i-1]+mean;
738  if(optz & 0x100) for(i=1; i<n16; i+=2) pw[i] *= -1; // heterodine back
739  }
740 
741 // if was not differentiated
742  else{
743  w += int(Bias);
744  }
745 
746  return w.size();
747 }
748 
749  void WaveRDC::Pop(unsigned int &u, int &ncd, int k)
750 {
751 // Pop unpacks one element of data. ncd - current position in the array
752 // freebits - number of unpacked bits in data[ncd]
753 
754  int lul = 8*sizeof(u);
755  unsigned int mask = (1<<k) - 1;
756 
757  if(k == 0) return;
758 
759  if (k <= freebits) {
760  freebits -= k;
761  u = data[ncd] >> freebits; // shift & save the word in u
762  }
763  else {
764  u = data[ncd++] << (k - freebits); // shift upper bits of the word
765  freebits += lul - k; // expected unpacked space in cd[k+1]
766  u |= data[ncd] >> freebits; // shift & add lower bits in u
767  }
768  u = mask & u; // mask u
769  return;
770 }
771 
772  int WaveRDC::Pop(int *pw, int j, int &ncd, int k, int n)
773 {
774 // Pop unpacks "n" elements of data which are stored in compressed
775 // array "data" as a series of "n" words of k-bit length. Puts
776 // unpacked data to integer array "pw" and returns number of words decoded
777 
778  int jj, x;
779  unsigned int u=0;
780 
781  if(n == 0 || k == 0) return 0;
782  jj = j;
783 
784  for (int i = 0; i < n; i++) {
785  Pop(u, ncd, k);
786  x = (u>>1) + 1;
787  if(!(u & 1)) x = -x;
788  if(x == Zero) x = 0;
789 
790  pw[jj++] = x;
791  }
792  return jj-j;
793 }
794 
795 
796 
797 
798 
799 
800 
801 
802 
803 
804 
float rmsLimit
Definition: waverdc.hh:67
virtual size_t size() const
Definition: wavearray.hh:127
int optz
Definition: waverdc.hh:24
static double g(double e)
Definition: GNGen.cc:98
printf("total live time: non-zero lags = %10.1f \n", liveTot)
virtual ~WaveRDC()
Definition: waverdc.cc:20
int kShort
Definition: waverdc.hh:62
wavearray< double > a(hp.size())
WSeries< float > v[nIFO]
Definition: cwb_net.C:62
int wint(double a)
Definition: waverdc.hh:79
int n
Definition: cwb_net.C:10
double getStatistics(double &mean, double &rms) const
Definition: wavearray.cc:2105
cout<< endl;cout<< "ts size = "<< ts.size()<< " ts rate = "<< ts.rate()<< endl;tf.Forward(ts, wdm);int levels=tf.getLevel();cout<< "tf size = "<< tf.size()<< endl;double dF=tf.resolution();double dT=1./(2 *dF);cout<< "rate(hz) : "<< RATE<< "\t layers : "<< nLAYERS<< "\t dF(hz) : "<< dF<< "\t dT(ms) : "<< dT *1000.<< endl;int itime=TIME_PIXEL_INDEX;int ifreq=FREQ_PIXEL_INDEX;int index=(levels+1)*itime+ifreq;double time=itime *dT;double freq=(ifreq >0)?ifreq *dF:dF/4;cout<< endl;cout<< "PIXEL TIME = "<< time<< " sec "<< endl;cout<< "PIXEL FREQ = "<< freq<< " Hz "<< endl;cout<< endl;wavearray< double > x
size_t getLSW(size_t opt)
Definition: waverdc.hh:81
int freebits
Definition: waverdc.hh:60
float getScale(const waveDouble &, double)
Definition: waverdc.cc:168
int m
Definition: cwb_net.C:10
int j
Definition: cwb_net.C:10
canvas cd(1)
i drho i
void getSign(const waveDouble &, waveShort &)
Definition: waverdc.cc:286
void waveAssign(wavearray< Tout > &aout, wavearray< Tin > &ain)
Definition: wavearray.hh:342
#define N
WaveRDC & operator=(const WaveRDC &)
Definition: waverdc.cc:111
virtual double mean() const
void Dir(int v=1)
Definition: waverdc.cc:26
size_t mode
virtual double rms()
wavearray< double > w
Definition: Test1.C:27
int nLayer
Definition: waverdc.hh:23
wavearray< double > h
Definition: Regression_H1.C:25
void getShort(const waveDouble &, waveShort &)
Definition: waverdc.cc:238
i() int(T_cor *100))
char fname[1024]
virtual int DumpRDC(const char *, int=0)
Definition: waverdc.cc:150
int kLong
Definition: waverdc.hh:61
int k
short Bias
Definition: waverdc.hh:64
WaveRDC & operator+=(const WaveRDC &)
Definition: waverdc.cc:124
double F
double dt
regression r
Definition: Regression_H1.C:44
s s
Definition: cwb_net.C:137
ifstream in
double fabs(const Complex &x)
Definition: numpy.cc:37
int kBSW
Definition: waverdc.hh:63
int wabs(int i)
Definition: waverdc.hh:77
Meyer< double > S(1024, 2)
double mask
int unCompress(waveFloat &, int level=1)
Definition: waverdc.cc:599
short Zero
Definition: waverdc.hh:65
int Pop(int *, int, int &, int, int)
Definition: waverdc.cc:772
fclose(ftrig)
int Compress(const waveShort &)
Definition: waverdc.cc:327
virtual void resize(unsigned int)
float Scale
Definition: waverdc.hh:66
wavearray< double > y
Definition: Test10.C:31
int nSample
Definition: waverdc.hh:22
int Push(short *, int, unsigned int *, int &, int, int)
Definition: waverdc.cc:537