RTXI  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ar_proc.cpp
Go to the documentation of this file.
1 //
2 // File = ar_proc.cpp
3 //
4 
5 #include "ar_proc.h"
6 #include "gausrand.h"
7 #include "sig_type.h"
8 #include <fstream>
9 #include <stdlib.h>
10 
11 #ifdef _DEBUG
12 extern std::ofstream DebugFile;
13 #endif
14 
15 using namespace std;
16 
17 //===============================================
18 // ArProcess
19 
20 template <class T>
22 {
23  A_Coeffs = NULL;
24  Old_Output = NULL;
25  Sum_Wgn_Samps = 0.0;
26  Sum_Wgn_Sqrs = 0.0;
27  Sum_Samps = 0.0;
28  Sum_Squares = 0.0;
29  Num_Samps = 0;
30 }
31 //==================================================
32 // Destructor
33 
34 template <class T>
36 {
37  delete[] A_Coeffs;
38  delete[] Old_Output;
39 }
40 //==================================================
41 // Function to dump AR parameters to output
42 // stream indicated by uout
43 
44 template <class T>
45 void
47 {
48  uout << "Drv_Noise_Var = " << Drv_Noise_Var << std::endl;
49  for (int indx = 0; indx <= Ar_Order; indx++) {
50  uout << "a[" << indx << "] = " << A_Coeffs[indx] << std::endl;
51  }
52  return;
53 }
54 //==================================================
55 // Function to copy AR parameters to array
56 // pointed to by a_coeff
57 
58 template <class T>
59 void
60 ArProcess<T>::GetParameters(int* ar_order, T* a_coeff)
61 {
62  *ar_order = Ar_Order;
63  for (int indx = 0; indx <= Ar_Order; indx++) {
64  a_coeff[indx] = A_Coeffs[indx];
65  }
66  return;
67 }
68 //---------------------------------------------------
69 // function to generate an output sequence of
70 // length seq_len. If noise_seed_init is nonzero,
71 // value supplied will be used as the new seed
72 // for the noise generator. Otherwise, the stored
73 // value will be used.
74 
75 template <class T>
76 T*
77 ArProcess<T>::OutputSequence(long noise_seed_init, int seq_len)
78 {
79  int samp_indx, i;
80  T *out_seq, out_samp;
81  long noise_seed;
82  double std_dev;
83 
84  std_dev = sqrt(Drv_Noise_Var);
85  out_seq = new T[seq_len];
86 
87  if (noise_seed_init == 0)
88  noise_seed = Noise_Seed;
89  else
90  noise_seed = noise_seed_init;
91 
92  for (samp_indx = 0; samp_indx < seq_len; samp_indx++) {
93  out_samp = std_dev * GaussRandom(&noise_seed);
94  for (i = 0; i < Ar_Order; i++) {
95  out_samp -= A_Coeffs[i + 1] * Old_Output[i];
96  }
97 
98  for (i = Ar_Order - 1; i > 0; i--) {
99  Old_Output[i] = Old_Output[i - 1];
100  }
101  Old_Output[0] = out_samp;
102  out_seq[samp_indx] = out_samp;
103 #ifdef _DEBUG
104  DebugFile << samp_indx << ", " << out_samp << std::endl;
105 #endif
106  } // end of loop over samp_indx
107 
108  Noise_Seed = noise_seed;
109  return (out_seq);
110 }
111 //---------------------------------------------------
112 
113 template <class T>
114 T
115 ArProcess<T>::NextSample(long noise_seed_init)
116 {
117  Sum_Wgn_Samps = 0.0;
118  Sum_Wgn_Sqrs = 0.0;
119  Sum_Samps = 0.0;
120  Sum_Squares = 0.0;
121  Num_Samps = 0;
122  if (noise_seed_init != 0)
123  Noise_Seed = noise_seed_init;
124  return (NextSample());
125 }
126 
127 //---------------------------------------------------
128 template <class T>
129 T
131 {
132  return (Sum_Samps / double(Num_Samps));
133 }
134 //---------------------------------------------------
135 template <class T>
136 double
138 {
139  return (Drv_Noise_Var);
140 }
141 //---------------------------------------------------
142 template <class T>
143 T
145 {
146  T mean, var;
147  mean = Sum_Samps / double(Num_Samps);
148  var = (Sum_Squares / double(Num_Samps)) - (mean * mean);
149 #ifdef _DEBUG
150  DebugFile << "mean = " << mean << std::endl;
151  DebugFile << "variance = " << var << std::endl;
152 #endif
153  return (var);
154 }
155 //---------------------------------------------------
156 
157 template <class T>
158 T
160 {
161  int i;
162  T out_samp;
163  double std_dev;
164 
165  std_dev = sqrt(Drv_Noise_Var);
166 
167  out_samp = std_dev * GaussRandom(&Noise_Seed);
168  Sum_Wgn_Samps += out_samp;
169  Sum_Wgn_Sqrs += out_samp * out_samp;
170  for (i = 0; i < Ar_Order; i++) {
171  out_samp -= A_Coeffs[i + 1] * Old_Output[i];
172  }
173 
174  for (i = Ar_Order - 1; i > 0; i--) {
175  Old_Output[i] = Old_Output[i - 1];
176  }
177  Old_Output[0] = out_samp;
178  Sum_Samps += out_samp;
179  Sum_Squares += (out_samp * out_samp);
180  Num_Samps++;
181 
182  return (out_samp);
183 }
184 
185 template class ArProcess<type_of_sig_vals_T>;
void GetParameters(int *ar_order, T *a_coeff)
Definition: ar_proc.cpp:60
double GaussRandom(long *seed)
Definition: gausrand.cpp:10
void DumpParameters(std::ostream &uout)
Definition: ar_proc.cpp:46
T GetVariance(void)
Definition: ar_proc.cpp:144
double GetDrivingVariance(void)
Definition: ar_proc.cpp:137
complex sqrt(const complex _z)
Definition: complex.h:148
~ArProcess(void)
Definition: ar_proc.cpp:35
std::ofstream DebugFile
T NextSample(void)
Definition: ar_proc.cpp:159
T * OutputSequence(long noise_seed, int seq_len)
Definition: ar_proc.cpp:77
ArProcess(void)
Definition: ar_proc.cpp:21
T GetMean(void)
Definition: ar_proc.cpp:130