Digital Signal on plot

  • Follow


How do I draw a digital signal on the plot graph? let says I have 10 points incrementing by 1. How do I make the plot graph draw a line from x-axis = 0-1, 1-2, 2-3, 3-4, 4-5, 5-6, 7-8, 8-9, 9-10, etc. The line can be at y-axis = 1, 0, or -1 depend on what I want at those points? Lines also have to be connected as well so it's like a digital signal. I'm looking for a for loop that can do this or something better.
0
Reply Dave 3/7/2010 4:23:06 PM

Dave Smith wrote:
> How do I draw a digital signal on the plot graph? let says I have 10 
> points incrementing by 1. How do I make the plot graph draw a line from 
> x-axis = 0-1, 1-2, 2-3, 3-4, 4-5, 5-6, 7-8, 8-9, 9-10, etc. The line can 
> be at y-axis = 1, 0, or -1 depend on what I want at those points? Lines 
> also have to be connected as well so it's like a digital signal. I'm 
> looking for a for loop that can do this or something better.

just make a data stream that matches the desired waveform...

x=[0:10];
y=rand(size(x));
y(y>.6)=1; y(y<.3)=-1,y(y>.3 && y<.6)=0;
plot(x,y)

--
0
Reply dpb 3/7/2010 4:25:32 PM


dpb <none@non.net> wrote in message <hn0k4h$9c1$1@news.eternal-september.org>...
> Dave Smith wrote:
> > How do I draw a digital signal on the plot graph? let says I have 10 
> > points incrementing by 1. How do I make the plot graph draw a line from 
> > x-axis = 0-1, 1-2, 2-3, 3-4, 4-5, 5-6, 7-8, 8-9, 9-10, etc. The line can 
> > be at y-axis = 1, 0, or -1 depend on what I want at those points? Lines 
> > also have to be connected as well so it's like a digital signal. I'm 
> > looking for a for loop that can do this or something better.
> 
> just make a data stream that matches the desired waveform...
> 
> x=[0:10];
> y=rand(size(x));
> y(y>.6)=1; y(y<.3)=-1,y(y>.3 && y<.6)=0;
> plot(x,y)
> 
> --

Thank you. This is very close to what i wanted but can you explain what is rand(size(x)) do? I ran this on matlab and I'm getting a diagonal line going from says 1-2. but I only want horizontal line so like 0-1 is a horizontal line and a vertical line going down from 2 and then another horizontal line from 2-3. I hope you understand what I'm trying to said.
0
Reply Dave 3/7/2010 4:47:05 PM

Dave Smith wrote:
> dpb <none@non.net> wrote in message 
> <hn0k4h$9c1$1@news.eternal-september.org>...
....

> Thank you. This is very close to what i wanted but can you explain what 
> is rand(size(x)) do? 

Just generated a set of random numbers to start w/ of the same size as x...

....

> a horizontal line and a vertical line going down from 2 and then another 
> horizontal line from 2-3. I hope you understand what I'm trying to said.

Yeah, you'll have to duplicate the x-coordinates at the break points 
which the above doesn't do (didn't think thru the issue thoroughly enuf 
before, sorry).

The fundamental idea is the same, just that you'll have to have the x 
and y vectors contain the entries for both the (replicating) abcissa and 
ordinates to draw a true square wave.  What I showed would be a 
triangle, indeed.

Of course, a true square wave is an idealization anyway, no matter how 
good the rise/fall time of a wave generator... :)

If you perchance have the Signal Processing toolbox, it has a square() 
function but it is "realizable" in the above sense as it doesn't 
generate values at duplicated time points, either.

--
0
Reply dpb 3/7/2010 7:27:24 PM

