Asked by aaa
on 24 Apr 2012

Hi,

I am having some trouble plotting a sine wave and i'm not sure where i am going wrong.

i have

t = [0:0.1:2*pi]

a = sin(t);

plot(t,a)

this works by itself, but i want to be able to change the frequency. When i run the same code but make the change

a = sin(2*pi*60*t)

the code returns something bad. What am i doing wrong? How can i generate a sin wave with different frequencies?

Answer by Rick Rosson
on 24 Apr 2012

Accepted Answer

Please try:

%%Time specifications:

Fs = 8000; % samples per second

dt = 1/Fs; % seconds per sample

StopTime = 0.25; % seconds

t = (0:dt:StopTime-dt)'; % seconds

%%Sine wave:

Fc = 60; % hertz

x = cos(2*pi*Fc*t);

% Plot the signal versus time:

figure;

plot(t,x);

xlabel('time (in seconds)');

title('Signal versus Time');

zoom xon;

HTH.

Rick

Rajasekaran
on 14 Mar 2013

Thanks for your reply & detailed answer.

Nauman Hafeez
on 28 Dec 2018

How to calculate Fs for a particular frequency signal?

I am generating a stimulating signal using matlab for my impedance meter and it gives me different results on different Fs.

Sign in to comment.

Answer by omphile hahilton
on 3 Mar 2015

Sign in to comment.

Answer by Robert
on 28 Nov 2017

aaa,

What goes wrong: by multiplying time vector t by 2*pi*60 your discrete step size becomes 0.1*2*pi*60=37.6991. But you need at least two samples per cycle (2*pi) to depict your sine wave. Otherwise you'll get an alias frequency, and in you special case the alias frequency is infinity as you produce a whole multiple of 2*pi as step size, thus your plot never gets its arse off (roundabout) zero.

Using Rick's code you'll be granted enough samples per period.

Best regs

Robert

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Ahmed Grera (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/36428-sine-wave-plot#comment_481564

## Govinda Nahak (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/36428-sine-wave-plot#comment_489408

Sign in to comment.