@@ -52,6 +52,7 @@ POSSIBILITY OF SUCH DAMAGE.
5252#include " core/defines.h"
5353#include < Eigen/Core>
5454#include < vector>
55+ #include < algorithm>
5556#include < functional>
5657
5758namespace data_struct
@@ -254,6 +255,7 @@ class Spectra : public ArrayTr<_T>
254255TEMPLATE_CLASS_DLL_EXPORT Spectra<float >;
255256TEMPLATE_CLASS_DLL_EXPORT Spectra<double >;
256257
258+ // -----------------------------------------------------------------------------
257259// ----------------------------------------------------------------------------
258260
259261template <typename T_real>
@@ -345,31 +347,17 @@ DLL_EXPORT ArrayTr<T_real> snip_background(const Spectra<T_real> * const spectra
345347
346348 // FIRST SNIPPING
347349 int no_iterations = 2 ;
348-
349- int max_of_xmin = ( std::max) (xmin, (T_real) 0. 0 );
350- int min_of_xmax = ( std::min) (xmax, T_real (spectra->size () - 1 ));
350+
351+ int lo_limit = std::max< int > (xmin, 0 );
352+ int up_limit = std::min< int > (xmax, (spectra->size () - 1 ));
351353 for (int j = 0 ; j < no_iterations; j++)
352354 {
353355 for (long int k = 0 ; k < background.size (); k++)
354356 {
355- long int lo_index = k - current_width[k];
356- long int hi_index = k + current_width[k];
357- if (lo_index < max_of_xmin)
358- {
359- lo_index = max_of_xmin;
360- }
361- if (lo_index > min_of_xmax)
362- {
363- lo_index = min_of_xmax;
364- }
365- if (hi_index > min_of_xmax)
366- {
367- hi_index = min_of_xmax;
368- }
369- if (hi_index < max_of_xmin)
370- {
371- hi_index = max_of_xmin;
372- }
357+ int lo_index = std::max<int >(k - current_width[k], lo_limit);
358+ lo_index = std::min<int >(lo_index, up_limit);
359+ int hi_index = std::max<int >(k + current_width[k], lo_limit);
360+ hi_index = std::min<int >(k + current_width[k], up_limit);
373361 T_real temp = (background[lo_index] + background[hi_index]) / (T_real)2.0 ;
374362 if (background[k] > temp)
375363 {
@@ -382,24 +370,10 @@ DLL_EXPORT ArrayTr<T_real> snip_background(const Spectra<T_real> * const spectra
382370 {
383371 for (long int k = 0 ; k < background.size (); k++)
384372 {
385- long int lo_index = k - current_width[k];
386- long int hi_index = k + current_width[k];
387- if (lo_index < max_of_xmin)
388- {
389- lo_index = max_of_xmin;
390- }
391- if (lo_index > min_of_xmax)
392- {
393- lo_index = min_of_xmax;
394- }
395- if (hi_index > min_of_xmax)
396- {
397- hi_index = min_of_xmax;
398- }
399- if (hi_index < max_of_xmin)
400- {
401- hi_index = max_of_xmin;
402- }
373+ int lo_index = std::max<int >(k - current_width[k], lo_limit);
374+ lo_index = std::min<int >(lo_index, up_limit);
375+ int hi_index = std::max<int >(k + current_width[k], lo_limit);
376+ hi_index = std::min<int >(k + current_width[k], up_limit);
403377 T_real temp = (background[lo_index] + background[hi_index]) / (T_real)2.0 ;
404378 if (background[k] > temp)
405379 {
@@ -418,6 +392,46 @@ DLL_EXPORT ArrayTr<T_real> snip_background(const Spectra<T_real> * const spectra
418392}
419393
420394// ----------------------------------------------------------------------------
395+ /*
396+ template<typename T_real>
397+ DLL_EXPORT ArrayTr<T_real> snip_background2(const Spectra<T_real> * const spectra, T_real width)
398+ {
399+ width = width * 100.0;
400+ assert(spectra != nullptr);
401+ ArrayTr<T_real> boxcar;
402+ boxcar.resize(5);
403+ boxcar.setConstant(5, 1.0);
404+ ArrayTr<T_real> background = convolve1d(*spectra, boxcar);
405+ background += 1.0;
406+ background = Eigen::sqrt(background);
407+ background = Eigen::log(Eigen::log(background + (T_real)1.0) + (T_real)1.0);
408+
409+ while (width >= 0.5)
410+ {
411+ for (int k = 0; k < background.size(); k++)
412+ {
413+ int lo = std::max<int>( k - (int)width, 0);
414+ int hi = std::min<int>( k + (int)width, background.size()-1);
415+ T_real temp = (background[lo] + background[hi]) / (T_real)2.0;
416+ if (background[k] > temp)
417+ {
418+ background[k] = temp;
419+ }
420+ }
421+ width = width / T_real(M_SQRT2); // window_rf
422+ }
423+
424+ background = Eigen::exp(Eigen::exp(background) - (T_real)1.0) - (T_real)1.0;
425+ background = Eigen::pow(background, 2.0);
426+ background -= 1.0;
427+ background = background.unaryExpr([](T_real v) { return std::isfinite(v) ? v : (T_real)0.0; });
428+
429+ return background;
430+
431+ }
432+ */
433+ // ----------------------------------------------------------------------------
434+
421435
422436template <typename T_real>
423437using IO_Callback_Func_Def = std::function<void (size_t , size_t , size_t , size_t , size_t , Spectra<T_real>*, void *)>;
0 commit comments