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