0

I want to calculate a convolution in Matlab where the I declare the functions inside the script file. MWE is

a = 0.9; b = 0.5;

X = @(t) exp(-(b*t).^2);
Y = @(t) exp(-a*b*t.^2);

Z = convnfft(X,Y,'same'); % this is how you usually do convolution when t=linspace(-20,20,1000)

my_integral = integral(Z,-Inf,Inf)

I am using this convolution routine taken from the MathWorks website.

Are there any efficient Matlab convolution routines/programs that can convolve the X and Y functions? If I explicitly calculate the convolution integral using symbolic math, it takes so long for these MWE X and Y, and it will take even longer to calculate my actual functions.

My goal is to integrate the result of convolution from -Inf to Inf.

Medulla Oblongata
  • 3,771
  • 8
  • 36
  • 75
  • Why does it have to be a function? It looks to me that `a` and `b` are just scalars i.e. simple multiplication. – kkuilla Feb 24 '20 at 09:50
  • Not sure I understand what you mean. As I said in my post, this is a MWE. The `X` and `Y` in my actual code is more complicated than the `exp` functions here. I'm just wondering if there are any Matlab functions that can convolve these `X` and `Y` . – Medulla Oblongata Feb 24 '20 at 10:13

1 Answers1

1

Maybe you can try the code below

a = 0.9; b = 0.5;

X = @(t) exp(-(b*t).^2);
Y = @(t) exp(-a*b*t.^2);

% convolution is formulated as `integral(@(u) X(z-u).*Y(u),-Inf,Inf)` at any given value `z`, and we can vectorize the convolution by `arrayfun`
fconv = @(t) arrayfun(@(z) integral(@(u) X(z-u).*Y(u),-Inf,Inf), t);

and you can call the function fconv like below

>> fconv(1:5)
ans =

   1.803967   1.113875   0.498712   0.161908   0.038115
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81