how to generate periodic 0 1 signal?

I wrote a code,but it has something wrong. what should be modified? Output value should be binary.
t=0:0.01:10;
t=t(1:1000);
y=square(pi*t);
y=y(1:1000);
n=length(y);
for k=1:n
if y>0
str=1
else
str=0
end
end
out = dec2bin(str)

Answers (2)

Walter Roberson
Walter Roberson on 29 Apr 2012
In your loop, you should be looking at y(k) rather than y as a whole.
You will need to save the value of "str" in the loop.
Efficiency hint: if X is numeric 0 or numeric 1, then faster than dec2bin(X) is char(X + '0')

2 Comments

Thanks. How to save the value of "str"?
t=0:0.01:10;
t=t(1:1000);
y=square(pi*t);
y=y(1:1000);
n=length(y);
for k=1:n
if y(k)>0
str(k)=1
else
str(k)=0
end
end
figure;
plot(t,str);
Yes, that looks okay.
You can simplify this by replacing from "n=" downwards with
str = y > 0;
plot(t, str);

Sign in to comment.

Is this correct?
t=0:0.01:10;
t=t(1:1000);
y=square(pi*t);
y=y(1:1000);
n=length(y);
for k=1:n
if y(k)>0
str(k,:)=1
else
str(k,:)=0
end
end
figure;
plot(t,str);

Categories

Asked:

on 29 Apr 2012

Community Treasure Hunt

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

Start Hunting!