Repository navigation
Expand file tree
/
Copy pathwavearray.cc
More file actions
1935 lines (1532 loc) · 47.4 KB
/
Copy pathwavearray.cc
File metadata and controls
1935 lines (1532 loc) · 47.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------
* Package: Wavelet Analysis Tool
* generic data container to use with DMT and ROOT
* File name: wavearray.cc
*-------------------------------------------------------
*/
// wavearray class is the base class for wavelet data amd methods .
#include <time.h>
#include <iostream>
//#include <sstream>
//#include <strstream>
//#include "PConfig.h"
#ifdef __GNU_STDC_OLD
#include <gnusstream.h>
#else
#include <sstream>
#endif
#include "wavearray.hh"
#include "wavefft.hh"
extern "C" {
typedef int (*qsort_func) (const void*, const void*);
}
using namespace std;
//: Default constructor
template<class DataType_t>
wavearray<DataType_t>::wavearray() :
data(NULL), Size(0), Rate(1.), Start(0.)
{
Slice = std::slice(0,0,0);
}
//: allocates a data array with N=n elements.
template<class DataType_t>
wavearray<DataType_t>::wavearray(int n) :
Rate(1.), Start(0.)
{
if (n <= 0 ) n = 1;
data = (DataType_t *)malloc(n*sizeof(DataType_t));
Size = n;
Slice = std::slice(0,n,1);
*this = 0;
}
//: copy constructor
template<class DataType_t>
wavearray<DataType_t>::wavearray(const wavearray<DataType_t>& a) :
data(NULL), Size(0), Rate(1.), Start(0.)
{ *this = a; }
// explicit construction from array
template<class DataType_t> template<class T>
wavearray<DataType_t>::
wavearray(const T *p, unsigned int n, double r) :
data(NULL), Size(0), Rate(1.), Start(0.)
{
unsigned int i;
if(n != 0 && p != NULL){
data = (DataType_t *)malloc(n*sizeof(DataType_t));
for (i=0; i < n; i++) data[i] = p[i];
Size = n;
Rate = r;
}
Slice = std::slice(0,n,1);
}
//: destructor
template<class DataType_t>
wavearray<DataType_t>::~wavearray()
{
free(data);
}
//: operators =
template<class DataType_t>
wavearray<DataType_t>& wavearray<DataType_t>::
operator=(const wavearray<DataType_t>& a)
{
if (this==&a) return *this;
unsigned int i;
unsigned int N = a.Slice.size();
unsigned int m = a.Slice.stride();
if (N>0 && a.data) {
register DataType_t *pm = a.data + a.Slice.start();
wavearray<DataType_t>::resize(N);
for (i=0; i < N; i++) { data[i] = *pm; pm+=m; }
if(a.rate()>0.)
start(a.start() + a.Slice.start()/a.rate());
else
start(a.start());
rate(a.rate());
Slice = std::slice(0, size(),1);
const_cast<wavearray<DataType_t>&>(a).Slice = std::slice(0,a.size(),1);
}
else if(data) {
free(data);
data = NULL;
Size = 0;
Start = a.start();
Rate = a.rate();
Slice = std::slice(0,0,0);
}
return *this;
}
template<class DataType_t>
wavearray<DataType_t>& wavearray<DataType_t>::
operator<<(wavearray<DataType_t>& a)
{
unsigned int i;
unsigned int N = limit(a);
unsigned int n = Slice.stride();
unsigned int m = a.Slice.stride();
register DataType_t *p = a.data + a.Slice.start();
if(size())
for (i=Slice.start(); i<N; i+=n){ data[i] = *p; p += m; }
Slice = std::slice(0, size(),1);
a.Slice = std::slice(0,a.size(),1);
return *this;
}
template<class DataType_t>
wavearray<DataType_t>& wavearray<DataType_t>::
operator=(const DataType_t c)
{
unsigned int i;
unsigned int n = Slice.stride();
unsigned int N = limit();
if(size())
for (i=Slice.start(); i < N; i+=n) data[i] = c;
Slice = std::slice(0, size(),1);
return *this;
}
//: operators +=
template<class DataType_t>
wavearray<DataType_t>& wavearray<DataType_t>::
operator+=(wavearray<DataType_t> &a)
{
unsigned int i;
unsigned int N = limit(a);
unsigned int n = Slice.stride();
unsigned int m = a.Slice.stride();
register DataType_t *p = a.data + a.Slice.start();
if(size())
for (i=Slice.start(); i<N; i+=n){ data[i] += *p; p += m; }
Slice = std::slice(0, size(),1);
a.Slice = std::slice(0,a.size(),1);
return *this;
}
template<class DataType_t>
wavearray<DataType_t>& wavearray<DataType_t>::
operator+=(const DataType_t c)
{
unsigned int i;
unsigned int n = Slice.stride();
unsigned int N = limit();
if(size())
for (i=Slice.start(); i < N; i+=n) data[i] += c;
Slice = std::slice(0, size(),1);
return *this;
}
//: operators -=
template<class DataType_t>
wavearray<DataType_t>& wavearray<DataType_t>::
operator-=(wavearray<DataType_t> &a)
{
unsigned int i;
unsigned int N = limit(a);
unsigned int n = Slice.stride();
unsigned int m = a.Slice.stride();
register DataType_t *p = a.data + a.Slice.start();
if(size())
for (i=Slice.start(); i<N; i+=n){ data[i] -= *p; p += m; }
Slice = std::slice(0, size(),1);
a.Slice = std::slice(0,a.size(),1);
return *this;
}
template<class DataType_t>
wavearray<DataType_t>& wavearray<DataType_t>::
operator-=(const DataType_t c)
{
unsigned int i;
unsigned int n = Slice.stride();
unsigned int N = limit();
if(size())
for (i=Slice.start(); i < N; i+=n) data[i] -= c;
Slice = std::slice(0, size(),1);
return *this;
}
//: multiply all elements of data array by constant
template<class DataType_t>
wavearray<DataType_t>& wavearray<DataType_t>::
operator*=(const DataType_t c)
{
unsigned int i;
unsigned int n = Slice.stride();
unsigned int N = limit();
if(size())
for (i=Slice.start(); i < N; i+=n) data[i] *= c;
Slice = std::slice(0, size(),1);
return *this;
}
// scalar production
template<class DataType_t>
wavearray<DataType_t>& wavearray<DataType_t>::
operator*=(wavearray<DataType_t>& a)
{
unsigned int i;
unsigned int N = limit(a);
unsigned int n = Slice.stride();
unsigned int m = a.Slice.stride();
register DataType_t *p = a.data + a.Slice.start();
if(size())
for (i=Slice.start(); i<N; i+=n){ data[i] *= *p; p += m; }
Slice = std::slice(0, size(),1);
a.Slice = std::slice(0,a.size(),1);
return *this;
}
// operator[](const std::slice &)
template<class DataType_t>
wavearray<DataType_t>& wavearray<DataType_t>::
operator[](const std::slice& s)
{
Slice = s;
if(limit() > size()){
cout << "wavearray::operator[slice]: Illegal argument "<<limit()<<" "<<size()<<"\n";
Slice = std::slice(0,size(),1);
}
return *this;
}
// operator[](const std::slice &)
template<class DataType_t>
DataType_t & wavearray<DataType_t>::
operator[](const unsigned int n)
{
if(n >= size()){
cout << "wavearray::operator[int]: Illegal argument\n";
return data[0];
}
return data[n];
}
//: Dumps data array to file *fname in ASCII format.
template<class DataType_t>
void wavearray<DataType_t>::Dump(const char *fname, int app)
{
int n=size();
char mode[3] = "w";
if (app == 1) strcpy(mode, "a");
FILE *fp;
if ( (fp = fopen(fname, mode)) == NULL ) {
cout << " Dump() error: cannot open file " << fname <<". \n";
return;
};
if(app == 0) {
fprintf( fp,"# start time: -start %lf \n", this->Start );
fprintf( fp,"# sampling rate: -rate %lf \n", this->Rate );
fprintf( fp,"# number of samples: -size %d \n", (int)this->Size );
}
for (int i = 0; i < n; i++) fprintf( fp,"%e ", (float)data[i]);
fclose(fp);
}
//: Dumps data array to file *fname in binary format.
template<class DataType_t>
void wavearray<DataType_t>::DumpBinary(const char *fname, int app)
{
int n = size() * sizeof(DataType_t);
char mode[5];
strcpy (mode, "w");
if (app == 1) strcpy (mode, "a");
FILE *fp;
if ( (fp=fopen(fname, mode)) == NULL ) {
cout << " DumpBinary() error : cannot open file " << fname <<". \n";
return ;
}
fwrite(data, n, 1, fp);
fclose(fp);
}
//: Dumps data array to file *fname in binary format as "short".
template<class DataType_t>
void wavearray<DataType_t>::DumpShort(const char *fname, int app)
{
int n = size();
char mode[5] = "w";
if (app == 1) strcpy (mode, "a");
FILE *fp;
if ( (fp = fopen(fname, mode)) == NULL ) {
cout << " DumpShort() error : cannot open file " << fname <<". \n";
return;
}
short *dtemp;
dtemp=new short[n];
for ( int i=0; i<n; i++ ) dtemp[i]=short(data[i]) ;
n = n * sizeof(short);
fwrite(dtemp, n, 1, fp);
fclose(fp);
delete [] dtemp;
}
//: Read data from file in binary format.
template<class DataType_t>
void wavearray<DataType_t>::ReadBinary(const char *fname, int N)
{
int step = sizeof(DataType_t);
FILE *fp;
double d;
if ( (fp=fopen(fname,"r")) == NULL ) {
cout << " ReadBinary() error : cannot open file " << fname <<". \n";
exit(1);
}
if(N == 0){ // find the data length
while(!feof(fp)){
fread(&d,step,1,fp);
N++;
}
N--;
rewind(fp);
}
if(int(size()) != N) resize(N);
int n = size() * sizeof(DataType_t);
fread(data, n, 1, fp); // Reading binary record
fclose(fp);
}
//: Read data from file as short.
template<class DataType_t>
void wavearray<DataType_t>::ReadShort(const char *fname)
{
short *dtmp;
dtmp = new short[size()];
int n = size() * sizeof(short);
FILE *fp;
if ( (fp=fopen(fname,"r")) == NULL ) {
cout << " ReadShort() error : cannot open file " << fname <<". \n";
return;
}
cout << " Reading binary record, size="<< n <<"\n";
fread(dtmp, n, 1, fp);
for (unsigned int i = 0; i < size(); i++)
data[i] = DataType_t(dtmp[i]);
fclose(fp);
delete [] dtmp;
}
//: resizes data array to a new length n.
template<class DataType_t>
void wavearray<DataType_t>::resize(unsigned int n)
{
DataType_t *p = NULL;
if(n==0){
if(data) free(data);
data = NULL;
Size = 0;
Slice = std::slice(0,0,0);
return;
}
p = (DataType_t *)realloc(data,n*sizeof(DataType_t));
if(p){
data = p;
Size = n;
Slice = std::slice(0,n,1);
}
else
cout<<"wavearray::resize(): memory allocation failed.\n";
}
/************************************************************************
* Creates new data set by resampling the original data from "a" *
* with new sample frequency "f". Uses polynomial interpolation scheme *
* (Lagrange formula) with np-points, "np" must be even number, by *
* default np=6 (corresponds to 5-th order polynomial interpolation). *
* This function calls wavearray::resize() function to adjust *
* current data array if it's size does not exactly match new number *
* of points. *
************************************************************************/
template<class DataType_t>
void wavearray<DataType_t>::
resample(wavearray<DataType_t> const &a, double f, int nF)
{
int nP = nF;
if(nP<=1) nP = 6;
if(int(a.size())<nP) nP = a.size();
nP = (nP>>1)<<1;
int N;
int nP2 = nP/2;
const DataType_t *p = a.data;
register int i;
register int iL;
register double x;
double *temp=new double[nF];
rate(f);
double ratio = a.rate() / rate();
N = int(a.size()/ratio + 0.5);
if ( (int)size() != N ) resize(N);
// left border
int nL = int(nP2/ratio);
for (i = 0; i < nL; i++)
data[i] = (DataType_t)Nevill(i*ratio, nP, p, temp);
// in the middle of array
int nM = int((a.size()-nP2)/ratio);
if(nM < nL) nM = nL;
if(nM&1 && nM>nL) {
x = nL*ratio;
iL = int(x) - nP2 + 1 ;
data[i] = (DataType_t)Nevill(x-iL, nP, p+iL, temp);
nL++;
}
for (i = nL; i < nM; i+=2) {
x = i*ratio;
iL = int(x) - nP2 + 1 ;
data[i] = (DataType_t)Nevill(x-iL, nP, p+iL, temp);
x += ratio;
iL = int(x) - nP2 + 1 ;
data[i+1] = (DataType_t)Nevill(x-iL, nP, p+iL, temp);
}
// right border
int nR = a.size() - nP;
p += nR;
for (i = nM; i < N; i++)
data[i] = (DataType_t)Nevill(i*ratio-nR, nP, p, temp);
delete [] temp;
}
template<class DataType_t>
void wavearray<DataType_t>::resample(double f, int nF)
{
wavearray<DataType_t> a;
a = *this;
resample(a,f,nF);
}
template<class DataType_t>
void wavearray<DataType_t>::
Resample(wavearray<DataType_t> const &a, double f, int np)
{
int i1, N, n1, n2, n, np2 = np/2;
double s, x, *c, *v;
c=new double[np];
v=new double[np];
rate(f);
double ratio = a.rate() / rate();
n = a.size();
N = int(n/ratio + 0.5);
if ( (int)size() != N ) resize(N);
// calculate constant filter part c(k) = -(-1)^k/k!/(np-k-1)!
for (int i = 0; i < np; i++) {
int m = 1;
for (int j = 0; j < np; j++) if (j != i) m *= (i - j);
c[i] = 1./double(m);
}
for (int i = 0; i < N; i++)
{
x = i*ratio;
i1 = int(x);
x = x - i1 + np2 - 1;
// to treat data boundaries we need to calculate critical numbers n1 and n2
n1 = i1 - np2 + 1;
n2 = i1 + np2 + 1 - n;
// here we calculate the sum of products like h(k)*a(k), k=0..np-1,
// where h(k) are filter coefficients, a(k) are signal samples
// h(k) = c(k)*prod (x-i), i != k, c(k) = -(-1)^k/k!/(np-k-1)!
// get signal part multiplied by constant filter coefficients
if ( n1 >= 0 )
if ( n2 <= 0 )
// regular case - far from boundaries
for (int j = 0; j < np; j++) v[j] = c[j]*a.data[i1 + j - np2 + 1];
else {
// right border case
x = x + n2;
for (int j = 0; j < np; j++) v[j] = c[j]*a.data[n + j - np];
}
else {
// left border case
x = x + n1;
for (int j = 0; j < np; j++) v[j] = c[j]*a.data[j];
}
// multiply resulted v[j] by x-dependent factors (x-k), if (k != j)
for (int k = 0; k < np; k++) {
for (int j = 0; j < np; j++) if (j != k) v[j] *= x;
x -= 1.; }
s = 0.;
for (int j = 0; j < np; j++) s += v[j];
data[i] = (DataType_t)s;
}
delete [] c;
delete [] v;
}
/******************************************************************************
* copies data from data array of the object wavearray a to *this
* object data array. Procedure starts from the element "a_pos" in
* the source array which is copied to the element "pos" in the
* destination array. "length" is the number of data elements to copy.
* By default "length"=0, which means copy all source data or if destination
* array is shorter, copy data until the destination is full.
*****************************************************************************/
template<class DataType_t> void wavearray<DataType_t>::
cpf(const wavearray<DataType_t> &a, int length, int a_pos, int pos)
{
if (rate() != a.rate()){
cout << "wavearray::cpf() warning: sample rate mismatch.\n";
cout<<"rate out: "<<rate()<<" rate in: "<<a.rate()<<endl;
}
if (length == 0 )
length = ((size() - pos) < (a.size() - a_pos))?
(size() - pos) : (a.size() - a_pos);
if( length > (int)(size() - pos) ) length = size() - pos;
if( length > (int)(a.size() - a_pos) ) length = a.size() - a_pos;
for (int i = 0; i < length; i++)
data[i + pos] = a.data[i + a_pos];
rate(a.rate());
}
/******************************************************************************
* Adds data from data array of the object wavearray a to *this
* object data array. Procedure starts from the element "a_pos" in
* the source array which is added starting from the element "pos" in the
* destination array. "length" is the number of data elements to add.
* By default "length"=0, which means add all source data or if destination
* array is shorter, add data up to the end of destination.
*****************************************************************************/
template<class DataType_t> void wavearray<DataType_t>::
add(const wavearray<DataType_t> &a, int length, int a_pos, int pos)
{
if (rate() != a.rate())
cout << "wavearray::add() warning: sample rate mismatch.\n";
if (length == 0 )
length = ((size() - pos) < (a.size() - a_pos))?
(size() - pos) : (a.size() - a_pos);
if( length > (int)( size()- pos) ) length = size() - pos;
if( length > (int)(a.size() - a_pos) ) length = a.size() - a_pos;
for (int i = 0; i < length; i++)
data[i + pos] += a.data[i + a_pos];
}
/******************************************************************************
* Subtracts data array of the object wavearray a from *this
* object data array. Procedure starts from the element "a_pos" in
* the source array which is subtracted starting from the element "pos" in
* the destination array. "length" is number of data elements to subtract.
* By default "length"=0, which means subtract all source data or if the
* destination array is shorter, subtract data up to the end of destination.
******************************************************************************/
template<class DataType_t> void wavearray<DataType_t>::
sub(const wavearray<DataType_t> &a, int length, int a_pos, int pos)
{
if (rate() != a.rate())
cout << "wavearray::sub() warning: sample rate mismatch.\n";
if ( length == 0 )
length = ((size() - pos) < (a.size() - a_pos))?
(size() - pos) : (a.size() - a_pos);
if( length > (int)(size() - pos) ) length = size() - pos;
if( length > (int)(a.size() - a_pos) ) length = a.size() - a_pos;
for (int i = 0; i < length; i++)
data[i + pos] -= a.data[i + a_pos];
}
/*****************************************************************
* append two wavearrays with the same rate ignoring start time
* of input array.
*****************************************************************/
template<class DataType_t> size_t wavearray<DataType_t>::
append(const wavearray<DataType_t> &a)
{
size_t n = this->size();
size_t m = a.size();
if (this->rate() != a.rate())
cout << "wavearray::append() warning: sample rate mismatch.\n";
if(m == 0 ) return this->size();
this->resize(n+m);
this->cpf(a,m,0,n);
return n+m;
}
/*****************************************************************
* append a data point
*****************************************************************/
template<class DataType_t> size_t wavearray<DataType_t>::
append(DataType_t a)
{
size_t n = this->size();
this->resize(n+1);
this->data[n] = a;
return n+1;
}
/*****************************************************************
* Calculates Fourier Transform for real signal using
* Fast Fourier Transform (FFT) algorithm. Packs resulting
* complex data into original array of real numbers.
* Calls wavefft() function which is capable to deal with any
* number of data points. FFT(1) means forward transformation,
* which is default, FFT(-1) means inverse transformation.
*****************************************************************/
template<class DataType_t>
void wavearray<DataType_t>::FFT(int direction)
{
double *a, *b;
int N = size();
int n2 = N/2;
a=new double[N];
b=new double[N];
switch (direction)
{ case 1:
// direct transform
for (int i=0; i<N; i++) { a[i] = data[i]; b[i]=0.;}
wavefft(a, b, N, N, N, -1);
// pack complex numbers to real array
for (int i=0; i<n2; i++)
{ data[2*i] = (DataType_t)a[i]/N;
data[2*i+1] = (DataType_t)b[i]/N;
}
// data[1] is not occupied because imag(F[0]) is always zero and we
// store in data[1] the value of F[N/2] which is pure real for even "N"
data[1] = (DataType_t)a[n2]/N;
// in the case of odd number of data points we store imag(F[N/2])
// in the last element of array data[N]
if ((N&1) == 1) data[N-1] = (DataType_t)b[n2]/N;
break;
case -1:
// inverse transform
// unpack complex numbers from real array
for (int i=1;i<n2;i++)
{ a[i]=data[2*i];
b[i]=data[2*i+1];
a[N-i]=data[2*i];
b[N-i]=-data[2*i+1];
}
a[0]=data[0];
b[0]=0.;
if ((N&1) == 1)
{ a[n2]=data[1]; b[n2]=data[N-1]; } // for odd n
else
{ a[n2]=data[1]; b[n2]=0.; }
wavefft(a, b, N, N, N, 1); // call FFT for inverse tranform
for (int i=0; i<N; i++)
data[i] = (DataType_t)a[i]; // copy the result from array "a"
}
delete [] b;
delete [] a;
}
/*************************************************************************
* Stack generates wavearray *this by stacking data from wavearray td.
* Input data are devided on subsets with with samples "length"
* then they are added together. Average over the all subsets is saved in
* *this.
*************************************************************************/
template<class DataType_t>
double wavearray<DataType_t>::
Stack(const wavearray<DataType_t> &td, int length)
{
double ss, s0, s2;
rate(td.rate());
int k = td.size()/length;
int n = k*length;
if (k == 0) {
cout <<" Stack() error: data length too short to contain \n"
<< length << " samples\n";
return 0.;
}
if (size() != (unsigned int)length) resize(length);
// sum (stack) all k periods of frequency f to produce 1 cycle
s0 = 0.;
for (int i = 0; i < length; i++) {
ss = 0.;
for (int j = i; j < n; j += length) ss += td.data[j];
data[i] = (DataType_t)ss/k;
s0 += ss;
}
s0 /= (k*length);
// remove constant displacement (zero frequency component)
s2 = 0.;
for (int i = 0; i < length; i++) {
data[i] -= (DataType_t)s0;
s2 += data[i]*data[i];
}
s2 /= length;
return s2; // return stacked signal power (energy/N)
}
/*************************************************************************
* Another version of Stack:
* Input data (starting from sample "start") are devided on "k" subsets
* with sections "length"
* Average over the all sections is saved in *this.
*************************************************************************/
template<class DataType_t>
double wavearray<DataType_t>::
Stack(const wavearray<DataType_t> &td, int length, int start)
{
double avr, rms;
rate(td.rate());
if(start+length > (int)td.size()) length = td.size()-start;
int k = (size()<1) ? 0 : length/size();
if (k == 0) {
cout <<" Stack() error: data length too short to contain \n"
<< length << " samples\n";
return 0.;
}
*this = 0;
for (int i = 0; i<k; i++) add(td, size(), start+i*size());
*this *= DataType_t(1./k);
getStatistics(avr,rms);
*this -= DataType_t(avr);
return rms*rms; // return stacked signal power
}
// Stack generates wavearray *this by stacking data from wavearray td.
// Input data are devided on subsets with with samples "length"
// then they are added together. Average over the all subsets is saved in
// *this.
template<class DataType_t>
double wavearray<DataType_t>::
Stack(const wavearray<DataType_t> &td, double window)
{
return this->Stack(td, int(td.rate() * window));
}
// wavearray mean
template<class DataType_t>
double wavearray<DataType_t>::mean(double sigma) const
{
register size_t i;
register double x = 0;
size_t N = (size()>>2)<<2;
register DataType_t *p = data + size() - N;
if(!size()) return 0.;
if(sigma<=0.){
for(i=0; i<size()-N; i++) { x += data[i]; }
for(i=0; i<N; i+=4)
x += p[i] + p[i+1] + p[i+2] + p[i+3];
return double(x)/size();
}
else{
int m=0;
double a, y=0.;
for(i=0; i<N; i+=4){
x += p[i] + p[i+1] + p[i+2] + p[i+3];
y += p[i]*p[i] + p[i+1]*p[i+1] + p[i+2]*p[i+2] + p[i+3]*p[i+3];
}
a = x/size();
sigma *= sqrt(y/size()-x*x);
x = 0.;
for(i=0; i<N; i+=4){
if(fabs(p[i]-a) < sigma) { x += p[i]; m++; }
if(fabs(p[i]-a) < sigma) { x += p[i]; m++; }
if(fabs(p[i]-a) < sigma) { x += p[i]; m++; }
if(fabs(p[i]-a) < sigma) { x += p[i]; m++; }
}
return (m > 0) ? x/m : a;
}
}
template<class DataType_t>
double wavearray<DataType_t>::mean(const std::slice &s)
{
register size_t i;
register double x = 0.;
register DataType_t *p = data + s.start();
size_t N = s.size();
size_t m = (s.stride()<=0) ? 1 : s.stride();
if(size()<limit(s)) N = (limit(s) - s.start() - 1)/m;
for(i=0; i<N; i++) { x += *p; p += m; }
return (N==0) ? 0. : x/N;
}
// running mean
template<class DataType_t>
void wavearray<DataType_t>::mean(double t,
wavearray<DataType_t> *pm,
bool clean,
size_t skip)
{
DataType_t* p=NULL;
DataType_t* q=NULL;
DataType_t* xx;
double sum = 0.;
size_t i,last;
size_t step = Slice.stride();
size_t N = Slice.size(); // number of samples in wavearray
size_t n = size_t(t*rate()/step); // # of samples in the window
if(n<4) {
cout<<"wavearray<DataType_t>::mean() short time window"<<endl;
return;
}
if(n&1) n--; // # of samples in the window - 1
size_t nM = n/2; // index of median sample
size_t nL = N-nM-1;
if(pm){
pm->resize(N/skip);
pm->start(start());
pm->rate(rate());
}
xx = (DataType_t *)malloc((n+1)*sizeof(DataType_t));
p = data+Slice.start();
q = data+Slice.start();
for(i=0; i<=n; i++) {
xx[i] = *p;
sum += xx[i];
p += step;
}
last = 0;
for(i=0; i<N; i++){
if(pm) {
pm->data[i/skip] = DataType_t(sum/(n+1.));
if(clean) q[i*step] -= DataType_t(sum/(n+1.));
}
else {
if(clean) q[i*step] -= DataType_t(sum/(n+1.));
else q[i*step] = DataType_t(sum/(n+1.));
}
if(i>=nM && i<nL) { // copy next sample into last
sum -= xx[last];
sum += *p;
xx[last++] = *p;
p += step;
}
if(last>n) last = 0;
}
free(xx);
return;
}
template<class DataType_t>
double wavearray<DataType_t>::rms()
{
register size_t i;
register double x = 0.;
register double y = 0.;
size_t N = (size()>>2)<<2;
register DataType_t *p = data + size() - N;
if(!size()) return 0.;
for(i=0; i<size()-N; i++) { x += data[i]; y += data[i]*data[i]; }
for(i=0; i<N; i+=4){
x += p[i] + p[i+1] + p[i+2] + p[i+3];
y += p[i]*p[i] + p[i+1]*p[i+1] + p[i+2]*p[i+2] + p[i+3]*p[i+3];
}
x /= size();
return sqrt(y/size()-x*x);
}
// running 50% percentile rms
template<class DataType_t>
void wavearray<DataType_t>::rms(double t,