sine wave plot

4 124 vues (au cours des 30 derniers jours)
aaa le 24 Avr 2012
Commenté : DGM le 18 Août 2023
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);
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?
  6 commentaires
Walter Roberson
Walter Roberson le 10 Août 2021
In order to solve that, you need some hardware to do analog to digital conversion between your 3V source and MATLAB.
3V is too large for audio work, so you are not going to be able to use microphone inputs to do this. You are going to need hardware such as a National Instruments ADC or at least an arduino (you might need to put in a resistor to lower the voltage range.)
The software programming needed on the MATLAB end depends a lot on which analog to digital convertor you use.
The appropriate analog to digital convertor to use is going to depend in part on what sampling frequency you need to use; you did not define that, so we cannot make any hardware recommendations yet.
Gokul Krishna N
Gokul Krishna N le 13 Oct 2021
Just been reading the comments in this question. Hats off to you, sir @Walter Roberson

Connectez-vous pour commenter.

Réponse acceptée

Rick Rosson
Rick Rosson le 24 Avr 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:
xlabel('time (in seconds)');
title('Signal versus Time');
zoom xon;
  2 commentaires
Rajasekaran le 14 Mar 2013
Thanks for your reply & detailed answer.
Nauman Hafeez
Nauman Hafeez le 28 Déc 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.

Connectez-vous pour commenter.

Plus de réponses (9)

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

Junyoung Ahn
Junyoung Ahn le 16 Juin 2020
f=60; %frequency [Hz]
a=1; %amplitude [V]
phi=0; %phase
  2 commentaires
DARSHAN le 8 Jan 2023
why should we multiply f with 100?
Walter Roberson
Walter Roberson le 8 Jan 2023
I think the intent was to give 100 samples per cycle.

Connectez-vous pour commenter.

Robert le 28 Nov 2017
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

shampa das
shampa das le 26 Déc 2020
Modifié(e) : Walter Roberson le 31 Jan 2021
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);

soumyendu banerjee
soumyendu banerjee le 1 Nov 2019
%% if Fs= the frequency u want,
x = -pi:0.01:pi;

wilfred nwakpu
wilfred nwakpu le 1 Fév 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:
xlabel('time (in seconds)');
title('Signal versus Time');
zoom xon;

sevde busra bayrak
sevde busra bayrak le 24 Août 2020
sampling_rate = 250;
time = 0:1/sampling_rate:2;
freq = 2;
%general formula : Amplitude*sin(2*pi*freq*time)
signal = sin(2*pi*time*freq);
title('Sine Wave')

Ana Maria
Ana Maria le 15 Mar 2023
Implement a function to generate a column vector containing a sine wave, sin(2πf(t)t), with a growing frequency, f(t) from f(0) = f1 to f(T) = f2. The inputs of the function are the duration, T in seconds, the frequencies, f1 and f2, in Hz and the sampling rate, fs, in samples per second x = chirpT one(T, f1, f2, fs)
  2 commentaires
Walter Roberson
Walter Roberson le 15 Mar 2023
Could you explain how the process you set out will solve the original question posted in 2012 ?
DGM le 15 Mar 2023
Modifié(e) : DGM le 16 Mar 2023
You're copying and pasting an assignment text. This is not an answer, so it doesn't belong here as an answer. I'm compelled to keep things where they belong and remove them when they don't.
This is ultimately your task to perform. The information already present on this page is largely sufficient to complete it. I'm sure with enough effort, you can find even more specific examples elsewhere on the forum.
If you want to ask a question, please open a new question using the 'ask' button at the top of the page. If and when you do, ask an actual question, but also be prepared to prove that you've exhausted what due diligence provides.
To prove the point, I'm just going to grab the answer directly above and make one simple change. Other than changing the specific parameters (a matter of choice), the only real change is that instead of being a scalar, freq is a vector generated from two specified values.
% these are parameters
samplerate = 500;
duration = 2;
flim = [0 8];
% both these vectors have the same size
time = 0:1/samplerate:duration; % time is a linear vector
freq = linspace(flim(1),flim(2),numel(time)); % freq is a linear vector
Generating a vector of uniformly-spaced values is very basic MATLAB stuff. The remaining change necessary to make freq work as a vector is also basic (literally one single character), but I have to leave something for you to do.

Connectez-vous pour commenter.

Adewole le 17 Août 2023
Base upon the following equation what percent of y values are greater than 0.8 for x =0:10 and y=sinx
  2 commentaires
Rik le 17 Août 2023
How does this answer the question? You can find guidelines for posting homework on this forum here. If you have trouble with Matlab basics you may consider doing the Onramp tutorial (which is provided for free by Mathworks). If your main issue is with understanding the underlying concept, you may consider re-reading the material you teacher provided and ask them for further clarification.
This answer will be deleted in 24h unless you respond.
DGM le 18 Août 2023
I was getting ready do delete this when I realized something. If we trust that this is a rather literal duplication of the assignment text, then this is a pretty terrible assignment. Given that x is a coarse discrete set of points, what exactly is this trivial ratio (i.e. the solution) supposed to teach the student other than some vague precaution regarding aliasing? Given that the calculation of the solution is so utterly simplistic, I find it hard to believe that any lesson so complicated is intended.
On the other hand, is x really supposed to be a discrete set of 11 integers, or are we supposed to be considering all values on the closed interval [0 10]? That would actually make a decent problem both in terms of math fundamentals and using MATLAB, but it's contrary to the text as given. Poster's mistake? Instructor's mistake?
Either way, the answer is exactly
2*(pi - 2*asin(4/5))/10
or maybe it's exactly
They're not equivalent, so which is which? Which is right? Is either one right? If not, why?
I should also point out that even if you copied and pasted the right one and submitted it, any decent TA would still give you zero points, since you'd have no work to show, and chances are you might think that my final question was worth ignoring.

Connectez-vous pour commenter.


En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by