The Flutter Shutter Code Optimizer
flutter_optimizer_gaussian.cpp
Go to the documentation of this file.
00001 /*flutter_optmizer_gaussian.cpp*/
00002 /*
00003 * Copyright 2014 IPOL Image Processing On Line http://www.ipol.im/
00004 *
00005 * This program is free software: you can redistribute it and/or modify
00006 * it under the terms of the GNU General Public License as published by
00007 * the Free Software Foundation, either version 3 of the License, or
00008 * (at your option) any later version.
00009 *
00010 * This program is distributed in the hope that it will be useful,
00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 * GNU General Public License for more details.
00014 *
00015 * You should have received a copy of the GNU General Public License
00016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 */
00028 #include <math.h>
00029 #include "flutter_optimizer_gaussian.h"
00033 #define eps  0.001//epsilon definition
00034 
00035 #ifndef ABS
00036 
00039 #define ABS(x)    (((x) > 0) ? (x) : (-(x)))
00040 #endif
00041 
00042 
00043 #ifndef M_PI
00044 
00047 #define M_PI 3.14159265358979323846
00048 #endif
00049 
00050 
00051 
00057 
00068 void gaussian_optimisator(double* code,double sigma,
00069                           int code_length,double deltat)
00070 {
00073 
00074     double sum_code=0; // for the normalisation so that \int  \alpha (t) dt=1.
00075     // recall that \int \alpha(t)dt=\Deltat \sum \alpha_k.
00076 
00078     for ( int k=0; k<code_length; k++)
00079     {
00080 
00082         double a=0;
00083         double b=M_PI*(4*sigma)*deltat;
00084         if (b>M_PI) b=M_PI;
00085         // Recall that for the Gaussian model v_{max}=4sigma.
00086         double h=(b-a)/2;
00087         double s1=gaussian_integrand(sigma,deltat,a,k-round(code_length/2+.5))+
00088                   gaussian_integrand(sigma,deltat,b,k-round(code_length/2+.5));
00089         double s2=0;
00090         double s4=gaussian_integrand(sigma,deltat,a+h,
00091                                      k-round(code_length/2+.5));
00092         double tn=h*(s1+4*s4)/3;
00093         double ta=tn+2*eps*tn;
00095         int zh=2;
00096         int j;
00097 
00099         while (ABS(ta-tn)>eps*ABS(tn))
00100         {
00101             ta=tn;
00102             zh=2*zh;
00103             h=h/2;
00104             s2=s2+s4;
00105             s4=0;
00106             j=1;
00107             while (j<=zh)
00108             {
00109                 s4=s4+gaussian_integrand(sigma,deltat,a+j*h,
00110                                          k-round(code_length/2+.5));
00111                 j=j+2;
00112             }
00113             tn = h*(s1+2*s2+4*s4)/3;
00114         }//End of the loop, integral evelocityaluated
00115 
00116         code[k]=(double)tn; //storing the code.
00117         sum_code=sum_code+code[k];
00118     }
00119 
00120     for ( int k=0; k<code_length; k++)
00121         code[k]=code[k]/(deltat*sum_code);
00122 
00123 }
00124 
00125 
00126 
00127 
00133 
00144 double gaussian_integrand(double sigma,double deltat,
00145                           double xi, int k)
00146 {
00147 
00148     if (xi!=0)
00149     {
00150         return( (xi/(2*sin(xi/2)))*pow(gaussian_w(sigma,
00151                                        xi/deltat),0.25)*cos((k+.5)*xi));
00152     }
00153     else
00154     {
00155         return ( pow(gaussian_w(sigma,0),0.25));
00156     }
00157 
00158 
00159 }
00160 
00161 
00162 
00163 
00164 
00165 
00166 
00167 
00173 
00182 double gaussian_w(double sigma, double xi)
00183 {
00184     double velocity_max=4*sigma;
00185     if (ABS(xi)>velocity_max*M_PI) return(0);
00186     // to speed up some, as if ABS(xi)>velocity_max*M_PI*deltat by construction
00187     //the integrand is zero.
00188     double a=sigma/1000;//Avoids singularity, OK since the
00189     //function is in L^1 see also remark 1 page 8 and Algorithm 1 step 3.
00190     double b=velocity_max;
00191     double h=(b-a)/2;
00192     double s1=gaussian_w_integrand(a,sigma,xi)+
00193               gaussian_w_integrand(b,sigma,xi);
00194     double s2=0;
00195     double s4=gaussian_w_integrand(a+h,sigma,xi);
00196     double tn=h*(s1+4*s4)/3;
00197     double ta=tn+2*eps*tn;
00199     int zh=2;
00200     int j;
00201 
00202     while (ABS(ta-tn)>eps*ABS(tn))
00203     {
00204         ta=tn;
00205         zh=2*zh;
00206         h=h/2;
00207         s2=s2+s4;
00208         s4=0;
00209         j=1;
00210         while (j<=zh)
00211         {
00212             s4=s4+gaussian_w_integrand(a+j*h,sigma,xi);
00213             j=j+2;
00214         }
00215         tn = h*(s1+2*s2+4*s4)/3;
00216     }
00217     return(tn);
00218 
00219 }
00220 
00221 
00222 
00223 
00229 
00240 double gaussian_w_integrand(double velocity, double sigma,
00241                             double xi)
00242 {
00243 
00244 
00245     if (ABS(xi)<ABS(M_PI*velocity))
00246     {
00247         return(exp(-velocity*velocity/(2*sigma*sigma))/ABS(velocity));
00248     }
00249     else
00250     {
00251         return(0);
00252     }
00253 }
 All Files Functions Defines