/*******************************************************
Digital Micrograph script 'hanning_FFT.s'
by Ruben Bjorge
v.1.0 2007-10-09
This script multiplies a rectangular selection or image with dimensions of the power of two by a Hanning window
before applying the fast Fourier transform. The image average is subtracted first.
This script is partly based on the 'HanningWindow.s' script found on the Gatan website.
*/
number size, sizeX, sizeY, top, left, bottom, right, ii, posX, posY
number zoom, test
image front, hannX, hannY, hann, avg, hannout
compleximage fft
front := GetFrontImage();
GetSize(front, sizeX, sizeY);
GetSelection(front, top, left, bottom, right);
GetWindowPosition(front, posX, posY);
zoom = GetZoom(front);
// Test if image/selection has dimensions of the power of two.
test = bottom - top;
while( test / 2 >= 1 )
test = test/2;
if( test != 1 )
Throw( "Only for images (selections) of the power of two!" );
test = right - left;
while( test / 2 >= 1 )
test = test/2;
if( test != 1 )
Throw( "Only for images (selections) of the power of two!" );
// Create Hanning window.
ii = 1;
hannX := CreateFloatImage("", (right-left), (bottom-top));
hannX = 0;
hannX[0, 0, 1, (right-left)] = 1 - cos( 2 * Pi() * icol / (right-left));
while( ii < (bottom-top) )
{
hannX[ii, 0, 2*ii, (right-left)] = hannX[0, 0, ii, (right-left)];
ii = ii * 2;
}
ii = 1;
hannY := CreateFloatImage("", (right-left), (bottom-top));
hannY = 0;
hannY[0, 0, (bottom-top), 1] = 1 - cos( 2 * Pi() * irow / (bottom-top));
while( ii < (right-left) )
{
hannY[0, ii, (bottom-top), 2*ii] = hannY[0, 0, (bottom-top), ii];
ii = ii * 2;
}
hann = hannX * hannY;
// Subtract average from image.
avg = front - Average(front);
// Multiply with Hanning window.
hannout = avg[top, left, bottom, right] * hann;
// Do fast Fourier transform and display image.
fft = RealFFT(hannout);
SetName(fft, GetName(front) + "_FFT");
DisplayAt(fft, posX + 14, posY + 21);