On Friday, September 28, 2012 06:20:19 AM Jim wrote:
> I agree, however the same effect can be achieved by increasing the sample
> rate. This results in a trade-off of speed vs fidelity.
Maybe we're talking about different things, but to clarify possible confusion:
increasing the sample rate won't solve the boundary condition issue that
Elliot first raised. (It will change the amount of data you have to process,
and increasing the rate will generally slow down the computation.) There's a
nice illustration on the page about circular convolution that Elliot linked
to: in the 4th and 5th panels of the figure, note that the red bit on the right
edge wraps around to the left, and the summation (6th panel) naturally
includes both. If you don't want that to happen, you have to pad.
However, if you're really interested in speed, AND you know that the support
of one of the two items that you're convolving with is limited (perhaps to a
small percentage of the total area), THEN you don't actually need full two-
fold padding. All you need to pad by is the size of the support.
Finally, a treat for those interested in speed, FFTs, and padding: FFT
algorithms are most efficient---sometimes dramatically so---when the domain size
is a product of small primes. I wrote "nextprod" (Julia bonus! Not present in
most, or perhaps any, other languages!) to compute the smallest integer
expressible as a product of given primes, greater than or equal to a given
size. For example:
julia> n = 1973 # a not-small prime number, FFTs will be slow
1973
julia> z = randn(n);
julia> @time for i = 1:1000; fft(z); end
elapsed time: 0.4927511215209961 seconds
julia> @time for i = 1:1000; fft(z); end
elapsed time: 0.503350019454956 seconds
julia> p = nextprod([2,3,5,7], n) # next int of form 2^a*3^b*5^c*7^d
2000
julia> z = randn(p);
julia> @time for i = 1:1000; fft(z); end
elapsed time: 0.13319087028503418 seconds
julia> @time for i = 1:1000; fft(z); end
elapsed time: 0.12734603881835938 seconds
If you can be flexible about your padding size, I highly recommend using it.
With [2,3,5,7] the ratio of p/n never gets more than a few percent above 1, so
it barely increases your storage requirements. That's why this strategy beats
the old way of just using powers of 2, which can have really bad consequences
for 3-dimensional FFTs.
Perhaps conv2 should be rewritten to use it...
--Tim