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