dpb <none@non.net> wrote in message <hn0uph$jl1$1@news.eternal-september.org>...
> Dave Smith wrote:
> > dpb <none@non.net> wrote in message 
> > <hn0k4h$9c1$1@news.eternal-september.org>...
> ...
> 
> > Thank you. This is very close to what i wanted but can you explain what 
> > is rand(size(x)) do? 
> 
> Just generated a set of random numbers to start w/ of the same size as x...
> 
> ...
> 
> > a horizontal line and a vertical line going down from 2 and then another 
> > horizontal line from 2-3. I hope you understand what I'm trying to said.
> 
> Yeah, you'll have to duplicate the x-coordinates at the break points 
> which the above doesn't do (didn't think thru the issue thoroughly enuf 
> before, sorry).
> 
> The fundamental idea is the same, just that you'll have to have the x 
> and y vectors contain the entries for both the (replicating) abcissa and 
> ordinates to draw a true square wave.  What I showed would be a 
> triangle, indeed.
> 
> Of course, a true square wave is an idealization anyway, no matter how 
> good the rise/fall time of a wave generator... :)
> 
> If you perchance have the Signal Processing toolbox, it has a square() 
> function but it is "realizable" in the above sense as it doesn't 
> generate values at duplicated time points, either.
> 
> --

duplicate x-coordinates? can you help me out, I been struggling with this all day, so frustrating
0
Reply Dave 3/8/2010 1:05:24 AM

Dave Smith:
This is very easy.  Just use the line command.  You give it the x1,y1
and the x2,y2 (end points of the line) and it does it.  You just need
to figure out what they are (and it sounds like you already know that,
you just need to know how to plot it.)

Like this

X=rand(1,20);
plot(X);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
hold on;
line([2 7], [0.5 0.5], 'LineWidth', 3, 'Color', [0 .4 0]);
line([7 7], [0.5 0.3], 'LineWidth', 3, 'Color', [.8 0 0.1]);
line([7 17], [0.3 0.3], 'LineWidth', 3, 'Color', [0 .4 0]);
0
Reply ImageAnalyst 3/8/2010 2:10:52 AM

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <bc83d7f7-f279-4c88-bb19-fc8b9edf11b6@g7g2000yqe.googlegroups.com>...
> Dave Smith:
> This is very easy.  Just use the line command.  You give it the x1,y1
> and the x2,y2 (end points of the line) and it does it.  You just need
> to figure out what they are (and it sounds like you already know that,
> you just need to know how to plot it.)
> 
> Like this
> 
> X=rand(1,20);
> plot(X);
> set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
> hold on;
> line([2 7], [0.5 0.5], 'LineWidth', 3, 'Color', [0 .4 0]);
> line([7 7], [0.5 0.3], 'LineWidth', 3, 'Color', [.8 0 0.1]);
> line([7 17], [0.3 0.3], 'LineWidth', 3, 'Color', [0 .4 0]);

I don't really understand this. Look like you're just drawing line on top on the plot where as I'm trying to draw digital signal with 0 and 1 from any binary input. Could it be I don't know how to apply this to my problem.
0
Reply Dave 3/8/2010 3:26:06 AM

Well I showed you how to draw a line because you said "How do I make
the plot graph draw a line..." and then you gave a series of line
segments.  If you want to do it all in one shot with the plot command,
then you're going to have to double up on some x values - insert an
coordinate at the same x value but a different y value.  Plot can plot
a polyline but you have to give it all the coordinates (vertices) of
the polyline you want to plot.
0
Reply ImageAnalyst 3/8/2010 4:08:19 AM

Dave Smith wrote:
> dpb <none@non.net> wrote in message 
> <hn0uph$jl1$1@news.eternal-september.org>...
>> Dave Smith wrote:
>> > dpb <none@non.net> wrote in message > 
>> <hn0k4h$9c1$1@news.eternal-september.org>...
>> ...
>>
>> > Thank you. This is very close to what i wanted but can you explain 
>> what > is rand(size(x)) do?
>> Just generated a set of random numbers to start w/ of the same size as 
>> x...
>>
>> ...
>>
>> > a horizontal line and a vertical line going down from 2 and then 
>> another > horizontal line from 2-3. I hope you understand what I'm 
>> trying to said.
>>
>> Yeah, you'll have to duplicate the x-coordinates at the break points 
>> which the above doesn't do (didn't think thru the issue thoroughly 
>> enuf before, sorry).
>>
>> The fundamental idea is the same, just that you'll have to have the x 
>> and y vectors contain the entries for both the (replicating) abcissa 
>> and ordinates to draw a true square wave.  What I showed would be a 
>> triangle, indeed.
>>
>> Of course, a true square wave is an idealization anyway, no matter how 
>> good the rise/fall time of a wave generator... :)
>>
>> If you perchance have the Signal Processing toolbox, it has a square() 
>> function but it is "realizable" in the above sense as it doesn't 
>> generate values at duplicated time points, either.
>>
>> -- 
> 
> duplicate x-coordinates? can you help me out, I been struggling with 
> this all day, so frustrating

