how to generate periodic 0 1 signal?
Show older comments
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
on 29 Apr 2012
0 votes
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
Hank
on 29 Apr 2012
Walter Roberson
on 29 Apr 2012
Yes, that looks okay.
You can simplify this by replacing from "n=" downwards with
str = y > 0;
plot(t, str);
Hank
on 29 Apr 2012
Categories
Find more on Labels and Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!