필터 지우기
필터 지우기

Is Conv2 based on direct convolution?

조회 수: 8 (최근 30일)
Håkon Hoel
Håkon Hoel 2023년 12월 4일
댓글: Christine Tobler 2023년 12월 7일
Is the conv2 function in Matlab 2023a, in particular the way it operates on 1d vectors, based on direct convolution or fft? I understand that you do not want to share all details on how this function is implemented, but for making use of this function in a scientific work where round-off errors and computational cost enter, it would be good to state with confidence that the method is either direct-based or fft-based.

채택된 답변

Matt J
Matt J 2023년 12월 4일
편집: Matt J 2023년 12월 4일
It is not FFT-based.
To do FFT-based convolution (using the overlap-and-add method) you could use fftfilt.
  댓글 수: 4
Håkon Hoel
Håkon Hoel 2023년 12월 5일
I agree the cpu-timing shows that conv is not implemented using FFT. I have observed similarly, and I have also observed that the result of calling conv(f,g) is almost identical to the result of a direct computation of (*) with a "naive" double for-loop, while it is not always almost identical to using FFT-based convolution. That is, when you convolve vectors with all elements smaller than the double precision eps_double in f and g, then FFT seems to introduce round-off errors of magintude eps_double, making the relative error of the convolution very large. This does not happen with conv(f,g), which in my experience manages to keep the relative error small also in such settings, as one would expect from a direct convolution method. My issue is that these are all indications and I would like a definite yes or no, but your insight was nonetheless helpful!
Christine Tobler
Christine Tobler 2023년 12월 7일
I can confirm that conv2 uses a direct convolution method and not fft, and have passed along the suggestion to make this explicit in the documentation.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2023년 12월 4일
Historically, MATLAB implemented several different cases according to size and data type (and matters such as complex-valued or not.) The general case was done by direct convolution -- but possibly by calls into high speed libraries. The special cases involved decomposition into kernels and some kind of transform.
The source code for the direct convolution used to be visible. The decomposition etc., was something I do not recall ever seeing the source code for (that is, it might have been present in MATLAB source form in sufficiently old versions in the time before I thought to look for it.)
  댓글 수: 2
Walter Roberson
Walter Roberson 2023년 12월 4일
Note: historically, conv2() was implemented as a series of calls to conv() with transposes between.
Håkon Hoel
Håkon Hoel 2023년 12월 5일
편집: Håkon Hoel 2023년 12월 5일
Thank you for complementing the above answer! I would accept this answer too, if possible. I believe that parallelization would not produce the round-off errors I have in mind, as they relate to the relative error blowing up when eps_double \approx 10^{-16} is large in magnitude relative to all elements in the vectors being convolved. In such cases my observation is that round-off errors from FFT-based convolution can lead to O(1) relative error while conv2() manages to maintain a low relative error.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by