Reply by Ion Suberviola October 28, 20082008-10-28
Hi!

The thing is that you have aliasing! Tray this:

t=0:1/(4*100e3):1e-3; %2 seconds of timeline
x=sin(2*pi*50e3*t);
plot(t,x);

Regards!

2008/10/27

> Hi everyone,
> >
> >I'm new in matlab and I'm struggling quite a lot. I don't get very well
> how to define de time vectors so that the sine wave I want to plot displays
> correctly.
> >
> >As an example, if I put this code to represent a 50 Khz sine:
> >
> >t=0:1/100e3:2 %2 seconds of timeline
> >x=sin(2*pi*50e3*t);
> >plot(t,x);
> >
> >The result is quite awful. Like noise growing until it gets the end of the
> timeline. However, if I put a sampling frequency of fs=(2fc)+1: 100001 Hz,
> the result is kind of strange as well but not so awful as the previous one,
> it seems like a sine but as a sin(x)sin(kx).
> >
> >What's wrong?
> >
> >Thanks!
> >
> >
> >
> > hehe,the reply above is right,but it is none of business of the original
> question.although at 0 or pi, the plot is zero, why it didnot plot zero, why
> it is so awful?
>
> in my opinion, the awful plot is due to float computation error. And u can
> avoid zero by 2 ways.
>
> 1. inrease sampling rate
>
> 2. change the time vector head point, not in the 0 or k*pi exactly.
>
> Thanks.
>
Reply by hyee...@yahoo.com.cn October 27, 20082008-10-27
Hi everyone,
>
>I'm new in matlab and I'm struggling quite a lot. I don't get very well how to define de time vectors so that the sine wave I want to plot displays correctly.
>
>As an example, if I put this code to represent a 50 Khz sine:
>
>t=0:1/100e3:2 %2 seconds of timeline
>x=sin(2*pi*50e3*t);
>plot(t,x);
>
>The result is quite awful. Like noise growing until it gets the end of the timeline. However, if I put a sampling frequency of fs=(2·fc)+1: 100001 Hz, the result is kind of strange as well but not so awful as the previous one, it seems like a sine but as a sin(x)·sin(kx).
>
>What's wrong?
>
>Thanks!
>
>

hehe,the reply above is right,but it is none of business of the original question.although at 0 or pi, the plot is zero, why it didnot plot zero, why it is so awful?

in my opinion, the awful plot is due to float computation error. And u can avoid zero by 2 ways.

1. inrease sampling rate

2. change the time vector head point, not in the 0 or k*pi exactly.

Thanks.
Reply by sachin nanda October 26, 20082008-10-26
You have to be careful about you time vector also.
f=1;
t=0:0.0001:2;
x=sin(2*pi*f*t);
plot(t,x);
That is 2 seconds of time, so you will see 2 full cycles on your plot, f = 1hz.

When t is samples
t=0:16000;
f=1;
x=sin(2*pi*f*t/32000);
plot(t,x);
You will see half a cycle, you have 16000 samples and you specified fs2000
its more like
sinwave = sine(2*pi*f*t./fs); when t is in samples

for your case since you want seconds, I would suggest using the second method
fP000;
fs0000;
t=0:50000;
x=sin(2*pi*f*t./fs);
plot(t,x);

Your main problem was that you had less points than the needed, you were right on the edge of sampling theorem.
try this I changed the 100 to 110
t=0:1/110e3:2 ; %2 seconds of timeline
x=sin(2*pi*50e3*t);
plot(t,x);

-S
________________________________
From: "r...@gmail.com"
To: m...
Sent: Thursday, October 23, 2008 11:37:30 AM
Subject: [matlab] how to plot a sine wave correctly

Hi everyone,

I'm new in matlab and I'm struggling quite a lot. I don't get very well how to define de time vectors so that the sine wave I want to plot displays correctly.

As an example, if I put this code to represent a 50 Khz sine:

t=0:1/100e3:2 ; %2 seconds of timeline
x=sin(2*pi*50e3*t);
plot(t,x);

