Reputation: 23
In the code that follows I am using a step function which uses MATLAB's sign function to create a square pulse train for the variable x. How can this be implemented with a for loop to make things more tidy?
del_t=.005
t=-10:del_t:10; % Time span
L=length(t);
x=stp_fn(t)-stp_fn(t-1)+stp_fn(t-2)-stp_fn(t-3)... %input x(t) using step function
+stp_fn(t-4)-stp_fn(t-5)+stp_fn(t-6)-stp_fn(t-7)... %
+stp_fn(t-8)-stp_fn(t-9)+stp_fn(t-10); %
%step function
function u =stp_fn(t)
u=0.5*(sign(t+eps)+1);
Upvotes: 2
Views: 158
Reputation: 1981
Looks a bit unusual, but hey...
x = 0;
for i = 0 : 10
x = x + (-1)^i*stp_fn(t-i); % ^i for alternating signs...
end
Upvotes: 2
Reputation: 28698
x = stp_fn(t);
for i = 1 : 10
if mod(i, 2)
x = x - stp_fn(t - i);
else
x = x + stp_fn(t - i);
end
end
Upvotes: 1