aimsalgo  5.1.2
Neuroimaging image processing
splinefilter_d.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 #ifndef AIMS_SIGNALFILTER_SPLINEFILTER_D_H
35 #define AIMS_SIGNALFILTER_SPLINEFILTER_D_H
36 
38 
39 //============================================================================
40 //
41 // DIRECT B-SPLINE FILTER
42 //
43 //============================================================================
44 
45 namespace aims {
46 
47  //--------------------------------------------------------------------------
48  // Execution
49  //--------------------------------------------------------------------------
50 
51  template <typename OUTP, typename INP>
53  const carto::VolumeRef<INP> & in, carto::VolumeRef<OUTP> & out ) const
54  {
56  std::vector<int> size = in.getSize();
57  double div = 1;
58  for( int i = 0; i < 4; ++i )
59  if( this->_dir[i] && size[i] > 1 )
60  div *= ( (int)this->_func.size() > i ? this->_func[i].scale()
61  : this->_func[0].scale() );
62  out /= div;
63  return out;
64  }
65 
66 } // namespace aims
67 
68 // Commenting this class out
69 // See splinefilter.h for the explanation
70 #if 0
71 //============================================================================
72 //
73 // INVERSE SMOOTHING B-SPLINE FILTER
74 //
75 //============================================================================
76 
77 #include <exception>
78 #include <string>
79 
80 namespace aims {
81 
82  //--------------------------------------------------------------------------
83  // Constructor / Copy
84  //--------------------------------------------------------------------------
85 
86  InverseSmoothingSplineFilter::InverseSmoothingSplineFilter( float lambda, unsigned n ):
87  IIRFilterBase(),
88  _order( n ),
89  _lambda( lambda ),
90  _causal(),
91  _anticausal(),
92  _symmetric(),
93  _interpolation(n)
94  {
95  setFilters();
96  }
97 
98  InverseSmoothingSplineFilter::InverseSmoothingSplineFilter( const InverseSmoothingSplineFilter & other ):
99  IIRFilterBase( other ),
100  _order( other._order ),
101  _lambda( other._lambda ),
102  _causal( other._causal ),
103  _anticausal( other._anticausal ),
104  _symmetric( other._symmetric ),
105  _interpolation( other._interpolation )
106  {}
107 
108 
109  InverseSmoothingSplineFilter::~InverseSmoothingSplineFilter()
110  {}
111 
112  InverseSmoothingSplineFilter & InverseSmoothingSplineFilter::operator=( const InverseSmoothingSplineFilter & other )
113  {
114  if( this != &other )
115  {
116  IIRFilterBase::operator=( other );
117  _order = other._order;
118  _lambda = other._lambda;
119  _causal = other._causal;
120  _anticausal = other._anticausal;
121  _symmetric = other._symmetric;
122  _interpolation = other._interpolation;
123  }
124  return *this;
125  }
126 
127  //--------------------------------------------------------------------------
128  // Parameters
129  //--------------------------------------------------------------------------
130 
131  const unsigned & InverseSmoothingSplineFilter::order() const
132  {
133  return _order;
134  }
135 
136  const float & InverseSmoothingSplineFilter::lambda() const
137  {
138  return _lambda;
139  }
140 
141  void InverseSmoothingSplineFilter::setOrder( unsigned n )
142  {
143  _order = n;
144  setFilters();
145  }
146 
147  void InverseSmoothingSplineFilter::setLambda( float l )
148  {
149  _lambda = l;
150  setFilters();
151  }
152 
153  //--------------------------------------------------------------------------
154  // Helpers
155  //--------------------------------------------------------------------------
156 
157  // For details, see:
158  // Unser, Aldroubi & Eden, "B-Spline Signal Processing: Part II"
159  // in IEEE Transactions on Signal Processing (1993)
160 
161  void InverseSmoothingSplineFilter::setFilters()
162  {
163  if( _order == 3 )
164  {
165  if( _lambda == 0. )
166  {
167  _ftype = Interpolation;
168  }
169  else
170  {
171 
172  double tmp1 = 24. * _lambda;
173  double tmp2 = tmp1 * std::sqrt( 3. + 144. * _lambda );
174  double ksi = 1. - 96. * _lambda + tmp2;
175  double rho = ( tmp1 - 1. - std::sqrt(ksi) ) / tmp1
176  * std::sqrt( ( 48. * _lambda + tmp2 ) / ksi );
177  double tan_omega = std::sqrt( ( 144. * _lambda - 1. ) / ksi );
178  double cos_omega = std::sqrt( 1. / ( 1 + tan_omega * tan_omega ) );
179  double b1 = 2. * rho * cos_omega;
180  double b2 = - rho * rho;
181  double gain = 1 - b1 - b2;
182  double delta = 4. * b2 + b1 * b1;
183 
184  std::cout << "tmp1 : " << tmp1 << std::endl;
185  std::cout << "tmp2 : " << tmp2 << std::endl;
186  std::cout << "ksi : " << ksi << std::endl;
187  std::cout << "rho : " << rho << std::endl;
188  std::cout << "tan_omega : " << tan_omega << std::endl;
189  std::cout << "cos_omega : " << cos_omega << std::endl;
190  std::cout << "b1 : " << b1 << std::endl;
191  std::cout << "b2 : " << b2 << std::endl;
192  std::cout << "delta : " << delta << std::endl;
193 
194  if( delta > 0 )
195  {
196  std::vector<double> poles;
197  poles.push_back( - ( b1 - std::sqrt( delta ) ) / ( 2. * b2 ) );
198  poles.push_back( - ( b1 + std::sqrt( delta ) ) / ( 2. * b2 ) );
199  std::cout << "z1 : " << poles[0] << std::endl;
200  std::cout << "z2 : " << poles[1] << std::endl;
201  _symmetric.setPoles( poles );
202  _symmetric.setGain( gain * gain );
203  _ftype = SymRealPole;
204  }
205  else
206  {
207  std::vector<double> coeffs;
208  coeffs.push_back( b1 );
209  coeffs.push_back( b2 );
210  _causal.setCoeffs( coeffs );
211  _causal.setGain( gain );
212  _anticausal.setCoeffs( coeffs );
213  _anticausal.setGain( gain );
214  _ftype = SymComplexPole;
215  }
216  }
217  }
218  else
219  throw std::logic_error( std::string("InverseSmoothingSplineFilter with") +
220  std::string(" n = ") + carto::toString(_order) +
221  std::string(" not implemented yet.") );
222  }
223 
224  void InverseSmoothingSplineFilter::filter1d( const carto::VolumeRef<double> in,
225  carto::VolumeRef<double> out, int dir ) const
226  {
227  switch( _ftype )
228  {
229  case Interpolation:
230  _interpolation.filter1d( in, out, dir );
231  break;
232  case SymComplexPole:
233  _symmetric.filter1d( in, out, dir );
234  break;
235  case SymRealPole:
236  _causal.filter1d( in, out, dir );
237  _anticausal.filter1d( out, out, dir );
238  break;
239  }
240  }
241 
242 } // namespace aims
243 #endif
244 
245 #endif // AIMS_SIGNALFILTER_SPLINEFILTER_D_H
std::vector< DiscreteBSpline > _func
carto::VolumeRef< OUT > execute(const carto::VolumeRef< IN > &in) const
Execution.
carto::VolumeRef< OUTP > execute(const carto::VolumeRef< INP > &in, carto::VolumeRef< OUTP > &out) const
Execution.
IIRFilterBase & operator=(const IIRFilterBase &other)
std::vector< int > getSize() const
std::string toString(const T &object)