The Flutter Shutter Code Optimizer
|
00001 /*flutter_optimizer_handcrafted.cpp*/ 00002 /* 00003 * Copyright 2013 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 */ 00024 #include <stdio.h> 00025 00026 #include <math.h> 00027 #include "flutter_optimizer_handcrafted.h" 00031 #define eps 0.001 //epsilon definition 00032 00033 00034 #ifndef ABS 00035 00038 #define ABS(x) (((x) > 0) ? (x) : (-(x))) 00039 #endif 00040 00041 00042 #ifndef M_PI 00043 00046 #define M_PI 3.14159265358979323846 00047 #endif 00048 00054 00076 void handcrafted_optimisator(double* code,double s,double proba_handcrafted_0, 00077 int code_length, double deltat) 00078 { 00079 00080 double v_max=s; 00081 00082 double max_abscode=0;// For code normalization; contains max(abs(code:)) 00083 double sum=0; 00085 for ( int k=0; k<code_length; k++) 00086 { 00087 00089 double a=0; 00090 double b=M_PI*v_max*deltat; 00091 double h=(b-a)/2; 00092 double s1=handcrafted_integrand(s,proba_handcrafted_0,deltat,a, 00093 k-round(code_length/2+.5))+ 00094 handcrafted_integrand(s,proba_handcrafted_0,deltat,b, 00095 k-round(code_length/2+.5)); 00096 double s2=0; 00097 double s4=handcrafted_integrand(s,proba_handcrafted_0,deltat,a+h, 00098 k-round(code_length/2+.5)); 00099 double tn=h*(s1+4*s4)/3; 00100 double ta=tn+2*eps*tn; 00101 00102 int zh=2; 00103 int j; 00104 00106 while (ABS(ta-tn)>eps*ABS(tn)) 00107 { 00108 ta=tn; 00109 zh=2*zh; 00110 h=h/2; 00111 s2=s2+s4; 00112 s4=0; 00113 j=1; 00114 while (j<=zh) 00115 { 00116 s4=s4+handcrafted_integrand(s,proba_handcrafted_0,deltat,a+j*h, 00117 k-round(code_length/2+.5)); 00118 j=j+2; 00119 } 00120 tn = h*(s1+2*s2+4*s4)/3; 00121 }//End of the loop, integral evaluated 00122 00123 code[k]=(double)tn; //storing the code. 00124 sum=sum+tn; 00125 00127 } 00128 for ( int k=0; k<code_length; k++) 00129 { 00130 code[k]=code[k]; 00131 if (ABS(code[k])>max_abscode) max_abscode=code[k]; 00132 } 00133 00135 for ( int k=0; k<code_length; k++) 00136 { 00137 00138 code[k]=code[k]/max_abscode; 00139 } 00140 } 00141 00142 00143 00149 00159 double handcrafted_integrand(double s, double proba_handcrafted_0, 00160 double deltat, double xi, int k) 00161 { 00162 if(ABS(xi*s*deltat)>M_PI) return(0); 00163 if (xi!=0) //avoids 0/0 division. 00164 { 00165 00166 return( (xi/(2*sin(xi/2)))*pow(handcrafted_w(s, proba_handcrafted_0, 00167 deltat,xi),0.25)* 00168 cos((k+.5)*xi)); 00169 00170 } 00171 else 00172 { 00173 return ( pow(handcrafted_w(s, proba_handcrafted_0, 00174 deltat,0),0.25)); 00175 } 00176 00177 00178 } 00179 00180 00181 00182 00183 00189 00200 double handcrafted_w(double s, double proba_handcrafted_0, double deltat, 00201 double xi) 00202 { 00203 00204 00205 if (ABS(xi)>ABS(M_PI*s*deltat)) return(0); 00206 if (ABS(xi)<=ABS(s)/1000) return(proba_handcrafted_0*1000); 00207 else return((1-proba_handcrafted_0)/(ABS(s))); 00208 00209 00210 } 00211