MATLAB Answers

sine wave plot

12,217 views (last 30 days)
aaa
aaa on 24 Apr 2012
Edited: Jessica on 26 Nov 2020 at 13:58
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?

  4 Comments

Show 1 older comment
Govinda Nahak
Govinda Nahak on 2 Oct 2017
in sine function in MATLAB it is always sin(wt). here frequency w is in radian/sec not f (in HZ) so w will give you the no.of the cycle.
suppose w=1 it is one cycle and so on
if you want to use the sin(2*pi*60*t) you can use the sind(2*pi*9.545*t). why i use the 9.545 bcz we should convert the f to w in the time interval of 2*pi.
Jorge Ignacio Cisneros Saldana
How can we make it 3 phase system? for 50 Hz and 230 V peak to peak
Mellamputi Sai geetha
Mellamputi Sai geetha on 16 Nov 2020 at 12:00
x(t)=∆(t+7)-∆(t-7) prepare a MATLAB graph

Sign in to comment.

Accepted Answer

Rick Rosson
Rick Rosson on 24 Apr 2012
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

  4 Comments

Show 1 older comment
Nauman Hafeez
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.
Sameeksha Singh
Sameeksha Singh on 30 Oct 2020 at 8:19
This task has students detect the frequency of a sinusoid embedded within a noisy signal. To do so, we generate a set of candidate sinusoidal functions, each representing our guess for the unknown frequency. We then cross-correlate these functions with the input signal and then use the magnitude of the peaks in the output as a score for the presence of each frequency in the input signal. Let our signal be a sinusoid x(t) containing an unknown frequency that lies in the range from 1 Hz to 2 Hz. This signal is sampled at a rate of 30 samples per second by an analog-to-digital converter to create a digital signal x[n]. In this circumstance we will proceed as follows:
1. Hypothesize a sinusoidal sequence having frequencies ranging from f = [1Hz, 2Hz] in increments of 0.2 Hz. Create a digital lter h[n] to cross-correlate with the input x[n].
2. Adjust values of your lter coecients so that the sum of the lter coecients is equal to 0. This can be accomplished by sampling the sinusoid symmetrically about the origin since the sine function is odd symmetric, i.e., sin(x) = − sin(−x). Note that since sin(0) = 0 the sample h[0] has no contribution to either your lter or the output value.
3. For each sequence, cross-correlate your signal with the input signal. 4. Observe the output signal and nd the cross-correlation signal having largest peak response. This frequency is then the best guess for the unknown frequency in the input signal.
please help me I am new to MATLAB
Jessica
Jessica 13 minutes ago
Att: Sameeksha Singh
Do not ask questions within other persons questions.
Here is how you do it:
Top right there is a "Ask the Community" button.
Click and copy/paste your question there.
Make sure that you have as much code or specific facts with, not so much text as possible.

Sign in to comment.

More Answers (6)

Mike Mki
Mike Mki on 29 Nov 2016
Dear Mr. Rick, Is it possible to create knit structure in Matlab as follows:

  0 Comments

Sign in to comment.


Robert
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

  0 Comments

Sign in to comment.


soumyendu banerjee
soumyendu banerjee on 1 Nov 2019
%% if Fs= the frequency u want,
x = -pi:0.01:pi;
y=sin(Fs.*x);
plot(y)

  0 Comments

Sign in to comment.


wilfred nwakpu
wilfred nwakpu on 1 Feb 2020
%%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;

  0 Comments

Sign in to comment.


Junyoung Ahn
Junyoung Ahn on 16 Jun 2020
clear;
clc;
close;
f=60; %frequency [Hz]
t=(0:1/(f*100):1);
a=1; %amplitude [V]
phi=0; %phase
y=a*sin(2*pi*f*t+phi);
plot(t,y)
xlabel('time(s)')
ylabel('amplitude(V)')

  0 Comments

Sign in to comment.


sevde busra bayrak
sevde busra bayrak on 24 Aug 2020
sampling_rate = 250;
time = 0:1/sampling_rate:2;
freq = 2;
%general formula : Amplitude*sin(2*pi*freq*time)
figure(1),clf
signal = sin(2*pi*time*freq);
plot(time,signal)
xlabel('time')
title('Sine Wave')

  0 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!