Medulla Oblongata
Medulla Oblongata

Reputation: 3973

Convolution of Matlab functions

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.

Upvotes: 0

Views: 869

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 102710

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

Upvotes: 1

Related Questions