- Timestamp:
- 03/24/11 18:04:56 (14 years ago)
- Location:
- trunk/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/Scantable.cpp
r2058 r2064 1966 1966 } 1967 1967 1968 std::vector<float> Scantable::doCubicSplineFitting(const std::vector<float>& data, const std::vector<bool>& mask, std::vector<int>& sectionRanges, std::vector<float>& params, int nPiece, float thresClip, int nIterClip, bool getResidual) {1968 std::vector<float> Scantable::doCubicSplineFitting(const std::vector<float>& data, const std::vector<bool>& mask, std::vector<int>& idxEdge, std::vector<float>& params, int nPiece, float thresClip, int nIterClip, bool getResidual) { 1969 1969 if (nPiece < 1) { 1970 1970 throw(AipsError("wrong number of the sections for fitting")); … … 1986 1986 int nData = x.size(); 1987 1987 int nElement = (int)(floor(floor((double)(nData/nPiece))+0.5)); 1988 1989 std::vector<int> sectionList0, sectionList1; 1990 std::vector<double> sectionListR; 1991 sectionList0.push_back(x[0]); 1992 sectionRanges.clear(); 1993 sectionRanges.push_back(x[0]); 1988 std::vector<double> invEdge; 1989 1990 idxEdge.clear(); 1991 idxEdge.push_back(x[0]); 1992 1994 1993 for (int i = 1; i < nPiece; ++i) { 1995 1994 int valX = x[nElement*i]; 1996 sectionList0.push_back(valX); 1997 sectionList1.push_back(valX); 1998 sectionListR.push_back(1.0/(double)valX); 1999 2000 sectionRanges.push_back(valX-1); 2001 sectionRanges.push_back(valX); 2002 } 2003 sectionList1.push_back(x[x.size()-1]+1); 2004 sectionRanges.push_back(x[x.size()-1]); 2005 2006 std::vector<double> x1, z1, r1; 1995 idxEdge.push_back(valX); 1996 invEdge.push_back(1.0/(double)valX); 1997 } 1998 1999 idxEdge.push_back(x[x.size()-1]+1); 2000 2001 std::vector<double> x1, x2, x3, z1, x1z1, x2z1, x3z1, r1; 2007 2002 for (int i = 0; i < nChan; ++i) { 2008 x1.push_back((double)i); 2009 z1.push_back((double)data[i]); 2003 double di = (double)i; 2004 double dD = (double)data[i]; 2005 x1.push_back(di); 2006 x2.push_back(di*di); 2007 x3.push_back(di*di*di); 2008 z1.push_back(dD); 2009 x1z1.push_back(dD*di); 2010 x2z1.push_back(dD*di*di); 2011 x3z1.push_back(dD*di*di*di); 2010 2012 r1.push_back(0.0); 2011 2013 } … … 2015 2017 2016 2018 for (int nClip = 0; nClip < nIterClip+1; ++nClip) { 2017 //xMatrix : horizontal concatenation of the least-sq. matrix (left) and an 2018 //identity matrix (right). 2019 //the right part is used to calculate the inverse matrix of the left part. 2019 // xMatrix : horizontal concatenation of 2020 // the least-sq. matrix (left) and an 2021 // identity matrix (right). 2022 // the right part is used to calculate the inverse matrix of the left part. 2020 2023 double xMatrix[nDOF][2*nDOF]; 2021 2024 double zMatrix[nDOF]; … … 2029 2032 2030 2033 for (int n = 0; n < nPiece; ++n) { 2031 for (int i = sectionList0[n]; i < sectionList1[n]; ++i) { 2034 for (int i = idxEdge[n]; i < idxEdge[n+1]; ++i) { 2035 2032 2036 if (maskArray[i] == 0) continue; 2033 double xp1 = x1[i]; 2034 double xp2 = xp1*xp1; 2035 double xp3 = xp2*xp1; 2036 double xp4 = xp3*xp1; 2037 double xp5 = xp4*xp1; 2038 double xp6 = xp5*xp1; 2037 2039 2038 xMatrix[0][0] += 1.0; 2040 xMatrix[0][1] += x p1;2041 xMatrix[0][2] += x p2;2042 xMatrix[0][3] += x p3;2043 xMatrix[1][1] += x p2;2044 xMatrix[1][2] += x p3;2045 xMatrix[1][3] += x p4;2046 xMatrix[2][2] += x p4;2047 xMatrix[2][3] += x p5;2048 xMatrix[3][3] += x p6;2039 xMatrix[0][1] += x1[i]; 2040 xMatrix[0][2] += x2[i]; 2041 xMatrix[0][3] += x3[i]; 2042 xMatrix[1][1] += x2[i]; 2043 xMatrix[1][2] += x3[i]; 2044 xMatrix[1][3] += x2[i]*x2[i]; 2045 xMatrix[2][2] += x2[i]*x2[i]; 2046 xMatrix[2][3] += x3[i]*x2[i]; 2047 xMatrix[3][3] += x3[i]*x3[i]; 2049 2048 zMatrix[0] += z1[i]; 2050 zMatrix[1] += xp1*z1[i]; 2051 zMatrix[2] += xp2*z1[i]; 2052 zMatrix[3] += xp3*z1[i]; 2049 zMatrix[1] += x1z1[i]; 2050 zMatrix[2] += x2z1[i]; 2051 zMatrix[3] += x3z1[i]; 2052 2053 2053 for (int j = 0; j < n; ++j) { 2054 double q = 1.0 - x p1*sectionListR[j];2054 double q = 1.0 - x1[i]*invEdge[j]; 2055 2055 q = q*q*q; 2056 2056 xMatrix[0][j+4] += q; 2057 xMatrix[1][j+4] += q*x p1;2058 xMatrix[2][j+4] += q*x p2;2059 xMatrix[3][j+4] += q*x p3;2057 xMatrix[1][j+4] += q*x1[i]; 2058 xMatrix[2][j+4] += q*x2[i]; 2059 xMatrix[3][j+4] += q*x3[i]; 2060 2060 for (int k = 0; k < j; ++k) { 2061 double r = 1.0 - x p1*sectionListR[k];2061 double r = 1.0 - x1[i]*invEdge[k]; 2062 2062 r = r*r*r; 2063 2063 xMatrix[k+4][j+4] += r*q; … … 2066 2066 zMatrix[j+4] += q*z1[i]; 2067 2067 } 2068 2068 2069 } 2069 2070 } … … 2125 2126 2126 2127 for (int n = 0; n < nPiece; ++n) { 2127 for (int i = sectionList0[n]; i < sectionList1[n]; ++i) {2128 r1[i] = a0 + a1*x1[i] + a2*x 1[i]*x1[i] + a3*x1[i]*x1[i]*x1[i];2128 for (int i = idxEdge[n]; i < idxEdge[n+1]; ++i) { 2129 r1[i] = a0 + a1*x1[i] + a2*x2[i] + a3*x3[i]; 2129 2130 } 2130 2131 params.push_back(a0); … … 2136 2137 2137 2138 double d = y[4+n]; 2138 a0 += d; 2139 a1 -= 3.0*d*sectionListR[n]; 2140 a2 += 3.0*d*sectionListR[n]*sectionListR[n]; 2141 a3 -= d*sectionListR[n]*sectionListR[n]*sectionListR[n]; 2139 double iE = invEdge[n]; 2140 a0 += d; 2141 a1 -= 3.0*d*iE; 2142 a2 += 3.0*d*iE*iE; 2143 a3 -= d*iE*iE*iE; 2142 2144 } 2143 2145 … … 2165 2167 } 2166 2168 if (newNData == currentNData) { 2167 break; //no additional flag. finish iteration.2169 break; //no more flag to add. iteration stops. 2168 2170 } else { 2169 2171 currentNData = newNData; … … 2186 2188 } 2187 2189 2188 2190 void Scantable::sinusoidBaseline(const std::vector<bool>& mask, int nMinWavesInSW, int nMaxWavesInSW, float thresClip, int nIterClip, bool outLogger, const std::string& blfile) { 2189 2191 ofstream ofs; 2190 2192 String coordInfo; … … 2324 2326 std::vector<double> x1, x2, x3, z1, x1z1, x2z1, x3z1, r1; 2325 2327 for (int i = 0; i < nChan; ++i) { 2326 x1.push_back((double)i); 2327 x2.push_back((double)(i*i)); 2328 x3.push_back((double)(i*i*i)); 2329 z1.push_back((double)data[i]); 2330 x1z1.push_back(((double)i)*(double)data[i]); 2331 x2z1.push_back(((double)(i*i))*(double)data[i]); 2332 x3z1.push_back(((double)(i*i*i))*(double)data[i]); 2328 double di = (double)i; 2329 double dD = (double)data[i]; 2330 x1.push_back(di); 2331 x2.push_back(di*di); 2332 x3.push_back(di*di*di); 2333 z1.push_back(dD); 2334 x1z1.push_back(di*dD); 2335 x2z1.push_back(di*di*dD); 2336 x3z1.push_back(di*di*di*dD); 2333 2337 r1.push_back(0.0); 2334 2338 } … … 2548 2552 2549 2553 /* for cspline. will be merged once cspline is available in fitter (2011/3/10 WK) */ 2550 void Scantable::outputFittingResult(bool outLogger, bool outTextFile, const std::vector<bool>& chanMask, int whichrow, const casa::String& coordInfo, bool hasSameNchan, ofstream& ofs, const casa::String& funcName, const std::vector<int>& pieceEdges, const std::vector<float>& params, const std::vector<bool>& fixed) {2554 void Scantable::outputFittingResult(bool outLogger, bool outTextFile, const std::vector<bool>& chanMask, int whichrow, const casa::String& coordInfo, bool hasSameNchan, ofstream& ofs, const casa::String& funcName, const std::vector<int>& edge, const std::vector<float>& params, const std::vector<bool>& fixed) { 2551 2555 if (outLogger || outTextFile) { 2552 2556 float rms = getRms(chanMask, whichrow); … … 2555 2559 if (outLogger) { 2556 2560 LogIO ols(LogOrigin("Scantable", funcName, WHERE)); 2557 ols << formatPiecewiseBaselineParams( pieceEdges, params, fixed, rms, masklist, whichrow, false) << LogIO::POST ;2561 ols << formatPiecewiseBaselineParams(edge, params, fixed, rms, masklist, whichrow, false) << LogIO::POST ; 2558 2562 } 2559 2563 if (outTextFile) { 2560 ofs << formatPiecewiseBaselineParams( pieceEdges, params, fixed, rms, masklist, whichrow, true) << flush;2564 ofs << formatPiecewiseBaselineParams(edge, params, fixed, rms, masklist, whichrow, true) << flush; 2561 2565 } 2562 2566 } … … 2635 2639 } 2636 2640 2637 std::string Scantable::formatBaselineParams(const std::vector<float>& params, const std::vector<bool>& fixed, float rms, const std::string& masklist, int whichrow, bool verbose) const 2638 { 2639 ostringstream oss; 2640 oss << formatBaselineParamsHeader(whichrow, masklist, verbose); 2641 2642 if (params.size() > 0) { 2643 for (uInt i = 0; i < params.size(); ++i) { 2644 if (i > 0) { 2641 std::string Scantable::formatBaselineParams(const std::vector<float>& params, const std::vector<bool>& fixed, float rms, const std::string& masklist, int whichrow, bool verbose, int start, int count, bool resetparamid) const 2642 { 2643 int nParam = (int)(params.size()); 2644 2645 if (nParam < 1) { 2646 return(" Not fitted"); 2647 } else { 2648 2649 ostringstream oss; 2650 oss << formatBaselineParamsHeader(whichrow, masklist, verbose); 2651 2652 if (start < 0) start = 0; 2653 if (count < 0) count = nParam; 2654 int end = start + count; 2655 if (end > nParam) end = nParam; 2656 int paramidoffset = (resetparamid) ? (-start) : 0; 2657 2658 for (int i = start; i < end; ++i) { 2659 if (i > start) { 2645 2660 oss << ","; 2646 2661 } 2647 std::string fix = ((fixed.size() > 0) && (fixed[i]) && verbose) ? "(fixed)" : ""; 2648 float vParam = params[i]; 2649 std::string equals = "= " + (String)((vParam < 0.0) ? "" : " "); 2650 oss << " p" << i << fix << equals << setprecision(6) << vParam; 2651 } 2662 std::string sFix = ((fixed.size() > 0) && (fixed[i]) && verbose) ? "(fixed)" : ""; 2663 oss << " p" << (i+paramidoffset) << sFix << "= " << right << setw(13) << setprecision(6) << params[i]; 2664 } 2665 2666 oss << endl; 2667 oss << formatBaselineParamsFooter(rms, verbose); 2668 2669 return String(oss); 2670 } 2671 2672 } 2673 2674 std::string Scantable::formatPiecewiseBaselineParams(const std::vector<int>& ranges, const std::vector<float>& params, const std::vector<bool>& fixed, float rms, const std::string& masklist, int whichrow, bool verbose) const 2675 { 2676 int nOutParam = (int)(params.size()); 2677 int nPiece = (int)(ranges.size()) - 1; 2678 2679 if (nOutParam < 1) { 2680 return(" Not fitted"); 2681 } else if (nPiece < 0) { 2682 return formatBaselineParams(params, fixed, rms, masklist, whichrow, verbose); 2683 } else if (nPiece < 1) { 2684 return(" Bad count of the piece edge info"); 2685 } else if (nOutParam % nPiece != 0) { 2686 return(" Bad count of the output baseline parameters"); 2652 2687 } else { 2653 oss << " Not fitted"; 2654 } 2655 oss << endl; 2656 2657 oss << formatBaselineParamsFooter(rms, verbose); 2658 oss << flush; 2659 2660 return String(oss); 2661 } 2662 2663 std::string Scantable::formatPiecewiseBaselineParams(const std::vector<int>& ranges, const std::vector<float>& params, const std::vector<bool>& fixed, float rms, const std::string& masklist, int whichrow, bool verbose) const 2664 { 2665 ostringstream oss; 2666 oss << formatBaselineParamsHeader(whichrow, masklist, verbose); 2667 2668 if ((params.size() > 0) && (ranges.size() > 0)) { 2669 if (((ranges.size() % 2) == 0) && ((params.size() % (ranges.size() / 2)) == 0)) { 2670 uInt nParam = params.size() / (ranges.size() / 2); 2671 stringstream ss; 2672 ss << ranges[ranges.size()-1] << flush; 2673 int wRange = ss.str().size()*2+5; 2688 2689 int nParam = nOutParam / nPiece; 2690 2691 ostringstream oss; 2692 oss << formatBaselineParamsHeader(whichrow, masklist, verbose); 2693 2694 stringstream ss; 2695 ss << ranges[nPiece] << flush; 2696 int wRange = ss.str().size() * 2 + 5; 2697 2698 for (int i = 0; i < nPiece; ++i) { 2674 2699 ss.str(""); 2675 for (uInt j = 0; j < ranges.size(); j+=2) { 2676 ss << " [" << ranges[j] << "," << ranges[j+1] << "]" << flush; 2677 oss << setw(wRange) << left << ss.str(); 2678 ss.str(""); 2679 for (uInt i = 0; i < nParam; ++i) { 2680 if (i > 0) { 2681 oss << ","; 2682 } 2683 std::string fix = ((fixed.size() > 0) && (fixed[i]) && verbose) ? "(fixed)" : ""; 2684 float vParam = params[j/2*nParam+i]; 2685 std::string equals = "= " + (String)((vParam < 0.0) ? "" : " "); 2686 oss << " p" << i << fix << equals << setprecision(6) << vParam; 2687 } 2688 oss << endl; 2689 } 2690 } else { 2691 oss << " " << endl; 2692 } 2693 } else { 2694 oss << " Not fitted" << endl; 2695 } 2696 2697 oss << formatBaselineParamsFooter(rms, verbose); 2698 oss << flush; 2699 2700 return String(oss); 2700 ss << " [" << ranges[i] << "," << (ranges[i+1]-1) << "]"; 2701 oss << left << setw(wRange) << ss.str(); 2702 oss << formatBaselineParams(params, fixed, rms, masklist, whichrow, false, i*nParam, nParam, true); 2703 } 2704 2705 oss << formatBaselineParamsFooter(rms, verbose); 2706 2707 return String(oss); 2708 } 2709 2701 2710 } 2702 2711 -
trunk/src/Scantable.h
r2058 r2064 544 544 const std::string& masklist, 545 545 int whichrow, 546 bool verbose=false) const; 546 bool verbose=false, 547 int start=-1, 548 int count=-1, 549 bool resetparamid=false) const; 547 550 std::string formatPiecewiseBaselineParams(const std::vector<int>& ranges, 548 551 const std::vector<float>& params, … … 669 672 std::vector<float> doCubicSplineFitting(const std::vector<float>& data, 670 673 const std::vector<bool>& mask, 671 std::vector<int>& sectionRanges,674 std::vector<int>& idxEdge, 672 675 std::vector<float>& params, 673 676 int nPiece=2, … … 695 698 //std::vector<bool> getCompositeChanMask(int whichrow, const std::vector<bool>& inMask, const std::vector<int>& edge, const int minEdgeSize, STLineFinder& lineFinder); 696 699 void outputFittingResult(bool outLogger, bool outTextFile, const std::vector<bool>& chanMask, int whichrow, const casa::String& coordInfo, bool hasSameNchan, std::ofstream& ofs, const casa::String& funcName, Fitter& fitter); 697 void outputFittingResult(bool outLogger, bool outTextFile, const std::vector<bool>& chanMask, int whichrow, const casa::String& coordInfo, bool hasSameNchan, std::ofstream& ofs, const casa::String& funcName, const std::vector<int>& pieceEdges, const std::vector<float>& params, const std::vector<bool>& fixed);700 void outputFittingResult(bool outLogger, bool outTextFile, const std::vector<bool>& chanMask, int whichrow, const casa::String& coordInfo, bool hasSameNchan, std::ofstream& ofs, const casa::String& funcName, const std::vector<int>& edge, const std::vector<float>& params, const std::vector<bool>& fixed); 698 701 void outputFittingResult(bool outLogger, bool outTextFile, const std::vector<bool>& chanMask, int whichrow, const casa::String& coordInfo, bool hasSameNchan, std::ofstream& ofs, const casa::String& funcName, const std::vector<float>& params, const std::vector<bool>& fixed); 699 702
Note:
See TracChangeset
for help on using the changeset viewer.