Code for matlab convolution by FFT

In summary, the conversation is about computing the convolution of two functions in Matlab using the fast Fourier transform. The conversation includes a discussion about theoretical and non-theoretical methods, the need for zero-padding, and a request for a book recommendation on the topic. The conversation also includes a snippet of code and a plot showing the results of the convolution. The summary concludes with the speaker expressing their desire for a book to help them troubleshoot their code.
  • #1
bjnartowt
284
3

Homework Statement



You have two functions in Matlab (represented as column vectors). Compute their convolution using the fast Fourier transform.


Homework Equations





The Attempt at a Solution




I am having trouble finding a book with this topic. I would like to know where the Matlab-code for such an algorithm would be discussed. It does not seem to be treated in my Giordano/Nakanishi computational text, nor is it in a text on pure Matlab programming. I just need someone to tell me a book.
 
Physics news on Phys.org
  • #2
Ignoring Matlab for a moment, do you know how to compute a convolution using an FFT, theoretically?
 
  • #3
Yeah--I know how to theoretically a convolution.See attached.

I also know that "non-theoretically", the domain over which you convolve is not infinite, so you need to zero-pad. I also know you have to "shift" the convolution.

I have written some code already, and little gremlins keep making it not work. Just kidding...but that's what it seems like. I was looking for a text to help me figure out how to do it correctly.
 

Attachments

  • conv.png
    conv.png
    4.4 KB · Views: 704
  • #4
I know how to do it, and I have code that, in theory, should work properly. The code is behaviing in such a bizarre manner that I am almost certain I need to consult books about the "theory" of convolving-by-fft in order to grasp what the bug is.
 
  • #5
Would you be able to explain what you think is bizarre? The result of ifft(fft(f).*fft(g)) should be the same as cconv(f, g, length(f)) (sizes of f and g must be the same). That is, it will be the same as circular convolution, modulo the length of the vectors.

It will not be the same as conv(f, g), since that would be a vector of length length(f)+length(g)-1.
 
  • Like
Likes 1 person
  • #6
I took two boxcar functions, f=bxc(x,-1,1); g=bxc(x,-2,2); (f has a value of 1 from -1 to 1, and g has value of 1 from -2 to 2; they are 0 everywhere else). The analytic form convolution can be computed, integrate(f(x')*g(x-x')), from x integrated over all domain, yields,

analytic=-(3+xp).*heaviside(-3-xp)+(1+xp).*heaviside(-1-xp)-(-2+xp).*heaviside(2-xp)+xp.*heaviside(-xp);

You have the vectors x = -1:0.01:1; xp = -2-0.005:0.01:2+0.005; where the vector xp is double the length of vector x, because the padding doubles the length of the convolved function.

The program produced the following plot [see attached .png], where "result" is circularly shifted,
plot(xp,circshift(result',200)',xp,circshift(analytic',0)');

-------------------------------------------------------------
clear; clc;
bxc = @ (x,xmin,xmax) heaviside(xmax-x)-heaviside(xmin-x);

x = -1:0.01:1; xp = -2-0.005:0.01:2+0.005;
L = length(x) -1;

% f = sin(pi*x); g = [ zeros(1,L/2) 1 zeros(1,L/2) ];
f=bxc(x,-1,1)*1.2; g=bxc(x,-2,2)*1.2;
analytic=-(3+xp).*heaviside(-3-xp)+(1+xp).*heaviside(-1-xp)-(-2+xp).*heaviside(2-xp)+xp.*heaviside(-xp);

fpad = [ zeros(1,L/2) f zeros(1,1 + L/2) ];
gpad = [ zeros(1,L/2) g zeros(1,1 + L/2) ];

fpadfft = fft(fpad);
gpadfft = fft(gpad);
prodfft = fpadfft.*gpadfft;
result = ifft(prodfft)*0.01;
% result2 = ifft(gpadfft.*gpadfft);

indexi = 1:(2*L + 2);
shiftzero = 1 + mod(indexi -1 +L,(2*L + 2));

plot(xp,circshift(result',200)',xp,circshift(analytic',0)');
 

Attachments

  • conv2.png
    conv2.png
    4.3 KB · Views: 711
  • #7
I am sorry--I see at least one mistake. My analytic convolution was taken for boxcar functions that run to intervals as wide as -2 to 2, but the convolution I effect numerically is over the interval -1 to 1. On remedying this problem, one convolution-function still appears stretched out with respect to the other.
 
  • #8
Anyway--I don't expect someone to troubleshoot my code. I just need the name of a book in which this is discussed and treated pedagogically.
 

Related to Code for matlab convolution by FFT

1. What is the purpose of using FFT in convolution for Matlab?

The Fast Fourier Transform (FFT) is used in convolution for Matlab to speed up the process of calculating convolutions by converting the convolution operation into a multiplication in the frequency domain. This allows for faster and more efficient calculation of convolutions in Matlab.

2. How does FFT help improve the performance of convolution in Matlab?

FFT improves the performance of convolution in Matlab by reducing the computational complexity from O(n^2) to O(nlogn). This is because FFT breaks down the convolution operation into smaller and simpler operations, making it faster and more efficient.

3. Can FFT be used for any type of convolution in Matlab?

Yes, FFT can be used for any type of convolution in Matlab, including 1D, 2D, and higher-dimensional convolutions. However, the size of the inputs must be powers of 2 for the FFT algorithm to work efficiently.

4. How accurate is the FFT-based convolution compared to traditional convolution in Matlab?

The FFT-based convolution in Matlab is just as accurate as the traditional convolution method. FFT simply improves the efficiency of the calculation without affecting the accuracy of the results.

5. Are there any limitations to using FFT for convolution in Matlab?

One limitation of using FFT for convolution in Matlab is that the inputs must be of equal size and powers of 2. This means that in some cases, padding or cropping of the inputs may be necessary before using FFT for convolution. Additionally, the FFT-based convolution may not be suitable for certain types of signals, such as non-stationary or non-linear signals.

Similar threads

  • Differential Equations
Replies
11
Views
2K
  • Other Physics Topics
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Linear and Abstract Algebra
Replies
2
Views
817
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
Replies
1
Views
1K
  • Programming and Computer Science
Replies
7
Views
688
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
16
Views
13K
Back
Top