convolution of two functions

151 views (last 30 days)
richard
richard on 1 Sep 2013
Commented: Michael Reshko on 4 Apr 2019
I want to convolve two functions:
f=inline('gaussmf(x,[3,0])','x')
h=inline('normpdf(x,0,4)','x')
So the conv will not work since it only deals with vectors, I have functions I am wanting to convolve.
How do I tell matlab to do this? Do I turn my function into a matrix/vector or something??

Answers (3)

John BG
John BG on 25 Aug 2016
Edited: John BG on 25 Aug 2016
Richard
You don't really need the Symbolic toolbox. MATLAB already have classes to define standard and custom probability functions. For instance to define the Gaussian pdf:
pd1=ProbDistUnivParam('normal',[0 3]);
Since plotting is windowing, you have to define the x range, and then obtain the values of the pdf to input to conv:
x=-10:.1:10;y1=pdf(pd1,x);
The second pdf:
pd2=makedist('Uniform');
pd2.Lower=0;pd2.Upper=4;
y2=pdf(pd2,x);
Now convolving y1 and y2
z=conv(y1,y2);figure(1);plot(x,y1,x,y2);figure(2);plot(z);
The use of function int suggested by Roger comes from the definition of the convolution, that can be obtained with symbolic parameters. But you will need to 'frame' or 'window' anyway when attempting any plot as you mention is your goal here.
Such answer, integrating along t the product pdf1(t)*pdf2(t-x), is explained in the question
with accepted answer by Ghada Saleh, following the key lines of that answer:
syms x b c t;
f1 = a*exp((-x)/(b))+1;
f2 = (1/(4*pi*c^2))*exp((-x^2)/(4*c^2));
f2_c = (1/(4*pi*c^2))*exp((-(t-x)^2)/(4*c^2)); %f2_c = f2(t-x)
result = int(f1*f2_c,t,-inf,inf);
Richard, please if you find my answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
  1 Comment
Michael Reshko
Michael Reshko on 4 Apr 2019
You begin with two functions defined on [-10:10], but your last plot of the convolution is on [0; 450]. What are the x values corresponding to the convolution z?

Sign in to comment.


Roger Stafford
Roger Stafford on 2 Sep 2013
This is hopefully a problem that the symbolic toolbox's 'int' function could solve for you. The convolution of the two functions you have given can be expressed as:
F(t) = int(f(s)*h(t-s),'s',-inf,+inf)
If 'int' can get an answer, it will depend on t and that will be your convolution function.
The reason I have hope that 'int' can succeed in this is that I know I could solve it by hand. Your integrand would look something like this:
exp(-s^2/18)*1/sqrt(2*pi)/4*exp(-(t-s)^2/32) =
1/sqrt(2*pi)/4 * exp(-s^2/18-(t-s)^2/32)
The quantity within 'exp' is a quadratic in s and by doing a "completion of the square" it can be expressed in the form
exp(-(s-a)/(2*b^2)) * something that doesn't depend on s
where a and b are certain quantities which depend on t. Hence this is something that can be evaluated exactly as a function of t (with sufficient sweat.) But as I say, hopefully 'int' could save you that sweat.
  1 Comment
richard
richard on 2 Sep 2013
My goal is to plot F(t) but an 'explicit integral' could not be found. Am I at a dead end, or can matlab plot F(t) without knowing what it is/computing it...

Sign in to comment.


Roger Stafford
Roger Stafford on 2 Sep 2013
I was hoping 'int' would solve this for you. Shame on 'int'! Oh well, assisted by my own ancient symbolic toolbox, I have hand-derived the convolution function for you. It is simply this. With the functions
f(x) = gaussmf(x,[sg,mg])
h(x) = normpdf(x,mn,sn))
their convolution will be:
F(t) = int(f(s)*h(t-s),'s',-inf,inf) =
sg/sqrt(sg^2+sn^2) * exp(-(t-mn-mg)^2/(2*(sg^2+sn^2)))
You can plug in your desired values for the mean and standard deviation for the two distributions as values for mg, sg, mn, sn.
  2 Comments
richard
richard on 4 Sep 2013
So I know realize the goal of my actual assignment was not to find the actual F(t) function, I just had to plot it, and find the area under the F(t) curve.
I plotted out F(t) by using the conv2 function, and then I used the trapz command to find the area under F(t).
The F(t) you found is correct, because I reconciled finding the integral of your F(t) (using 'int') & using the trapz command for the curve I obtained and the results matched.
I thought I owed you an explanation for your assistance, and your help made me think more about the problem (and it helped me check my work!)
Now I know how 'conv2' works, I was using the wrong command in 'conv'. How does 'accept this answer' work anyway? Do you get free toolboxes if enough people approve your answers?
Roger Stafford
Roger Stafford on 4 Sep 2013
You don't actually need to use 'trapz' to find the area under F(t). There is an exact answer.
int(F(t),'t','-inf','inf') =
sg/sqrt(sg^2+sn^2) * sqrt(2*pi)*sqrt(sg^2+sn^2) =
sqrt(2*pi)*sg
which interestingly enough is the normalizing factor whose reciprocal is needed to convert the 'gaussmf' gaussian membership function to a normal pdf distribution.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!