plot binary vs time

2 次查看(过去 30 天)
Aswas
Aswas 2018-8-3
回答: Guillaume 2018-8-3
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
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.
Aswas
Aswas 2018-8-3
编辑:Guillaume 2018-8-3
It is for PSK demodulation, and yes, -1 and + 1....
% Binary PSK demodulation
mn=[];
for n=ss:ss:length(m)
t=bp/99:bp/99:bp;
y=A*(sin(2*pi*f*t2+pi))%+(sin(2*pi*f*t+pi));
%y=sin(2*pi*f*t)for 0 deg shift; % carrier signal
mm=y.*m((n-(ss-1)):n);% mm=y.*m((n-(ss-1)):n)
t4=bp/99:bp/99:bp;
z=trapz(t4,mm) % intregation method
zz=round((2*z/bp))
if(zz>0)
a=1;
else
a=0;
end
mn=[mn a];
end
disp(' Binary information at Reciver :');
disp(mn);

请先登录,再进行评论。

回答(1 个)

Guillaume
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);

Community Treasure Hunt

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

Start Hunting!

Translated by