plot binary vs time
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi, I have 1's and 0's but want 1's to be plotted as a positive number and o's as negative number:
Code:
if(z>0)
a=1;
else
a=0;
end
mn=[t1 a];
댓글 수: 2
Guillaume
2018년 8월 3일
"want 1's to be plotted as a positive number"
Which number? +1?
"and o's as negative number"
Which number? -1?
The code snippet you show makes no sense at all and is not commented so we have no idea what its intent is.
답변 (1개)
Guillaume
2018년 8월 3일
If you want +1 and -1, why don't you store that in the first place in your vector instead of 1/0?
if zz > 0
a = 1;
else
a = -1;
end
Why go use 0 if you want -1?. If you really do want to do it afterwards for some reason:
%after the loop
mn(mn == 0) = -1;
By the way, there's a lot of variable that get recreated each step of the loop but never change ( t, y, t4). These should be calculated once before the loop. You're just wasting time recalculating the same thing over and over.
Also, the way you construct you mn is very awkward and slow as it gets resized a every step of the loop. Preallocation and indexing would be faster:
%stuff that doesn't need to be calculated in the loop
t=bp/99:bp/99:bp;
y=A*(sin(2*pi*f*t2+pi))%+(sin(2*pi*f*t+pi));
t4=bp/99:bp/99:bp;
%loop preparation,
v = ss:ss:length(m);
mn = zeros(1, numel(v); %preallocation
%loop
for idx = 1:numel(v)
n = v(idx);
mm=y.*m((n-(ss-1)):n);% mm=y.*m((n-(ss-1)):n);
z=trapz(t4,mm); % intregation method
zz=round((2*z/bp));
if zz > 0
mn(idx) = 1;
else
mn(idx) = -1; %or 0 if you really must
end
end
plot(mn);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 PSK에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!