source: Sophya/trunk/AddOn/TAcq/brprocGain.cc@ 3992

Last change on this file since 3992 was 3992, checked in by campagne, 14 years ago

halfmedwith poru le filtrage des freq. TIMWIN pour tagger les fenetres des paquets

File size: 6.3 KB
Line 
1//----------------------------------------------------------------
2// Projet BAORadio - (C) LAL/IRFU 2008-2010
3// Classes de threads de traitement pour BAORadio
4//----------------------------------------------------------------
5
6#include <stdlib.h>
7#include <string.h>
8#include <unistd.h>
9#include <fstream>
10#include <signal.h>
11#include <algorithm>
12
13#include "pexceptions.h"
14#include "tvector.h"
15#include "ntuple.h"
16#include "datatable.h"
17#include "histos.h"
18#include "fioarr.h"
19#include "matharr.h"
20#include "timestamp.h"
21#include "ctimer.h"
22#include "fftpserver.h"
23#include "fitsarrhand.h"
24
25#include "FFTW/fftw3.h"
26
27
28#include "pciewrap.h"
29#include "brpaqu.h"
30
31#include "brprocGain.h"
32
33
34
35
36//---------------------------------------------------------------------
37// Classe de traitement de spectres -
38// Calcul de spectres de gain
39//---------------------------------------------------------------------
40/* --Methode-- */
41BRGainCalculator::BRGainCalculator(RAcqMemZoneMgr& memgr, string outpath, uint_4 nmean,
42 bool fgdatafft, bool fgsinglechan)
43 : BRMeanSpecCalculator(memgr,outpath,nmean,fgdatafft,fgsinglechan), forceMedianFreqFilter_(false), medianFilterHalfWidth_(50), nbwin4mean_(nmean)
44{
45
46 // cout << "(JEC): BRGainCalculator Ctor " << endl;
47
48
49 setNameId("gainCalc",1);
50
51 uint_4 nb_octets_entrop = 0; //this value is valid for Dec. 2010 data at Nancay
52
53 const char* venvp = NULL;
54 venvp=getenv("BRANA_NBYTECUT");
55 if (venvp!=NULL){
56 nb_octets_entrop = atoi(venvp);
57 cout << "BRGainCalculator : BRANA_NBYTECUT : " << nb_octets_entrop << endl;
58 }
59
60 BRPaquet paq(memgr_.PaqSize()-nb_octets_entrop);
61
62 if (fgsinglechannel_) {
63 medfiltspecmtx_.SetSize(memgr_.NbFibres(), paq.DataSize()/2);
64 }
65 else {
66 medfiltspecmtx_.SetSize(2*memgr_.NbFibres(), paq.DataSize()/4);
67 }
68 //init
69 medfiltspecmtx_ = (r_4)(0.);
70 nummedianfile_ = 0;
71 nbtot_specwin_ = 0;
72 windowTime_ = TimeStamp(); //default
73
74}
75
76
77/* --Methode-- */
78BRGainCalculator::~BRGainCalculator()
79{
80 cout << "(JEC): BRGainCalculator Dtor: nbtot_specwin = " << nbtot_specwin_
81 << "nbwin4mean = " << nbwin4mean_ << endl;
82 if(nbtot_specwin_>nbwin4mean_*0.5)SaveMedianSpectra();
83}
84
85
86
87/* --Methode-- */
88void BRGainCalculator::ProcSpecWin(uint_8 numpaqstart, uint_8 numpaqend)
89{
90
91 // cout << "(JEC): BRGainCalculator ProcSpecWin: "<< nbtot_specwin_
92 // << endl;
93
94
95
96 //JEC 13/12/10 save the filtered spectra
97 if ( (nbtot_specwin_>0)&&(nbtot_specwin_%nbwin4mean_==0) ) SaveMedianSpectra();
98
99 // Get time of window
100 windowTime_ = getObsTime();
101
102 if (prtlev_>1) {
103 cout << " BRGainCalculator::ProcSpecWin() num_win=" << nbtot_specwin_ << " numpaqstart=" << numpaqstart
104 << " numpaqend=" << numpaqend << " nbwin4mean = " << nbwin4mean_ << endl;
105 cout << " (1) ObsTime=" << windowTime_ << " TimeTag=" << getCurTimeTagSeconds() << " s. FrameCounter="
106 << getCurFrameCounter() << endl;
107 }
108 //DBG cout << "BRGainCalculator::ProcSpecWin()/Debug: numpaqstart=" << numpaqstart
109 //DBG << " numpaqend=" << numpaqend << endl;
110
111
112 //Implement the median filter at a fixed freq. but on all paquets of the window
113 //then apply a median filter on the frequencies
114 for(sa_size_t i=0; i<spec_window_.SizeY(); i++) { //loop on channel (voie)
115 TVector<r_4> medianFilterPaq(spec_window_.SizeX());
116 //we keep freq. = 0 for compatibility
117 for (sa_size_t f=0; f<spec_window_.SizeX(); f++) {
118 std::vector<r_4> val1;
119 TVector<r_4> tval1(spec_window_(Range(f),Range(i),Range::all()).CompactAllDimensions());
120 tval1.FillTo(val1);
121 std::nth_element(val1.begin(),
122 val1.begin()+val1.size()/2,
123 val1.end());
124 medianFilterPaq(f) = *(val1.begin()+val1.size()/2);
125 }//eo loop on frequencies
126
127
128
129 if(forceMedianFreqFilter_) {
130 //perform a median filter on the frequencies
131 TVector<r_4> medianFilterFreq = medfiltspecmtx_.Row(i);
132 sa_size_t hww = medianFilterHalfWidth_; //half freq window for filtering
133 sa_size_t fMin = 0.;
134 sa_size_t fMax = spec_window_.SizeX()-1;
135 for (sa_size_t f=0; f<spec_window_.SizeX(); f++) {
136 sa_size_t first = (f-hww >= fMin) ? f-hww : fMin;
137 sa_size_t last = (f+hww <= fMax) ? f+hww : fMax;
138 std::vector<r_4> val2;
139 medianFilterPaq(Range(first,last)).FillTo(val2);
140 std::nth_element(val2.begin(),
141 val2.begin()+val2.size()/2,
142 val2.end());
143 medianFilterFreq(f) = *(val2.begin()+val2.size()/2); //Notice the "+" for later take the mean
144 }//eo loop on frequencies
145 } else {
146 // cout<< "(JEC) ProcSpecWin debug medfiltspecmtx/medianFilterPaq "<< endl;
147 // medfiltspecmtx_.Row(i).Show();
148 // medianFilterPaq.Transpose().Show();
149 medfiltspecmtx_.Row(i) += medianFilterPaq.Transpose();
150 }
151 }//eo loop on channels
152
153 //We always end here with a medfiltspecmtx Matrix Row=[0,NCha-1] x Col=[0,fMax]
154
155
156 if (nbtot_specwin_<nmaxfiles_specw_) SaveSpectraWindow();
157
158
159 nbtot_specwin_++;
160 return;
161}
162
163
164/* --Methode-- */
165//JEC 13/12/10 Start
166void BRGainCalculator::SaveMedianSpectra()
167{
168
169 cout << "(JEC): BRGainCalculator SaveMedianSpectra ("<<nummedianfile_<<") nbwin = "<< nbwin4mean_
170 << " nbtot_specwin = " << nbtot_specwin_
171 << endl;
172 if (prtlev_>1) {
173 cout << " BRGainCalculator:SaveMedianSpectra() nbwin4mean = " << nbwin4mean_ << endl
174 << " (2) ObsTime=" << windowTime_ << " Total Intensity " << 0.5*medfiltspecmtx_.Sum() << endl;
175 }
176
177 //JEC compute power spectrum
178
179
180
181
182 for(sa_size_t ir=0; ir< medfiltspecmtx_.NRows(); ir++){
183 char buff[32];
184 sprintf(buff,"NPAQSUM_%d",(int)ir);
185 medfiltspecmtx_.Info()["NPAQSUM"] = nbtot_specwin_;
186 medfiltspecmtx_.Info()[buff] = nbtot_specwin_;
187 //JEC timeStamp in the day @ millisec: faudra gerer le passage a minuit!
188 medfiltspecmtx_.Info()["TIMEWIN"] = (windowTime_.SecondsPart())*1000.;
189
190
191 if ( nbtot_specwin_ > 0) {
192 medfiltspecmtx_.Row(ir) /= (r_4)nbtot_specwin_;
193 }
194 }//eo loop on channels
195 char nfile[64];
196 string flnm;
197 {
198 sprintf(nfile,"medfiltmtx%d.fits",nummedianfile_);
199 flnm="!"+outpath_+nfile;
200 FitsInOutFile fos(flnm,FitsInOutFile::Fits_Create);
201 fos << medfiltspecmtx_;
202 cout << nummedianfile_ << "-BRGainCalculator::ProcSpecWin() save filtered spectra matrix in "
203 << flnm << endl;
204 }
205
206 //increment the file index
207 nummedianfile_++;
208
209 //reset the matirx
210 medfiltspecmtx_ = (r_4)(0.);
211
212 //reset counter
213 nbtot_specwin_ = 0;
214
215 return;
216}
217//JEC 13/12/10 End
Note: See TracBrowser for help on using the repository browser.