Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help with plotting simple digital signals in MATLAB

Status
Not open for further replies.

daVani

Newbie level 4
Joined
Jan 11, 2011
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,331
Hi everybody

I'm trying to plot some simple digital signals in MATLAB, for example this one:

x=linspace(0,1,100);
y=-cos(2*pi*(4500000)*x)+sin(2*pi*(4500000)*x);
plot(x,y);

As you can see when plotting this signal it repeats itself and it's kinda edgy, so I want to reduce the time period (0 - 0.2) and make the curve more soft but I can't do it. When I change the time period the signal is still edgy as hell! I think it's easy to do it but I'm new to MATLAB. :oops:

Any help?
 

Try changing the step size in the linspace command.
 
  • Like
Reactions: daVani

    daVani

    Points: 2
    Helpful Answer Positive Rating
Hi and thank you for your reply!

I tried changing to this:

x=linspace(0,0.2,70);
y=-cos(2*pi*(4500000)*x)+sin(2*pi*(4500000)*x);
plot(x,y);

But it's still edgy. Is it possible to get smoother curves?
 

Is it necessary that you need to smooth by changing the time period spacing or use other techniques, like using a moving average system if you can compromise for over sampling!
 
  • Like
Reactions: daVani

    daVani

    Points: 2
    Helpful Answer Positive Rating
The problem is that your signal is undersampled, from both "Nyquist" and "visual" point of view. Your signal has a frequency of 4500000 Hz, that means a period of 222 ns. From Nyquist point of view you need a sampling rate faster than 111 ns to recover the signal. However in order to have a smooth plotting you'll need an higher rate.
In the first code you have set x=linspace(0,1,100) that means an interval of 1 second diveded into 100 parts that is 10 ms, in the last code you have set x=linspace(0,0.2,70) that means a sampling rate of 2.9 ms. both or them are definitely too low. Since they are lower than Nyquist minimum sampling rate you will experience the aliasing phenomenon (frequency will appear lower than real and you could also have AM modulation of the signal).
To correctly set the linspace command you have to define first of all the final time that is the time of the last sample: let's call it Tf; then you have to calculate the period of you signal as Ts=1/freq and then you have to decide how many point per period diplay (Np) with Np>2. Then you will need a total numbe of point N = Np*Tf/T. Thus:

x = linspace(0,Tf,N)

If, for instance, you want to see 10 periods of your 4500000 Hz signal (i.e.: 222 * 10 = 2220 ns) with a sampling rate of 20 point per period (that is a sample every 11.1 ns), you will need:

N = 20*2220ns/222ns = 200 points from which x=linspace(0,2.2e-6,200);

If your signal is the sum of many signals each of them having a different frequency, the calculation of the sampling rate must be done taking into account the higher frequency among those of all the signals.
 
Last edited:
  • Like
Reactions: daVani

    daVani

    Points: 2
    Helpful Answer Positive Rating
Thank you very much for your reply!

I tried plotting the signal with just 5 periods and with a sampling rate of 50 point per period. This means: 222 * 5 = 1110 ns

N = 50*1110ns/222ns = 250 points

Linspace is then: x=linspace(0,2.2e-6,250);

But this plot doesn't give me 5 periods, it gives me 9. Why is this? Maybe I understand this wrong... :oops:

But thank you very much for your reply, your help is very appreciated.

EDIT: Alright, it should be x=linspace(0,1.1e-6,250);. Now I get 5 periods. :D
 
Last edited:

Because you write x=linspace(0,2.2e-6,250); instead of x=linspace(0,1.1e-6,250);
 
  • Like
Reactions: daVani

    daVani

    Points: 2
    Helpful Answer Positive Rating
i am not sure this example could helps, but it gives you better idea. Happy sharing.
 

Attachments

  • Real Time Signal Processing Using DSP.pdf
    430 KB · Views: 129
  • Like
Reactions: daVani

    daVani

    Points: 2
    Helpful Answer Positive Rating
Hi again. I've continued playing around a little with MATLAB and have another question.

See this signal:
sxg9oj.jpg


There are four different signals in this plot and you can clearly see the transition, it just jumps from one signal to another. How can I make them connect so that where signal 1 stops, signal 2 starts? I bet it pretty easy to do, but as I said earlier I just started learning MATLAB. :D

The code used for the plot is here:

Code:
>> x=linspace(0,2*pi,200);
>> f = 0.4; 
>> y=3*cos(2*pi*f*x);
>> plot(x,y);
>> x1=linspace(2*pi,4*pi,200);
>> hold on;
>> y1=cos(2*pi*f*x1);
>> plot(x1,y1);
>> x2=linspace(4*pi,6*pi,200);
>> y2=(-1)*cos(2*pi*f*x2);
>> plot(x2,y2);
>> x3=linspace(6*pi,8*pi,200);
>> y3=(-3)*cos(2*pi*f*x3);
>> plot(x3,y3);

Help is appreciated, thanks!
 

As far as I understood your question you can simply group all the signals in a single vector. The command is:

ytot = [y y1 y2 y3];

Please check the synopsis in Matlab, since I'm actually using Scilab (however I think shouldn't be different)
 
  • Like
Reactions: daVani

    daVani

    Points: 2
    Helpful Answer Positive Rating
I can't make it work, MATLAB just gives me an error message:

"Error using ==> plot
Vectors must be the same lengths."

The same signal was made in Excel and it looks like this:
dy6k5x.jpg


The transition here is much better, except maybe after the 3rd signal, but still it much better than the one created in MATLAB. How can I make it like that in MATLAB?
 

I think you didn't define the new x axis as:

xtot = linspace(0,8*pi,800);

this is why you see the error message that means x and y in the plot command have different size.

I think in excel you defined a different function where the transitions are placed at different abscissa with respect to that defined in matlab.
 
  • Like
Reactions: daVani

    daVani

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top