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