Well, you have to decide the on/off positions but you need something like

x = [  0  1  2 2 3 4 5  5  6  7  8 8 9 10 ];
y = [ -1 -1 -1 1 1 1 1 -1 -1 -1 -1 1 1  1 ];
plot(x,y)
axis([0 10 -2 2])  % fix y-axis so can see clearly now...

The trick is to determine programmatically what are the breakpoints 
where you need the duplicated x values to create the vertical 
transition. (Assuming that is what you really want irrespective of the 
realizability issues raised earlier)

--
0
Reply dpb 3/8/2010 4:08:47 AM

Dave Smith wrote:
> How do I draw a digital signal on the plot graph? let says I have 10 
> points incrementing by 1. How do I make the plot graph draw a line from 
> x-axis = 0-1, 1-2, 2-3, 3-4, 4-5, 5-6, 7-8, 8-9, 9-10, etc. The line can 
> be at y-axis = 1, 0, or -1 depend on what I want at those points? Lines 
> also have to be connected as well so it's like a digital signal. I'm 
> looking for a for loop that can do this or something better.

Presuming that you want the signal to start at 0 and to return to 0, then:

newx = reshape(repmat(1:length(y)+1,2,1),1,[]);
newy = [0 reshape(repmat(y(:),1,2).',1,[]) 0];
0
Reply Walter 3/8/2010 4:11:21 AM

dpb <none@non.net> wrote in message <hn1tb3$pos$1@news.eternal-september.org>...
> Dave Smith wrote:
> > dpb <none@non.net> wrote in message 
> > <hn0uph$jl1$1@news.eternal-september.org>...
> >> Dave Smith wrote:
> >> > dpb <none@non.net> wrote in message > 
> >> <hn0k4h$9c1$1@news.eternal-september.org>...
> >> ...
> >>
> >> > Thank you. This is very close to what i wanted but can you explain 
> >> what > is rand(size(x)) do?
> >> Just generated a set of random numbers to start w/ of the same size as 
> >> x...
> >>
> >> ...
> >>
> >> > a horizontal line and a vertical line going down from 2 and then 
> >> another > horizontal line from 2-3. I hope you understand what I'm 
> >> trying to said.
> >>
> >> Yeah, you'll have to duplicate the x-coordinates at the break points 
> >> which the above doesn't do (didn't think thru the issue thoroughly 
> >> enuf before, sorry).
> >>
> >> The fundamental idea is the same, just that you'll have to have the x 
> >> and y vectors contain the entries for both the (replicating) abcissa 
> >> and ordinates to draw a true square wave.  What I showed would be a 
> >> triangle, indeed.
> >>
> >> Of course, a true square wave is an idealization anyway, no matter how 
> >> good the rise/fall time of a wave generator... :)
> >>
> >> If you perchance have the Signal Processing toolbox, it has a square() 
> >> function but it is "realizable" in the above sense as it doesn't 
> >> generate values at duplicated time points, either.
> >>
> >> -- 
> > 
> > duplicate x-coordinates? can you help me out, I been struggling with 
> > this all day, so frustrating
> 
> Well, you have to decide the on/off positions but you need something like
> 
> x = [  0  1  2 2 3 4 5  5  6  7  8 8 9 10 ];
> y = [ -1 -1 -1 1 1 1 1 -1 -1 -1 -1 1 1  1 ];
> plot(x,y)
> axis([0 10 -2 2])  % fix y-axis so can see clearly now...
> 
> The trick is to determine programmatically what are the breakpoints 
> where you need the duplicated x values to create the vertical 
> transition. (Assuming that is what you really want irrespective of the 
> realizability issues raised earlier)
> 
> --

Ok, I have test the duplicating x values and yes this is what I wanted. But now the trick like you said is to determine the breakpoints. Here is what I have so far:

