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
00072
00073
00074
00075 #include <complex>
00076 #include <gtk/gtk.h>
00077 #include "config.h"
00078 #include "gxsm/plugin.h"
00079 #include "gxsm/xsmmath.h"
00080
00081
00082 static void autocorrelation_init( void );
00083 static void autocorrelation_about( void );
00084 static void autocorrelation_configure( void );
00085 static void autocorrelation_cleanup( void );
00086
00087
00088 #define GXSM_ONE_SRC_PLUGIN__DEF
00089
00090
00091
00092 #ifdef GXSM_ONE_SRC_PLUGIN__DEF
00093
00094 static gboolean autocorrelation_run( Scan *Src, Scan *Dest );
00095 #else
00096
00097 static gboolean autocorrelation_run( Scan *Src1, Scan *Src2, Scan *Dest );
00098 #endif
00099
00100
00101 GxsmPlugin autocorrelation_pi = {
00102 NULL,
00103 NULL,
00104 0,
00105 NULL,
00106
00107
00108
00109
00110 "autocorrelation-"
00111 #ifdef GXSM_ONE_SRC_PLUGIN__DEF
00112 "M1S"
00113 #else
00114 "M2S"
00115 #endif
00116 "-ST",
00117
00118
00119
00120
00121
00122 NULL,
00123
00124 "Computes the Auto-Correlation of a Scan.",
00125
00126 "Erik Muller",
00127
00128 N_("_Math/_Statistics/"),
00129
00130 N_("Auto Correlation"),
00131
00132 N_("Do a Auto-Correlation of the scan"),
00133
00134 "Calc. Auto Correlation of Scan",
00135 NULL,
00136 NULL,
00137
00138
00139 autocorrelation_init,
00140
00141
00142 NULL,
00143
00144
00145 autocorrelation_about,
00146
00147
00148 autocorrelation_configure,
00149
00150
00151 NULL,
00152
00153
00154 autocorrelation_cleanup
00155 };
00156
00157
00158
00159
00160 #ifdef GXSM_ONE_SRC_PLUGIN__DEF
00161 GxsmMathOneSrcPlugin autocorrelation_m1s_pi
00162 #else
00163 GxsmMathTwoSrcPlugin autocorrelation_m2s_pi
00164 #endif
00165 = {
00166
00167 autocorrelation_run
00168 };
00169
00170
00171 static const char *about_text = N_("Gxsm autocorrelation Plugin\n\n"
00172 "Computes the Auto-Correlation of a scan:\n"
00173 "The Auto-Correlation of a scan is computed\n"
00174 "by FT the source Scan, setting the phase\n"
00175 "to zero and doing the IFT.");
00176
00177
00178
00179 GxsmPlugin *get_gxsm_plugin_info ( void ){
00180 autocorrelation_pi.description = g_strdup_printf(N_("Gxsm MathOneArg autocorrelation plugin %s"), VERSION);
00181 return &autocorrelation_pi;
00182 }
00183
00184
00185
00186
00187 #ifdef GXSM_ONE_SRC_PLUGIN__DEF
00188 GxsmMathOneSrcPlugin *get_gxsm_math_one_src_plugin_info( void ) {
00189 return &autocorrelation_m1s_pi;
00190 }
00191 #else
00192 GxsmMathTwoSrcPlugin *get_gxsm_math_two_src_plugin_info( void ) {
00193 return &autocorrelation_m2s_pi;
00194 }
00195 #endif
00196
00197
00198
00199
00200 static void autocorrelation_init(void)
00201 {
00202 PI_DEBUG (DBG_L2, "autocorrelation Plugin Init");
00203 }
00204
00205
00206 static void autocorrelation_about(void)
00207 {
00208 const gchar *authors[] = { autocorrelation_pi.authors, NULL};
00209 gtk_widget_show(gnome_about_new ( autocorrelation_pi.name,
00210 VERSION,
00211 N_("(C) 2000 the Free Software Foundation"),
00212 about_text,
00213 authors,
00214 NULL, NULL, NULL
00215 ));
00216 }
00217
00218 int WindowDefault=0;
00219 int WindowLast=0;
00220
00221 int window_callback(GtkComboBox *widget, gpointer data){
00222 WindowDefault = gtk_combo_box_get_active (widget);
00223 return 0;
00224 }
00225
00226
00227 static void autocorrelation_configure(void)
00228 {
00229
00230
00231 }
00232
00233
00234 static void autocorrelation_cleanup(void)
00235 {
00236 PI_DEBUG (DBG_L2, "autocorrelation Plugin Cleanup");
00237 }
00238
00239
00240
00241 static gboolean autocorrelation_run(Scan *Src, Scan *Dest)
00242 {
00243
00244
00245
00246
00247
00248 int StartX=0,EndX=0,StartY=0,EndY=0;
00249
00250 EndX=Src->mem2d->GetNx();
00251 EndY=Src->mem2d->GetNy();
00252
00253 double hi, lo, z;
00254 int success = FALSE;
00255 int n_obj = Src->number_of_object ();
00256 Point2D p[2];
00257
00258 while (n_obj--){
00259 scan_object_data *obj_data = Src->get_object_data (n_obj);
00260
00261 if (strncmp (obj_data->get_name (), "Rectangle", 9) )
00262 continue;
00263
00264 if (obj_data->get_num_points () != 2)
00265 continue;
00266
00267 double x,y;
00268 obj_data->get_xy_pixel (0, x, y);
00269 p[0].x = (int)x; p[0].y = (int)y;
00270 obj_data->get_xy_pixel (1, x, y);
00271 p[1].x = (int)x; p[1].y = (int)y;
00272
00273 StartX=p[0].x; EndX=p[1].x;
00274 StartY=p[0].y; EndY=p[1].y;
00275
00276 success = TRUE;
00277 break;
00278 }
00279
00280
00281 if (StartX < 0 | StartY < 0 | EndX > Src->mem2d->GetNx() | EndY > Src->mem2d->GetNy() )
00282 return MATH_SELECTIONERR;
00283
00284
00285
00286 GtkWidget *dialog, *label, *wmenu;
00287
00288
00289
00290 dialog = gtk_dialog_new_with_buttons ("Message",
00291 NULL,
00292
00293 GTK_DIALOG_MODAL,
00294 GTK_STOCK_OK,
00295 GTK_RESPONSE_NONE,
00296 NULL);
00297
00298 wmenu = gtk_combo_box_new_text ();
00299
00300 gtk_combo_box_append_text (GTK_COMBO_BOX (wmenu),"No Window");
00301 gtk_combo_box_append_text (GTK_COMBO_BOX (wmenu),"Hanning");
00302 gtk_combo_box_append_text (GTK_COMBO_BOX (wmenu),"Hamming");
00303 gtk_combo_box_append_text (GTK_COMBO_BOX (wmenu),"Bartlett");
00304 gtk_combo_box_append_text (GTK_COMBO_BOX (wmenu),"Blackman");
00305 gtk_combo_box_append_text (GTK_COMBO_BOX (wmenu),"Connes");
00306
00307 gtk_signal_connect (GTK_OBJECT(wmenu),"changed", GTK_SIGNAL_FUNC (window_callback), (gpointer) 1 );
00308
00309 gchar *message="Please chose a window function";
00310
00311
00312 label = gtk_label_new (message);
00313
00314
00315 g_signal_connect_swapped (dialog,
00316 "response",
00317 G_CALLBACK (gtk_widget_destroy),
00318 dialog);
00319
00320
00321
00322
00323
00324
00325 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),
00326 label);
00327
00328 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),
00329 wmenu);
00330 gtk_widget_show_all (dialog);
00331 gtk_dialog_run (GTK_DIALOG (dialog));
00332
00333
00334
00335 int window = WindowDefault;
00336 int Nx, Ny;
00337 Nx=(EndX-StartX);
00338 Ny=(EndY-StartY);
00339
00340
00341
00342 Dest->mem2d->Resize(Nx, Ny, ZD_FLOAT);
00343
00344
00345 fftw_complex *in1 = new fftw_complex [Nx*Ny];
00346 fftw_complex *in2 = new fftw_complex [Nx*Ny];
00347
00348 fftw_complex *out1 = new fftw_complex [Nx*Ny];
00349 fftw_complex *out2 = new fftw_complex [Nx*Ny];
00350
00351
00352
00353
00354
00355 double xfactor=1,yfactor=1;
00356
00357
00358 for (int pos=0, line=StartY; line < EndY; ++line)
00359 for (int col=StartX; col < EndX; ++col, ++pos){
00360
00361 if(window == 0){
00362 xfactor=1.0;
00363 yfactor=1.0;
00364 }
00365 else if(window == 1){
00366 xfactor = 0.5*(1.0+cos(2.0*M_PI*((line-StartY)-Ny/2.0)/Ny));
00367 yfactor = 0.5*(1.0+cos(2.0*M_PI*((col-StartX)-Nx/2.0)/Nx));
00368 }
00369 else if(window == 2){
00370 xfactor = 0.54+0.46*cos(2.0*M_PI*((line-StartY)-Ny/2.0)/Ny);
00371 yfactor = 0.54+0.46*cos(2.0*M_PI*((col-StartX)-Nx/2.0)/Nx);
00372 }
00373 else if(window == 3){
00374 xfactor=(1.0-2.0*sqrt(((line-StartY)-Ny/2.0)*(line-StartY-Ny/2.0))/Ny);
00375 yfactor=(1.0-2.0*sqrt(((col-StartX)-Nx/2.0)*((col-StartX)-Nx/2.0))/Nx);
00376 }
00377 else if(window == 4){
00378 xfactor = 0.42+0.5*cos(2.0*M_PI*((line-StartY)-Ny/2.0)/Ny)+0.08*cos(4.0*M_PI*((line-StartY)-Ny/2.0)/Ny);
00379 yfactor = 0.42+0.5*cos(2.0*M_PI*((col-StartX)-Nx/2.0)/Nx)+0.08*cos(4.0*M_PI*((col-StartX)-Nx/2.0)/Nx);
00380 }
00381 else if(window == 5){
00382 xfactor = (1.0-4.0*((line-StartY)-Ny/2.0)*((line-StartY)-Ny/2.0)/Ny/Ny)*(1.0-4.0*((line-StartY)-Ny/2.0)*((line-StartY)-Ny/2.0)/Ny/Ny);
00383 yfactor = (1.0-4.0*((col-StartX)-Nx/2.0)*((col-StartX)-Nx/2.0)/Nx/Nx)*(1.0-4.0*((col-StartX)-Nx/2.0)*((col-StartX)-Nx/2.0)/Nx/Nx);
00384 }
00385
00386 c_re(in1[pos]) =(Src->mem2d->GetDataPkt(col, line))*xfactor*yfactor;
00387 c_im(in1[pos]) = 0.;
00388 }
00389
00390
00391
00392
00393
00394
00395 fftw_plan plan1 = fftw_plan_dft_2d (Ny, Nx, in1, out1, FFTW_FORWARD, FFTW_ESTIMATE);
00396
00397
00398 fftw_execute (plan1);
00399
00400 for(int i=0; i < Ny; ++i)
00401 for(int j=0; j < Nx; ++j){
00402 c_re(in2[j+Nx*i])=0.0;
00403 c_im(in2[j+Nx*i])=0.0;
00404 c_re(in2[j+Nx*i])=c_re(out1[j+Nx*i])*c_re(out1[j+Nx*i])+c_im(out1[j+Nx*i])*c_im(out1[j+Nx*i]);
00405 c_im(in2[j+Nx*i])=c_im(out1[j+Nx*i])*c_re(out1[j+Nx*i])-c_re(out1[j+Nx*i])*c_im(out1[j+Nx*i]);
00406 }
00407
00408
00409 fftw_plan plan2 = fftw_plan_dft_2d (Ny, Nx, in2, out2, FFTW_BACKWARD, FFTW_ESTIMATE);
00410
00411 fftw_execute (plan2);
00412
00413
00414
00415
00416 double SumNorm=0;
00417 for(int i=0; i < Nx*Ny; i++){
00418 SumNorm += c_re(in1[i])*c_re(in1[i]);
00419 }
00420
00421
00422
00423
00424 for (int pos=0, i=0; i < Ny; ++i)
00425 for (int j=0; j < Nx; ++j, ++pos){
00426 Dest->mem2d->PutDataPkt(c_re(out2[pos])/SumNorm/Nx/Ny,
00427 QSWP (j,Nx),
00428 QSWP (i, Ny));
00429 }
00430
00431
00432
00433 fftw_destroy_plan (plan1);
00434 fftw_destroy_plan (plan2);
00435
00436
00437
00438 delete in1;
00439 delete in2;
00440
00441 delete out1;
00442 delete out2;
00443
00444
00445 Dest->mem2d->data->MkXLookup(0., Src->data.s.rx*Nx/Src->mem2d->GetNx());
00446 Dest->mem2d->data->MkYLookup(0., Src->data.s.ry*Ny/Src->mem2d->GetNy());
00447 UnitObj *u;
00448
00449 Dest->data.SetZUnit(u=new LinUnit(" "," ",1.,0));
00450 Dest->data.SetXUnit(u=new LinUnit(" "," ",1.,0));
00451 Dest->data.SetYUnit(u=new LinUnit(" "," ",1.,0));
00452
00453
00454 Dest->data.s.dz=1.0;
00455 Dest->data.s.dx=1.0;
00456 Dest->data.s.dy=1.0;
00457 delete u;
00458
00459
00460
00461 return MATH_OK;
00462
00463 }
00464