Search tips

Search tips

# Discussion Groups | Matlab DSP | how to plot a sine wave correctly

Technical discussion about Matlab and issues related to Digital Signal Processing.

Is this thread worth a thumbs up?

0

# how to plot a sine wave correctly - roge...@gmail.com - Oct 24 8:25:34 2008

```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!

```

# RE: how to plot a sine wave correctly - anser mehboob - Oct 26 16:08:12 2008

```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);
Anser Mehboob

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

> From: r...@gmail.com <r...@gmail.com>
> Subject: [matlab] how to plot a sine wave correctly
> To: m...@yahoogroups.com
> 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!
>

```

# Re: how to plot a sine wave correctly - sachin nanda - Oct 26 16:08:48 2008

```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;
fs0000;
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" <r...@gmail.com>
To: m...@yahoogroups.com
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!

```

# Re: how to plot a sine wave correctly - hyee...@yahoo.com.cn - Oct 27 7:42:28 2008

```
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.

```