The result is quite awful. Like noise growing until it gets the end of the timeline. However, if I put a sampling frequency of fs=(2路fc)+1: 100001 Hz, the result is kind of strange as well but not so awful as the previous one, it seems like a sine but as a sin(x)路sin(kx).

What's wrong?

Thanks!
Reply by anser mehboob October 26, 20082008-10-26
Dear rogercanalda

When plotting sine wave you have to be careful if you are sampling frequency is exactly double than the signal frequency. You know that sin(0)=0, sin(180)=0 and sin(360) is also equal to 0. In your code you are actually generating values of sine function at the positions where it gives zero.

When using sin() function don’t start from 0; I mean start from (1/2*100e3) when defining vector 't' OR use cos() function if you want to start from zero because cos(0)=1

so the code should be

t=1/(2*100e2):1/100e3:2 %2 seconds of timeline
x=sin(2*pi*50e3*t);
plot(t,x);

OR

t=0:1/100e3:2 %2 seconds of timeline
x=cos(2*pi*50e3*t);
plot(t,x);
Hopefully, answers the question
Anser Mehboob

--- On Thu, 10/23/08, r...@gmail.com wrote:

> From: r...@gmail.com
> Subject: [matlab] how to plot a sine wave correctly
> To: m...
> Date: Thursday, October 23, 2008, 11:37 AM
> Hi everyone,
>
> I'm new in matlab and I'm struggling quite a lot. I
> don't get very well how to define de time vectors so
> that the sine wave I want to plot displays correctly.
>
> As an example, if I put this code to represent a 50 Khz
> sine:
>
> t=0:1/100e3:2 %2 seconds of timeline
> x=sin(2*pi*50e3*t);
> plot(t,x);
>
> The result is quite awful. Like noise growing until it gets
> the end of the timeline. However, if I put a sampling
> frequency of fs=(2路fc)+1: 100001 Hz, the result is kind of
> strange as well but not so awful as the previous one, it
> seems like a sine but as a sin(x)路sin(kx).
>
> What's wrong?
>
> Thanks!
>
Reply by Michael Bezenchuk October 26, 20082008-10-26
pay attention that when you use k/(2*fc), k-int, you will get
sin(2*pi*fc*k/2/fc)=sin(pi*k)=0
therefore you see junk.
you need to define

fcPe3;
t=0:1/100/fc:6/fc;
x=sin(2*pi*fc*t);
plot(t,x);

in order to see beautiful sine wave with 6 periods
-----------------------------
Michael Bezenchuk
M.Sc Student
Electrical and Computer Engineering
Ben Gurion University
b...@ee.bgu.ac.il / b...@gmail.com
On Thu, Oct 23, 2008 at 8:37 PM, wrote:

> Hi everyone,
>
> I'm new in matlab and I'm struggling quite a lot. I don't get very well how
> to define de time vectors so that the sine wave I want to plot displays
> correctly.
>
> As an example, if I put this code to represent a 50 Khz sine:
>
> t=0:1/100e3:2 %2 seconds of timeline
> x=sin(2*pi*50e3*t);
> plot(t,x);
>
> The result is quite awful. Like noise growing until it gets the end of the
> timeline. However, if I put a sampling frequency of fs=(2fc)+1: 100001 Hz,
> the result is kind of strange as well but not so awful as the previous one,
> it seems like a sine but as a sin(x)sin(kx).
>
> What's wrong?
>
> Thanks!
Reply by roge...@gmail.com October 24, 20082008-10-24
Hi everyone,

I'm new in matlab and I'm struggling quite a lot. I don't get very well how to define de time vectors so that the sine wave I want to plot displays correctly.

As an example, if I put this code to represent a 50 Khz sine:

t=0:1/100e3:2 %2 seconds of timeline
x=sin(2*pi*50e3*t);
plot(t,x);

The result is quite awful. Like noise growing until it gets the end of the timeline. However, if I put a sampling frequency of fs=(2·fc)+1: 100001 Hz, the result is kind of strange as well but not so awful as the previous one, it seems like a sine but as a sin(x)·sin(kx).

What's wrong?

Thanks!