XMIN=1;
XMAX=12;
YMIN=-2;
YMAX=2;

B=[0 1 0 0 1 1 0 0 0 1 1];
[row,column]=size(B);

for c=1:column;
    if B(c)==1;
    B(c)=-1;
    end 
    if B(c)==0;
    B(c)=1;
    end
end
B(12)=B(11);
X=(1:11);

subplot(3,1,1);
plot(X,B);
axis([XMIN XMAX YMIN YMAX]);

I wanted the program to read the B matrix and if it see a 1, it will goes -1 and if it see a 0, it will goes 1. I had determine that everytime it switches between 0 and 1, I need to deplicate that x values and add that many to the matrix. For example, right now it's 12 and it changes from 1 to 0 or 0 to 1 5 times so I need to add 5 + 12 = 17 so in the end. it's a 17 columns matrix. I thought about it but I need some suggestion on what to do.
0
Reply Dave 3/8/2010 6:46:04 AM

Dave Smith wrote:
....

> Ok, I have test the duplicating x values and yes this is what I wanted. 
> But now the trick like you said is to determine the breakpoints. Here is 
> what I have so far:
> 
....

> B=[0 1 0 0 1 1 0 0 0 1 1];
....

> I wanted the program to read the B matrix and if it see a 1, it will 
> goes -1 and if it see a 0, it will goes 1. 

Well, that part's simple enough...

B(B==0)=-1;
B(B==1)=1;

....

> everytime it switches between 0 and 1, I need to deplicate that x values 
> and add that many to the matrix. For example, right now it's 12 and it 
> changes from 1 to 0 or 0 to 1 5 times so I need to add 5 + 12 = 17 so in 
> the end. it's a 17 columns matrix. I thought about it but I need some 
> suggestion on what to do.

look at

doc diff

and see if that doesn't give you any ideas on how to determine where to 
insert the added points...

--

0
Reply dpb 3/8/2010 2:31:20 PM

Dave Smith wrote:

> Ok, I have test the duplicating x values and yes this is what I wanted. 
> But now the trick like you said is to determine the breakpoints. Here is 
> what I have so far:

Was there a problem with the two-line solution I suggested?
0
Reply Walter 3/8/2010 3:29:13 PM

Walter Roberson <roberson@hushmail.com> wrote in message <hn3549$pig$1@canopus.cc.umanitoba.ca>...
> Dave Smith wrote:
> 
> > Ok, I have test the duplicating x values and yes this is what I wanted. 
> > But now the trick like you said is to determine the breakpoints. Here is 
> > what I have so far:
> 
> Was there a problem with the two-line solution I suggested?

No, there were no problem actually. I just never got a chance to try your suggestion as I was busy spending hours on trying to duplicated the x-axis going by what dpb suggested. But your suggestion solved the problem the moment I tried it so thank you very much. Can you explain to me in as much details as possible on how those two lines worked? I looked up reshape and repmat in help file but I still don't fully understand it. This is just one type of scheme graphs I have to do, I still got 6 other more to do and I don't know if these two lines will work for them all so that is why I would like to understand the code.
0
Reply Dave 3/8/2010 3:46:22 PM

Dave Smith wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message 
> <hn3549$pig$1@canopus.cc.umanitoba.ca>...

>> Was there a problem with the two-line solution I suggested?

> No, there were no problem actually. I just never got a chance to try 
> your suggestion as I was busy spending hours on trying to duplicated the 
> x-axis going by what dpb suggested. But your suggestion solved the 
> problem the moment I tried it so thank you very much. Can you explain to 
> me in as much details as possible on how those two lines worked? I 
> looked up reshape and repmat in help file but I still don't fully 
> understand it. This is just one type of scheme graphs I have to do, I 
> still got 6 other more to do and I don't know if these two lines will 
> work for them all so that is why I would like to understand the code.

