MATLAB Answers

X axis for convoluted function

7 views (last 30 days)
dinesh kumar mohan
dinesh kumar mohan on 9 Oct 2020
Answered: David Goodmanson on 11 Oct 2020
I convoluted gamma distributed having x axis range from 1 to 100 (m=100) and normal distribution with x axis range from -25 to 24 (n=50). By using conv function in MATLAB I got m+n-1 = 149 values. How can I determine the corresponding X axis range, as I need corresponding x axis values for these 149 values obtained after convolution. I have attached the sample code for reference.
x= -25:1:24; x1 = 1:1:100;
normal = normpdf(x,0,1); gamma = gampdf(x1,20,2);
w = conv(gamma,normal);

  4 Comments

Show 1 older comment
dinesh kumar mohan
dinesh kumar mohan on 10 Oct 2020
Sorry could you please elaborate. My question is what is the corresponding X axis for the convoluted function
Image Analyst
Image Analyst on 10 Oct 2020
The x axis will have as many elements as the sum of your two vector lengths minus 1, or 50+150-1 = 149. Where the "origin" is depends on what you're defining as your base function, liks Rik said.
Jeff Miller
Jeff Miller on 10 Oct 2020
Excuse me for possibly going off-topic here, but I wonder whether the 'conv' function actually does what the original poster (OP) wants.
In the context of probability distributions like the normal and gamma that were mentioned, a "convolution" is the distribution of a random variable that is formed by summing two other distributions. The attached figure shows some example probability density functions (pdfs). From left to right, the first shows the pdf of a normal random variable N, the second shows the pdf of a gamma G, and the third shows the pdf of the sum of N+G (assuming independence).
I wonder if the OP thinks that the w values computed by 'conv' represent the pdf values of N+G. If so, OP might be asking what the corresponding x values are (e.g., so that the convolution can be plotted). Now I haven't fully understood the relation between what 'conv' computes and these statistical "convolution" distributions, but they are clearly not the same.
So, back to the OP, are you actually trying to compute the pdf for the distribution of the sum of independent normal and gamma random variables?

Sign in to comment.

Answers (1)

David Goodmanson
David Goodmanson on 11 Oct 2020
Hi dinesh,
Here is an example with two normal (gaussian) pdfs. Arrays x and f(x) describe one of them, and arrays y and g(y) describe the other. To make it general, x and y have different ranges and different number of total points, However, to keep things sensible each of the x and y arrays have the same spacing. The convolution of f and g, called fOg here, is a function fOg(z) of the array z. The key point is that the starting point of the z array is the sum of the starting points of the x and y arrays.
In this example, f(x) peaks at x = 5 and g(y) peaks at y = 9 so you would expect the convolution, which describes the pdf of (x+y), to peak at z = 14 which it does. (Since a gaussian is symmetric about the peak, the peak value and the mean value are the same. And the convolution of gaussians is still a gaussian). If the array limits of x and y are changed, the peak remains at z = 14 (unless the array boundaries start to crowd into the tails of the gaussians and make them not gaussians).
I didn't pay attention to normalization of the pdfs here since the main point is to describe the convolution position. However it should still be true that the integrals described in the last two lines are equal, which they are.
del = .001; % array spacing
xa = 1; % x array min, max
xb = 7;
nx = (xb-xa)/del; % number of x arrray intervals
x = xa + del*(0:nx);
f = exp(-5*(x-5).^2); % mean = 5
ya = 5; % y array min, max
yb = 12;
ny = (yb-ya)/del;
y = ya + del*(0:ny);
g = exp(-4*(y-9).^2); % mean = 9
fOg = conv(f,g)*del; % multiply by del so conv sum --> riemann integral
z = xa+ya+del*(0:nx+ny);
figure(1)
plot(x,f,y,g,z,fOg)
grid on
% these two normalization-type integrals should agree,
% even if none of the functions are normalize to 1
trapz(x,f)*trapz(y,g)
trapz(z,fOg)

  0 Comments

Sign in to comment.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!