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

Last change on this file since 1687 was 1603, checked in by TakTsutsumi, 15 years ago

New Development: No, merge with asap2.3.1

JIRA Issue: Yes CAS-1450

Ready to Release: Yes/No

Interface Changes: Yes/No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes

Module(s): single dish

Description: Upgrade of alma branch based on ASAP2.2.0

(rev.1562) to ASAP2.3.1 (rev.1561)


  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
RevLine 
[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
[125]32#include <casa/aips.h>
[176]33#include <casa/Arrays/Vector.h>
[1373]34#include <casa/Arrays/Slice.h>
[137]35#include <casa/Arrays/MaskedArray.h>
[136]36#include <casa/Arrays/MaskArrMath.h>
[1603]37#include <casa/Arrays/VectorSTLIterator.h>
[136]38#include <casa/BasicSL/String.h>
[1373]39#include <scimath/Mathematics/MedianSlider.h>
[1446]40#include <casa/Exceptions/Error.h>
[37]41
[125]42#include "MathUtils.h"
43
44using namespace casa;
45
[829]46float mathutil::statistics(const String& which,
[996]47 const MaskedArray<Float>& data)
[136]48{
49 String str(which);
50 str.upcase();
[1446]51 if (str.matches(String("MIN"))) {
[829]52 return min(data);
[1446]53 } else if (str.matches(String("MAX"))) {
[136]54 return max(data);
[1446]55 } else if (str.matches(String("SUMSQ"))) {
[136]56 return sumsquares(data);
[1446]57 } else if (str.matches(String("SUM"))) {
[136]58 return sum(data);
[1446]59 } else if (str.matches(String("MEAN"))) {
[136]60 return mean(data);
[1446]61 } else if (str.matches(String("VAR"))) {
[829]62 return variance(data);
[1446]63 } else if (str.matches(String("STDDEV"))) {
[136]64 return stddev(data);
[1446]65 } else if (str.matches(String("AVDEV"))) {
[136]66 return avdev(data);
[1446]67 } else if (str.matches(String("RMS"))) {
[136]68 uInt n = data.nelementsValid();
69 return sqrt(sumsquares(data)/n);
[1446]70 } else if (str.matches(String("MEDIAN"))) {
[136]71 return median(data);
[1446]72 } else {
73 String msg = str + " is not a valid type of statistics";
74 throw(AipsError(msg));
75 }
[996]76 return 0.0;
[136]77}
[176]78
[1514]79IPosition mathutil::minMaxPos(const String& which,
80 const MaskedArray<Float>& data)
81{
82 Float minVal, maxVal;
83 IPosition minPos(data.ndim(), 0), maxPos(data.ndim(), 0);
84 minMax(minVal, maxVal, minPos, maxPos, data);
85 String str(which);
86 str.upcase();
[1530]87 if (str.contains(String("MIN"))) {
[1514]88 return minPos;
[1530]89 } else if (str.contains(String("MAX"))) {
[1514]90 return maxPos;
91 } else {
92 String msg = str + " is not a valid type of statistics";
93 throw(AipsError(msg));
94 }
95 //return 0.0;
96}
[829]97
[209]98void mathutil::replaceMaskByZero(Vector<Float>& data, const Vector<Bool>& mask)
[176]99{
100 for (uInt i=0; i<data.nelements(); i++) {
101 if (!mask[i]) data[i] = 0.0;
102 }
103}
[382]104
105
[829]106std::vector<std::string> mathutil::tovectorstring(const Vector<String>& in)
[382]107{
[465]108 std::vector<std::string> out;
[1603]109 out.reserve(in.nelements());
110 for (Array<String>::const_iterator it = in.begin(); it != in.end(); ++it) {
[465]111 out.push_back(*it);
112 }
113 return out;
114}
115
[829]116Vector<String> mathutil::toVectorString(const std::vector<std::string>& in)
[465]117{
118 Vector<String> out(in.size());
[1603]119 Array<String>::iterator oit = out.begin();
120 for (std::vector<std::string>::const_iterator it=in.begin() ;
121 it != in.end(); ++it,++oit) {
122 *oit = *it;
[465]123 }
124 return out;
125}
[1325]126
127void mathutil::hanning(Vector<Float>& out, Vector<Bool>& outmask,
128 const Vector<Float>& in, const Vector<Bool>& mask,
129 Bool relaxed, Bool ignoreOther) {
130 Vector< Vector<Float> > weights(8);
131 Vector<Float> vals(3);
132 vals = 0.0;weights[0] = vals;// FFF
133 vals[0] = 1.0; vals[1] = 0.0; vals[2] = 0.0; weights[1] = vals;// TFF
134 vals[0] = 0.0; vals[1] = 1.0; vals[2] = 0.0; weights[2] = vals;// FTF
135 vals[0] = 1.0/3.0; vals[1] = 2.0/3.0; vals[2] = 0.0; weights[3] = vals;// TTF
136 vals[0] = 0.0; vals[1] = 0.0; vals[2] = 1.0;weights[4] = vals;// FFT
137 vals[0] = 0.5; vals[1] = 0.0; vals[2] = 0.5; weights[5] = vals;// TFT
138 vals[0] = 0.0; vals[1] = 2.0/3.0; vals[2] = 1.0/3.0; weights[6] = vals;// FTT
139 vals[0] = 0.25; vals[1] = 0.5; vals[2] = 0.25; weights[7] = vals;// TTT
140 // Chris' case
141 Vector<Bool> weighted(8);
142 if (relaxed) {
143 weighted = False;
144 weighted[7] = True;
145
146 } else {
147 weighted = True;
148 weighted[0] = False;
149 }
150
151 out.resize(in.nelements());
152 outmask.resize(mask.nelements());
153 // make special case for first and last
154 /// ...here
155 // loop from 1..n-2
156 out.resize(in.nelements());
157 out[0] = in[0];out[out.nelements()-1] = in[in.nelements()-1];
158 outmask.resize(mask.nelements());
159 outmask = False;
160 uInt m;Vector<Float>* w;
161 for (uInt i=1; i < out.nelements()-1;++i) {
162 m = mask[i-1] + 2*mask[i] + 4*mask[i+1];
163 w = &(weights[m]);
164 if (weighted[m]) {
165 out[i] = (*w)[0]*in[i-1] + (*w)[1]*in[i] + (*w)[2]*in[i+1];
166 outmask[i] = True;
167 } else { // mask it
168 out[i] = in[i];//use arbitrary value
169 outmask[i] = False;
170 }
171 }
172}
[1373]173
174
175void mathutil::runningMedian(Vector<Float>& out, Vector<Bool>& outflag,
176 const Vector<Float>& in, const Vector<Bool>& flag,
177 float width)
178{
179 Int hwidth = Int(width+0.5);
180 Int fwidth = hwidth*2+1;
181 out.resize(in.nelements());
182 outflag.resize(flag.nelements());
183 MedianSlider ms(hwidth);
184 Slice sl(0, fwidth-1);
185 Float medval = ms.add(const_cast<Vector<Float>& >(in)(sl),
186 const_cast<Vector<Bool>& >(flag)(sl));
187 uInt n = in.nelements();
188 for (uInt i=hwidth; i<(n-hwidth); ++i) {
189 // add data value
[1382]190 out[i] = ms.add(in[i+hwidth], flag[i+hwidth]);
[1373]191 outflag[i] = (ms.nval() == 0);
192 }
193 // replicate edge values from fisrt value with full width of values
194 for (uInt i=0;i<hwidth;++i) {
195 out[i] = out[hwidth];
196 outflag[i] = outflag[hwidth];
197 out[n-1-i] = out[n-1-hwidth];
198 outflag[n-1-i] = outflag[n-1-hwidth];
199 }
200}
Note: See TracBrowser for help on using the repository browser.