Cheetah - SKA - PSS - Prototype Time Domain Search Pipeline
TimeFrequencyStats.cpp
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2016 The SKA organisation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "cheetah/data/TimeFrequencyStats.h"
25 #include <algorithm>
26 
27 
28 namespace ska {
29 namespace cheetah {
30 namespace data {
31 
32 template<typename TimeFrequencyType>
33 TimeFrequencyStats<TimeFrequencyType>::Statistics::Statistics()
34  : mean(0)
35  , variance(0)
36 {
37 }
38 
39 template<typename TimeFrequencyType>
40 TimeFrequencyStats<TimeFrequencyType>::TimeFrequencyStats(std::shared_ptr<TimeFrequencyType> const& ptr)
41  : BaseT(std::move(ptr))
42 {
43 }
44 
45 template<typename TimeFrequencyType>
47 {
48 }
49 
50 template<typename TimeFrequencyType>
52 {
53  const std::size_t nspectra=this->get().number_of_spectra();
54  const std::size_t nchans=this->get().number_of_channels();
55  _channel_stats.assign(nchans, Statistics());
56  _spectrum_stats.assign(nspectra, Statistics());
57 
58  auto it=this->get().begin();
59  auto end=this->get().end();
60  typename TimeFrequencyType::ConstIterator chan_end;
61  auto spectrum_stats_it = _spectrum_stats.begin();
62  do {
63  chan_end=it+nchans;
64  auto& spectrum_stat = *spectrum_stats_it;
65  auto channel_stat_it = _channel_stats.begin();
66  //boost::accumulators::accumulator_set<NumericalRep, boost::accumulators::stats<boost::accumulators::tag::median>> spectrum_acc;
67  while(it!=chan_end) {
68  double val = static_cast<double>(*it);
69 
70  // mean
71  spectrum_stat.mean += val/(double)nchans;
72 
73  auto& channel_stat = *channel_stat_it;
74  channel_stat.mean += val/(double)nspectra;
75 
76  // variance
77  const double v_squared = (double)(val * val);
78  spectrum_stat.variance += v_squared/(double)nchans;
79  channel_stat.variance += v_squared/(double)nspectra;
80 
81  // median
82  //spectrum_acc(*it);
83 
84  ++it;
85  ++channel_stat_it;
86  }
87  //spectrum_stat.median = boost::accumulators::median(spectrum_acc);
88  spectrum_stat.variance -= (spectrum_stat.mean * spectrum_stat.mean);
89  ++spectrum_stats_it;
90  } while(it != end);
91 
92  // store results
93  auto channel_stats_it = _channel_stats.begin();
94  while(channel_stats_it != _channel_stats.end())
95  {
96  (*channel_stats_it).variance -= ((*channel_stats_it).mean * (*channel_stats_it).mean);
97  ++channel_stats_it;
98  }
99 
100 }
101 
102 template<typename TimeFrequencyType>
103 std::vector<typename TimeFrequencyStats<TimeFrequencyType>::Statistics> const& TimeFrequencyStats<TimeFrequencyType>::channel_stats() const
104 {
105  if(_channel_stats.size() != this->template dimension<data::Frequency>()) {
106  calculate_stats();
107  }
108  return _channel_stats;
109 }
110 
111 template<typename TimeFrequencyType>
112 std::vector<typename TimeFrequencyStats<TimeFrequencyType>::Statistics> const& TimeFrequencyStats<TimeFrequencyType>::spectrum_stats() const
113 {
114  if(_spectrum_stats.size() != this->template dimension<data::Time>()) {
115  calculate_stats();
116  }
117  return _spectrum_stats;
118 }
119 
120 } // namespace data
121 } // namespace cheetah
122 } // namespace ska
std::vector< Statistics > const & channel_stats() const
: return the Statistics for each channel in the block
Some limits and constants for FLDO.
Definition: Brdz.h:35
A class to compute timefrequency stats (mean and variance.
TimeFrequencyStats(std::shared_ptr< TimeFrequencyType > const &)
: construct an object with median and variance statistics for each channel and each spectrum...
void calculate_stats() const
: recompute the statistics
std::vector< Statistics > const & spectrum_stats() const
: return the Statistics (mean and variance) for each spectrum