Consider the x coordinates. If you start from a baseline (0 in the case 
of the code) then you need the initial x value (1) for that, and then 
you need the same x value to draw the up (or down) stroke to reach the 
first y value. Then you will need the 2nd x value to draw the line 
across to the second value, and the 2nd x again to draw the up or down 
stroke to the second y value. Keep going in this manner, with 2 copies 
of each x coordinate. When you reach the last y value, you have an 
implicit draw across _past_ that y value to where the next y value would 
start, and you have an implicit return to baseline, so you will need two 
copies of the x coordinate which is length(y)+1 . Thus, what we do is 
construct (1:length(y)+1), then we use repmat(,2,1) two take an 
identical copy of that row. Matlab stores values down columns, so _in 
memory_ the adjacent values at this point will be two copies of each of 
the x coordinates. We could use a temporary variable to hold that and 
then use (:) indexing to "unravel" that down the columns to x1 x1 x2 x2 
etc., or we can do like I did and call reshape explicitly to do the 
unravelling. reshape(,1,[]) means to take the data that is in memory and 
change its dimensions to 1 by "however much you need", so after the 
reshape, we wil be left with x1 x1 x2 x2 .... xn xn xn+1 xn+1 .

Now consider the y coordinates. Because we are starting from the 
baseline, the adjusted y coordinates will need to start with a 0; 
likewise because we return to the baseline at the end, they will need to 
end with 0. That accounts for the [0 <something> 0] portion of the code. 
Now, we need to draw the upstroke or downstroke to reach the first y 
value, so the first y value comes next. We need to hold that y value 
over to the next x value, so a duplicate of the first y value is needed. 
After that, we need to draw up or down to the second y value, and then 
need to hold that value to the next x coordinate, and so on. Thus we are 
once more in the situation of needing to duplicate a vector y1 y2 y3 to 
become y1 y1 y2 y2 y3 and so on, which uses the same duplication code as 
we used for the x. But in the x case, we know that 1:length(y)+1 will 
generate a row vector, but for the purposes of this two-liner, we cannot 
assume that y is already a row vector. To force y to be a row vector, we 
use y(:) which forces y to be a _column_ vector; once we have the column 
vector, we repmat(,1,2) it to make a duplicate column, and then we use 
..' to transpose it into two identical row vectors; the reshape(,1,[]) 
then converts it into a single long row vector. Then the [0 ... 0] 
wrapper for the returns to baseline.

The length of the newy vector thus becomes 1 + 2*length(y) + 1, which is 
2*length(y) + 2, which is 2*(length(y)+1) which is the same as for the 
newx vector, so we have all the coordinates matched up.


This isn't the _optimal_ drawing for the line, but that probably doesn't 
matter. If it does matter, then because the structure of it is so clear, 
it should be fairly straight-forward to filter out redundant points; I 
would suggest first removing the redundant up/down strokes (which will 
show up as identical pairs of x and y coordinates adjacent to each 
other), and then to remove the redundant x coordinates (which would show 
up as (x1 y1) (x2 y1) (x3 y1), which is compressible to (x1 y1) (x3 y1))
0
Reply Walter 3/8/2010 4:37:43 PM

