| 1 | #include "meanvarestim.h"
 | 
|---|
| 2 | #include "toimanager.h"
 | 
|---|
| 3 | #include "slinparbuff.h"
 | 
|---|
| 4 | 
 | 
|---|
| 5 | MeanVarEstimator::MeanVarEstimator(int n) {
 | 
|---|
| 6 |   nsamples = n;
 | 
|---|
| 7 | }
 | 
|---|
| 8 | 
 | 
|---|
| 9 | void MeanVarEstimator::init() {
 | 
|---|
| 10 |   declareInput("signal");
 | 
|---|
| 11 |   declareOutput("mean");
 | 
|---|
| 12 |   declareOutput("variance");
 | 
|---|
| 13 |   name = "MeanEstimator";
 | 
|---|
| 14 | 
 | 
|---|
| 15 |   neededHistory = nsamples;
 | 
|---|
| 16 | }
 | 
|---|
| 17 | 
 | 
|---|
| 18 | void MeanVarEstimator::run() {
 | 
|---|
| 19 |   int snb = getMinIn();
 | 
|---|
| 20 |   int sne = getMaxIn();
 | 
|---|
| 21 | 
 | 
|---|
| 22 |   cout << "MeanVarEstimator -- " << snb << " to " << sne << endl;
 | 
|---|
| 23 | 
 | 
|---|
| 24 |   bool hasMean = checkOutputTOIIndex(0);
 | 
|---|
| 25 |   bool hasVar  = checkOutputTOIIndex(1);
 | 
|---|
| 26 | 
 | 
|---|
| 27 |   if (snb>sne) {
 | 
|---|
| 28 |     cout<<"MeanVarEstimator::run() - Bad sample interval"<<snb<<" , "<<sne<<endl;
 | 
|---|
| 29 |     throw ParmError("MeanVarEstimator::run() - Bad sample interval");
 | 
|---|
| 30 |   }
 | 
|---|
| 31 | 
 | 
|---|
| 32 |   if (!checkInputTOIIndex(0)) {
 | 
|---|
| 33 |     cout<<"MeanVarEstimator::run() - Input TOI (signal) not connected! "<<endl;
 | 
|---|
| 34 |     throw ParmError("MeanVarEstimator::run() Input TOI (signal) not connected!");
 | 
|---|
| 35 |   }
 | 
|---|
| 36 | 
 | 
|---|
| 37 | 
 | 
|---|
| 38 |   if (!hasMean && !hasVar) {
 | 
|---|
| 39 |     cerr << " MeanVarEstimator::run() - No Output TOI connected! "
 | 
|---|
| 40 |          << endl;
 | 
|---|
| 41 |     throw ParmError("MeanVarEstimator::run() No output TOI connected!");
 | 
|---|
| 42 |   }
 | 
|---|
| 43 |   
 | 
|---|
| 44 |   if (sne-snb < nsamples) {
 | 
|---|
| 45 |     cerr << "MeanVarEstimator::run() -  sne-snb < nsamples!" << endl;
 | 
|---|
| 46 |     throw ParmError("MeanVarEstimator::run() -  sne-snb < nsamples!");
 | 
|---|
| 47 |   }
 | 
|---|
| 48 |   
 | 
|---|
| 49 |   SLinParBuff slb(nsamples, 100, 0., 0., true);    
 | 
|---|
| 50 |   
 | 
|---|
| 51 |   for (int sn=snb; sn<=sne; sn++) {
 | 
|---|
| 52 |     r_8 sg; uint_8 fg=0;
 | 
|---|
| 53 |     getData(0,sn,sg,fg);
 | 
|---|
| 54 |     
 | 
|---|
| 55 |     if (fg&flgNotLookAt) slb.Pop();
 | 
|---|
| 56 |     else slb.Push((r_8)sn,sg);
 | 
|---|
| 57 | 
 | 
|---|
| 58 |     r_8 mean;
 | 
|---|
| 59 |     r_8 sig = slb.Compute(mean);
 | 
|---|
| 60 |     
 | 
|---|
| 61 |     if (hasMean)
 | 
|---|
| 62 |       putData(0, sn, mean, fg);
 | 
|---|
| 63 |     if (hasVar)
 | 
|---|
| 64 |       putData(1, sn, sig*sig, fg);
 | 
|---|
| 65 |   }
 | 
|---|
| 66 | }
 | 
|---|
| 67 | 
 | 
|---|