generate a delta function without using any preexisting function related to it in matlab

The implementation of the Dirac Delta Function is a bit tricky, as mathematically speaking it's not an ordinary function. One popular way to define the Delta Function is to use a limit of a Gaussian function, as follows:

main.m
% Define a Gaussian Function:
x = linspace(-10,10,1000);
gauss = exp(-x.^2);

% Normalize the Gaussian Function:
gauss = gauss./sum(gauss);

% Define Delta Function as the limit of the Normalized Gaussian Function:
delta = zeros(size(x));
delta(500) = 1/(x(2)-x(1)); % delta x = (x[2] - x[1])
293 chars
11 lines

The above code generates a normalized Gaussian function, then assigns a limit to it, making it tend to infinity while its area remains 1 (so as to keep the definition of the Delta Function).

You can plot both functions to see that the Delta Function looks much like a "spike" at the point where x=0:

main.m
% Plot both functions:
figure;
plot(x,gauss)
hold on
plot(x,delta)
67 chars
6 lines

Note: The length of the signal should be big enough to be able to resolve the effect of delta function. And there are more methods to generate delta function like using derivative of step function or Fourier transform etc.

related categories

gistlibby LogSnag