00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 #include <complex>
00072 #include <gtk/gtk.h>
00073 #include "config.h"
00074 #include "gxsm/plugin.h"
00075 #include "gxsm/xsmmath.h"
00076
00077
00078
00079 #define ComplexP (__complex__ double*)
00080 #define ComplexProd(A,B,C) *(ComplexP(C)) = *(ComplexP(A)) * *(ComplexP(B))
00081
00082
00083
00084
00085 static void crosscorrelation_init( void );
00086 static void crosscorrelation_about( void );
00087 static void crosscorrelation_configure( void );
00088 static void crosscorrelation_cleanup( void );
00089
00090
00091
00092 #define GXSM_TWO_SRC_PLUGIN__DEF
00093
00094
00095 #ifdef GXSM_ONE_SRC_PLUGIN__DEF
00096
00097 static gboolean crosscorrelation_run( Scan *Src, Scan *Dest );
00098 #else
00099
00100 static gboolean crosscorrelation_run( Scan *Src1, Scan *Src2, Scan *Dest );
00101 #endif
00102
00103
00104 GxsmPlugin crosscorrelation_pi = {
00105 NULL,
00106 NULL,
00107 0,
00108 NULL,
00109
00110
00111
00112
00113 "crosscorrelation-"
00114 #ifdef GXSM_ONE_SRC_PLUGIN__DEF
00115 "M1S"
00116 #else
00117 "M2S"
00118 #endif
00119 "-ST",
00120
00121
00122
00123
00124
00125 NULL,
00126
00127 "Computes the Cross-Correlation of a Scan.",
00128
00129 "Percy Zahl",
00130
00131 N_("_Math/_Statistics/"),
00132
00133 N_("Cross Correlation"),
00134
00135 N_("Do a Cross-Correlation of the scan"),
00136
00137 "Calc. Cross Correlation of Scan",
00138 NULL,
00139 NULL,
00140
00141
00142 crosscorrelation_init,
00143
00144
00145 NULL,
00146
00147
00148 crosscorrelation_about,
00149
00150
00151 crosscorrelation_configure,
00152
00153
00154 NULL,
00155
00156
00157 crosscorrelation_cleanup
00158 };
00159
00160
00161
00162
00163 #ifdef GXSM_ONE_SRC_PLUGIN__DEF
00164 GxsmMathOneSrcPlugin crosscorrelation_m1s_pi
00165 #else
00166 GxsmMathTwoSrcPlugin crosscorrelation_m2s_pi
00167 #endif
00168 = {
00169
00170 crosscorrelation_run
00171 };
00172
00173
00174 static const char *about_text = N_("Gxsm crosscorrelation Plugin\n\n"
00175 "Computes the Cross-Correlation of a scan:\n"
00176 "The Cross-Correlation of a scan is computed\n"
00177 "by FT the source Scan, setting the phase\n"
00178 "to zero and doing the IFT.");
00179
00180
00181
00182 GxsmPlugin *get_gxsm_plugin_info ( void ){
00183 crosscorrelation_pi.description = g_strdup_printf(N_("Gxsm MathOneArg crosscorrelation plugin %s"), VERSION);
00184 return &crosscorrelation_pi;
00185 }
00186
00187
00188
00189
00190 #ifdef GXSM_ONE_SRC_PLUGIN__DEF
00191 GxsmMathOneSrcPlugin *get_gxsm_math_one_src_plugin_info( void ) {
00192 return &crosscorrelation_m1s_pi;
00193 }
00194 #else
00195 GxsmMathTwoSrcPlugin *get_gxsm_math_two_src_plugin_info( void ) {
00196 return &crosscorrelation_m2s_pi;
00197 }
00198 #endif
00199
00200
00201
00202 static void crosscorrelation_init(void)
00203 {
00204 PI_DEBUG (DBG_L2, "crosscorrelation Plugin Init");
00205 }
00206
00207
00208 static void crosscorrelation_about(void)
00209 {
00210 const gchar *authors[] = { crosscorrelation_pi.authors, NULL};
00211 gtk_widget_show(gnome_about_new ( crosscorrelation_pi.name,
00212 VERSION,
00213 N_("(C) 2000 the Free Software Foundation"),
00214 about_text,
00215 authors,
00216 NULL, NULL, NULL
00217 ));
00218 }
00219
00220 double WindowDefault=0;
00221 double WindowLast=0;
00222
00223
00224 static void crosscorrelation_configure(void)
00225 {
00226
00227
00228
00229 crosscorrelation_pi.app->ValueRequest("Enter Value", "Window",
00230 "Please enter the window type\n"
00231 "0: Uniform\n"
00232 "1: Hanning\n"
00233 "2: Hamming\n"
00234 "3: Bartlett\n"
00235 "4: Blackman\n"
00236 "5: Connes",
00237 crosscorrelation_pi.app->xsm->Unity,
00238 0, 5, ".0f", &WindowDefault);
00239
00240
00241 }
00242
00243
00244 static void crosscorrelation_cleanup(void)
00245 {
00246 PI_DEBUG (DBG_L2, "crosscorrelation Plugin Cleanup");
00247 }
00248
00249
00250
00251
00252 static gboolean crosscorrelation_run(Scan *Src1, Scan *Src2, Scan *Dest)
00253 {
00254 double window=WindowLast;
00255 if(fabs(WindowDefault) > 1e-10)
00256 window = WindowDefault;
00257 else
00258 crosscorrelation_pi.app->ValueRequest("Enter Value", "Window",
00259 "Please enter the window type\n"
00260 "0: Uniform\n"
00261 "1: Hanning\n"
00262 "2: Hamming\n"
00263 "3: Bartlett\n"
00264 "4: Blackman\n"
00265 "5: Connes",
00266 crosscorrelation_pi.app->xsm->Unity,
00267 0, 5, ".0f", &window);
00268
00269
00270
00271 Dest->mem2d->Resize(Dest->data.s.nx, Dest->data.s.ny, ZD_FLOAT);
00272
00273
00274 int Nx1,Ny1,Nx2,Ny2;
00275 Nx1=Src1->mem2d->GetNx();
00276 Ny1=Src1->mem2d->GetNy();
00277 Nx2=Src2->mem2d->GetNx();
00278 Ny2=Src2->mem2d->GetNy();
00279
00280
00281
00282 fftw_complex *in1 = new fftw_complex [Nx1*Ny1];
00283 fftw_complex *in2 = new fftw_complex [Nx2*Ny2];
00284 fftw_complex *in3 = new fftw_complex [Nx1*Ny1];
00285 fftw_complex *out1 = new fftw_complex [Nx1*Ny1];
00286 fftw_complex *out2 = new fftw_complex [Nx2*Ny2];
00287 fftw_complex *out3 = new fftw_complex [Nx1*Ny1];
00288
00289
00290
00291
00292
00293 double xfactor=1,yfactor=1;
00294
00295
00296 for (int pos=0, line=0; line < Ny1; ++line)
00297 for (int col=0; col < Nx1; ++col, ++pos){
00298
00299 if(window == 0.){
00300 xfactor=1.0;
00301 yfactor=1.0;
00302 }
00303 else if(window == 1.){
00304 xfactor = 0.5*(1.0+cos(2.0*M_PI*(line-Ny1/2.0)/Ny1));
00305 yfactor = 0.5*(1.0+cos(2.0*M_PI*(col-Nx1/2.0)/Nx1));
00306 }
00307 else if(window == 2.){
00308 xfactor = 0.54+0.46*cos(2.0*M_PI*(line-Ny1/2.0)/Ny1);
00309 yfactor = 0.54+0.46*cos(2.0*M_PI*(col-Nx1/2.0)/Nx1);
00310 }
00311 else if(window == 3.){
00312 xfactor=(1.0-2.0*sqrt((line-Ny1/2.0)*(line-Ny1/2.0))/Ny1);
00313 yfactor=(1.0-2.0*sqrt((col-Nx1/2.0)*(col-Nx1/2.0))/Nx1);
00314 }
00315 else if(window == 4.){
00316 xfactor = 0.42+0.5*cos(2.0*M_PI*(line-Ny1/2.0)/Ny1)+0.08*cos(4.0*M_PI*(line-Ny1/2.0)/Ny1);
00317 yfactor = 0.42+0.5*cos(2.0*M_PI*(col-Nx1/2.0)/Nx1)+0.08*cos(4.0*M_PI*(col-Nx1/2.0)/Nx1);
00318 }
00319 else if(window == 5.){
00320 xfactor = (1.0-4.0*(line-Ny1/2.0)*(line-Ny1/2.0)/Ny1/Ny1)*(1.-4.0*(line-Ny1/2.0)*(line-Ny1/2.0)/Ny1/Ny1);
00321 yfactor = (1.0-4.0*(col-Nx1/2.0)*(col-Nx1/2.0)/Nx1/Nx1)*(1.-4.0*(col-Nx1/2.0)*(col-Nx1/2.0)/Nx1/Nx1);
00322 }
00323
00324 c_re(in1[pos]) = Src1->mem2d->GetDataPkt(col, line)*xfactor*yfactor;
00325 c_im(in1[pos]) = 0.;
00326 }
00327
00328
00329
00330
00331
00332 for (int pos=0, line=0; line < Ny2; ++line)
00333 for (int col=0; col < Nx2; ++col, ++pos){
00334
00335 if(window == 0.){
00336 xfactor=1.0;
00337 yfactor=1.0;
00338 }
00339 else if(window == 1.){
00340 xfactor = 0.5*(1.0+cos(2.0*M_PI*(line-Ny2/2.0)/Ny2));
00341 yfactor = 0.5*(1.0+cos(2.0*M_PI*(col-Nx2/2.0)/Nx2));
00342 }
00343 else if(window == 2.){
00344 xfactor = 0.54+0.46*cos(2.0*M_PI*(line-Ny2/2.0)/Ny2);
00345 yfactor = 0.54+0.46*cos(2.0*M_PI*(col-Nx2/2.0)/Nx2);
00346 }
00347 else if(window == 3.){
00348 xfactor=(1.0-2.0*sqrt((line-Ny2/2.0)*(line-Ny2/2.0))/Ny2);
00349 yfactor=(1.0-2.0*sqrt((col-Nx2/2.0)*(col-Nx2/2.0))/Nx2);
00350 }
00351 else if(window == 4.){
00352 xfactor = 0.42+0.5*cos(2.0*M_PI*(line-Ny2/2.0)/Ny2)+0.08*cos(4.0*M_PI*(line-Ny2/2.0)/Ny2);
00353 yfactor = 0.42+0.5*cos(2.0*M_PI*(col-Nx2/2.0)/Nx2)+0.08*cos(4.0*M_PI*(col-Nx2/2.0)/Nx2);
00354 }
00355 else if(window == 5.){
00356 xfactor = (1.0-4.0*(line-Ny2/2.0)*(line-Ny2/2.0)/Ny2/Ny2)*(1.0-4.0*(line-Ny2/2.0)*(line-Ny2/2.0)/Ny2/Ny2);
00357 yfactor = (1.0-4.0*(col-Nx2/2.0)*(col-Nx2/2.0)/Nx2/Nx2)*(1.0-4.0*(col-Nx2/2.0)*(col-Nx2/2.0)/Nx2/Nx2);
00358 }
00359
00360 c_re(in2[pos]) = Src2->mem2d->GetDataPkt(col, line)*xfactor*yfactor;
00361 c_im(in2[pos]) = 0.;
00362 }
00363
00364
00365
00366
00367
00368 fftw_plan plan1 = fftw_plan_dft_2d (Ny1, Nx1, in1, out1, FFTW_FORWARD, FFTW_ESTIMATE);
00369
00370
00371 fftw_execute (plan1);
00372
00373
00374
00375 fftw_plan plan2 = fftw_plan_dft_2d (Ny2, Nx2, in2, out2, FFTW_FORWARD, FFTW_ESTIMATE);
00376
00377
00378 fftw_execute (plan2);
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392 for(int i=0; i < Ny1; ++i)
00393 for(int j=0; j < Nx2; ++j){
00394 c_re(in3[j+Nx1*i])=0;
00395 c_im(in3[j+Nx1*i])=0;
00396 c_re(in3[j+Nx1*i])=c_re(out1[j+Nx1*i])*c_re(out2[j+Nx2*i])+c_im(out1[j+Nx1*i])*c_im(out2[j+Nx2*i]);
00397 c_im(in3[j+Nx1*i])=c_im(out1[j+Nx1*i])*c_re(out2[j+Nx2*i])-c_re(out1[j+Nx1*i])*c_im(out2[j+Nx2*i]);
00398 }
00399
00400
00401 fftw_plan plan3 = fftw_plan_dft_2d (Ny1, Nx1, in3, out3, FFTW_BACKWARD, FFTW_ESTIMATE);
00402
00403 fftw_execute (plan3);
00404
00405
00406
00407
00408 double SumNorm=0.0;
00409 for(int i=0; i < Nx1*Ny1; i++){
00410 SumNorm += c_re(in1[i])*c_re(in2[i]);
00411 }
00412
00413
00414
00415 for (int pos=0, i=0; i < Ny1; ++i)
00416 for (int j=0; j < Nx1; ++j, ++pos){
00417 Dest->mem2d->PutDataPkt(c_re(out3[pos])/SumNorm/Nx1/Ny1,
00418 QSWP (j,Nx1),
00419 QSWP (i, Ny1));
00420 }
00421
00422
00423
00424 fftw_destroy_plan (plan1);
00425 fftw_destroy_plan (plan2);
00426 fftw_destroy_plan (plan3);
00427
00428
00429 delete in1;
00430 delete in2;
00431 delete in3;
00432 delete out1;
00433 delete out2;
00434 delete out3;
00435
00436 Dest->mem2d->data->MkXLookup(0., Src1->data.s.rx);
00437 Dest->mem2d->data->MkYLookup(0., Src1->data.s.ry);
00438 UnitObj *u;
00439
00440 Dest->data.SetZUnit(u=new LinUnit(" "," ",1.,0));
00441 Dest->data.s.dz=1.0;
00442
00443
00444 delete u;
00445
00446 return MATH_OK;
00447 }