[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 |
|
---|
[2125] | 32 | #include <iostream>
|
---|
[125] | 33 | #include <casa/aips.h>
|
---|
[176] | 34 | #include <casa/Arrays/Vector.h>
|
---|
[1373] | 35 | #include <casa/Arrays/Slice.h>
|
---|
[137] | 36 | #include <casa/Arrays/MaskedArray.h>
|
---|
[136] | 37 | #include <casa/Arrays/MaskArrMath.h>
|
---|
[465] | 38 | #include <casa/Arrays/VectorSTLIterator.h>
|
---|
[136] | 39 | #include <casa/BasicSL/String.h>
|
---|
[1373] | 40 | #include <scimath/Mathematics/MedianSlider.h>
|
---|
[1819] | 41 | #include <casa/Exceptions/Error.h>
|
---|
[37] | 42 |
|
---|
[1570] | 43 | #include <scimath/Fitting/LinearFit.h>
|
---|
| 44 | #include <scimath/Functionals/Polynomial.h>
|
---|
| 45 | #include <scimath/Mathematics/AutoDiff.h>
|
---|
| 46 |
|
---|
| 47 |
|
---|
[125] | 48 | #include "MathUtils.h"
|
---|
| 49 |
|
---|
| 50 | using namespace casa;
|
---|
| 51 |
|
---|
[829] | 52 | float mathutil::statistics(const String& which,
|
---|
[996] | 53 | const MaskedArray<Float>& data)
|
---|
[136] | 54 | {
|
---|
| 55 | String str(which);
|
---|
| 56 | str.upcase();
|
---|
[1819] | 57 | if (str.matches(String("MIN"))) {
|
---|
[829] | 58 | return min(data);
|
---|
[1819] | 59 | } else if (str.matches(String("MAX"))) {
|
---|
[136] | 60 | return max(data);
|
---|
[1819] | 61 | } else if (str.matches(String("SUMSQ"))) {
|
---|
[136] | 62 | return sumsquares(data);
|
---|
[1819] | 63 | } else if (str.matches(String("SUM"))) {
|
---|
[136] | 64 | return sum(data);
|
---|
[1819] | 65 | } else if (str.matches(String("MEAN"))) {
|
---|
[136] | 66 | return mean(data);
|
---|
[1819] | 67 | } else if (str.matches(String("VAR"))) {
|
---|
[829] | 68 | return variance(data);
|
---|
[1819] | 69 | } else if (str.matches(String("STDDEV"))) {
|
---|
[136] | 70 | return stddev(data);
|
---|
[1819] | 71 | } else if (str.matches(String("AVDEV"))) {
|
---|
[136] | 72 | return avdev(data);
|
---|
[1819] | 73 | } else if (str.matches(String("RMS"))) {
|
---|
[136] | 74 | uInt n = data.nelementsValid();
|
---|
| 75 | return sqrt(sumsquares(data)/n);
|
---|
[1819] | 76 | } else if (str.matches(String("MEDIAN"))) {
|
---|
[136] | 77 | return median(data);
|
---|
[1819] | 78 | } else {
|
---|
| 79 | String msg = str + " is not a valid type of statistics";
|
---|
| 80 | throw(AipsError(msg));
|
---|
| 81 | }
|
---|
[996] | 82 | return 0.0;
|
---|
[136] | 83 | }
|
---|
[176] | 84 |
|
---|
[1819] | 85 | IPosition mathutil::minMaxPos(const String& which,
|
---|
| 86 | const MaskedArray<Float>& data)
|
---|
| 87 | {
|
---|
| 88 | Float minVal, maxVal;
|
---|
| 89 | IPosition minPos(data.ndim(), 0), maxPos(data.ndim(), 0);
|
---|
| 90 | minMax(minVal, maxVal, minPos, maxPos, data);
|
---|
| 91 | String str(which);
|
---|
| 92 | str.upcase();
|
---|
| 93 | if (str.contains(String("MIN"))) {
|
---|
| 94 | return minPos;
|
---|
| 95 | } else if (str.contains(String("MAX"))) {
|
---|
| 96 | return maxPos;
|
---|
| 97 | } else {
|
---|
| 98 | String msg = str + " is not a valid type of statistics";
|
---|
| 99 | throw(AipsError(msg));
|
---|
| 100 | }
|
---|
| 101 | //return 0.0;
|
---|
| 102 | }
|
---|
[829] | 103 |
|
---|
[209] | 104 | void mathutil::replaceMaskByZero(Vector<Float>& data, const Vector<Bool>& mask)
|
---|
[176] | 105 | {
|
---|
| 106 | for (uInt i=0; i<data.nelements(); i++) {
|
---|
| 107 | if (!mask[i]) data[i] = 0.0;
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
[382] | 110 |
|
---|
| 111 |
|
---|
[829] | 112 | std::vector<std::string> mathutil::tovectorstring(const Vector<String>& in)
|
---|
[382] | 113 | {
|
---|
[465] | 114 | std::vector<std::string> out;
|
---|
[1412] | 115 | out.reserve(in.nelements());
|
---|
| 116 | for (Array<String>::const_iterator it = in.begin(); it != in.end(); ++it) {
|
---|
[465] | 117 | out.push_back(*it);
|
---|
| 118 | }
|
---|
| 119 | return out;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[829] | 122 | Vector<String> mathutil::toVectorString(const std::vector<std::string>& in)
|
---|
[465] | 123 | {
|
---|
| 124 | Vector<String> out(in.size());
|
---|
[1412] | 125 | Array<String>::iterator oit = out.begin();
|
---|
| 126 | for (std::vector<std::string>::const_iterator it=in.begin() ;
|
---|
| 127 | it != in.end(); ++it,++oit) {
|
---|
| 128 | *oit = *it;
|
---|
[465] | 129 | }
|
---|
| 130 | return out;
|
---|
| 131 | }
|
---|
[1325] | 132 |
|
---|
| 133 | void mathutil::hanning(Vector<Float>& out, Vector<Bool>& outmask,
|
---|
| 134 | const Vector<Float>& in, const Vector<Bool>& mask,
|
---|
| 135 | Bool relaxed, Bool ignoreOther) {
|
---|
| 136 | Vector< Vector<Float> > weights(8);
|
---|
| 137 | Vector<Float> vals(3);
|
---|
| 138 | vals = 0.0;weights[0] = vals;// FFF
|
---|
| 139 | vals[0] = 1.0; vals[1] = 0.0; vals[2] = 0.0; weights[1] = vals;// TFF
|
---|
| 140 | vals[0] = 0.0; vals[1] = 1.0; vals[2] = 0.0; weights[2] = vals;// FTF
|
---|
| 141 | vals[0] = 1.0/3.0; vals[1] = 2.0/3.0; vals[2] = 0.0; weights[3] = vals;// TTF
|
---|
| 142 | vals[0] = 0.0; vals[1] = 0.0; vals[2] = 1.0;weights[4] = vals;// FFT
|
---|
| 143 | vals[0] = 0.5; vals[1] = 0.0; vals[2] = 0.5; weights[5] = vals;// TFT
|
---|
| 144 | vals[0] = 0.0; vals[1] = 2.0/3.0; vals[2] = 1.0/3.0; weights[6] = vals;// FTT
|
---|
| 145 | vals[0] = 0.25; vals[1] = 0.5; vals[2] = 0.25; weights[7] = vals;// TTT
|
---|
| 146 | // Chris' case
|
---|
| 147 | Vector<Bool> weighted(8);
|
---|
| 148 | if (relaxed) {
|
---|
| 149 | weighted = False;
|
---|
| 150 | weighted[7] = True;
|
---|
| 151 |
|
---|
| 152 | } else {
|
---|
| 153 | weighted = True;
|
---|
| 154 | weighted[0] = False;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | out.resize(in.nelements());
|
---|
| 158 | outmask.resize(mask.nelements());
|
---|
| 159 | // make special case for first and last
|
---|
| 160 | /// ...here
|
---|
| 161 | // loop from 1..n-2
|
---|
| 162 | out.resize(in.nelements());
|
---|
| 163 | out[0] = in[0];out[out.nelements()-1] = in[in.nelements()-1];
|
---|
[2125] | 164 | //--s
|
---|
| 165 | cout << "out[0]=" << out[0] << ";" << endl;
|
---|
| 166 | //--e
|
---|
[1325] | 167 | outmask.resize(mask.nelements());
|
---|
[2125] | 168 | outmask[0] = mask[0]; outmask[outmask.nelements()-1] = mask[mask.nelements()-1];
|
---|
[1325] | 169 | uInt m;Vector<Float>* w;
|
---|
| 170 | for (uInt i=1; i < out.nelements()-1;++i) {
|
---|
| 171 | m = mask[i-1] + 2*mask[i] + 4*mask[i+1];
|
---|
| 172 | w = &(weights[m]);
|
---|
| 173 | if (weighted[m]) {
|
---|
| 174 | out[i] = (*w)[0]*in[i-1] + (*w)[1]*in[i] + (*w)[2]*in[i+1];
|
---|
[2125] | 175 | //outmask[i] = True;
|
---|
[1325] | 176 | } else { // mask it
|
---|
| 177 | out[i] = in[i];//use arbitrary value
|
---|
[2125] | 178 | //outmask[i] = False;
|
---|
[1325] | 179 | }
|
---|
[2125] | 180 | //--s
|
---|
| 181 | if (i < 13) {
|
---|
| 182 | cout << "mask[" << i-1 << "-" << i+1 << "]=[" << mask[i-1] << ", " << mask[i] << ", " << mask[i+1] << "]; ";
|
---|
| 183 | cout << "m=" << m << "; ";
|
---|
| 184 | cout << "weights[" << m << "]=" << weights[m] << "; ";
|
---|
| 185 | cout << "in[" << i-1 << "-" << i+1 << "]=[" << in[i-1] << ", " << in[i] << ", " << in[i+1] << "] --> ";
|
---|
| 186 | cout << out[i] << ";" << endl;
|
---|
| 187 | cout << "-------------------------------" << endl;
|
---|
| 188 | }
|
---|
| 189 | //--e
|
---|
| 190 | outmask[i] = mask[i];
|
---|
[1325] | 191 | }
|
---|
| 192 | }
|
---|
[1373] | 193 |
|
---|
| 194 |
|
---|
| 195 | void mathutil::runningMedian(Vector<Float>& out, Vector<Bool>& outflag,
|
---|
| 196 | const Vector<Float>& in, const Vector<Bool>& flag,
|
---|
| 197 | float width)
|
---|
| 198 | {
|
---|
| 199 | Int hwidth = Int(width+0.5);
|
---|
| 200 | Int fwidth = hwidth*2+1;
|
---|
| 201 | out.resize(in.nelements());
|
---|
| 202 | outflag.resize(flag.nelements());
|
---|
| 203 | MedianSlider ms(hwidth);
|
---|
| 204 | Slice sl(0, fwidth-1);
|
---|
[1570] | 205 | Float medval = ms.add(const_cast<Vector<Float>& >(in)(sl),
|
---|
[1373] | 206 | const_cast<Vector<Bool>& >(flag)(sl));
|
---|
| 207 | uInt n = in.nelements();
|
---|
| 208 | for (uInt i=hwidth; i<(n-hwidth); ++i) {
|
---|
| 209 | // add data value
|
---|
[1570] | 210 | out[i] = ms.add(in[i+hwidth], flag[i+hwidth]);
|
---|
| 211 | outflag[i] = (ms.nval() == 0);
|
---|
[1373] | 212 | }
|
---|
[1570] | 213 | // replicate edge values from first value with full width of values
|
---|
[1373] | 214 | for (uInt i=0;i<hwidth;++i) {
|
---|
| 215 | out[i] = out[hwidth];
|
---|
[1570] | 216 | outflag[i] = outflag[hwidth];
|
---|
[1373] | 217 | out[n-1-i] = out[n-1-hwidth];
|
---|
[1570] | 218 | outflag[n-1-i] = outflag[n-1-hwidth];
|
---|
[1373] | 219 | }
|
---|
| 220 | }
|
---|
[1570] | 221 |
|
---|
| 222 | void mathutil::polyfit(Vector<Float>& out, Vector<Bool>& outmask,
|
---|
| 223 | const Vector<Float>& in, const Vector<Bool>& mask,
|
---|
| 224 | float width, int order)
|
---|
| 225 | {
|
---|
| 226 | Int hwidth = Int(width+0.5);
|
---|
| 227 | Int fwidth = hwidth*2+1;
|
---|
| 228 | out.resize(in.nelements());
|
---|
| 229 | outmask.resize(mask.nelements());
|
---|
| 230 | LinearFit<Float> fitter;
|
---|
| 231 | Polynomial<Float> poly(order);
|
---|
| 232 | fitter.setFunction(poly);
|
---|
| 233 | Vector<Float> sigma(fwidth);
|
---|
| 234 | sigma = 1.0;
|
---|
| 235 | Vector<Float> parms;
|
---|
| 236 | Vector<Float> x(fwidth);
|
---|
| 237 | indgen(x);
|
---|
| 238 |
|
---|
| 239 | uInt n = in.nelements();
|
---|
| 240 |
|
---|
| 241 | for (uInt i=hwidth; i<(n-hwidth); ++i) {
|
---|
| 242 | // add data value
|
---|
| 243 | if (mask[i]) {
|
---|
| 244 | Slice sl(i-hwidth, fwidth);
|
---|
| 245 | const Vector<Float> &y = const_cast<Vector<Float>& >(in)(sl);
|
---|
| 246 | const Vector<Bool> &m = const_cast<Vector<Bool>& >(mask)(sl);
|
---|
| 247 | parms = fitter.fit(x, y, sigma, &m);
|
---|
| 248 |
|
---|
| 249 | poly.setCoefficients(parms);
|
---|
| 250 | out[i] = poly(x[hwidth]);//cout << in[i] <<"->"<<out[i]<<endl;
|
---|
| 251 | } else {
|
---|
| 252 | out[i] = in[i];
|
---|
| 253 | }
|
---|
| 254 | outmask[i] = mask[i];
|
---|
| 255 | }
|
---|
| 256 | // replicate edge values from first value with full width of values
|
---|
| 257 | for (uInt i=0;i<hwidth;++i) {
|
---|
| 258 | out[i] = out[hwidth];
|
---|
| 259 | outmask[i] = outmask[hwidth];
|
---|
| 260 | out[n-1-i] = out[n-1-hwidth];
|
---|
| 261 | outmask[n-1-i] = outmask[n-1-hwidth];
|
---|
| 262 | }
|
---|
| 263 | }
|
---|