Ignore:
Timestamp:
06/29/09 12:04:00 (15 years ago)
Author:
Malte Marquarding
Message:

Ticket #167: c++ part of running polynomial smoothing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/MathUtils.cpp

    r1412 r1570  
    3838#include <casa/BasicSL/String.h>
    3939#include <scimath/Mathematics/MedianSlider.h>
     40
     41#include <scimath/Fitting/LinearFit.h>
     42#include <scimath/Functionals/Polynomial.h>
     43#include <scimath/Mathematics/AutoDiff.h>
     44
    4045
    4146#include "MathUtils.h"
     
    161166  MedianSlider ms(hwidth);
    162167  Slice sl(0, fwidth-1);
    163   Float medval = ms.add(const_cast<Vector<Float>& >(in)(sl), 
     168  Float medval = ms.add(const_cast<Vector<Float>& >(in)(sl),
    164169                  const_cast<Vector<Bool>& >(flag)(sl));
    165170  uInt n = in.nelements();
    166171  for (uInt i=hwidth; i<(n-hwidth); ++i) {
    167172    // add data value
    168     out[i] = ms.add(in[i+hwidth], flag[i+hwidth]); 
    169     outflag[i] = (ms.nval() == 0);   
    170   }
    171   // replicate edge values from fisrt value with full width of values
     173    out[i] = ms.add(in[i+hwidth], flag[i+hwidth]);
     174    outflag[i] = (ms.nval() == 0);
     175  }
     176  // replicate edge values from first value with full width of values
    172177  for (uInt i=0;i<hwidth;++i) {
    173178    out[i] = out[hwidth];
    174     outflag[i] = outflag[hwidth];   
     179    outflag[i] = outflag[hwidth];
    175180    out[n-1-i] = out[n-1-hwidth];
    176     outflag[n-1-i] = outflag[n-1-hwidth];   
    177   }
    178 }
     181    outflag[n-1-i] = outflag[n-1-hwidth];
     182  }
     183}
     184
     185void mathutil::polyfit(Vector<Float>& out, Vector<Bool>& outmask,
     186                       const Vector<Float>& in, const Vector<Bool>& mask,
     187                       float width, int order)
     188{
     189  Int hwidth = Int(width+0.5);
     190  Int fwidth = hwidth*2+1;
     191  out.resize(in.nelements());
     192  outmask.resize(mask.nelements());
     193  LinearFit<Float> fitter;
     194  Polynomial<Float> poly(order);
     195  fitter.setFunction(poly);
     196  Vector<Float> sigma(fwidth);
     197  sigma = 1.0;
     198  Vector<Float> parms;
     199  Vector<Float> x(fwidth);
     200  indgen(x);
     201
     202  uInt n = in.nelements();
     203
     204  for (uInt i=hwidth; i<(n-hwidth); ++i) {
     205    // add data value
     206    if (mask[i]) {
     207      Slice sl(i-hwidth, fwidth);
     208      const Vector<Float> &y = const_cast<Vector<Float>& >(in)(sl);
     209      const Vector<Bool> &m = const_cast<Vector<Bool>& >(mask)(sl);
     210      parms = fitter.fit(x, y, sigma, &m);
     211
     212      poly.setCoefficients(parms);
     213      out[i] = poly(x[hwidth]);//cout << in[i] <<"->"<<out[i]<<endl;
     214    } else {
     215      out[i] = in[i];
     216    }
     217    outmask[i] = mask[i];
     218  }
     219  // replicate edge values from first value with full width of values
     220  for (uInt i=0;i<hwidth;++i) {
     221    out[i] = out[hwidth];
     222    outmask[i] = outmask[hwidth];
     223    out[n-1-i] = out[n-1-hwidth];
     224    outmask[n-1-i] = outmask[n-1-hwidth];
     225  }
     226}
Note: See TracChangeset for help on using the changeset viewer.