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/MaskedArray.h>
|
---|
35 | #include <casa/Arrays/MaskArrMath.h>
|
---|
36 | #include <casa/Arrays/VectorSTLIterator.h>
|
---|
37 | #include <casa/BasicSL/String.h>
|
---|
38 |
|
---|
39 | #include "MathUtils.h"
|
---|
40 |
|
---|
41 | using namespace casa;
|
---|
42 |
|
---|
43 | float mathutil::statistics(const String& which,
|
---|
44 | const MaskedArray<Float>& data)
|
---|
45 | {
|
---|
46 | String str(which);
|
---|
47 | str.upcase();
|
---|
48 | if (str.contains(String("MIN"))) {
|
---|
49 | return min(data);
|
---|
50 | } else if (str.contains(String("MAX"))) {
|
---|
51 | return max(data);
|
---|
52 | } else if (str.contains(String("SUMSQ"))) {
|
---|
53 | return sumsquares(data);
|
---|
54 | } else if (str.contains(String("SUM"))) {
|
---|
55 | return sum(data);
|
---|
56 | } else if (str.contains(String("MEAN"))) {
|
---|
57 | return mean(data);
|
---|
58 | } else if (str.contains(String("VAR"))) {
|
---|
59 | return variance(data);
|
---|
60 | } else if (str.contains(String("STDDEV"))) {
|
---|
61 | return stddev(data);
|
---|
62 | } else if (str.contains(String("AVDEV"))) {
|
---|
63 | return avdev(data);
|
---|
64 | } else if (str.contains(String("RMS"))) {
|
---|
65 | uInt n = data.nelementsValid();
|
---|
66 | return sqrt(sumsquares(data)/n);
|
---|
67 | } else if (str.contains(String("MED"))) {
|
---|
68 | return median(data);
|
---|
69 | }
|
---|
70 | return 0.0;
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | void mathutil::replaceMaskByZero(Vector<Float>& data, const Vector<Bool>& mask)
|
---|
75 | {
|
---|
76 | for (uInt i=0; i<data.nelements(); i++) {
|
---|
77 | if (!mask[i]) data[i] = 0.0;
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | std::vector<std::string> mathutil::tovectorstring(const Vector<String>& in)
|
---|
83 | {
|
---|
84 | std::vector<std::string> out;
|
---|
85 | VectorSTLIterator<String> it(in);
|
---|
86 | for (uInt i=0; it != in.end(); ++it,++i) {
|
---|
87 | out.push_back(*it);
|
---|
88 | }
|
---|
89 | return out;
|
---|
90 | }
|
---|
91 |
|
---|
92 | Vector<String> mathutil::toVectorString(const std::vector<std::string>& in)
|
---|
93 | {
|
---|
94 | Vector<String> out(in.size());
|
---|
95 | uInt i=0;
|
---|
96 | std::vector<std::string>::const_iterator it;
|
---|
97 | for (it=in.begin();it != in.end();++it,++i) {
|
---|
98 | out[i] = casa::String(*it);
|
---|
99 | }
|
---|
100 | return out;
|
---|
101 | }
|
---|
102 |
|
---|
103 | void mathutil::hanning(Vector<Float>& out, Vector<Bool>& outmask,
|
---|
104 | const Vector<Float>& in, const Vector<Bool>& mask,
|
---|
105 | Bool relaxed, Bool ignoreOther) {
|
---|
106 | Vector< Vector<Float> > weights(8);
|
---|
107 | Vector<Float> vals(3);
|
---|
108 | vals = 0.0;weights[0] = vals;// FFF
|
---|
109 | vals[0] = 1.0; vals[1] = 0.0; vals[2] = 0.0; weights[1] = vals;// TFF
|
---|
110 | vals[0] = 0.0; vals[1] = 1.0; vals[2] = 0.0; weights[2] = vals;// FTF
|
---|
111 | vals[0] = 1.0/3.0; vals[1] = 2.0/3.0; vals[2] = 0.0; weights[3] = vals;// TTF
|
---|
112 | vals[0] = 0.0; vals[1] = 0.0; vals[2] = 1.0;weights[4] = vals;// FFT
|
---|
113 | vals[0] = 0.5; vals[1] = 0.0; vals[2] = 0.5; weights[5] = vals;// TFT
|
---|
114 | vals[0] = 0.0; vals[1] = 2.0/3.0; vals[2] = 1.0/3.0; weights[6] = vals;// FTT
|
---|
115 | vals[0] = 0.25; vals[1] = 0.5; vals[2] = 0.25; weights[7] = vals;// TTT
|
---|
116 | // Chris' case
|
---|
117 | Vector<Bool> weighted(8);
|
---|
118 | if (relaxed) {
|
---|
119 | weighted = False;
|
---|
120 | weighted[7] = True;
|
---|
121 |
|
---|
122 | } else {
|
---|
123 | weighted = True;
|
---|
124 | weighted[0] = False;
|
---|
125 | }
|
---|
126 |
|
---|
127 | out.resize(in.nelements());
|
---|
128 | outmask.resize(mask.nelements());
|
---|
129 | // make special case for first and last
|
---|
130 | /// ...here
|
---|
131 | // loop from 1..n-2
|
---|
132 | out.resize(in.nelements());
|
---|
133 | out[0] = in[0];out[out.nelements()-1] = in[in.nelements()-1];
|
---|
134 | outmask.resize(mask.nelements());
|
---|
135 | outmask = False;
|
---|
136 | uInt m;Vector<Float>* w;
|
---|
137 | for (uInt i=1; i < out.nelements()-1;++i) {
|
---|
138 | m = mask[i-1] + 2*mask[i] + 4*mask[i+1];
|
---|
139 | w = &(weights[m]);
|
---|
140 | if (weighted[m]) {
|
---|
141 | out[i] = (*w)[0]*in[i-1] + (*w)[1]*in[i] + (*w)[2]*in[i+1];
|
---|
142 | outmask[i] = True;
|
---|
143 | } else { // mask it
|
---|
144 | out[i] = in[i];//use arbitrary value
|
---|
145 | outmask[i] = False;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|