LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
n_pulse_score.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2014, Sam Schetterer, Nathan Kutz, University of Washington
3 Authors: Sam Schetterer
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 
10 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11 
12 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 
16 */
17 #include "n_pulse_score.h"
18 #include "comp_funcs.hpp"
19 #include "types/type_register.hpp"
20 //This calculates the kurtosis of an input array
21 template class type_register<n_pulse_score>;
22 static double kurtosis(double in[], size_t len){
23  double mean, mom4, mom2;
24  mean = mom4 = mom2 = 0;
25  for(size_t i = 0; i < len; i++){
26  mean += in[i];
27  }
28  mean /= len;
29  for(size_t i = 0; i < len; i++){
30  double sq = (in[i] - mean)*(in[i]-mean);
31  mom2+= sq;
32  mom4+= sq*sq;
33  }
34  mom4/=len;
35  mom2/=len;
36  return mom4/(mom2*mom2);
37 }
44 double n_pulse_score::score(comp* ucur){
45  //This is not an optimal solution,
46  //but isn't too slow and this code should never be a bottleneck
47  //This verison is far easier to read/understand than the optimal version
48  for(size_t i = 0; i < nts; i++){
49  help[i] = _sqabs(ucur[i]);
50  for(size_t j = 1; j < n_pulse; j++){
51  help[i] += _sqabs(ucur[i+j*nts]);
52  }
53  help[i] = sqrt(help[i]);
54  kurtosis_help[i] = help[i];
55  }
56  double ener = energy(help, nts);
58  for(size_t i = 0; i < nts; i++){
59  help[i] = abs(kurtosis_help[i]);
60  }
61  double kurtosis_v = 1.0/(kurtosis(help, nts));
62  double score = kurtosis_v* ener;
63  return score;
64 }
66 
73 std::vector<std::string> n_pulse_score::dependencies() const {
74  std::string deps[] = {"num_pulses"};
75  return make_append(deps, objective::dependencies());
76 }
78  objective::postprocess(invals);
79  int _n_pulse = 0;
80  invals.retrieve(_n_pulse, "num_pulses", this);
81  n_pulse = _n_pulse;
82  if(dimension%n_pulse){
83  err("n_pulse_score requires a dimension divisible by the number of pulses",
84  "bi_pulse_score::postprocess", "objective/bi_pulse_score.cpp", FATAL_ERROR);
85  }
88 }
90 
92  return "n_pulse_score";
93 }