I have a problem about calculating with Delta Function. I am trying write matlab code for these function.
I wrote the following code for this function.
n = -5:1:7;
x = delta(n+1) - delta(n) + unit(n+1) - unit(n-2);
stem(n,x,'fill');
axis([-6 8 -1.5 1.5])
xlabel('n')
ylabel('x[n]')
grid
But I am getting the following error.
I don't now how can ı write the delta function in this way ,
function y = unit(x);
y = zeros(size(x));
y(x>0) = 1;
end
Can you help me write the delta function as the 'unit' function you see above? Thank you for your helping.

 採用された回答

Star Strider
Star Strider 2021 年 3 月 24 日

2 投票

If you have the Symbolic Math Toolbox, use the dirac function. It can be used with non-symbolic arguments as well.

4 件のコメント

Walter Roberson
Walter Roberson 2021 年 3 月 24 日
dirac() is also available for double() and single() without symbolic toolbox.
Kutlu Yigitturk
Kutlu Yigitturk 2021 年 3 月 24 日
When I write the code like this,
n = -5:1:7;
x = dirac(n+1) - dirac(n) + unit(n+1) - unit(n-2);
subplot(2,2,3);stem(n,x,'fill');
axis([-6 8 -1.5 1.5])
xlabel('n')
ylabel('x[n]')
grid
I get a graph like this,
But my main goal is to get this graph, what can I do about it?
Walter Roberson
Walter Roberson 2021 年 3 月 24 日
That is not a true Dirac δ function.
delta = @(x) x==0;
unit = @(x) x>=0;
n = -5:1:7;
x = delta(n+1) - delta(n) + unit(n+1) - unit(n-2);
stem(n,x,'fill');
axis([-6 8 -1.5 2])
xlabel('n')
ylabel('x[n]')
grid
Kutlu Yigitturk
Kutlu Yigitturk 2021 年 3 月 24 日
Thank you for your help Mr. Roberson. I got the result I wanted.

サインインしてコメントする。

その他の回答 (1 件)

Carlos M. Velez S.
Carlos M. Velez S. 2025 年 7 月 24 日

0 投票

The examples above refer to the Kronecker delta function for discrete signals. If you want to apply the Dirac delta function in simulation to continuous-time systems, the following code is enough:
function y = delta_dirac(u)
[n,m] = size(u);
if max(n,m) ==1
dt = 1e-6; % Define a small time increment for the delta function
else
dt = u(2) - u(1);
end
y = zeros(n,m);
for i=1:max(m,n)
if u(i) == 0
y(i) = 1/dt;
else
y(i) = 0;
end
end

製品

Community Treasure Hunt

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

Start Hunting!

Translated by