[med-svn] [SCM] aghermann branch, master, updated. 2523fe1ce5b3da7059d8e7aa2f2a4251854704cf

Andrei Zavada johnhommer at gmail.com
Mon Jul 22 23:31:23 UTC 2013


The following commit has been merged in the master branch:
commit 2e5485095074b8da7d9d2449a92f4da4f82d7c2c
Author: Andrei Zavada <johnhommer at gmail.com>
Date:   Mon Jul 22 01:54:11 2013 +0300

    simplify, template sigproc::interpolate() on 3rd arg

diff --git a/src/aghermann/ui/sf/channel.cc b/src/aghermann/ui/sf/channel.cc
index e7bfb35..bb2506a 100644
--- a/src/aghermann/ui/sf/channel.cc
+++ b/src/aghermann/ui/sf/channel.cc
@@ -216,7 +216,7 @@ get_psd_course()
 		auto xi = vector<size_t> (tmp.size());
 		for ( size_t i = 0; i < tmp.size(); ++i )
 			xi[i] = i;
-		psd.course = sigproc::interpolate( xi, 3600/_p.pagesize(), tmp, 3./3600);
+		psd.course = sigproc::interpolate<TFloat>( xi, 3600/_p.pagesize(), tmp, 3./3600);
 	} else
 		psd.course = tmp;
 }
@@ -235,9 +235,10 @@ get_psd_in_bands()
 				_upto = _p._p.ED->freq_bands[b][1];
 			auto tmp = crecording.psd_profile.course( _from, _upto);
 			psd.course_in_bands[b] =
-				sigproc::interpolate( xi, 3600/_p.pagesize(),
-						      tmp,
-						      3./3600);
+				sigproc::interpolate<TFloat>(
+					xi, 3600/_p.pagesize(),
+					tmp,
+					3./3600);
 		}
 	} else
 		for ( size_t b = 0; b <= psd.uppermost_band; ++b ) {
@@ -260,7 +261,7 @@ get_swu_course()
 		auto xi = vector<size_t> (tmp.size());
 		for ( size_t i = 0; i < tmp.size(); ++i )
 			xi[i] = i;
-		swu.course = sigproc::interpolate( xi, 3600/_p.pagesize(), tmp, 3./3600);
+		swu.course = sigproc::interpolate<TFloat>( xi, 3600/_p.pagesize(), tmp, 3./3600);
 	} else
 		swu.course = tmp;
 }
@@ -277,7 +278,8 @@ get_mc_course()
 		auto xi = vector<size_t> (tmp.size());
 		for ( size_t i = 0; i < tmp.size(); ++i )
 			xi[i] = i;
-		mc.course = sigproc::interpolate( xi, 3600/_p.pagesize(), tmp, 3./3600);
+		mc.course = sigproc::interpolate<TFloat>(
+			xi, 3600/_p.pagesize(), tmp, 3./3600);
 	} else
 		mc.course = tmp;
 }
diff --git a/src/libsigproc/sigproc.cc b/src/libsigproc/sigproc.cc
index d78804a..730dd20 100644
--- a/src/libsigproc/sigproc.cc
+++ b/src/libsigproc/sigproc.cc
@@ -11,8 +11,6 @@
 
 #include <vector>
 #include <valarray>
-#include <gsl/gsl_interp.h>
-#include <gsl/gsl_spline.h>
 
 #include "sigproc.hh"
 
