Steve on Image Processing

September 11th, 2008

Independent computation in software tests

Today's post is brought to you directly from the software developer half of my brain.

MATLAB 6 was the first version to use the FFTW library for fast Fourier transform computations, and I was one of the developers who worked on that change.

Making changes to fft and related functions made me a bit nervous. What if we made a mistake and broke it somehow? That would be 15 minutes of fame I would definitely not enjoy.

The best way to proceed when changing existing, working code is to make sure first that you have thorough, effective tests. (See, for example, "The First Step in Refactoring" in Martin Fowler's book, Refactoring: Improving the Design of Existing Code.) And for numerical computation, nothing gives you more confidence in your tests than using independent computation methods to validate your code.

Besides using fft, I can think of at least two other reasonably easy ways to compute the discrete Fourier transform in MATLAB. One is to write an M-file that directly implements the discrete Fourier transform equation. Another is to use a matrix-vector multiply with the Fourier matrix (see the doc for the dftmtx function in the Signal Processing Toolbox.) Normally, you would never want to use either of these methods when you can instead use a much faster fast Fourier transform algorithm.

However, pure speed is not so critical for testing purposes. Many of our automated tests for fft and related functions test their output against the result found using the matrix-vector multiplication. That turned out to be very helpful for making sure that our changes to fft did not result in incorrect output. (If you'd like to know more about MATLAB and FFTW, read this Cleve's Corner.)

Here's another testing example illustrating the need for independent computation. The Mapping Toolbox has functions for computing minimum distance curves on an ellipsoid. No closed-form solutions exist, so series or iterative approximations are commonly used. The Mapping Toolbox test suite checks the numerical accuracy of these approximations by comparing them to the results obtained using MATLAB ODE solvers. (This testing technique was published in a paper. See R. Comer and N. Ramarathnam, "Numerical Validation of Curves on the Ellipsoid," Proceedings of the ASPRS 2005 Annual Conference.)

Often you can independently compute small positive test cases by hand. Many image morphology test cases in the Image Processing Toolbox test suite were computed by hand directly from the relevant formulas and definitions.

It's tempting sometimes, especially for operations that are especially tedious to compute by hand or by another method, to construct test case validation data by saving the output data from the code you are trying to test. Such a test is only useful for making the sure that today's output from the code is the same as yesterday's; it doesn't tell you anything about whether the answers are correct. That's better than no test at all, but not nearly as good as independent validation of correctness.

2 Responses to “Independent computation in software tests”

  1. Chas Kenny replied on :

    Hi Steve,
    Does your new fft allow comparison of phase, one image to another, greater than modulo 2 pi?
    Thanks,
    Chas

  2. Steve replied on :

    Chas—The fft function computes the discrete Fourier transform. No more, and no less.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Steve Eddins manages the Image & Geospatial development team at The MathWorks and coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

  • Sana: hi steve, could you explain to me how i would be able to use the dir function, to do a loop through a directory...
  • Nishtha: Sir, I have preprocessed the image in following steps: [1] adaptive histogram equalization [2] thresholding...
  • Kristof: I also strongly support the idea. I have just recently bumped into the problem that im2single was not...
  • Steve: David—I’ m glad you found it useful!
  • David Lalejini: I found your example very useful for finding connected nodes in a large set of input pairs. I start...
  • tommy: Dear Steve, I have a question,please if you are kind to help me regarding the accumulator array dimensions of...
  • Steve: Abc—I don’t know how to distinguish the faces. You might try posting your question in the MATLAB...
  • Manju: well if we have a few ovals within each other like in a cell how do we measure the distance from the center...
  • Steve: Manju—What do you mean? How is each region defined?
  • Manju: if we have 2-3 regions within each other how do we measure the regions of each one?

These postings are the author's and don't necessarily represent the opinions of The MathWorks.