| 1 |
|
| 2 |
## -*- cperl -*- |
| 3 |
## |
| 4 |
## This file is part of Athena, copyright (c) 2001-2006 Bruce Ravel |
| 5 |
## |
| 6 |
## This file contains the fourier transform teaching dialog |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
sub teach_ft { |
| 11 |
|
| 12 |
## you must define a hash which will contain the parameters needed |
| 13 |
## to perform the task. the hash_pointer global variable will point |
| 14 |
## to this hash for use in set_properties. you might draw these |
| 15 |
## values from configuration parameters |
| 16 |
my %tft_params = (r1 => 2, |
| 17 |
r2 => 3, |
| 18 |
r3 => 0, |
| 19 |
ext => 20, |
| 20 |
npts => 400, |
| 21 |
kmin => 2, |
| 22 |
kmax => 18, |
| 23 |
rmin => 1.75, |
| 24 |
rmax => 2.25, |
| 25 |
dk => 2, |
| 26 |
dr => 0.5, |
| 27 |
kwin => "Kaiser-Bessel", |
| 28 |
rwin => "Kaiser-Bessel", |
| 29 |
plot => 0, |
| 30 |
); |
| 31 |
|
| 32 |
## these two global variables must be set before this view is displayed |
| 33 |
$fat_showing = 'teach_ft'; |
| 34 |
$hash_pointer = \%tft_params; |
| 35 |
|
| 36 |
## disable many menus. this makes the chore of managing the views |
| 37 |
## much easier. the idea is that the main view is "home base". if |
| 38 |
## you want to do a different analysis chore, you must first return |
| 39 |
## to the main view |
| 40 |
map {$_ -> configure(-state=>'disabled')} |
| 41 |
($data_menu, $merge_menu, $anal_menu, $settings_menu); |
| 42 |
|
| 43 |
## this removes the currently displayed view without destroying its |
| 44 |
## contents |
| 45 |
$fat -> packForget; |
| 46 |
|
| 47 |
## define the parent Frame for this analysis chore and pack it in |
| 48 |
## the correct location |
| 49 |
my $tft = $container->Frame(@fatgeom, -relief=>'sunken', -borderwidth=>3) |
| 50 |
-> pack(-fill=>'both', -expand=>1); |
| 51 |
#$conv -> packPropagate(0); |
| 52 |
## global variable identifying which Frame is showing |
| 53 |
$which_showing = $tft; |
| 54 |
|
| 55 |
## the standard label along the top identifying this analysis chore |
| 56 |
$tft -> Label(-text=>"Understanding Fourier transforms", |
| 57 |
-font=>$config{fonts}{large}, |
| 58 |
-foreground=>$config{colors}{activehighlightcolor}) |
| 59 |
-> pack(-side=>'top', -fill=>'x', -anchor=>'w'); |
| 60 |
|
| 61 |
|
| 62 |
my @lab = (-foreground=>$config{colors}{activehighlightcolor}); |
| 63 |
my $fr = $tft -> Frame(-relief=>'sunken', -borderwidth=>2) |
| 64 |
-> pack(-side=>'top', -fill=>'x', -anchor=>'w', -padx=>2, -pady=>2, -ipady=>8); |
| 65 |
my $row = 0; |
| 66 |
|
| 67 |
$fr -> Label(-text=>'Wave #1', @lab) |
| 68 |
-> grid(-row=>$row, -column=>0, -sticky=>'e'); |
| 69 |
$widget{tft_r1} = $fr -> Entry(-width=>5, |
| 70 |
-validate=>'key', |
| 71 |
-validatecommand=>[\&set_variable, 'tft_r1'], |
| 72 |
-textvariable=>\$tft_params{r1}, |
| 73 |
) |
| 74 |
-> grid(-row=>$row, -column=>1, -sticky=>'w'); |
| 75 |
$fr -> Label(-text=>'Angstroms', @lab) |
| 76 |
-> grid(-row=>$row, -column=>2, -sticky=>'w'); |
| 77 |
|
| 78 |
++$row; |
| 79 |
$fr -> Label(-text=>'Wave #2', @lab) |
| 80 |
-> grid(-row=>$row, -column=>0, -sticky=>'e'); |
| 81 |
$widget{tft_r2} = $fr -> Entry(-width=>5, |
| 82 |
-validate=>'key', |
| 83 |
-validatecommand=>[\&set_variable, 'tft_r2'], |
| 84 |
-textvariable=>\$tft_params{r2}, |
| 85 |
) |
| 86 |
-> grid(-row=>$row, -column=>1, -sticky=>'w'); |
| 87 |
$fr -> Label(-text=>'Angstroms', @lab) |
| 88 |
-> grid(-row=>$row, -column=>2, -sticky=>'w'); |
| 89 |
|
| 90 |
++$row; |
| 91 |
$fr -> Label(-text=>'Wave #3', @lab) |
| 92 |
-> grid(-row=>$row, -column=>0, -sticky=>'e'); |
| 93 |
$widget{tft_r3} = $fr -> Entry(-width=>5, |
| 94 |
-validate=>'key', |
| 95 |
-validatecommand=>[\&set_variable, 'tft_r3'], |
| 96 |
-textvariable=>\$tft_params{r3}, |
| 97 |
) |
| 98 |
-> grid(-row=>$row, -column=>1, -sticky=>'w'); |
| 99 |
$fr -> Label(-text=>'Angstroms', @lab) |
| 100 |
-> grid(-row=>$row, -column=>2, -sticky=>'w'); |
| 101 |
|
| 102 |
++$row; |
| 103 |
$fr -> Label(-text=>'k extent', @lab) |
| 104 |
-> grid(-row=>$row, -column=>0, -sticky=>'e'); |
| 105 |
$widget{tft_ext} = $fr -> Entry(-width=>5, |
| 106 |
-validate=>'key', |
| 107 |
-validatecommand=>[\&set_variable, 'tft_ext'], |
| 108 |
-textvariable=>\$tft_params{ext}, |
| 109 |
) |
| 110 |
-> grid(-row=>$row, -column=>1, -sticky=>'w'); |
| 111 |
$fr -> Label(-text=>'Angstroms', @lab) |
| 112 |
-> grid(-row=>$row, -column=>2, -sticky=>'w'); |
| 113 |
|
| 114 |
|
| 115 |
++$row; |
| 116 |
$fr -> Label(-text=>'kmin', @lab) |
| 117 |
-> grid(-row=>$row, -column=>0, -sticky=>'e'); |
| 118 |
$widget{tft_kmin} = $fr -> Entry(-width=>5, |
| 119 |
-validate=>'key', |
| 120 |
-validatecommand=>[\&set_variable, 'tft_kmin'], |
| 121 |
-textvariable=>\$tft_params{kmin}, |
| 122 |
) |
| 123 |
-> grid(-row=>$row, -column=>1, -sticky=>'w'); |
| 124 |
$fr -> Label(-text=>'kmax', @lab) |
| 125 |
-> grid(-row=>$row, -column=>2, -sticky=>'e'); |
| 126 |
$widget{tft_kmax} = $fr -> Entry(-width=>5, |
| 127 |
-validate=>'key', |
| 128 |
-validatecommand=>[\&set_variable, 'tft_kmax'], |
| 129 |
-textvariable=>\$tft_params{kmax}, |
| 130 |
) |
| 131 |
-> grid(-row=>$row, -column=>3, -sticky=>'w'); |
| 132 |
|
| 133 |
|
| 134 |
++$row; |
| 135 |
$fr -> Label(-text=>'dk', @lab) |
| 136 |
-> grid(-row=>$row, -column=>0, -sticky=>'e'); |
| 137 |
$widget{tft_dk} = $fr -> Entry(-width=>5, |
| 138 |
-validate=>'key', |
| 139 |
-validatecommand=>[\&set_variable, 'tft_dk'], |
| 140 |
-textvariable=>\$tft_params{dk}, |
| 141 |
) |
| 142 |
-> grid(-row=>$row, -column=>1, -sticky=>'w'); |
| 143 |
$fr -> Label(-text=>'k window', @lab) |
| 144 |
-> grid(-row=>$row, -column=>2, -sticky=>'e'); |
| 145 |
$fr -> Optionmenu(-options=>[qw(Kaiser-Bessel Hanning Parzen Welch)], |
| 146 |
-variable=>\$tft_params{kwin},) |
| 147 |
-> grid(-row=>$row, -column=>3, -sticky=>'w'); |
| 148 |
|
| 149 |
++$row; |
| 150 |
$fr -> Label(-text=>'Rmin', @lab) |
| 151 |
-> grid(-row=>$row, -column=>0, -sticky=>'e'); |
| 152 |
$widget{tft_rmin} = $fr -> Entry(-width=>5, |
| 153 |
-validate=>'key', |
| 154 |
-validatecommand=>[\&set_variable, 'tft_rmin'], |
| 155 |
-textvariable=>\$tft_params{rmin}, |
| 156 |
) |
| 157 |
-> grid(-row=>$row, -column=>1, -sticky=>'w'); |
| 158 |
$fr -> Label(-text=>'Rmax', @lab) |
| 159 |
-> grid(-row=>$row, -column=>2, -sticky=>'e'); |
| 160 |
$widget{tft_rmax} = $fr -> Entry(-width=>5, |
| 161 |
-validate=>'key', |
| 162 |
-validatecommand=>[\&set_variable, 'tft_rmax'], |
| 163 |
-textvariable=>\$tft_params{rmax}, |
| 164 |
) |
| 165 |
-> grid(-row=>$row, -column=>3, -sticky=>'w'); |
| 166 |
|
| 167 |
++$row; |
| 168 |
$fr -> Label(-text=>'dR', @lab) |
| 169 |
-> grid(-row=>$row, -column=>0, -sticky=>'e'); |
| 170 |
$widget{tft_dr} = $fr -> Entry(-width=>5, |
| 171 |
-validate=>'key', |
| 172 |
-validatecommand=>[\&set_variable, 'tft_dr'], |
| 173 |
-textvariable=>\$tft_params{dr}, |
| 174 |
) |
| 175 |
-> grid(-row=>$row, -column=>1, -sticky=>'w'); |
| 176 |
$fr -> Label(-text=>'R window', @lab) |
| 177 |
-> grid(-row=>$row, -column=>2, -sticky=>'e'); |
| 178 |
$fr -> Optionmenu(-options=>[qw(Kaiser-Bessel Hanning Parzen Welch)], |
| 179 |
-variable=>\$tft_params{rwin},) |
| 180 |
-> grid(-row=>$row, -column=>3, -sticky=>'w'); |
| 181 |
|
| 182 |
|
| 183 |
my $red = $config{colors}{single}; |
| 184 |
++$row; |
| 185 |
$fr -> Radiobutton(-text =>"Plot waves in k", |
| 186 |
-variable =>\$tft_params{plot}, |
| 187 |
-selectcolor =>$red, |
| 188 |
-value =>0, |
| 189 |
-command => sub{tft_plot_waves(\%tft_params)}) |
| 190 |
-> grid(-row=>$row, -column=>0, -columnspan=>4, -sticky=>'w'); |
| 191 |
|
| 192 |
++$row; |
| 193 |
$fr -> Radiobutton(-text => "Plot sum*window in k", , |
| 194 |
-variable => \$tft_params{plot}, |
| 195 |
-selectcolor => $red, |
| 196 |
-value => 1, |
| 197 |
-command => sub{tft_plot_windowed(\%tft_params)},) |
| 198 |
-> grid(-row=>$row, -column=>0, -columnspan=>4, -sticky=>'w'); |
| 199 |
|
| 200 |
++$row; |
| 201 |
$fr -> Radiobutton(-text=>"Plot magnitude of FT", , |
| 202 |
-variable => \$tft_params{plot}, |
| 203 |
-selectcolor => $red, |
| 204 |
-value => 2, |
| 205 |
-command => sub{tft_plot_r(\%tft_params, 'm')},) |
| 206 |
-> grid(-row=>$row, -column=>0, -columnspan=>4, -sticky=>'w'); |
| 207 |
|
| 208 |
++$row; |
| 209 |
$fr -> Radiobutton(-text=>"Plot real part of FT", , |
| 210 |
-variable => \$tft_params{plot}, |
| 211 |
-selectcolor => $red, |
| 212 |
-value => 3, |
| 213 |
-command => sub{tft_plot_r(\%tft_params, 'r')},) |
| 214 |
-> grid(-row=>$row, -column=>0, -columnspan=>4, -sticky=>'w'); |
| 215 |
|
| 216 |
++$row; |
| 217 |
$fr -> Radiobutton(-text=>"Plot BFT + sum*window", , |
| 218 |
-variable => \$tft_params{plot}, |
| 219 |
-selectcolor => $red, |
| 220 |
-value => 4, |
| 221 |
-command => sub{tft_plot_kq(\%tft_params)},) |
| 222 |
-> grid(-row=>$row, -column=>0, -columnspan=>4, -sticky=>'w'); |
| 223 |
|
| 224 |
++$row; |
| 225 |
$fr -> Radiobutton(-text=>"Plot edge step", , |
| 226 |
-variable => \$tft_params{plot}, |
| 227 |
-selectcolor => $red, |
| 228 |
-value => 5, |
| 229 |
-command => sub{tft_plot_step(\%tft_params)},) |
| 230 |
-> grid(-row=>$row, -column=>0, -columnspan=>4, -sticky=>'w'); |
| 231 |
++$row; |
| 232 |
$fr -> Radiobutton(-text=>"Plot FT of edge step", , |
| 233 |
-variable => \$tft_params{plot}, |
| 234 |
-selectcolor => $red, |
| 235 |
-value => 6, |
| 236 |
-command => sub{tft_plot_stepft(\%tft_params)},) |
| 237 |
-> grid(-row=>$row, -column=>0, -columnspan=>3, -sticky=>'w'); |
| 238 |
$fr -> Button(-text=>"Replot", @button_list, |
| 239 |
-command => sub{tft_replot(\%tft_params)} |
| 240 |
) |
| 241 |
-> grid(-row=>$row, -column=>3, -sticky=>'ew'); |
| 242 |
|
| 243 |
## at the bottom of the frame, there are full width buttons for |
| 244 |
## returning to the main view and for going to the appropriate |
| 245 |
## document section |
| 246 |
$tft -> Button(-text=>'Return to the main window', @button_list, |
| 247 |
-background=>$config{colors}{background2}, |
| 248 |
-activebackground=>$config{colors}{activebackground2}, |
| 249 |
-command=>sub{&reset_window($tft, "understanding FTs", 0); |
| 250 |
}) |
| 251 |
-> pack(-side=>'bottom', -fill=>'x'); |
| 252 |
## help button |
| 253 |
$tft -> Button(-text=>'Document section: Understanding Fourier transforms', @button_list, |
| 254 |
-command=>sub{pod_display("bkg::ft.pod")}) |
| 255 |
-> pack(-side=>'bottom', -fill=>'x', -pady=>4); |
| 256 |
|
| 257 |
|
| 258 |
## and finally.... |
| 259 |
tft_plot_waves(\%tft_params); |
| 260 |
$top -> update; |
| 261 |
|
| 262 |
}; |
| 263 |
|
| 264 |
sub tft_replot { |
| 265 |
my ($rhash) = @_; |
| 266 |
my @callbacks = (sub{tft_plot_waves($rhash)}, |
| 267 |
sub{tft_plot_windowed($rhash)}, |
| 268 |
sub{tft_plot_r($rhash, 'm')}, |
| 269 |
sub{tft_plot_r($rhash, 'r')}, |
| 270 |
sub{tft_plot_kq($rhash)}, |
| 271 |
sub{tft_plot_step($rhash)}, |
| 272 |
sub{tft_plot_stepft($rhash)}, |
| 273 |
); |
| 274 |
my $this = $callbacks[$$rhash{plot}]; |
| 275 |
&$this; |
| 276 |
}; |
| 277 |
|
| 278 |
|
| 279 |
sub tft_make_arrays { |
| 280 |
my ($rhash) = @_; |
| 281 |
$$rhash{npts} = int($$rhash{ext}/0.05); |
| 282 |
my $command = q{}; |
| 283 |
$command .= sprintf("set t___ft.k = indarr(%d)*0.05\n", $$rhash{npts}); |
| 284 |
foreach my $w (qw(r1 r2 r3)) { |
| 285 |
if ($$rhash{$w} > 0) { |
| 286 |
$command .= sprintf("set t___ft.%s = 0.5*sin(t___ft.k*2*%.4f)\n", $w, $$rhash{$w}); |
| 287 |
} else { |
| 288 |
$command .= sprintf("set t___ft.%s = zeros(%d)\n", $w, $$rhash{npts}); |
| 289 |
}; |
| 290 |
}; |
| 291 |
$command .= "set t___ft.sum = t___ft.r1 + t___ft.r2 + t___ft.r3\n"; |
| 292 |
$command .= sprintf("fftf(t___ft.sum, k=t___ft.k, kweight=0, kmin=%.4f, kmax=%.4f, dk=%.4f, kwindow=%s)\n", |
| 293 |
$$rhash{kmin}, $$rhash{kmax}, $$rhash{dk}, $$rhash{kwin}); |
| 294 |
$command .= sprintf("fftr(real=t___ft.chir_re, imag=t___ft.chir_im, rmin=%.4f, rmax=%.4f, dr=%.4f, rwindow=%s)\n", |
| 295 |
$$rhash{rmin}, $$rhash{rmax}, $$rhash{dr}, $$rhash{rwin}); |
| 296 |
$groups{"Default Parameters"} -> dispose($command, $dmode); |
| 297 |
}; |
| 298 |
|
| 299 |
sub tft_plot_waves { |
| 300 |
my ($rhash) = @_; |
| 301 |
$top -> Busy; |
| 302 |
tft_make_arrays($rhash); |
| 303 |
my $command = q{}; |
| 304 |
$command .= "newplot(t___ft.k, t___ft.sum, color=blue, key=sum, xlabel=\"k (\\A\\u-1\\d)\", ylabel=\"sum of waves\")\n"; |
| 305 |
$command .= "plot(title=\"understanding Fourier transforms\")\n"; |
| 306 |
my $offset = -1; |
| 307 |
if ($$rhash{r1}>0) { |
| 308 |
$command .= "plot(t___ft.k, \"t___ft.r1+$offset\", color=red, key=\"first wave\")\n"; |
| 309 |
$offset--; |
| 310 |
}; |
| 311 |
if ($$rhash{r2}>0) { |
| 312 |
$command .= "plot(t___ft.k, \"t___ft.r2+$offset\", color=darkgreen, key=\"second wave\")\n"; |
| 313 |
$offset--; |
| 314 |
}; |
| 315 |
if ($$rhash{r3}>0) { |
| 316 |
$command .= "plot(t___ft.k, \"t___ft.r3+$offset\", color=darkviolet, key=\"third wave\")\n"; |
| 317 |
$offset--; |
| 318 |
}; |
| 319 |
$groups{"Default Parameters"} -> dispose($command, $dmode); |
| 320 |
$top -> Unbusy; |
| 321 |
return $offset; |
| 322 |
}; |
| 323 |
|
| 324 |
sub tft_plot_windowed { |
| 325 |
my ($rhash) = @_; |
| 326 |
$top -> Busy; |
| 327 |
tft_make_arrays($rhash); |
| 328 |
my $command = q{}; |
| 329 |
$command .= "newplot(t___ft.k, t___ft.sum, color=blue, key=sum, xlabel=\"k (\\A\\u-1\\d)\", ylabel=\"sum of waves\")\n"; |
| 330 |
$command .= "plot(title=\"understanding Fourier transforms\")\n"; |
| 331 |
$command .= "plot(t___ft.k, t___ft.win, color=darkgreen, key=window)\n"; |
| 332 |
$command .= "plot(t___ft.k, \"t___ft.sum*t___ft.win\", color=red, key=\"windowed sum\")\n"; |
| 333 |
$groups{"Default Parameters"} -> dispose($command, $dmode); |
| 334 |
$top -> Unbusy; |
| 335 |
}; |
| 336 |
|
| 337 |
sub tft_plot_r { |
| 338 |
my ($rhash, $part) = @_; |
| 339 |
$part = ($part =~ m{[impr]}) ? $part : 'r'; |
| 340 |
$top -> Busy; |
| 341 |
tft_make_arrays($rhash); |
| 342 |
my $command = q{}; |
| 343 |
my %suff = ('m'=>'chir_mag', r=>'chir_re', i=>'chir_im', p=>'chir_pha'); |
| 344 |
$command .= "newplot(t___ft.r, t___ft.$suff{$part}, xmax=7, color=blue, key=\"FT of sum\", xlabel=\"R (\\A)\", ylabel=\"FT of sum of waves\")\n"; |
| 345 |
$command .= "plot(t___ft.r, \"t___ft.$suff{$part}*t___ft.rwin\", color=red, key=\"windowed FT\")\n"; |
| 346 |
$command .= "plot(t___ft.r, t___ft.rwin, color=darkgreen, key=window)\n"; |
| 347 |
$groups{"Default Parameters"} -> dispose($command, $dmode); |
| 348 |
$top -> Unbusy; |
| 349 |
}; |
| 350 |
|
| 351 |
sub tft_plot_kq { |
| 352 |
my ($rhash) = @_; |
| 353 |
$top -> Busy; |
| 354 |
tft_make_arrays($rhash); |
| 355 |
my $offset = abs(tft_plot_waves($rhash)); |
| 356 |
my $command = q{}; |
| 357 |
#$command .= "newplot(t___ft.k, \"t___ft.sum*t___ft.win\", xmax=$$rhash{ext}, key=\"windowed sum\", color=blue, xlabel=\"k (\\A\\u-1\\d)\", ylabel=\"sum of waves\")\n"; |
| 358 |
#$command .= "plot(title=\"understanding Fourier transforms\")\n"; |
| 359 |
$command .= "plot(t___ft.q, t___ft.chiq_re, color=deeppink, xmax=$$rhash{ext}, key=backtransform)\n"; |
| 360 |
$groups{"Default Parameters"} -> dispose($command, $dmode); |
| 361 |
$top -> Unbusy; |
| 362 |
}; |
| 363 |
|
| 364 |
sub tft_step_arrays { |
| 365 |
my ($rhash) = @_; |
| 366 |
my $command = q{}; |
| 367 |
$command .= sprintf("set t___ft.k = indarr(%d)*0.05\n", $$rhash{npts}); |
| 368 |
$command .= sprintf("set t___ft.r%s = 0.5*sin(t___ft.k*2*%.4f)\n", 1, $$rhash{r1}); |
| 369 |
$command .= sprintf("set t___ft.step = 0.5*(1+erf(t___ft.k-%.3f))*(1+0.1*t___ft.r%s)\n", $$rhash{npts}/4*0.05, 1); |
| 370 |
return $command; |
| 371 |
}; |
| 372 |
|
| 373 |
sub tft_plot_step { |
| 374 |
my ($rhash) = @_; |
| 375 |
$top -> Busy; |
| 376 |
my $command = tft_step_arrays($rhash); |
| 377 |
$command .= "newplot(t___ft.k, t___ft.step, color=blue, xmax=$$rhash{ext}, key=\"step function + 2 \\A wave\")\n"; |
| 378 |
$groups{"Default Parameters"} -> dispose($command, $dmode); |
| 379 |
$top -> Unbusy; |
| 380 |
}; |
| 381 |
|
| 382 |
sub tft_plot_stepft { |
| 383 |
my ($rhash) = @_; |
| 384 |
$top -> Busy; |
| 385 |
my $command = tft_step_arrays($rhash); |
| 386 |
$command .= sprintf("fftf(t___ft.step, k=t___ft.k, kweight=0, kmin=%.4f, kmax=%.4f, dk=%.4f, kwindow=%s)\n", |
| 387 |
0, $$rhash{kmax}, $$rhash{dk}, $$rhash{kwin}); |
| 388 |
$command .= "newplot(t___ft.r, t___ft.chir_mag, color=blue, xmax=7, key=\"FT of step function + 2\\A wave\")\n"; |
| 389 |
$command .= "plot_arrow(x1=2, y1= 2, x2=2, y2=0.5, barb=0, size=1)\n"; |
| 390 |
$command .= "plot_text(x=2,y=2.3, text='location of 2\\A peak')\n"; |
| 391 |
$groups{"Default Parameters"} -> dispose($command, $dmode); |
| 392 |
$top -> Unbusy; |
| 393 |
}; |