MATLAB Answers

sine wave plot

7.864 views (last 30 days)
aaa
aaa on 24 Apr 2012
Answered: Zaianb Adil on 18 Jan 2021 at 9:03
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?

  7 Comments

Show 4 older comments
SREEGANESH VAKA
SREEGANESH VAKA on 13 Jan 2021 at 4:52
Write a MATHLAB code to generate a CT step signal with peak Amplitude of 5 Volts and should be plotted on the time scale from 0 to 1000.
Walter Roberson
Walter Roberson on 13 Jan 2021 at 5:37
That does not seem to be relevant to the Question that was asked?
Subhranil Barman
Subhranil Barman on 13 Jan 2021 at 6:11
Write a MATHLAB code to generate a CT step signal with peak Amplitude of 5 Volts and should be plotted on the time scale from 0 to 1000.

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

  6 Comments

Show 3 older comments
Jessica
Jessica on 26 Nov 2020
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.
MUHAMMAD BASIM
MUHAMMAD BASIM on 14 Dec 2020
Can anybody please provide the MATLAB code for kalman filter for filtering out noise from a sine signal.please refer to attached picture for detail
Walter Roberson
Walter Roberson on 14 Dec 2020
When you searched MATLAB Answrs for kalman filter, and found the more than 600 results, what was unique about your situation such that none of the more than 600 results were suitable for even starting the code?

Sign in to comment.

More Answers (9)

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.


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.


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.


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.


shampa das
shampa das on 26 Dec 2020 at 20:04
clc; t=0:0.01:1; f=1; x=sin(2*pi*f*t); figure(1); plot(t,x);
fs1=2*f; n=-1:0.1:1; y1=sin(2*pi*n*f/fs1); figure(2); stem(n,y1);
fs2=1.2*f; n=-1:0.1:1; y2=sin(2*pi*n*f/fs2); figure(3); stem(n,y2);
fs3=3*f; n=-1:0.1:1; y3=sin(2*pi*n*f/fs3); figure(4); stem(n,y3); figure (5);
subplot(2,2,1); plot(t,x); subplot(2,2,2); plot(n,y1); subplot(2,2,3); plot(n,y2); subplot(2,2,4); plot(n,y3);

  0 Comments

Sign in to comment.


Radwa Ahmed
Radwa Ahmed on 5 Jan 2021 at 3:38
9- Consider modulating a carrier cos(10t) with the following signals:
(a) 𝑥1 (𝑡) = 𝑒 −|𝑡| . Use MATLAB to find the Fourier transform of 𝑦1 (𝑡) = 𝑥1 (𝑡) cos(10𝑡) and plot y1(t) and its magnitude spectra. (b) 𝑥2 (𝑡) = 0.2[𝑟(𝑡 + 5) − 2𝑟(𝑡) + 𝑟(𝑡 + 5)], where r(t) is the ramp signal. Use MATLAB to 𝑥2 (𝑡) 𝑎𝑛𝑑 𝑦1 (𝑡) = 𝑥2 (𝑡) cos(10𝑡) and compute and plot the magnitude of their Fourier transforms.

  1 Comment

Walter Roberson
Walter Roberson on 13 Jan 2021 at 5:38
This is not an Answer to the question that was asked.

Sign in to comment.


Zaianb Adil
Zaianb Adil on 18 Jan 2021 at 9:03
Q2/ Generate the following signals by using MATLAB code, use a time vector 't' from -5 to 5 with 1000 value. 1. 2 * δ (0) 2. δ (n-2) 3. u (n+4) 4. cosine with 4 periods 5.
6. ramp delayed by 2.5s

  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!