[37] | 1 | //#---------------------------------------------------------------------------
|
---|
| 2 | //# MathUtilities.cc: General math operations
|
---|
| 3 | //#---------------------------------------------------------------------------
|
---|
| 4 | //# Copyright (C) 2004
|
---|
[125] | 5 | //# ATNF
|
---|
[37] | 6 | //#
|
---|
| 7 | //# This program is free software; you can redistribute it and/or modify it
|
---|
| 8 | //# under the terms of the GNU General Public License as published by the Free
|
---|
| 9 | //# Software Foundation; either version 2 of the License, or (at your option)
|
---|
| 10 | //# any later version.
|
---|
| 11 | //#
|
---|
| 12 | //# This program is distributed in the hope that it will be useful, but
|
---|
| 13 | //# WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
---|
| 15 | //# Public License for more details.
|
---|
| 16 | //#
|
---|
| 17 | //# You should have received a copy of the GNU General Public License along
|
---|
| 18 | //# with this program; if not, write to the Free Software Foundation, Inc.,
|
---|
| 19 | //# 675 Massachusetts Ave, Cambridge, MA 02139, USA.
|
---|
| 20 | //#
|
---|
| 21 | //# Correspondence concerning this software should be addressed as follows:
|
---|
| 22 | //# Internet email: Malte.Marquarding@csiro.au
|
---|
| 23 | //# Postal address: Malte Marquarding,
|
---|
| 24 | //# Australia Telescope National Facility,
|
---|
| 25 | //# P.O. Box 76,
|
---|
| 26 | //# Epping, NSW, 2121,
|
---|
| 27 | //# AUSTRALIA
|
---|
| 28 | //#
|
---|
| 29 | //# $Id:
|
---|
| 30 | //#---------------------------------------------------------------------------
|
---|
| 31 |
|
---|
[125] | 32 | #include <casa/aips.h>
|
---|
[176] | 33 | #include <casa/Arrays/Vector.h>
|
---|
[1373] | 34 | #include <casa/Arrays/Slice.h>
|
---|
[137] | 35 | #include <casa/Arrays/MaskedArray.h>
|
---|
[136] | 36 | #include <casa/Arrays/MaskArrMath.h>
|
---|
| 37 | #include <casa/BasicSL/String.h>
|
---|
[1373] | 38 | #include <scimath/Mathematics/MedianSlider.h>
|
---|
[1446] | 39 | #include <casa/Exceptions/Error.h>
|
---|
[37] | 40 |
|
---|
[125] | 41 | #include "MathUtils.h"
|
---|
| 42 |
|
---|
| 43 | using namespace casa;
|
---|
| 44 |
|
---|
[829] | 45 | float mathutil::statistics(const String& which,
|
---|
[996] | 46 | const MaskedArray<Float>& data)
|
---|
[136] | 47 | {
|
---|
| 48 | String str(which);
|
---|
| 49 | str.upcase();
|
---|
[1446] | 50 | if (str.matches(String("MIN"))) {
|
---|
[829] | 51 | return min(data);
|
---|
[1446] | 52 | } else if (str.matches(String("MAX"))) {
|
---|
[136] | 53 | return max(data);
|
---|
[1446] | 54 | } else if (str.matches(String("SUMSQ"))) {
|
---|
[136] | 55 | return sumsquares(data);
|
---|
[1446] | 56 | } else if (str.matches(String("SUM"))) {
|
---|
[136] | 57 | return sum(data);
|
---|
[1446] | 58 | } else if (str.matches(String("MEAN"))) {
|
---|
[136] | 59 | return mean(data);
|
---|
[1446] | 60 | } else if (str.matches(String("VAR"))) {
|
---|
[829] | 61 | return variance(data);
|
---|
[1446] | 62 | } else if (str.matches(String("STDDEV"))) {
|
---|
[136] | 63 | return stddev(data);
|
---|
[1446] | 64 | } else if (str.matches(String("AVDEV"))) {
|
---|
[136] | 65 | return avdev(data);
|
---|
[1446] | 66 | } else if (str.matches(String("RMS"))) {
|
---|
[136] | 67 | uInt n = data.nelementsValid();
|
---|
| 68 | return sqrt(sumsquares(data)/n);
|
---|
[1446] | 69 | } else if (str.matches(String("MEDIAN"))) {
|
---|
[136] | 70 | return median(data);
|
---|
[1446] | 71 | } else {
|
---|
| 72 | String msg = str + " is not a valid type of statistics";
|
---|
| 73 | throw(AipsError(msg));
|
---|
| 74 | }
|
---|
[996] | 75 | return 0.0;
|
---|
[136] | 76 | }
|
---|
[176] | 77 |
|
---|
[829] | 78 |
|
---|
[209] | 79 | void mathutil::replaceMaskByZero(Vector<Float>& data, const Vector<Bool>& mask)
|
---|
[176] | 80 | {
|
---|
| 81 | for (uInt i=0; i<data.nelements(); i++) {
|
---|
| 82 | if (!mask[i]) data[i] = 0.0;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
[382] | 85 |
|
---|
| 86 |
|
---|
[829] | 87 | std::vector<std::string> mathutil::tovectorstring(const Vector<String>& in)
|
---|
[382] | 88 | {
|
---|
[465] | 89 | std::vector<std::string> out;
|
---|
[1400] | 90 | Vector<String>::const_iterator it = in.begin();
|
---|
[465] | 91 | for (uInt i=0; it != in.end(); ++it,++i) {
|
---|
| 92 | out.push_back(*it);
|
---|
| 93 | }
|
---|
| 94 | return out;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[829] | 97 | Vector<String> mathutil::toVectorString(const std::vector<std::string>& in)
|
---|
[465] | 98 | {
|
---|
| 99 | Vector<String> out(in.size());
|
---|
| 100 | uInt i=0;
|
---|
| 101 | std::vector<std::string>::const_iterator it;
|
---|
| 102 | for (it=in.begin();it != in.end();++it,++i) {
|
---|
| 103 | out[i] = casa::String(*it);
|
---|
| 104 | }
|
---|
| 105 | return out;
|
---|
| 106 | }
|
---|
[1325] | 107 |
|
---|
| 108 | void mathutil::hanning(Vector<Float>& out, Vector<Bool>& outmask,
|
---|
| 109 | const Vector<Float>& in, const Vector<Bool>& mask,
|
---|
| 110 | Bool relaxed, Bool ignoreOther) {
|
---|
| 111 | Vector< Vector<Float> > weights(8);
|
---|
| 112 | Vector<Float> vals(3);
|
---|
| 113 | vals = 0.0;weights[0] = vals;// FFF
|
---|
| 114 | vals[0] = 1.0; vals[1] = 0.0; vals[2] = 0.0; weights[1] = vals;// TFF
|
---|
| 115 | vals[0] = 0.0; vals[1] = 1.0; vals[2] = 0.0; weights[2] = vals;// FTF
|
---|
| 116 | vals[0] = 1.0/3.0; vals[1] = 2.0/3.0; vals[2] = 0.0; weights[3] = vals;// TTF
|
---|
| 117 | vals[0] = 0.0; vals[1] = 0.0; vals[2] = 1.0;weights[4] = vals;// FFT
|
---|
| 118 | vals[0] = 0.5; vals[1] = 0.0; vals[2] = 0.5; weights[5] = vals;// TFT
|
---|
| 119 | vals[0] = 0.0; vals[1] = 2.0/3.0; vals[2] = 1.0/3.0; weights[6] = vals;// FTT
|
---|
| 120 | vals[0] = 0.25; vals[1] = 0.5; vals[2] = 0.25; weights[7] = vals;// TTT
|
---|
| 121 | // Chris' case
|
---|
| 122 | Vector<Bool> weighted(8);
|
---|
| 123 | if (relaxed) {
|
---|
| 124 | weighted = False;
|
---|
| 125 | weighted[7] = True;
|
---|
| 126 |
|
---|
| 127 | } else {
|
---|
| 128 | weighted = True;
|
---|
| 129 | weighted[0] = False;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | out.resize(in.nelements());
|
---|
| 133 | outmask.resize(mask.nelements());
|
---|
| 134 | // make special case for first and last
|
---|
| 135 | /// ...here
|
---|
| 136 | // loop from 1..n-2
|
---|
| 137 | out.resize(in.nelements());
|
---|
| 138 | out[0] = in[0];out[out.nelements()-1] = in[in.nelements()-1];
|
---|
| 139 | outmask.resize(mask.nelements());
|
---|
| 140 | outmask = False;
|
---|
| 141 | uInt m;Vector<Float>* w;
|
---|
| 142 | for (uInt i=1; i < out.nelements()-1;++i) {
|
---|
| 143 | m = mask[i-1] + 2*mask[i] + 4*mask[i+1];
|
---|
| 144 | w = &(weights[m]);
|
---|
| 145 | if (weighted[m]) {
|
---|
| 146 | out[i] = (*w)[0]*in[i-1] + (*w)[1]*in[i] + (*w)[2]*in[i+1];
|
---|
| 147 | outmask[i] = True;
|
---|
| 148 | } else { // mask it
|
---|
| 149 | out[i] = in[i];//use arbitrary value
|
---|
| 150 | outmask[i] = False;
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
[1373] | 154 |
|
---|
| 155 |
|
---|
| 156 | void mathutil::runningMedian(Vector<Float>& out, Vector<Bool>& outflag,
|
---|
| 157 | const Vector<Float>& in, const Vector<Bool>& flag,
|
---|
| 158 | float width)
|
---|
| 159 | {
|
---|
| 160 | Int hwidth = Int(width+0.5);
|
---|
| 161 | Int fwidth = hwidth*2+1;
|
---|
| 162 | out.resize(in.nelements());
|
---|
| 163 | outflag.resize(flag.nelements());
|
---|
| 164 | MedianSlider ms(hwidth);
|
---|
| 165 | Slice sl(0, fwidth-1);
|
---|
| 166 | Float medval = ms.add(const_cast<Vector<Float>& >(in)(sl),
|
---|
| 167 | const_cast<Vector<Bool>& >(flag)(sl));
|
---|
| 168 | uInt n = in.nelements();
|
---|
| 169 | for (uInt i=hwidth; i<(n-hwidth); ++i) {
|
---|
| 170 | // add data value
|
---|
[1382] | 171 | out[i] = ms.add(in[i+hwidth], flag[i+hwidth]);
|
---|
[1373] | 172 | outflag[i] = (ms.nval() == 0);
|
---|
| 173 | }
|
---|
| 174 | // replicate edge values from fisrt value with full width of values
|
---|
| 175 | for (uInt i=0;i<hwidth;++i) {
|
---|
| 176 | out[i] = out[hwidth];
|
---|
| 177 | outflag[i] = outflag[hwidth];
|
---|
| 178 | out[n-1-i] = out[n-1-hwidth];
|
---|
| 179 | outflag[n-1-i] = outflag[n-1-hwidth];
|
---|
| 180 | }
|
---|
| 181 | }
|
---|