Newer
Older
/////////////////////////////////////////////////////////////////////
//
// Performs deconvolution, converting signal to spline coefficients.
//
/////////////////////////////////////////////////////////////////////
template<class T>
bool Splinterpolator<T>::calc_coef(const T *data, bool copy)
if (_order < 2 && !copy) { _cptr = data; return(false); }
// Allocate memory and put the original data into _coef
unsigned int ts=1;
for (unsigned int i=0; i<_dim.size(); i++) ts *= _dim[i];
memcpy(_coef,data,ts*sizeof(T));
if (_order < 2) return(true); // If nearest neighbour or linear, that's all we need
// Loop over all non-singleton dimensions and deconvolve along them
//
std::vector<unsigned int> tdim(_dim.size()-1,0);
for (unsigned int cdir=0; cdir<_dim.size(); cdir++) {
if (_dim[cdir] > 1) deconv_along(cdir);
/////////////////////////////////////////////////////////////////////
//
// Performs deconvolution along one of the dimensions, visiting
// all points along the other dimensions.
//
/////////////////////////////////////////////////////////////////////
template<class T>
void Splinterpolator<T>::deconv_along(unsigned int dim)
{
// Set up to reflect "missing" dimension
//
std::vector<unsigned int> rdim(4,1); // Sizes along remaining dimensions
std::vector<unsigned int> rstep(4,1); // Step-sizes (in "volume") of remaining dimensions
unsigned int mdim = 1; // Size along "missing" dimension
unsigned int mstep = 1; // Step-size along "missing" dimension
for (unsigned int i=0, j=0, ss=1; i<5; i++) {
if (i == dim) { // If it is our "missing" dimension
mdim = _dim[i];
mstep = ss;
}
else {
rdim[j] = _dim[i];
rstep[j++] = ss;
}
ss *= _dim[i];
}
SplineColumn col(mdim,mstep); // Column helps us do the job
for (unsigned int l=0; l<rdim[3]; l++) {
for (unsigned int k=0; k<rdim[2]; k++) {
for (unsigned int j=0; j<rdim[1]; j++) {
T *dp = _coef + l*rstep[3] + k*rstep[2] + j*rstep[1];
for (unsigned int i=0; i<rdim[0]; i++, dp+=rstep[0]) {
col.Get(dp); // Extract a column from the volume
col.Deconv(_order,_et[dim],_prec); // Deconvolve it
col.Set(dp); // Put back the deconvolved column
}
}
}
}
return;
}
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
/////////////////////////////////////////////////////////////////////
//
// Here starts private member functions for SplineColumn
//
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
//
// This function returns the poles and scale-factors for splines
// of order 2--7. The values correspond to those found in
// table 1 in Unsers 1993 paper:
// B-spline signal processing. II. Efficiency design and applications
//
// The actual values have been taken from
// http://bigwww.epfl.ch/thevenaz/interpolation/coeff.c
//
/////////////////////////////////////////////////////////////////////
template<class T>
unsigned int Splinterpolator<T>::SplineColumn::get_poles(unsigned int order, double *z, unsigned int *sf) const
{
unsigned int np = 0; // # of poles
switch (order) {
case 2:
np = 1;
z[0] = 2.0*sqrt(2.0) - 3.0;
*sf = 8;
break;
case 3:
np = 1;
z[0] = sqrt(3.0) - 2.0;
*sf = 6;
break;
case 4:
np = 2;
z[0] = sqrt(664.0 - sqrt(438976.0)) + sqrt(304.0) - 19.0;
z[1] = sqrt(664.0 + sqrt(438976.0)) - sqrt(304.0) - 19.0;
*sf = 384;
break;
case 5:
np = 2;
z[0] = sqrt(135.0 / 2.0 - sqrt(17745.0 / 4.0)) + sqrt(105.0 / 4.0) - 13.0 / 2.0;
z[1] = sqrt(135.0 / 2.0 + sqrt(17745.0 / 4.0)) - sqrt(105.0 / 4.0) - 13.0 / 2.0;
*sf = 120;
break;
case 6:
np = 3;
z[0] = -0.48829458930304475513011803888378906211227916123938;
z[1] = -0.081679271076237512597937765737059080653379610398148;
z[2] = -0.0014141518083258177510872439765585925278641690553467;
*sf = 46080;
break;
case 7:
np = 3;
z[0] = -0.53528043079643816554240378168164607183392315234269;
z[1] = -0.12255461519232669051527226435935734360548654942730;
z[2] = -0.0091486948096082769285930216516478534156925639545994;
*sf = 5040;
break;
default:
throw SplinterpolatorException("SplineColumn::get_poles: invalid order of spline");
break;
}
return(np);
}
/////////////////////////////////////////////////////////////////////
//
// Initialises the first value for the forward sweep. The initialisation
// will always be an approximation (this is where the "infinite" in IIR
// breaks down) so the value will be calculated to a predefined precision.
//
/////////////////////////////////////////////////////////////////////
template<class T>
double Splinterpolator<T>::SplineColumn::init_fwd_sweep(double z, ExtrapolationType et, double prec) const
{
//
// Move logs away from here after debugging
//
unsigned int n = static_cast<unsigned int>((log(prec)/log(abs(z))) + 1.5);
n = (n > _sz) ? _sz : n;
double iv = _col[0];
if (et == Periodic) {
double *ptr=&_col[_sz-1];
double z2i=z;
for (unsigned int i=1; i<n; i++, ptr--, z2i*=z) iv += z2i * *ptr;
}
else {
double z2i=z;
for (unsigned int i=1; i<n; i++, ptr++, z2i*=z) iv += z2i * *ptr;
}
return(iv);
}
/////////////////////////////////////////////////////////////////////
//
// Initialises the first value for the backward sweep. The initialisation
// will always be an approximation (this is where the "infinite" in IIR
// breaks down) so the value will be calculated to a predefined precision.
//
/////////////////////////////////////////////////////////////////////
template<class T>
double Splinterpolator<T>::SplineColumn::init_bwd_sweep(double z, double lv, ExtrapolationType et, double prec) const
{
double iv = 0.0;
unsigned int n = static_cast<unsigned int>((log(prec)/log(abs(z))) + 1.5);
n = (n > _sz) ? _sz : n;
iv = z * _col[_sz-1];
double z2i = z*z;
double *ptr=_col;
for (unsigned int i=1; i<n; i++, ptr++, z2i*=z) {
iv += z2i * *ptr;
}
iv /= (z2i-1.0);
}
else {
iv = -z/(1.0-z*z) * (2.0*_col[_sz-1] - lv);
}
return(iv);
}
} // End namespace SPLINTERPOLATOR
#endif // End #ifndef splinterpolator.h