A friend from my grad school days (back in the previous millenium) is an electrical engineering professor. Students in his
class recently asked him why the conv function is not implemented using FFTs.
I'm not on the team responsible for conv, but I wrote back with my thoughts, and I thought I would share them here as well.
Let's review the basics. Using the typical convolution formula to compute the one-dimensional convolution of a P-element
sequence A with Q-element sequence B has a computational complexity of . However, the discrete Fourier transform (DFT) can be used to implement convolution as follows:
1. Compute the L-point DFT of A, where .
2. Compute the L-point DFT of B.
3. Multiply the two DFTs.
4. Compute the inverse DFT to get the convolution.
Here's a simple MATLAB function for computing convolution using the Fast Fourier Transform (FFT), which is simply a fast algorithm
for computing the DFT.
type conv_fft
function c = conv_fft(a, b)
P = numel(a);
Q = numel(b);
L = P + Q - 1;
K = 2^nextpow2(L);
c = ifft(fft(a, K) .* fft(b, K));
c = c(1:L);
Note that the code uses the next power-of-two greater than or equal to L, although this is not strictly necessary. The fft function operates in time whether or not L is a power of two.
The overall computational complexity of these steps is . For P and Q sufficiently large, then, using the DFT to implement convolution is a computational win.
So why don't we do it?
There are several technical factors. Let's look at speed, exact computation, and memory.
Speed
One factor is that DFT-based computation is not always faster. Let's do an experiment where we compute the convolution of a 1000-element sequence with another sequence of varying
length. (Get the timeit function from the MATLAB Central File Exchange.)
x = rand(1, 1000);
nn = 25:25:1000;
t_normal = zeros(size(nn));
t_fft = zeros(size(nn));
for k = 1:numel(nn)
n = nn(k);
y = rand(1, n);
t_normal(k) = timeit(@() conv(x, y));
t_fft(k) = timeit(@() conv_fft(x, y));
end
For sequences y shorter than a certain length, called the cross-over point, it's quicker to use the normal computation.
Exact computation
A second consideration is whether the computation is subject to floating-point round-off errors and to what degree. There
are applications, for example, where the convolution of integer-valued sequences is computed. For such applications, a user
would reasonably expect the output sequence to be integer-valued as well.
function c = binom(n)
c = 1;
for k = 1:n
c = conv(c, [1 1]);
end
binom(7)
ans =
1 7 21 35 35 21 7 1
I wrote a variation called binom_fft that is the same as binom except that it calls conv_fft.
format long
binom_fft(7)
ans =
Columns 1 through 4
1.000000000000000 6.999999999999998 21.000000000000000 35.000000000000000
Columns 5 through 8
35.000000000000000 21.000000000000000 7.000000000000000 0.999999999999996
Whoops! What's going on? The answer is that the FFT-based implementation of convolution is subject to floating-point round-off
error.
I imagine that most MATLAB users would consider the output of binom_fft to be wrong.
Memory
The last technical consideration I want to mention is memory. Because of the padding and complex arithmetic involved in the
FFT computations, the FFT-based convolution implementation requires a lot more memory than the normal computation. This may
not often be a problem for one-dimensional computations, but it can be a big deal for multidimensional computations.
Final thoughts
The technical considerations listed above can all be solved in principle. The implementation could switch to using the normal
method for short or integer-valued sequences, for example. And there are FFT-based techniques such as overlap-and-add to reduce the memory load.
But the problem can get quite complicated. Testing floating-point values to see if they are integers, for example, can be
slow. Also, I suspect (but have not checked) that a multithreaded implemention of the normal computation will take advantage
of multiple cores better than a multithreaded FFT-based method. That would change the cross-over point between the two methods.
And the exact cross-over point will vary from computer to computer, making it likely that our implementation would be somewhat
slower for some people for some problems.
Overall, my inclination would be to provide FFT-based convolution as a separate function rather than reimplementing conv. And that's what we've done. See the function fftfilt in the Signal Processing Toolbox.
Do you disagree with this approach? Post your comment.
The R2009b release of MATLAB contains a new Tiff class. The primary purpose of this class is to provide lower-level access to creating and modifying image data and metadata in
an existing TIFF file.
Several customers have asked for the ability to embed an ICC profile into a TIFF file. The Image Processing Toolbox function
iccread can read a profile embedded in a TIFF file, but iccwrite can only write a stand-alone profile file.
Here is code using the new Tiff class to embed a profile in an existing TIFF file. Let's start by rewriting one of the Image
Processing Toolbox sample PNG image files as a TIFF file.
rgb = imread('peppers.png');
imwrite(rgb, 'peppers.tif');
s = dir('peppers.tif')
We've been working for a while now to make Image Processing Toolbox functions run faster. The R2009b release notes mention several performance improvements. We've gotten some feedback, though, that our release notes are pretty vague about
the improvements. I can't argue with that impression. We tend to be vague because performance optimization is a very complex
topic, and it can be quite difficult to characterize performance changes in a way that is brief, understandable, and accurate
for every user's own hardware and data.
But I've decided to start posting more detailed information here about the performance improvements. I have more flexibility
(and room!) here than we have with the release notes.
Today I'll tackle imreconstruct, which performs morphological reconstruction. Reconstruction is a very useful operation that I've written about here before.
For example, see my post from last year on opening by reconstruction. Several other Image Processing Toolbox functions call imreconstruct, including imclearborder, imfill, imhmax, imhmin, imextendedmax, imextendedmin, and imimposemin.
So how long does that call to imreconstruct take in R2009b? I'll use my function timeit, which you can download from the MATLAB Central File Exchange.
timeit(@() imreconstruct(marker, text))
ans =
0.0241
That time is about 45 times faster than the same operation performed in R2009a. Note that I'm running on two-core computer;
the improved imreconstruct is multithreaded, so the performance improvement would be greater on a four-core computer, for example.
Now let's time gray-scale reconstruction. I'll make a 1024-by-1024 test image and compute a marker image by subtraction.
This kind of operation is often used to suppress small peaks in an image.
I = repmat(imread('rice.png'), 4, 4);
marker = I - 20;
timeit(@() imreconstruct(marker, I))
ans =
0.0201
This time is about 30 times faster than R2009a, again running on my two-core laptop.
Now for some key caveats you should know. For now, the performance improvements described here only work for 2-D inputs that
are uint8, uint16, or single, and only when the specified connectivity is 4 or 8. We'll be working in the future to extend
the speed improvements to other inputs.
I spent all of yesterday interacting with visitors to the MATLAB Virtual Conference. It was great fun! A blogger wrote that it was the "high point of geekism and nerdity of this year." (I think that was intended to be a compliment!)
It's not too late to hear the presentations. You can still visit the conference link and listen to the talks there, or you can download them as podcasts.
I started out early in the morning in the Image and Video Processing booth, participating in the group chat there. When it got busy it was a bit challenging to follow all the conversational threads, but I think most people got their questions answered, and I came away with a lot of good product feedback.
Then I went over to hear Tom Kush and Roy Lurie give their "MATLAB Universe" keynote address. If you want to hear about the world-wide impact of MATLAB and get some insight into where we think MATLAB is going, check out this talk. (I didn't get a chance to listen to the other talks yesterday, but I will later.)
The experience reminded me of some of the MATLAB user conferences in the 1990s. One of the only times I've ever heard company president Jack Little tell a joke was during his keynote address at the 1995 user conference in Cambridge, Massachusetts. We had been working for several years on MATLAB 5, which was going to be a huge release. Conference attendees had been promised a preview. So at the opening of the conference, in an enormous ballroom, Jack fired up the development build of MATLAB 5 on the big screen and started to talk about all the exciting new features to come. He said there would be a few changes that might take some getting used to. (We developers were sitting on the back row, holding our breath.) Jack explained that we had decided to adopt RPN (Reverse Polish Notation, popular in HP engineering calculators such as my beloved HP 15C) in MATLAB. He proceeded to type something like:
>> 5 3 +
There was a massive collective gasp! from the audience as we about fell off our chairs in the back trying not to laugh out loud.
To any reader who was there (and is still recovering), I can only say, "We're sorry!" ;-)
OK, back to the present. Later in the day I gave a talk on MATLAB array indexing techniques that are useful for image processing, borrowing heavily from material I originally posted here on the blog. I was afraid the material would be too narrowly focused, but the audience stayed with me to the end and then asked a bunch of great questions about my last topic, neighbor indexing, which was pretty advanced.
For the first five minutes of the talk (which I recorded a couple of weeks ago), I was in a rapidly increasing panic, because a 2-second echo was making the audio completely unintelligible! I bolted out of my office to find someone to help. Kevin calmed me down and helped me figure out that I had a second, forgotten, browser window open to the talk, and the echo was coming there. Boy did I feel silly. Thanks, Kevin!
Readers, I have two questions for you. First, did you attend any of the original MATLAB user conferences in the 90s? Any favorite memories to share?
Second, did you attend the virtual conference yesterday? What did you think? If we do it again, what can we do better next time?
Thanks for taking the time to give us your feedback.
We discovered recently that we broke application deployment when using the MATLAB Compiler with Image Processing Toolbox functions in R2009b. The problem happens only on Windows and is caused by some missing shared libraries in the deployed application.
If you use the R2009b MATLAB Compiler on Windows with the Image Processing Toolbox, please see our published bug report for a workaround. (Note: there is no need to apply this workaround to earlier releases.)
We are reviewing our procedures to try to make sure something like this doesn't happen again.
Last week Oliver asked for the release in which CMYK TIFF support was added to imwrite. Oliver then followed up asking for MathWorks to make release notes available for old versions.
In case anyone else is interested, here's one way to navigate to release notes for older versions of MATLAB.
Note that release notes are available there for releases back to R14 in 2004. The release notes summarize new features, compatibility considerations, and fixed and known bugs.
OK, I'm embarrassed that I didn't even know about this particular new feature in R2009b. Yesterday I was in another developer's office looking at his latest project. He was typing away in MATLAB when all of a sudden he did something that made me sit up and look closer at the screen. "Hey!" I said. "Do that again!"
He had pressed TAB while typing in the filename input argument to imread, and it automatically completed the filename!
Really, I had no idea they had done this.
Here's a screen snippet.
The functions imread, imfinfo, and imwrite all now support tab completion for filenames in your current working folder.
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.
Recent Comments