| Bsoft 2.0.5 Bernard's software package

moving_average.cpp
Go to the documentation of this file.
1
9 #include "moving_average.h"
10 #include "utilities.h"
11
12 // Declaration of global variables
13 extern int verbose; // Level of output to the screen
14
28 double* moving_average(long number, double* x, long window)
29 {
30 long i, j=0, halfwindow = window/2;
31 double* mov_avg = new double[number];
32 for ( i=0; i<number; i++ ) mov_avg[i] = 0;
33
34 if ( verbose & VERB_PROCESS )
35 cout << "Calculating moving average with window of " << window << " points" << endl << endl;
36
37 // Calculate the moving sum
38 for ( i=0; i<number; i++ ) {
39 if ( i>0 ) mov_avg[i] = mov_avg[i-1];
40 if ( j-window >= 0 ) {
41 mov_avg[i] -= x[j-window];
42 if ( j>=number ) j++;
43 }
44 while ( j<i+halfwindow && j<number) {
45 mov_avg[i] += x[j];
46 j++;
47 }
48 }
49
50 // Normalize the moving sum
51 for ( i=0; i<number; i++ ) {
52 j = window;
53 if ( i-halfwindow < 0 ) j += i - halfwindow;
54 if ( i+halfwindow >= number ) j -= i + halfwindow - number;
55 mov_avg[i] /= j;
56 }
57
58 return mov_avg;
59 }
60
61
75 Complex<float>* moving_average_complex(long number, Complex<float>* x, long window)
76 {
77 long i, j=0, halfwindow = window/2;
78 Complex<float>* mov_avg = new Complex<float>[number];
79 for ( i=0; i<number; i++ ) mov_avg[i] = 0;
80
81 if ( verbose & VERB_PROCESS )
82 cout << "Calculating complex moving average with window of " << window << " points" << endl << endl;
83
84 // Calculate the moving sum
85 for ( i=0; i<number; i++ ) {
86 if ( i>0 ) mov_avg[i] = mov_avg[i-1];
87 if ( j-window >= 0 ) {
88 mov_avg[i] -= x[j-window];
89 if ( j>=number ) j++;
90 }
91 while ( j<i+halfwindow && j<number) {
92 mov_avg[i] += x[j];
93 j++;
94 }
95 }
96
97 // Normalize the moving sum
98 for ( i=0; i<number; i++ ) {
99 j = window;
100 if ( i-halfwindow < 0 ) j += i - halfwindow;
101 if ( i+halfwindow >= number ) j -= i + halfwindow - number;
102 mov_avg[i] /= j;
103 }
104
105 return mov_avg;
106 }
107
122 vector<double> moving_polynomial(long order, long number, double* x, long window)
123 {
124 vector<double> m;
125
126 if ( order < 1 ) {
127 cerr << "Error in moving_polynomial: the order must be > 0!" << endl;
128 return m;
129 }
130
131 long hw = window/2, i, j, is, i1, i2, w;
132 double coeff[order+1], v[window], p;
133 m.resize(number, 0);
134
135 for ( i=0; i<window; i++ ) v[i] = i;
136
137 for ( i=0; i<number; i++ ) {
138 is = 0;
139 i1 = i - hw;
140 if ( i1 < 0 ) { is = -i1; i1 = 0; }
141 i2 = i + hw;
142 if ( i2 >= number ) i2 = number - 1;
143 w = i2 - i1 + 1;
144 fit_polynomial(w, &v[is], &x[i1], order, coeff);
145 for ( j=0, m[i]=0, p=1; j<=order; j++, p*=v[hw] ) m[i] += coeff[j]*p;
146 }
147
148 return m;
149 }
150
fit_polynomial
double fit_polynomial(int n, double *x, double *y, int order, double *coeff)
Fits a data set to a polynomial function.
Definition: matrix_linear.cpp:166
moving_polynomial
vector< double > moving_polynomial(long order, long number, double *x, long window)
Calculates a moving polynomial fit over an array of data.
Definition: moving_average.cpp:122
moving_average.h
Header file moving average calculations.
utilities.h
Header file for general utilities.
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
verbose
int verbose
moving_average
double * moving_average(long number, double *x, long window)
Calculates a moving average over an array of data.
Definition: moving_average.cpp:28
Complex
Definition: Complex.h:23
moving_average_complex
Complex< float > * moving_average_complex(long number, Complex< float > *x, long window)
Calculates a moving average over an array of complex data.
Definition: moving_average.cpp:75