/********************************************************************** Filter.cc: class for filtering Author(s): Pieter.Kempeneers@ec.europa.eu Copyright (C) 2016-2022 European Union (Joint Research Centre) This file is part of jiplib. jiplib is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. jiplib is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with jiplib. If not, see . ***********************************************************************/ #include "Filter.h" #include // #include #include #include using namespace std; filter::Filter::Filter(void) : m_padding("symmetric") { } filter::Filter::Filter(const vector &taps) : m_padding("symmetric") { setTaps(taps); } void filter::Filter::setTaps(const vector &taps, bool normalize) { m_taps.resize(taps.size()); double norm=0; for(int itap=0;itap vnodata){ m_noDataValues=vnodata; return m_noDataValues.size(); }; void filter::Filter::dwtForward(Jim& input, Jim& output, const std::string& wavelet_type, int family){ const char* pszMessage; // void* pProgressArg=NULL; // GDALProgressFunc pfnProgress=GDALTermProgress; // double progress=0; // MyProgressFunc(progress,pszMessage,pProgressArg); Vector2d lineInput(input.nrOfBand(),input.nrOfCol()); Vector2d lineOutput(input.nrOfBand(),input.nrOfCol()); for(unsigned int y=0;y pixelInput(input.nrOfBand()); for(unsigned int x=0;x lineInput(input.nrOfBand(),input.nrOfCol()); Vector2d lineOutput(input.nrOfBand(),input.nrOfCol()); for(unsigned int y=0;y pixelInput(input.nrOfBand()); for(unsigned int x=0;x lineInput(input.nrOfBand(),input.nrOfCol()); Vector2d lineOutput(input.nrOfBand(),input.nrOfCol()); for(unsigned int y=0;y pixelInput(input.nrOfBand()); for(unsigned int x=0;x lineInput(input.nrOfBand(),input.nrOfCol()); Vector2d lineOutput(input.nrOfBand(),input.nrOfCol()); for(unsigned int y=0;y pixelInput(input.nrOfBand()); for(unsigned int x=0;x=band) pixelInput[iband]=0; } dwtInverse(pixelInput,wavelet_type,family); for(unsigned int iband=0;iband& data, const std::string& wavelet_type, int family){ int origsize=data.size(); //make sure data size if power of 2 while(data.size()&(data.size()-1)) data.push_back(data.back()); int nsize=data.size(); gsl_wavelet *w; gsl_wavelet_workspace *work; assert(nsize); w=gsl_wavelet_alloc(getWaveletType(wavelet_type),family); work=gsl_wavelet_workspace_alloc(nsize); gsl_wavelet_transform_forward(w,&(data[0]),1,nsize,work); data.erase(data.begin()+origsize,data.end()); gsl_wavelet_free (w); gsl_wavelet_workspace_free (work); } //todo: support different padding strategies void filter::Filter::dwtInverse(std::vector& data, const std::string& wavelet_type, int family){ int origsize=data.size(); //make sure data size if power of 2 while(data.size()&(data.size()-1)) data.push_back(data.back()); int nsize=data.size(); gsl_wavelet *w; gsl_wavelet_workspace *work; assert(nsize); w=gsl_wavelet_alloc(getWaveletType(wavelet_type),family); work=gsl_wavelet_workspace_alloc(nsize); gsl_wavelet_transform_inverse(w,&(data[0]),1,nsize,work); data.erase(data.begin()+origsize,data.end()); gsl_wavelet_free (w); gsl_wavelet_workspace_free (work); } //todo: support different padding strategies void filter::Filter::dwtCut(std::vector& data, const std::string& wavelet_type, int family, double cut){ int origsize=data.size(); //make sure data size if power of 2 while(data.size()&(data.size()-1)) data.push_back(data.back()); int nsize=data.size(); gsl_wavelet *w; gsl_wavelet_workspace *work; assert(nsize); w=gsl_wavelet_alloc(getWaveletType(wavelet_type),family); work=gsl_wavelet_workspace_alloc(nsize); gsl_wavelet_transform_forward(w,&(data[0]),1,nsize,work); std::vector abscoeff(data.size()); size_t* p=new size_t[data.size()]; for(int index=0;index1)? true:false; Vector2d lineInput(input.nrOfBand(),input.nrOfCol()); Vector2d lineOutput(input.nrOfBand(),input.nrOfCol()); if(verbose) std::cout << "Number of bands in input: " << lineInput.size() << std::endl; const char* pszMessage; // void* pProgressArg=NULL; // GDALProgressFunc pfnProgress=GDALTermProgress; // double progress=0; // MyProgressFunc(progress,pszMessage,pProgressArg); for(unsigned int y=0;y pixelInput(input.nrOfBand()); vector pixelOutput(input.nrOfBand()); for(unsigned int x=0;x lineInput(input.nrOfBand(),input.nrOfCol()); Vector2d lineOutput(input.nrOfBand(),input.nrOfCol()); const char* pszMessage; // void* pProgressArg=NULL; // GDALProgressFunc pfnProgress=GDALTermProgress; // double progress=0; // MyProgressFunc(progress,pszMessage,pProgressArg); for(unsigned int y=0;y pixelInput(input.nrOfBand()); vector pixelOutput(input.nrOfBand()); for(unsigned int x=0;x0); m_taps.resize(dim); for(int itap=0;itap lineInput(input.nrOfBand(),input.nrOfCol()); Vector2d lineOutput(input.nrOfBand(),input.nrOfCol()); const char* pszMessage; // void* pProgressArg=NULL; // GDALProgressFunc pfnProgress=GDALTermProgress; // double progress=0; // MyProgressFunc(progress,pszMessage,pProgressArg); for(unsigned int y=0;y pixelInput(input.nrOfBand()); vector pixelOutput(input.nrOfBand()); for(unsigned int x=0;x lineInput(input.nrOfBand(),input.nrOfCol()); assert(output.nrOfCol()==input.nrOfCol()); vector lineOutput(output.nrOfCol()); statfactory::StatFactory stat; stat.setNoDataValues(m_noDataValues); const char* pszMessage; // void* pProgressArg=NULL; // GDALProgressFunc pfnProgress=GDALTermProgress; // double progress=0; // MyProgressFunc(progress,pszMessage,pProgressArg); for(unsigned int y=0;y pixelInput(input.nrOfBand()); for(unsigned int x=0;x& methods) { assert(output.nrOfBand()==methods.size()); Vector2d lineInput(input.nrOfBand(),input.nrOfCol()); assert(output.nrOfCol()==input.nrOfCol()); Vector2d lineOutput(methods.size(),output.nrOfCol()); statfactory::StatFactory stat; stat.setNoDataValues(m_noDataValues); const char* pszMessage; // void* pProgressArg=NULL; // GDALProgressFunc pfnProgress=GDALTermProgress; // double progress=0; // MyProgressFunc(progress,pszMessage,pProgressArg); for(unsigned int y=0;y pixelInput(input.nrOfBand()); for(unsigned int x=0;x lineInput(input.nrOfBand(),input.nrOfCol()); Vector2d lineOutput(input.nrOfBand(),input.nrOfCol());; const char* pszMessage; // void* pProgressArg=NULL; // GDALProgressFunc pfnProgress=GDALTermProgress; // double progress=0; // MyProgressFunc(progress,pszMessage,pProgressArg); for(unsigned int y=0;y pixelInput(input.nrOfBand()); vector pixelOutput; for(unsigned int x=0;x &tapz, int np, int nl, int nr, int ld, int m) { int j, k, imj, ipj, kk, mm; double d, fac, sum; // c.resize(nl+1+nr); vector tmpc(np); if(np < nl + nr + 1 || nl < 0 || nr < 0 || ld > m || nl + nr < m) { std::string errorString="Error: could not get savgolay coefficients, inconsistent arguments (e.g., number of planes are insufficient to perform Savgolay filtering)"; throw(errorString); } vector indx(m + 1, 0); vector a((m + 1) * (m + 1), 0.0); vector b(m + 1, 0.0); for(ipj = 0; ipj <= (m << 1); ++ipj) { sum = (ipj ? 0.0 : 1.0); for(k = 1; k <= nr; ++k) sum += (int)pow((double)k, (double)ipj); for(k = 1; k <= nl; ++k) sum += (int)pow((double) - k, (double)ipj); mm = (ipj < 2 * m - ipj ? ipj : 2 * m - ipj); for(imj = -mm; imj <= mm; imj += 2) a[(ipj + imj) / 2 * (m + 1) + (ipj - imj) / 2] = sum; } ludcmp(a, indx, d); for(j = 0; j < m + 1; ++j) b[j] = 0.0; b[ld] = 1.0; lubksb(a, indx, b); // for(kk = 0; kk < np; ++kk) // c[kk] = 0.0; for(k = -nl; k <= nr; ++k) { // for(k = -nl; k < nr; ++k) { sum = b[0]; fac = 1.0; for(mm = 1; mm <= m; ++mm) sum += b[mm] * (fac *= k); // store in wrap=around order kk = (np - k) % np; //re-order c as I need for taps // kk=k+nl; tmpc[kk] = sum; } tapz.resize(nl+1+nr); // for(k=0;k &a, vector &indx, double &d) { const double TINY = 1.0e-20; int i, imax = -1, j, k; double big, dum, sum, temp; int n = indx.size(); vector vv(n, 0.0); d = 1.0; for(i = 0; i < n; ++i) { big = 0.0; for(j = 0; j < n; ++j) if((temp = fabs(a[i * n + j])) > big) big = temp; if(big == 0.0) { cerr << "Singular matrix in routine ludcmp" << endl; return; } vv[i] = 1. / big; } for(j = 0; j < n; ++j) { for(i = 0; i < j; ++i) { sum = a[i * n + j]; for(k = 0; k < i; ++k) sum -= a[i * n + k] * a[k * n + j]; a[i * n + j] = sum; } big = 0.0; for(i = j; i < n; ++i) { sum = a[i * n + j]; for(k = 0; k < j; ++k) sum -= a[i * n + k] * a[k * n + j]; a[i * n + j] = sum; if((dum = vv[i] * fabs(sum)) >= big) { big = dum; imax = i; } } if(j != imax) { for(k = 0; k < n; ++k) { dum = a[imax * n + k]; a[imax * n + k] = a[j * n + k]; a[j * n + k] = dum; } d = -d; vv[imax] = vv[j]; } indx[j] = imax; if(a[j * n + j] == 0.0) a[j * n + j] = TINY; if(j != n - 1) { dum = 1. / a[j * n + j]; for(i = j + 1; i < n; ++i) a[i * n + j] *= dum; } } } void filter::Filter::lubksb(vector &a, vector &indx, vector &b) { int i, ii = 0, ip, j; double sum; int n = indx.size(); for(i = 0; i < n; ++i) { ip = indx[i]; sum = b[ip]; b[ip] = b[i]; if(ii != 0) for(j = ii - 1; j < i; ++j) sum -= a[i * n + j] * b[j]; else if(sum != 0.0) ii = i + 1; b[i] = sum; } for(i = n - 1; i >= 0; --i) { sum = b[i]; for(j = i + 1; j < n; ++j) sum -= a[i * n + j] * b[j]; b[i] = sum / a[i * n + i]; } } double filter::Filter::getCentreWavelength(const std::vector &wavelengthIn, const Vector2d& srf, const std::string& interpolationType, double delta, bool verbose) { assert(srf.size()==2);//[0]: wavelength, [1]: response function int nband=srf[0].size(); double start=floor(wavelengthIn[0]); double end=ceil(wavelengthIn.back()); if(verbose) std::cout << "wavelengths in [" << start << "," << end << "]" << std::endl << std::flush; statfactory::StatFactory stat; gsl_interp_accel *acc; stat.allocAcc(acc); gsl_spline *spline; stat.getSpline(interpolationType,nband,spline); stat.initSpline(spline,&(srf[0][0]),&(srf[1][0]),nband); if(verbose) std::cout << "calculating norm of srf" << std::endl << std::flush; double norm=0; norm=gsl_spline_eval_integ(spline,srf[0].front(),srf[0].back(),acc); if(verbose) std::cout << "norm of srf: " << norm << std::endl << std::flush; gsl_spline_free(spline); gsl_interp_accel_free(acc); std::vector wavelength_fine; for(double win=floor(wavelengthIn[0]);win<=ceil(wavelengthIn.back());win+=delta) wavelength_fine.push_back(win); if(verbose) std::cout << "interpolate wavelengths to " << wavelength_fine.size() << " entries " << std::endl; std::vector srf_fine;//spectral response function, interpolated for wavelength_fine stat.interpolateUp(srf[0],srf[1],wavelength_fine,interpolationType,srf_fine,verbose); assert(srf_fine.size()==wavelength_fine.size()); gsl_interp_accel *accOut; stat.allocAcc(accOut); gsl_spline *splineOut; stat.getSpline(interpolationType,wavelength_fine.size(),splineOut); assert(splineOut); std::vector wavelengthOut(wavelength_fine.size()); for(unsigned int iband=0;iband &wavelengthIn, const Jim& input, const vector &wavelengthOut, const vector &fwhm, const std::string& interpolationType, Jim& output, bool verbose){ // Vector2d lineInput(input.nrOfBand(),input.nrOfCol()); // Vector2d lineOutput(wavelengthOut.size(),input.nrOfCol()); // const char* pszMessage; // void* pProgressArg=NULL; // GDALProgressFunc pfnProgress=GDALTermProgress; // double progress=0; // pfnProgress(progress,pszMessage,pProgressArg); // for(unsigned int y=0;y(wavelengthIn,lineInput,wavelengthOut,fwhm, interpolationType, lineOutput, verbose); // for(unsigned int iband=0;iband &wavelengthIn, const Jim& input, const vector< Vector2d > &srf, const std::string& interpolationType, Jim& output, bool verbose){ // assert(output.nrOfBand()==srf.size()); // double centreWavelength=0; // Vector2d lineInput(input.nrOfBand(),input.nrOfCol()); // const char* pszMessage; // void* pProgressArg=NULL; // GDALProgressFunc pfnProgress=GDALTermProgress; // double progress=0; // pfnProgress(progress,pszMessage,pProgressArg); // for(unsigned int y=0;y lineOutput(input.nrOfCol()); // centreWavelength=applySrf(wavelengthIn,lineInput,srf[isrf], interpolationType, lineOutput, verbose); // for(unsigned int iband=0;iband