RdChannelBandstopFilter.cc
Go to the documentation of this file.
2 
3 #include <fwk/CentralConfig.h>
4 
5 #include <utl/config.h>
6 #include <utl/ErrorLogger.h>
7 #include <utl/Reader.h>
8 #include <utl/TraceAlgorithm.h>
9 
10 #include <evt/Event.h>
11 #include <revt/REvent.h>
12 #include <revt/Station.h>
13 
14 #include <det/Detector.h>
15 #include <rdet/RDetector.h>
16 #include <rdet/Station.h>
17 
18 
19 using namespace revt;
20 using namespace fwk;
21 using namespace std;
22 using namespace utl;
23 
24 
26 
29  {
30  Branch topBranch = CentralConfig::GetInstance()->GetTopBranch("RdChannelBandstopFilter");
31  topBranch.GetChild("InfoLevel").GetData(fInfoLevel);
32  topBranch.GetChild("LowerBoundsNS").GetData(fLowerBoundsNS);
33  topBranch.GetChild("UpperBoundsNS").GetData(fUpperBoundsNS);
34  topBranch.GetChild("LowerBoundsEW").GetData(fLowerBoundsEW);
35  topBranch.GetChild("UpperBoundsEW").GetData(fUpperBoundsEW);
36  topBranch.GetChild("UseOnlineBandstopFilter").GetData(fUseOnlineBandstopFilter);
37  topBranch.GetChild("OnlineBandstopFilterSpectrumBaseline").GetData(fOnlineBandstopFilterSpectrumBaseline);
38  topBranch.GetChild("OnlineBandstopFilterDeviationFactor").GetData(fOnlineBandstopFilterDeviationFactor);
39 
40  // check for valid bounds in xml
41  if (fLowerBoundsNS.size() != fUpperBoundsNS.size()) {
42  ERROR("not same amount of upper/lower bounds in NS channel");
43  return eFailure;
44  }
45 
46  for(unsigned int i = 0; i < fLowerBoundsNS.size(); ++i) {
47  if(fLowerBoundsNS[i] > fUpperBoundsNS[i]) {
48  ERROR("NoiseStartFrequency greater than NoiseStopFrequency for NS channel");
49  return eFailure;
50  }
51  }
52 
53  if (fLowerBoundsEW.size() != fUpperBoundsEW.size()) {
54  ERROR("not same amount of upper/lower bounds in EW channel");
55  return eFailure;
56  }
57 
58  for(unsigned int i = 0; i < fLowerBoundsEW.size(); ++i) {
59  if(fLowerBoundsEW[i] > fUpperBoundsEW[i]) {
60  ERROR("NoiseStartFrequency greater than NoiseStopFrequency for EW channel 2");
61  return eFailure;
62  }
63  }
64 
65  return eSuccess;
66  }
67 
68 
70  RdChannelBandstopFilter::Run(evt::Event& event)
71  {
72  // Check if there are events at all
73  if(!event.HasREvent()) {
74  WARNING("No radio event found!");
75  return eContinueLoop;
76  }
77 
78  REvent& rEvent = event.GetREvent();
79  ostringstream info;
80 
81  // loop through stations and for every station through every channel
82  for (auto& station : rEvent.StationsRange()) {
83  for (auto& channel : station.ChannelsRange()) {
84 
85  if (!channel.IsActive())
86  continue;
87 
88  const rdet::RDetector& rDetector = det::Detector::GetInstance().GetRDetector();
89  const rdet::Channel& detChannel = rDetector.GetStation(station.GetId()).GetChannel(channel.GetId());
90 
91  // Work on the frequency spectrum of this channel
92  ChannelFFTDataContainer& myFFTDataContainer = channel.GetFFTDataContainer();
93  ChannelFrequencySpectrum& freqSpectrum = myFFTDataContainer.GetFrequencySpectrum();
94 
95  // online filtering for time-varying narrow-band RFI
96  if (fUseOnlineBandstopFilter) {
97  Trace<double> amplitude;
98  for (const auto& freq : freqSpectrum)
99  amplitude.PushBack(abs(freq));
100 
101  // get design frequencies
102  const double lowerfreq = detChannel.GetDesignLowerFreq();
103  const double upperfreq = detChannel.GetDesignUpperFreq();
104 
105  const unsigned int idxLowFreq = myFFTDataContainer.GetBinOfFrequency(lowerfreq);
106  const unsigned int idxUpFreq = myFFTDataContainer.GetBinOfFrequency(upperfreq);
107 
108  double baseline = 0; // fOnlineBandstopFilterSpectrumBaseline == "Zero"
109  if (fOnlineBandstopFilterSpectrumBaseline == "Mean")
110  baseline = TraceAlgorithm::Mean(amplitude, idxLowFreq, idxUpFreq);
111  else if (fOnlineBandstopFilterSpectrumBaseline == "Median")
112  baseline = TraceAlgorithm::Median(amplitude, idxLowFreq, idxUpFreq, 50);
113 
114  const double deviation = TraceAlgorithm::StandardDeviation(amplitude, idxLowFreq, idxUpFreq);
115 
116  info.str("");
117  info << "Channel " << channel.GetId()
118  << " in Station " << channel.GetStationId()
119  << " has a baseline of: " << baseline
120  << " (" << fOnlineBandstopFilterSpectrumBaseline << ") "
121  << " and a std. deviation of: " << deviation
122  << " with a deviation factor of: " << fOnlineBandstopFilterDeviationFactor << ".";
123  INFODebug(info);
124 
125  for (auto& frequency : freqSpectrum) {
126  if (abs(frequency) > baseline + fOnlineBandstopFilterDeviationFactor * deviation) {
127  info.str("");
128  info << "Suppress frequency " << frequency / MHz
129  << " of Channel " << channel.GetId()
130  << " in Station " << channel.GetStationId()
131  << " with a deviation factor of " << abs(abs(frequency) - baseline) / deviation
132  << ".";
133  INFOIntermediate(info);
134  frequency = 0;
135  }
136  }
137  }
138 
139  //fixme TH: frequencies specified in xml file are always suppressed in addition to the dynamic filter!?
140  CutNoiseFrequencies(channel, detChannel);
141  }
142  }
143 
144  return eSuccess;
145  }
146 
147 
149  RdChannelBandstopFilter::Finish()
150  {
151  return eSuccess;
152  }
153 
154 
155  void
156  RdChannelBandstopFilter::CutNoiseFrequencies(revt::Channel& channel, const rdet::Channel& detChannel)
157  {
158  // get orientation of channel
159  vector<double> lowerBounds, upperBounds;
160  if (CloseTo(detChannel.GetOrientationAzimuth(), fNorthSouth, fTolerance)) {
161  // North-South channel
162  lowerBounds = fLowerBoundsNS;
163  upperBounds = fUpperBoundsNS;
164  } else if (CloseTo(detChannel.GetOrientationAzimuth(), fEastWest, fTolerance)) {
165  // East-West channel
166  lowerBounds = fLowerBoundsEW;
167  upperBounds = fUpperBoundsEW;
168  } else {
169  INFOIntermediate("Channel not NS or EW oriented, not cutting out frequencies");
170  return;
171  }
172 
173  ostringstream info;
174 
175  ChannelFFTDataContainer& fftContainer = channel.GetFFTDataContainer();
176  ChannelFrequencySpectrum& freqSpectrum = fftContainer.GetFrequencySpectrum();
177 
178  for(unsigned int i = 0; i < lowerBounds.size(); ++i) {
179  const int idxNoiseStart = fftContainer.GetBinOfFrequency(lowerBounds[i]);
180  const int idxNoiseStop = fftContainer.GetBinOfFrequency(upperBounds[i]);
181 
182  info << "Cut out noise frequency from " << lowerBounds[i] / MHz << " MHz to "
183  << upperBounds[i] / MHz << " MHz (Bin " << idxNoiseStart<< " to " << idxNoiseStop
184  << ") in Channel " << channel.GetId() << endl;
185  INFOIntermediate(info);
186 
187  for(int j=idxNoiseStart; j<=idxNoiseStop; ++j) {
188  freqSpectrum[j] = 0;
189  }
190  }
191  }
192 
193 
194  bool
195  RdChannelBandstopFilter::CloseTo(const double a, const double b, const double tolerance)
196  {
197  return fabs(a - b) < tolerance;
198  }
199 
200 }
ChannelFFTDataContainer & GetFFTDataContainer()
retrieve Channel FFTDataContainer (write access)
Branch GetTopBranch() const
Definition: Branch.cc:63
double StandardDeviation(const std::vector< double > &v, const double mean)
Definition: Functions.cc:26
int GetId() const
Return Id of the Channel.
double GetDesignUpperFreq() const
Get design value of the freq-band.
constexpr double MHz
Definition: AugerUnits.h:159
int freq
Definition: dump1090.h:244
Interface class to access to the Radio part of an event.
Definition: REvent.h:42
double GetDesignLowerFreq() const
Get design value of the freq-band.
void Init()
Initialise the registry.
Detector description interface for Channel-related data.
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
Definition: Branch.cc:211
bool HasREvent() const
Detector description interface for RDetector-related data.
Definition: RDetector.h:46
#define INFOIntermediate(y)
Definition: VModule.h:162
Class representing a document branch.
Definition: Branch.h:107
C< F > & GetFrequencySpectrum()
read out the frequency spectrum (write access)
double abs(const SVector< n, T > &v)
#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
Class that holds the data associated to an individual radio channel.
double Mean(const std::vector< double > &v)
Definition: Functions.h:31
void PushBack(const T &value)
Insert a single value at the end.
Definition: Trace.h:119
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
const Station & GetStation(const int stationId) const
Get station by Station Id.
Definition: RDetector.cc:141
double GetOrientationAzimuth() const
Get azimuth-direction of Antenna for this Channel.
Predicate for approximate equality (for floating point)
Definition: Test.h:91

, generated on Tue Sep 26 2023.