Walter Roberson <roberson@hushmail.com> wrote in message <hn394o$2j0$1@canopus.cc.umanitoba.ca>... 
> Consider the x coordinates. If you start from a baseline (0 in the case 
> of the code) then you need the initial x value (1) for that, and then 
> you need the same x value to draw the up (or down) stroke to reach the 
> first y value. Then you will need the 2nd x value to draw the line 
> across to the second value, and the 2nd x again to draw the up or down 
> stroke to the second y value. Keep going in this manner, with 2 copies 
> of each x coordinate. When you reach the last y value, you have an 
> implicit draw across _past_ that y value to where the next y value would 
> start, and you have an implicit return to baseline, so you will need two 
> copies of the x coordinate which is length(y)+1 . Thus, what we do is 
> construct (1:length(y)+1), then we use repmat(,2,1) two take an 
> identical copy of that row. Matlab stores values down columns, so _in 
> memory_ the adjacent values at this point will be two copies of each of 
> the x coordinates. We could use a temporary variable to hold that and 
> then use (:) indexing to "unravel" that down the columns to x1 x1 x2 x2 
> etc., or we can do like I did and call reshape explicitly to do the 
> unravelling. reshape(,1,[]) means to take the data that is in memory and 
> change its dimensions to 1 by "however much you need", so after the 
> reshape, we wil be left with x1 x1 x2 x2 .... xn xn xn+1 xn+1 .
> 
> Now consider the y coordinates. Because we are starting from the 
> baseline, the adjusted y coordinates will need to start with a 0; 
> likewise because we return to the baseline at the end, they will need to 
> end with 0. That accounts for the [0 <something> 0] portion of the code. 
> Now, we need to draw the upstroke or downstroke to reach the first y 
> value, so the first y value comes next. We need to hold that y value 
> over to the next x value, so a duplicate of the first y value is needed. 
> After that, we need to draw up or down to the second y value, and then 
> need to hold that value to the next x coordinate, and so on. Thus we are 
> once more in the situation of needing to duplicate a vector y1 y2 y3 to 
> become y1 y1 y2 y2 y3 and so on, which uses the same duplication code as 
> we used for the x. But in the x case, we know that 1:length(y)+1 will 
> generate a row vector, but for the purposes of this two-liner, we cannot 
> assume that y is already a row vector. To force y to be a row vector, we 
> use y(:) which forces y to be a _column_ vector; once we have the column 
> vector, we repmat(,1,2) it to make a duplicate column, and then we use 
> .' to transpose it into two identical row vectors; the reshape(,1,[]) 
> then converts it into a single long row vector. Then the [0 ... 0] 
> wrapper for the returns to baseline.
> 
> The length of the newy vector thus becomes 1 + 2*length(y) + 1, which is 
> 2*length(y) + 2, which is 2*(length(y)+1) which is the same as for the 
> newx vector, so we have all the coordinates matched up.
> 
> 
> This isn't the _optimal_ drawing for the line, but that probably doesn't 
> matter. If it does matter, then because the structure of it is so clear, 
> it should be fairly straight-forward to filter out redundant points; I 
> would suggest first removing the redundant up/down strokes (which will 
> show up as identical pairs of x and y coordinates adjacent to each 
> other), and then to remove the redundant x coordinates (which would show 
> up as (x1 y1) (x2 y1) (x3 y1), which is compressible to (x1 y1) (x3 y1))

Thank you. I'm slowly understanding it the more I read it.
0
Reply Dave 3/8/2010 5:09:24 PM

Why all this complication???? There is a very simple trick to plot such a digital waveform in Matlab:

%Generate random binary data
N = 100;                            %number of binary values
a = 2*(randi(2,N,1)-1)-1;   %polar sequence of -1/+1

%Plot preparation
c = (a*[1 1]).';                   %repeat each data bit

%Construct time vector 
t = zeros(2,N);
epsilon = 0.0001;               %2*epsilon = "duration" of transition
t(1,:) = (1:N)+epsilon;
t(2,:) = t(1,:)-(2*epsilon-1);

%plot
plot(t(:), c(:), '-');
ylim([-2 2]);

Hope this helps.
Park Kyung Ho
0
Reply Kyung 9/2/2010 8:04:22 PM

"Kyung Ho Park" <adfafsadasdff@hotmail.com> wrote in message <i5p006$72e$1@fred.mathworks.com>...
> Why all this complication???? There is a very simple trick to plot such a digital waveform in Matlab:
> 
> %Generate random binary data
> N = 100;                            %number of binary values
> a = 2*(randi(2,N,1)-1)-1;   %polar sequence of -1/+1
> 
> %Plot preparation
> c = (a*[1 1]).';                   %repeat each data bit
> 
> %Construct time vector 
> t = zeros(2,N);
> epsilon = 0.0001;               %2*epsilon = "duration" of transition
> t(1,:) = (1:N)+epsilon;
> t(2,:) = t(1,:)-(2*epsilon-1);
> 
> %plot
> plot(t(:), c(:), '-');
> ylim([-2 2]);
> 
> Hope this helps.
> Park Kyung Ho

I'm not sure why this thread was revived, but since it was... None of this stuff is necessary at all. Use stairs().

"Stairstep plots are useful for drawing time history plots of zero-order-hold digital sampled-data systems."
0
Reply Alan 9/2/2010 9:24:04 PM

17 Replies
257 Views

(page loaded in 0.154 seconds)

Similiar Articles:


















7/28/2012 9:18:13 PM


Reply: