# Fitting a grid to an STM image

Suppose I have a scan from an STM image (very much like the things you see here). Suppose I have a simple square lattice with lattice parameter a.

What I'd like to do is to numerically find the lattice parameter, measured in units of pixels, assuming that calibration is done elsewhere.

A first idea of mine was to have a function that creates a grid of points with lattice constant a, some offsets where the grid starts and also some angle for the rotation of the lattice. I'd then sum the values of the STM image at each grid point and return that. I'd then use some optimization toolbox (MATLAB, Python, ...) to find the parameters that maximize this sum.

Unfortunately, I run into problems doing this. For example, the grid points are calculated from the angle, lattice constant and offset and are then rounded to an integer value so that I can actually address my 2D image in the form

ImgData[x][y]

but most optimization routines will then go on to vary the parameters only very slightly, so that the rounded coordinates don't change. The program then assumes that it has found a local minimum/maximum since the function value doesn't change if the parameters are changed only slightly.

There are other problems regarding the stability of this method, so I wondered if there is a more sophisticated way of doing this in an automated fashion. I could always take the FT of the image data and read off the lattice constant manually, but I'd then still like to put a best-fid grid overlay over my image data so I'd have to optimize for angle and offset.

• Is there any way you could automate the process of taking the Fourier transform of the image data to fit the lattice constant? The fewer decision variables you need for the fit of the STM image, the quicker the problem will solve. You may also be able to take advantage of knowing the lattice parameter when you formulate your optimization problem. Also, could you describe how you take the numerical image data and determine where the atoms (I'm presuming that's what the lattice points are) are? – Geoff Oxberry Jan 10 '12 at 23:49
• Very often the image contains lots of defects, so some human insight into choosing the correct points in the Fourier transform is necessary. Just picking the most intense points doesn't work. From the image data I define where the lattice points are as the points where the absolute value of the image data is largest. (This may or may not coincide with actual atomic positions depending on the electron orbitals). – Lagerbaer Jan 11 '12 at 4:09
• If the image contains a lot of defects, and some human insight in choosing the correct points is necessary for the Fourier transform case, how do you think an optimization routine will overcome the need for human intervention? Finding the lattice spacing from the FT of the image data seems like an easier version of your problem (because you are solving for a subset of the information you ask for in your question). Is there a way to combine reasonably the two approaches? – Geoff Oxberry Jan 12 '12 at 21:42

What you're doing is something like a fourier transform: you're projecting the image onto a function ($e^{ikx}$ in the case of the FT, and a sort of periodic "block" function in your case). It might be a little slow, but you could calculate the inner product of the lattice function and your image for every possible lattice function, and then find the maximum.