SpectralKurtosis.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2016 by Andrew Jameson and Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 #include "dsp/Transformation.h"
10 #include "dsp/WeightedTimeSeries.h"
11 #include "dsp/BitSeries.h"
12 #include "dsp/Memory.h"
13 #include "EventEmitter.h"
14 
15 #ifndef __SpectralKurtosis_h
16 #define __SpectralKurtosis_h
17 
18 #define ZAP_ALL 0
19 #define ZAP_SKFB 1
20 #define ZAP_FSCR 2
21 #define ZAP_TSCR 3
22 
23 namespace dsp {
24 
25  class SKLimits;
26 
28 
30  class SpectralKurtosis: public Transformation<TimeSeries,TimeSeries> {
31 
32  public:
33 
36 
39 
40  bool get_order_supported (TimeSeries::Order order) const;
41 
43  void load_configuration (const std::string& filename);
44 
45  void set_M (unsigned _M) { resolution[0].set_M(_M); }
46  void set_M (const std::vector<unsigned>&);
47 
49  void set_noverlap (unsigned _nover) { resolution[0].set_noverlap(_nover); }
50  void set_noverlap (const std::vector<unsigned>&);
51 
53  void set_thresholds (float _std_devs);
54  void set_thresholds (const std::vector<float>&);
55 
57  void set_detect_time_freq (bool flag=true) { detect_time_freq = flag; }
58 
60  void set_detect_freq (bool flag=true) { detect_freq = flag; }
61 
63  void set_detect_time (bool flag=true) { detect_time = flag; }
64 
66 
67  void set_omit_outliers (bool flag=true) { omit_outliers = flag; }
68 
70  unsigned get_nchan () const { return nchan; }
71 
73  unsigned get_npol () const { return sums_npol; }
74 
76  void set_channel_range (unsigned start, unsigned end);
77 
78  void reserve ();
79 
80  void prepare ();
81 
82  void prepare_output ();
83 
85  double get_delay_time () const override;
86 
88  unsigned get_M () const
89  { return resolution[0].get_M(); }
90 
92  unsigned get_excision_threshold () const
93  { return resolution[0].get_std_devs(); }
94 
96  void get_filtered_sum (std::vector<float>& sum) const
97  { sum = filtered_sum; }
98 
100  void get_filtered_hits (std::vector<uint64_t>& hits) const
101  { hits = filtered_hits; }
102 
104  void get_unfiltered_sum (std::vector<float>& sum) const
105  { sum = unfiltered_sum; }
106 
108  uint64_t get_unfiltered_hits () const { return unfiltered_hits; }
109 
111  void reset_count () { unfiltered_hits = 0; }
112 
114  class Engine;
115 
116  void set_engine (Engine*);
117 
118  template<class T>
119  class Reporter {
120  public:
121  virtual void operator() (T*, unsigned, unsigned, unsigned, unsigned) {};
122  };
123 
124  // An event emitter that takes a data array, and the nchan, npol, ndat and ndim
125  // associated with the data array
126  EventEmitter<Reporter<float> > float_reporter;
127 
128  // This is for reporting the state of the bit zapmask
129  EventEmitter<Reporter<unsigned char> > char_reporter;
130 
131  bool get_report () const { return report; }
132 
133  void set_report (bool _report) { report = _report; }
134 
136  bool has_zero_DM_input () const;
137  virtual void set_zero_DM_input (TimeSeries* zero_DM_input);
138  virtual const TimeSeries* get_zero_DM_input() const;
139  virtual TimeSeries* get_zero_DM_input();
140 
141  // bool has_zero_DM_input_container () const;
142  // virtual void set_zero_DM_input_container (const HasInput<TimeSeries> zero_DM_input_container&);
143  // virtual const HasInput<TimeSeries>& get_zero_DM_input_container() const;
144  // virtual HasInput<TimeSeries>& get_zero_DM_input_container();
145 
146  virtual void set_zero_DM_buffering_policy (BufferingPolicy* policy)
147  { zero_DM_buffering_policy = policy; }
148 
149  bool has_zero_DM_buffering_policy() const
150  { return zero_DM_buffering_policy; }
151 
152  BufferingPolicy* get_zero_DM_buffering_policy () const
153  { return zero_DM_buffering_policy; }
154 
156  bool get_zero_DM () const { return zero_DM; }
157 
159  void set_zero_DM (bool _zero_DM) { zero_DM = _zero_DM; }
160 
161  protected:
162 
164  void transformation ();
165 
168 
169  private:
170 
171  void compute ();
172 
173  void detect ();
174  void detect_tscr ();
175  void detect_skfb (unsigned ires);
176  void detect_fscr (unsigned ires);
177  void count_zapped ();
178 
179  void mask ();
180  void reset_mask ();
181 
182  void insertsk ();
183 
184  unsigned debugd = 1;
185 
186  class Count
187  {
188  public:
189  uint64_t tested = 0;
190  uint64_t zapped = 0;
191  double fraction_zapped () const { return (tested) ? double(zapped)/tested : 0.0; }
192  const Count& operator+= (const Count& that) { tested+=that.tested; zapped+=that.zapped; return *this;}
193  };
194 
195  class Resolution
196  {
197  private:
198 
200  mutable std::vector<bool> channels;
201 
203  mutable std::vector<float> thresholds;
204 
206  unsigned M = 128;
207 
209  unsigned Nd = 1;
210 
212  float std_devs = 3.0;
213 
215  unsigned noverlap = 1;
216 
218  unsigned overlap_offset = 128;
219 
221  uint64_t npart = 0;
222 
224  uint64_t output_ndat = 0;
225 
227  Count SK_time_freq;
228 
230  Count SK_time;
231 
233  void set_thresholds (bool verbose = false) const;
234 
235  public:
236 
238 
239  void add_include (const std::pair<unsigned, unsigned>&);
240 
242 
243  void add_exclude (const std::pair<unsigned, unsigned>&);
244 
246  const std::vector<bool>& get_channels (unsigned nchan) const;
247 
249  unsigned get_M () const { return M; }
250  void set_M (unsigned);
251 
253  unsigned get_Nd () const { return Nd; }
254  void set_Nd (unsigned);
255 
257  float get_std_devs () const { return std_devs; }
258  void set_std_devs (float);
259 
261  /* blocks used to estimate SK overlap by (noverlap-1)/noverlap */
262  unsigned get_noverlap () const { return noverlap; }
263  void set_noverlap (unsigned);
264 
266  void prepare (uint64_t ndat = 0);
267 
269  unsigned get_overlap_offset () const { return overlap_offset; }
270 
272  uint64_t get_npart () const { return npart; }
273 
275  uint64_t get_output_ndat () const { return output_ndat; }
276 
278  void compatible (Resolution& that);
279 
281  const std::vector<float>& get_thresholds () const;
282 
284  std::vector< std::pair<unsigned,unsigned> > include;
285 
287  std::vector< std::pair<unsigned,unsigned> > exclude;
288 
290  void increment_time_freq (const Count&);
291  const Count& get_count_time_freq () const;
292 
294  void increment_time (const Count&);
295  const Count& get_count_time () const;
296  };
297 
298  std::vector<Resolution> resolution;
299 
300  void resize_resolution (unsigned);
301 
303  void tscrunch_sums (Resolution& from, Resolution& to);
304 
305  // for sorting by M
306  static bool by_M (const Resolution& A, const Resolution& B);
307 
308  unsigned nchan = 0;
309  unsigned npol = 0;
310  unsigned ndim = 0;
311  unsigned integrated_Nd = 1;
312 
313  unsigned sums_npol = 0;
314 
317 
319  Reference::To<BitSeries> zapmask;
320 
322  std::vector<SKLimits*> dynamic_limits;
323  const SKLimits* get_limits (unsigned count, float std_devs);
324 
326  std::vector<float> filtered_sum;
327 
329  std::vector<uint64_t> filtered_hits;
330 
332  std::vector<float> unfiltered_sum;
333 
335  uint64_t unfiltered_hits = 0;
336 
338  Count SK_freq;
339 
341  bool detect_time_freq = true;
342  bool detect_freq = true; // after tscrunch
343  bool detect_time = true; // after fscrunch
344 
346 
347  bool omit_outliers = true;
348 
349  bool prepared = false;
350 
353  bool report = false;
354 
355  // //! Input TimeSeries that has not been dedispersed in some previous operation.
356  // Reference::To<dsp::TimeSeries> zero_DM_input;
357 
359  HasInput<TimeSeries> zero_DM_input_container;
360 
361  Reference::To<BufferingPolicy> zero_DM_buffering_policy;
362 
363  bool zero_DM = false;
364  double delay_time = 0.0;
365  };
366 
367  class SpectralKurtosis::Engine : public Reference::Able
368  {
369  public:
370 
371  virtual void setup () = 0;
372 
373  virtual void compute (const TimeSeries* input, TimeSeries* output,
374  TimeSeries *output_tscr, unsigned tscrunch) = 0;
375 
376  virtual void reset_mask (BitSeries* output) = 0;
377 
378  virtual void detect_ft (const TimeSeries* input, BitSeries* output,
379  float upper_thresh, float lower_thresh) = 0;
380 
381  virtual void detect_fscr (const TimeSeries* input, BitSeries* output,
382  const float mu2, const float std_devs,
383  unsigned schan, unsigned echan) = 0;
384 
385  virtual void detect_tscr (const TimeSeries* input,
386  const TimeSeries * input_tscr,
387  BitSeries* output,
388  float upper, float lower) = 0;
389 
390  virtual int count_mask (const BitSeries* output) = 0;
391 
392  virtual float * get_estimates (const TimeSeries* input) = 0;
393 
394  virtual unsigned char * get_zapmask (const BitSeries* input) = 0;
395 
396  virtual void mask (BitSeries* mask, const TimeSeries * in, TimeSeries* out, unsigned M) = 0;
397 
398  virtual void insertsk (const TimeSeries* input, TimeSeries* out, unsigned M) = 0;
399 
400  };
401 }
402 
403 #endif
uint64_t get_unfiltered_hits() const
Hits on unfiltered SK statistic, same for each channel.
Definition: SpectralKurtosis.h:108
unsigned get_nchan() const
Get the number of frequency channels for which SK is computed.
Definition: SpectralKurtosis.h:70
void set_noverlap(unsigned _nover)
Set the number of overlapping regions per time sample.
Definition: SpectralKurtosis.h:49
Computes the upper and lower bounds on estimates of the generalized spectral kurtosis.
Definition: SKLimits.h:35
void set_omit_outliers(bool flag=true)
On detecting an outlier, omit sample from any future computation of SK.
Definition: SpectralKurtosis.h:67
Contains all Baseband Data Reduction Library classes.
Definition: ASCIIObservation.h:17
Defines the interface by which Transformations are performed on data.
Definition: Transformation.h:54
unsigned get_excision_threshold() const
The excision threshold in number of standard deviations.
Definition: SpectralKurtosis.h:92
void set_detect_time(bool flag=true)
Evaluate SK for every time sample after integrating over all frequency channels.
Definition: SpectralKurtosis.h:63
void reset_count()
The arrays will be reset when count_zapped is next called.
Definition: SpectralKurtosis.h:111
void prepare()
Prepare for data operations.
Definition: SpectralKurtosis.C:366
Order
Order of the dimensions.
Definition: TimeSeries.h:39
Implements a simple event emitter class.
Definition: EventEmitter.h:32
SpectralKurtosis()
Default constructor.
Definition: SpectralKurtosis.C:29
~SpectralKurtosis()
Destructor.
Definition: SpectralKurtosis.C:46
double get_delay_time() const override
Get the time delay of this operation, if any, in seconds.
Definition: SpectralKurtosis.C:504
unsigned get_M() const
The number of time samples used to calculate the SK statistic.
Definition: SpectralKurtosis.h:88
void set_zero_DM(bool _zero_DM)
set the zero_DM flag
Definition: SpectralKurtosis.h:159
float * get_datptr(unsigned ichan=0, unsigned ipol=0)
Return pointer to the specified data block.
Definition: TimeSeries.C:304
virtual void set_buffering_policy(BufferingPolicy *policy)
Set the policy for buffering input and/or output data.
Definition: Transformation.h:85
void prepare_output()
Definition: SpectralKurtosis.C:448
const ScalarMath sqrt(const ScalarMath &x)
void transformation()
Perform the transformation on the input time series.
Definition: SpectralKurtosis.C:542
Buffers the Transformation input.
Definition: InputBuffering.h:26
void set_thresholds(float _std_devs)
Set the RFI thresholds with the specified factor.
Definition: SpectralKurtosis.C:801
Container of weighted time-major order floating point data.
Definition: WeightedTimeSeries.h:26
void load_configuration(const std::string &filename)
Load configuration from YAML filename.
Definition: SpectralKurtosis.C:100
Perform Spectral Kurtosis on Input Timeseries, creating output Time Series.
Definition: SpectralKurtosis.h:30
Arrays of consecutive samples for each polarization and frequency channel.
Definition: TimeSeries.h:29
Reference::To< Engine > engine
Interface to alternate processing engine (e.g. GPU)
Definition: SpectralKurtosis.h:167
void set_detect_freq(bool flag=true)
Evaluate SK for every frequency channel after integrating over all time samples.
Definition: SpectralKurtosis.h:60
void get_unfiltered_sum(std::vector< float > &sum) const
Total SK statistic for each poln/channel, before filtering.
Definition: SpectralKurtosis.h:104
static bool verbose
Global verbosity flag.
Definition: Operation.h:48
A container for storing digitized (generally not floating point) data
Definition: BitSeries.h:35
bool has_zero_DM_input() const
Return true if the zero_DM_input attribute has been set.
Definition: SpectralKurtosis.C:155
void get_filtered_hits(std::vector< uint64_t > &hits) const
Hits on filtered average for each channel.
Definition: SpectralKurtosis.h:100
bool get_zero_DM() const
get the zero_DM flag
Definition: SpectralKurtosis.h:156
void get_filtered_sum(std::vector< float > &sum) const
Total SK statistic for each poln/channel, post filtering.
Definition: SpectralKurtosis.h:96
unsigned get_npol() const
Get the number of polarizations for which SK is computed.
Definition: SpectralKurtosis.h:73
void reserve()
Reserve the maximum amount of memory required.
Definition: SpectralKurtosis.C:511
void set_channel_range(unsigned start, unsigned end)
Set the channel range to conduct detection.
Definition: SpectralKurtosis.C:826
void set_detect_time_freq(bool flag=true)
Evaluate SK for every time sample and frequency channel.
Definition: SpectralKurtosis.h:57
Pure virtual base class of objects that manage memory allocation and destruction.
Definition: Memory.h:23
float * get_dattfp()
Return pointer to the specified data block.
Definition: TimeSeries.C:325
Reference::To< const TimeSeries > input
Container from which input data will be read.
Definition: HasInput.h:49
@ OrderFPT
Frequency, Polarization, Time (default before 3 October 2008)
Definition: TimeSeries.h:47
@ OrderTFP
Time, Frequency, Polarization (better for many things)
Definition: TimeSeries.h:50
Order get_order() const
Get the order.
Definition: TimeSeries.C:92
Reference::To< TimeSeries > output
Container into which output data will be written.
Definition: HasOutput.h:49

Generated using doxygen 1.8.17