Berkeley Nuclear Data Software
SignalProcessing.hpp
Go to the documentation of this file.
1 
2 
3 template <class T>
5  unsigned int a_length,
6  unsigned int a_windowSize
7  )
8 {
9  //get a variable representing the new length
10  int numRtnSamples = a_length;
11  //allocate new storage for the filtered signal
12  double* rtrnPtr = new double[numRtnSamples];
13  //straigh forward implementation would be nested loop recalculating the
14  //average for each out put sample. Instead of the loop subtracting the first
15  //of the last element and adding the next element past the window will
16  //reducing the complexity
17 
18  //initialize the average to the first a_length samples
19  double avg =0;
20  for(int iS =0;iS < a_windowSize;iS++)
21  {
22  avg += static_cast<double>(a_signal[iS]);
23  }
24  //set the first element windowsize elements
25  for(int iS =0;iS < a_windowSize;iS++)
26  {
27  rtrnPtr[iS] = avg/static_cast<double>(a_windowSize);
28  }
29  for(int iS =a_windowSize; iS < numRtnSamples;iS++)
30  {
31  //subtract the first element of the last window
32  avg -= static_cast<double>(a_signal[iS-a_windowSize]);
33  //add the next element past the last window
34  avg += static_cast<double>(a_signal[iS]);
35  //set the next element of the array
36  rtrnPtr[iS] = avg/static_cast<double>(a_windowSize);
37  }
38  return rtrnPtr;
39 }
40 
47 template <class T> double* SignalProcessing<T>::
48  iterativeMovingAverage(T* a_signal,
49  unsigned int a_length,
50  unsigned int a_windowSize,
51  unsigned int a_numIter
52  )
53 {
54  double* smoothedInput = simpleMovingAverage(a_signal,
55  a_length,
56  a_windowSize);
57  for(int iI = 0; iI < a_numIter;iI++)
58  {
59  double* tmpPtr = smoothedInput;
60  smoothedInput =
62  a_length,
63  a_windowSize);
64  //delete the last array
65  delete[] tmpPtr;
66  }
67  return smoothedInput;
68 }
69 
70 template <class T> double* SignalProcessing<T>::
71  singlePoleLowPassFilter(T* a_signal,
72  unsigned int a_length,
73  double a_xDecay
74  )
75 {
76  //get a variable representing the length
77  int numRtnSamples = a_length;
78  //allocate new storage for the filtered signal
79  double* rtrnPtr = new double[numRtnSamples];
81  rtrnPtr[0] = static_cast<double>(a_signal[0]);
82  for(int iS =1;iS < a_length;iS++)
83  {
84  rtrnPtr[iS] = (1-a_xDecay)*static_cast<double>(a_signal[iS])
85  +a_xDecay*rtrnPtr[iS-1];
86  }
87  return rtrnPtr;
88 }
89 
90 template <class T> double* SignalProcessing<T>::
91  singlePoleHighPassFilter(T* a_signal,
92  unsigned int a_length,
93  double a_xDecay
94  )
95 {
96  //get a variable representing the length
97  int numRtnSamples = a_length;
98  //allocate new storage for the filtered signal
99  double* rtrnPtr = new double[numRtnSamples];
101  rtrnPtr[0] = static_cast<double>(a_signal[0]);
102  for(int iS =1;iS < a_length;iS++)
103  {
104  rtrnPtr[iS] = 0.5*(1+a_xDecay)*static_cast<double>(a_signal[iS])
105  -0.5*(1+a_xDecay)*static_cast<double>(a_signal[iS-1])
106  +a_xDecay*rtrnPtr[iS-1];
107  }
108  return rtrnPtr;
109 }
110 
111 template <class T> double* SignalProcessing<T>::
112  fourStageLowPassFilter(T* a_signal,
113  unsigned int a_length,
114  double a_xDecay
115  )
116 {
117  //get a variable representing the length
118  int numRtnSamples = a_length;
119  //allocate new storage for the filtered signal
120  double* rtrnPtr = new double[numRtnSamples];
122  double av4FirstSamples = static_cast<double>((a_signal[0]+a_signal[1]
123  +a_signal[2]+a_signal[3])/4.);
124  rtrnPtr[0] = av4FirstSamples;//static_cast<double>(a_signal[0]);
125  rtrnPtr[1] = av4FirstSamples;//static_cast<double>(a_signal[1]);
126  rtrnPtr[2] = av4FirstSamples;//static_cast<double>(a_signal[2]);
127  rtrnPtr[3] = av4FirstSamples;//static_cast<double>(a_signal[3]);
128  double coeff1 = pow(1-a_xDecay,4);
129  double coeff2 = 4*a_xDecay;
130  double coeff3 = -6*a_xDecay*a_xDecay;
131  double coeff4 = 4*pow(a_xDecay,3);
132  double coeff5 = -pow(a_xDecay,4);
133  for(int iS =4;iS < a_length;iS++)
134  {
135  rtrnPtr[iS] = coeff1*static_cast<double>(a_signal[iS])
136  +coeff2*rtrnPtr[iS-1]
137  +coeff3*rtrnPtr[iS-2]
138  +coeff4*rtrnPtr[iS-3]
139  +coeff5*rtrnPtr[iS-4];
140  }
141  return rtrnPtr;
142 }
143 
144 template <class T> double* SignalProcessing<T>::
145  trapezoidalFilter(T* a_signal,
146  unsigned int a_length,
147  int a_k,
148  int a_m
149  )
150 {
151  //get a variable representing the length
152  int numRtnSamples = a_length;
153  //allocate new storage for the filtered signal
154  double* rtrnPtr = new double[numRtnSamples];
155  for(int iS =0;iS < a_length;iS++)
156  {
157  if(iS<2*a_k+a_m)
158  {
159  rtrnPtr[iS] = static_cast<double>(a_signal[iS]);
160  }
161  else
162  {
163  rtrnPtr[iS] = static_cast<double>(a_signal[iS])
164  + rtrnPtr[iS-1]
165  - rtrnPtr[iS-a_k]
166  - rtrnPtr[iS-a_k-a_m]
167  + rtrnPtr[iS-2*a_k-a_m];
168  }
169  }
170  return rtrnPtr;
171 }
172 
173 template <class T> double SignalProcessing<T>::
174  calculateTIPS(T* a_signal,
175  unsigned int a_length,
176  int a_kFast,
177  int a_mFast,
178  int a_kSlow,
179  int a_mSlow
180  )
181 {
182  double* slowTrap = trapezoidalFilter(a_signal,a_length,a_kSlow,a_mSlow);
183  double* fastTrap = trapezoidalFilter(a_signal,a_length,a_kFast,a_mFast);
185  double maxSlow = 0;
186  double maxFast = 0;
187  for(int iS=0;iS<a_length;iS++)
188  {
189  if(slowTrap[iS]>maxSlow)
190  {
191  maxSlow = slowTrap[iS];
192  }
193  if(fastTrap[iS]>maxFast)
194  {
195  maxFast = fastTrap[iS];
196  }
197  }
198  return maxSlow/maxFast;
199 }
200 
201 #ifdef _CLING_
202 template class SignalProcessing<short>;
203 template class SignalProcessing<double>;
204 #endif
Definition: SignalProcessing.h:10
static double * singlePoleHighPassFilter(T *a_signal, unsigned int a_length, double a_xDecay)
Definition: SignalProcessing.hpp:91
static double * simpleMovingAverage(T *a_signal, unsigned int a_length, unsigned int a_windowSize)
Definition: SignalProcessing.hpp:4
static double * iterativeMovingAverage(T *a_signal, unsigned int a_length, unsigned int a_windowSize, unsigned int a_numIter)
Definition: SignalProcessing.hpp:48
double calculateTIPS(T *a_signal, unsigned int a_length, int a_kFast, int a_mFast, int a_kSlow, int a_mSlow)
Definition: SignalProcessing.hpp:174
static double * trapezoidalFilter(T *a_signal, unsigned int a_length, int a_k, int a_m)
Definition: SignalProcessing.hpp:145
static double * fourStageLowPassFilter(T *a_signal, unsigned int a_length, double a_xDecay)
Definition: SignalProcessing.hpp:112
static double * singlePoleLowPassFilter(T *a_signal, unsigned int a_length, double a_xDecay)
Definition: SignalProcessing.hpp:71