@@ -75,68 +73,6 @@ resample( const valarray<double>& signal,
 
 
 
-
-
-
-
-valarray<double>
-sigproc::
-interpolate_d( const vector<size_t>& xi,
-	       unsigned samplerate,
-	       const valarray<double>& y,
-	       float dt)
-{
-//	if ( 1. / samplerate > dt )
-//		return y;
-
-	size_t i;
-	valarray<double>
-		x_known (xi.size()),
-		y_known (xi.size());
-	for ( i = 0; i < xi.size(); ++i ) {
-		x_known[i] = (double)xi[i] / samplerate;
-		y_known[i] = y[ xi[i] ];
-	}
-
-	gsl_spline *spline = gsl_spline_alloc( gsl_interp_akima, xi.size());
-	gsl_interp_accel *acc = gsl_interp_accel_alloc();
-
-	gsl_spline_init( spline, &x_known[0], &y_known[0], xi.size());
-
-	double	t;
-	size_t	pad = (1./samplerate / dt) // this I understand
-			/ 2;                // this, I don't
-	valarray<double>
-		out (ceilf((x_known[x_known.size()-1] - x_known[0] + 1./samplerate) / dt) + 1);
-	for ( i = pad, t = x_known[0]; t < x_known[x_known.size()-1]; ++i, t += dt )
-		out[i] = gsl_spline_eval( spline, t, acc);
-
-	gsl_interp_accel_free( acc);
-	gsl_spline_free( spline);
-
-	return out;
-}
-
-
-valarray<float>
-sigproc::
-interpolate( const vector<size_t>& xi,
-	     unsigned samplerate,
-	     const valarray<float>& y,
-	     float dx)
-{
-	valarray<double> in (y.size());
-	for ( size_t i = 0; i < y.size(); ++i )
-		in[i] = y[i];
-
-	valarray<double> tmp = sigproc::interpolate_d( xi, samplerate, in, dx);
-
-	valarray<float> out (tmp.size());
-	for ( size_t i = 0; i < tmp.size(); ++i )
-		out[i] = tmp[i];
-	return out;
-}
-
 // Local Variables:
 // Mode: c++
 // indent-tabs-mode: 8
diff --git a/src/libsigproc/sigproc.hh b/src/libsigproc/sigproc.hh
index 6277687..5cce45b 100644
--- a/src/libsigproc/sigproc.hh
+++ b/src/libsigproc/sigproc.hh
@@ -18,6 +18,8 @@
 #include <stdexcept>
 
 #include <gsl/gsl_math.h>
+#include <gsl/gsl_interp.h>
+#include <gsl/gsl_spline.h>
 #include <samplerate.h>
 
 #include "common/lang.hh"
@@ -67,24 +69,14 @@ resample( const valarray<double>& signal,
 	  int alg);
 
 
-valarray<double>
-interpolate_d( const vector<size_t>&,
-	       unsigned, const valarray<double>&, float);
 
-valarray<float>
-interpolate( const vector<size_t>& xi,
-	     unsigned samplerate,
-	     const valarray<float>& y,
-	     float dx);
-
-inline valarray<double>
-interpolate( const vector<size_t>& xi,
-	     size_t samplerate,
-	     const valarray<double>& y,
-	     float dx)
-{
-	return interpolate_d( xi, samplerate, y, dx);
-}
+
+
+
+template <typename T, class Container>
+valarray<T>
+interpolate( const vector<unsigned long>& xi,
+	     unsigned samplerate, const Container& y, double dt);
 
 
 
diff --git a/src/libsigproc/sigproc.ii b/src/libsigproc/sigproc.ii
index 335b3ba..99a7b86 100644
--- a/src/libsigproc/sigproc.ii
+++ b/src/libsigproc/sigproc.ii
@@ -59,7 +59,7 @@ derivative( const valarray<T>& a)
 	valarray<T> out (a.size());
 	for ( size_t i = 1; i < a.size(); ++i )
 		out[i-1] = a[i] - a[i-1];
-	return out;
+	return move( out);
 }
 
 
@@ -67,6 +67,43 @@ derivative( const valarray<T>& a)
 
 
 
+template <typename T, class Container>
+valarray<T>
+interpolate( const vector<unsigned long>& xi,
+	     unsigned samplerate, const Container& y, double dt)
+{
+	size_t i;
+	valarray<double>
+		x_known (xi.size()),
+		y_known (xi.size());
+	for ( i = 0; i < xi.size(); ++i ) {
+		x_known[i] = (double)xi[i] / samplerate;
+		y_known[i] = y[ xi[i] ];
+	}
+
+	gsl_spline *spline = gsl_spline_alloc( gsl_interp_akima, xi.size());
+	gsl_interp_accel *acc = gsl_interp_accel_alloc();
+
+	gsl_spline_init( spline, &x_known[0], &y_known[0], xi.size());
+
+	double	t;
+	size_t	pad = (1./samplerate / dt) // this I understand
+			/ 2;                // this, I don't
+	valarray<T>
+		out (ceilf((x_known[x_known.size()-1] - x_known[0] + 1./samplerate) / dt) + 1);
+	for ( i = pad, t = x_known[0]; t < x_known[x_known.size()-1]; ++i, t += dt )
+		out[i] = gsl_spline_eval( spline, t, acc);
+
+	gsl_interp_accel_free( acc);
+	gsl_spline_free( spline);
+
+	return move( out);
+}
+
+
+
+
+
 
 template <typename T>
 size_t
@@ -114,9 +151,9 @@ envelope( const SSignalRef<T>& in,
 
 	if ( mini.size() > 5 && maxi.size() > 5 ) {
 		if ( env_lp )
-			*env_lp = interpolate( mini, in.samplerate, in.signal, dt_);
+			*env_lp = interpolate<T>( mini, in.samplerate, in.signal, dt_);
 		if ( env_up )
-			*env_up = interpolate( maxi, in.samplerate, in.signal, dt_);
+			*env_up = interpolate<T>( maxi, in.samplerate, in.signal, dt_);
 		if ( mini_p )
 			*mini_p = mini;
 		if ( maxi_p )
@@ -191,7 +228,7 @@ dzcdf( const SSignalRef<T>& in,
 		}
 		I = J;
 	}
-	return interpolate( xi, in.samplerate, y, 1./in.samplerate);
+	return move( interpolate<T>( xi, in.samplerate, y, 1./in.samplerate));
 }
 
 

-- 
Sleep experiment manager



More information about the debian-med-commit mailing list