source: BAORadio/AmasNancay/trunk/medianFilter.cc @ 675

Last change on this file since 675 was 641, checked in by campagne, 12 years ago

new algo for spiapp (jec)

File size: 4.6 KB
Line 
1#include "machdefs.h"
2
3//---- System et stdc++ include files
4#include <stdio.h>
5#include <stdlib.h>
6#include <math.h>
7#include <ctype.h>
8#include <string.h>
9#include <iostream>
10#include <fstream>
11#include <complex>
12
13#include <typeinfo>
14#include <string>
15#include <vector>
16#include <map>
17#include <functional>
18#include <list>
19
20//---- Sophya include files
21#include "sopnamsp.h"
22#include "basetools.h"
23#include "systools.h"
24#include "sutils.h"
25#include "ntools.h"
26#include "array.h"
27#include "histats.h"
28
29//---- Spiapp include files
30#include "nobjmgr.h"
31#include "servnobjm.h"
32
33//---- Include files from additionnal modules
34
35//---- function to compare bits on double
36int_8 BitCmp64(double v,int_8 flg) 
37{return ((int_8)((v<0.) ? v-0.1 : v+0.1))&flg;} 
38
39//---- function for Adding and displaying Objects 
40void Keep_Object(AnyDataObj & obj, string const & nom) 
41{ 
42  string name = nom; 
43  NamedObjMgr om; 
44  if (om.GetObj(name)) 
45    cerr << "KeepObj()/Warning Already kept object " << endl; 
46  else om.AddObj(obj, name); 
47} 
48
49void Display_Object(AnyDataObj & obj, string const & opt, string const & nom) 
50 { 
51  string name = nom; 
52  NamedObjMgr om; 
53  if (!om.GetObj(name)) 
54    om.AddObj(obj, name); 
55  om.DisplayObj(name, opt); 
56} 
57
58//---- function for getting and setting ObjectManager variables 
59void Set_ObjMgrVar(MuTyV v, string const & nom) 
60{ 
61  NamedObjMgr om; 
62  om.SetVar(nom, (string)v); 
63} 
64
65MuTyV Get_ObjMgrVar(const char * nom) 
66{ 
67  string name = nom; NamedObjMgr om; 
68  MuTyV v = om.GetVar(name); 
69  return v; 
70} 
71
72//---- Macro for Objects and variables saving
73#define KeepObj(obj) Keep_Object(obj, #obj)
74#define GetOMVar(var) Get_ObjMgrVar( #var )
75#define SetOMVar(var) Set_ObjMgrVar(var, #var )
76
77//---- Macro Displaying objects and command execution
78#define DispObj(obj, att) Display_Object(obj, att, #obj);
79
80#define ExecCmd(cmd) srvo.ExecuteCommand(cmd);
81
82
83
84//-------------------------------------------------//
85//----------------- User Functions ----------------//
86//-------------------------------------------------//
87
88extern "C" {
89  int doFilter( vector<string>& args );
90}
91
92template<class RandAccessIter>
93double median(RandAccessIter begin, RandAccessIter end) {
94  std::size_t size = end - begin;
95  std::size_t middleIdx = size/2;
96  RandAccessIter target = begin + middleIdx;
97  std::nth_element(begin, target, end);
98   
99  if(size % 2 != 0){ //Odd number of elements
100    return *target;
101  }else{            //Even number of elements
102    double a = *target;
103    RandAccessIter targetNeighbor= target-1;
104    std::nth_element(begin, targetNeighbor, end);
105    return (a+*targetNeighbor)/2.0;
106  }
107}
108
109
110void medianFiltering(const TMatrix<r_4> mtx, 
111                     sa_size_t halfwidth,
112                     TMatrix<r_4>& vec) {
113 
114  sa_size_t nr = mtx.NRows();
115  sa_size_t nc = mtx.NCols();
116  sa_size_t chMin = 0;
117  sa_size_t chMax = nc-1;
118 
119  for (sa_size_t ir=0; ir<nr; ir++){
120    for (sa_size_t ic=0; ic<nc; ic++) {
121      sa_size_t chLow = ic-halfwidth;
122      chLow = (chLow >= chMin) ? chLow : chMin;
123      sa_size_t chHigh = ic+halfwidth;
124      chHigh = ( chHigh <= chMax ) ? chHigh : chMax;
125      TVector<r_4> tmp(mtx(Range(ir),Range(chLow,chHigh)),false);
126      vector<r_4> val;
127      tmp.FillTo(val);
128      vec(ir,ic) = median(val.begin(),val.end());
129    }
130  }
131}
132
133
134int doFilter( vector<string>& args )
135{
136// Some definitions to help using spiapp;
137NamedObjMgr omg;
138Services2NObjMgr& srvo = *omg.GetServiceObj();
139
140//-------------- Object List --------------
141//Number of objects = 2
142string ___nomobj;
143
144___nomobj = "specv0";
145TVector< r_4 > * ___specv0 = dynamic_cast< TVector< r_4 >  * >(omg.GetObj(___nomobj));
146if(___specv0==NULL) throw NullPtrError("CxxExecutor::PutObject: Non existing object specv0... please update file");
147TVector< r_4 > & specv0 = (*___specv0);
148
149___nomobj = "specv1";
150TVector< r_4 > * ___specv1 = dynamic_cast< TVector< r_4 >  * >(omg.GetObj(___nomobj));
151if(___specv1==NULL) throw NullPtrError("CxxExecutor::PutObject: Non existing object specv1... please update file");
152TVector< r_4 > & specv1 = (*___specv1);
153
154
155
156//--------------------------------------------//
157//----------------- User Code ----------------//
158//--------------------------------------------//
159
160 sa_size_t NUMBER_OF_CHANNELS = 2;
161 sa_size_t NUMBER_OF_FREQ = 8192;
162 TMatrix<r_4> specMtxInPut(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
163 specMtxInPut.Row(0)=specv0;
164 specMtxInPut.Row(1)=specv1;
165
166 TMatrix<r_4> specMtxOutPut(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
167 
168 sa_size_t halfWidth=10;
169 medianFiltering(specMtxInPut,halfWidth,specMtxOutPut);
170
171 TVector<r_4>sfilt0=specMtxOutPut.Row(0).CompactAllDimensions();
172 TVector<r_4>sfilt1=specMtxOutPut.Row(1).CompactAllDimensions();
173 KeepObj(sfilt0);
174 KeepObj(sfilt1);
175 
176 
177 return 0;
178}
Note: See TracBrowser for help on using the repository browser.