RdChannelMedianFilter.cc
Go to the documentation of this file.
2 
3 #include <fwk/CentralConfig.h>
4 
5 #include <utl/ErrorLogger.h>
6 #include <utl/Reader.h>
7 #include <utl/config.h>
8 #include <utl/TraceAlgorithm.h>
9 
10 #include <evt/Event.h>
11 #include <revt/REvent.h>
12 #include <revt/Station.h>
13 #include <revt/Channel.h>
14 
15 
16 using namespace fwk;
17 using namespace utl;
18 using namespace std;
19 using namespace revt;
20 
21 
23 
26  {
27  Branch topBranch = CentralConfig::GetInstance()->GetTopBranch("RdChannelMedianFilter");
28  topBranch.GetChild("InfoLevel").GetData(fInfoLevel);
29  topBranch.GetChild("FilterBandwidth").GetData(fFilterBandwidth);
30  topBranch.GetChild("FilterSizeBins").GetData(fFilterSizeBins);
31  topBranch.GetChild("MedianSortAlgorithmLimit").GetData(fMedianSortAlgorithmLimit);
32 
33  return eSuccess;
34  }
35 
36 
38  RdChannelMedianFilter::Run(evt::Event& event)
39  {
40  // Check if there are events at all
41  if(!event.HasREvent()) {
42  WARNING("No radio event found!");
43  return eContinueLoop;
44  }
45 
46  REvent& rEvent = event.GetREvent();
47  ostringstream info;
48 
49  // loop through stations and for every station through every channel
50  for (auto& station : rEvent.StationsRange()) {
51  for (auto& channel : station.ChannelsRange()) {
52 
53  if (!channel.IsActive())
54  continue;
55 
56  info.str("");
57  info << "Supressing RFI of channel " << channel.GetId()
58  << " of station " << channel.GetStationId() << ".";
59  INFODebug(info);
60 
61  // Work on the frequency spectrum of this channel
62  revt::ChannelFrequencySpectrum& spectrum = channel.GetChannelFrequencySpectrum();
63  MedianFilter(spectrum);
64  }
65  }
66 
67  return eSuccess;
68  }
69 
70 
72  RdChannelMedianFilter::Finish()
73  {
74  return eSuccess;
75  }
76 
77 
78  void
79  RdChannelMedianFilter::MedianFilter(revt::ChannelFrequencySpectrum& spectrum)
80  {
81  // get the amplitude
82  Trace<double> amplitude;
83  for (const auto& val : spectrum)
84  amplitude.PushBack(abs(val));
85 
86  // check if filter bandwidth was provided instead of a filter size in bins,
87  // and calculate size in bins from bandwidth
88  // use provided size in bins as default, and modify if bandwidth > 0
89  unsigned int filterSizeBins = fFilterSizeBins;
90  if (fFilterBandwidth > 0) {
91  filterSizeBins = trunc(fFilterBandwidth/spectrum.GetBinning()/2.) * 2 + 1;
92 
93  ostringstream info;
94  info << "Use median filter size of " << filterSizeBins
95  << " bins, corresponding to a bandwidth of "
96  << filterSizeBins*spectrum.GetBinning()/megahertz << " MHz.";
97  INFOIntermediate(info);
98  }
99 
100  // get the filter size and check if it is small enough
101  if (filterSizeBins > amplitude.GetSize()) {
102  ostringstream info;
103  info << "Size of median filter (" << fFilterSizeBins
104  << " samples) should not be larger than the size of the spectrum ("
105  << amplitude.GetSize() << " samples)";
106  WARNING(info);
107  filterSizeBins = amplitude.GetSize();
108  }
109 
110  // index for looping through the spectrum
112 
113  // use constant median for the first part of the trace
114  double median = TraceAlgorithm::Median(amplitude, 0, filterSizeBins-1, fMedianSortAlgorithmLimit);
115 
116  // adjust amplitude of the spectrum to the median amplitude
117  // if the amplitude of a frequency bin is 0 it means that the
118  // phase is abitrary and must be defined while setting the amplitude
119  // to the median. At the moment phase = 0 is used,
120  // but an interpolation might be better.
121  // For real spectra there are hopefully no frequencies where the
122  // amplitude is exactly 0, so the problem should not be severe.
123  for (i = 0; i < ceil(filterSizeBins/2.0); ++i)
124  if (abs(spectrum[i]) > 0)
125  spectrum[i] *= median/abs(spectrum[i]);
126  else spectrum[i] = median;
127 
128  // use gliding median for the middle part
129  for(;i < (spectrum.GetSize()-ceil(filterSizeBins/2.0)); ++i)
130  if (abs(spectrum[i]) > 0)
131  spectrum[i] *=
132  TraceAlgorithm::Median(amplitude, i-trunc(filterSizeBins/2.0),
133  i+ceil(filterSizeBins/2.0)-1, fMedianSortAlgorithmLimit)/abs(spectrum[i]);
134  else
135  spectrum[i] =
136  TraceAlgorithm::Median(amplitude, i-trunc(filterSizeBins/2.0),
137  i+ceil(filterSizeBins/2.0)-1, fMedianSortAlgorithmLimit);
138 
139  // use constant median for the end of the spectrum
140  median = TraceAlgorithm::Median(amplitude, spectrum.GetSize()-filterSizeBins,
141  spectrum.GetSize()-1, fMedianSortAlgorithmLimit);
142  for(;i < spectrum.GetSize(); ++i)
143  if (abs(spectrum[i]) > 0)
144  spectrum[i] *= median/abs(spectrum[i]);
145  else spectrum[i] = median;
146  }
147 
148 }
Interface class to access to the Radio part of an event.
Definition: REvent.h:42
void Init()
Initialise the registry.
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
Definition: Branch.cc:211
bool HasREvent() const
#define INFOIntermediate(y)
Definition: VModule.h:162
Class representing a document branch.
Definition: Branch.h:107
std::vector< std::complex< double > >::size_type SizeType
Definition: Trace.h:58
double abs(const SVector< n, T > &v)
constexpr double megahertz
Definition: AugerUnits.h:155
SizeType GetSize() const
Definition: Trace.h:156
#define WARNING(message)
Macro for logging warning messages.
Definition: ErrorLogger.h:163
void GetData(bool &b) const
Overloads of the GetData member template function.
Definition: Branch.cc:644
#define INFODebug(y)
Definition: VModule.h:163
ResultFlag
Flag returned by module methods to the RunController.
Definition: VModule.h:60
static CentralConfig * GetInstance()
Use this the first time you get an instance of central configuration.
void PushBack(const T &value)
Insert a single value at the end.
Definition: Trace.h:119
utl::Branch GetTopBranch(const std::string &id)
Get top branch for moduleConfigLink with given id (XML files)

, generated on Tue Sep 26 2023.