source: branches/alma/src/MathUtils.cpp@ 1480

Last change on this file since 1480 was 1446, checked in by TakTsutsumi, 16 years ago

Merged recent updates (since 2007) from nrao-asap

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.9 KB
Line 
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/BasicSL/String.h>
38#include <scimath/Mathematics/MedianSlider.h>
39#include <casa/Exceptions/Error.h>
40
41#include "MathUtils.h"
42
43using namespace casa;
44
45float mathutil::statistics(const String& which,
46 const MaskedArray<Float>& data)
47{
48 String str(which);
49 str.upcase();
50 if (str.matches(String("MIN"))) {
51 return min(data);
52 } else if (str.matches(String("MAX"))) {
53 return max(data);
54 } else if (str.matches(String("SUMSQ"))) {
55 return sumsquares(data);
56 } else if (str.matches(String("SUM"))) {
57 return sum(data);
58 } else if (str.matches(String("MEAN"))) {
59 return mean(data);
60 } else if (str.matches(String("VAR"))) {
61 return variance(data);
62 } else if (str.matches(String("STDDEV"))) {
63 return stddev(data);
64 } else if (str.matches(String("AVDEV"))) {
65 return avdev(data);
66 } else if (str.matches(String("RMS"))) {
67 uInt n = data.nelementsValid();
68 return sqrt(sumsquares(data)/n);
69 } else if (str.matches(String("MEDIAN"))) {
70 return median(data);
71 } else {
72 String msg = str + " is not a valid type of statistics";
73 throw(AipsError(msg));
74 }
75 return 0.0;
76}
77
78
79void mathutil::replaceMaskByZero(Vector<Float>& data, const Vector<Bool>& mask)
80{
81 for (uInt i=0; i<data.nelements(); i++) {
82 if (!mask[i]) data[i] = 0.0;
83 }
84}
85
86
87std::vector<std::string> mathutil::tovectorstring(const Vector<String>& in)
88{
89 std::vector<std::string> out;
90 Vector<String>::const_iterator it = in.begin();
91 for (uInt i=0; it != in.end(); ++it,++i) {
92 out.push_back(*it);
93 }
94 return out;
95}
96
97Vector<String> mathutil::toVectorString(const std::vector<std::string>& in)
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}
107
108void 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}
154
155
156void 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
171 out[i] = ms.add(in[i+hwidth], flag[i+hwidth]);
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}
Note: See TracBrowser for help on using the repository browser.