Need helps with my unit step function
4 ビュー (過去 30 日間)
古いコメントを表示
So I wanted to create a unit step function that contain a time vector that specifies the finite range of the signal and a time shift value, which is equivalent to u(t + ts). But every time I tried to run the function, it did not work as I expected. The reason I believe it did not work was because of time vector in a function. I do not know how to make a time vector that can set a finite range by inputing any integer. I would be much thankful if anyone can help me. Here is my code I have problem with.
function [y] = unitstep(t, ts)
for i = -t:t
y = zeros(size(i));
if i >= ts
y = 1;
elseif i < ts
y = 0;
end
end
0 件のコメント
回答 (1 件)
Rik
2018 年 10 月 11 日
編集済み: Rik
2018 年 10 月 11 日
Your current code overwrites your output on every iteration. It looks like this is what you want:
function [y] = unitstep(t, ts)
t=-t:t;%only works if t is a scalar
y = zeros(size(t));
for ind = 1:numel(t)
if ind >= ts
y(ind) = 1;
else
y(ind) = 0;
end
end
Which can be simplified to this:
function [y] = unitstep(t, ts)
t=-t:t;%only works if t is a scalar
y=zeros(size(t));
y(y>=ts)=1;
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!