sine wave plot

조회 수: 1,183 (최근 30일)
aaa
aaa 2012년 4월 24일
답변: Steven Lord 2025년 9월 23일
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?
  댓글 수: 6
Walter Roberson
Walter Roberson 2021년 8월 10일
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 2021년 10월 13일
Just been reading the comments in this question. Hats off to you, sir @Walter Roberson

댓글을 달려면 로그인하십시오.

채택된 답변

Rick Rosson
Rick Rosson 2012년 4월 24일
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
  댓글 수: 3
Nauman Hafeez
Nauman Hafeez 2018년 12월 28일
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.
alex
alex 2025년 9월 23일 10:38
class you are mate, bang on

댓글을 달려면 로그인하십시오.

추가 답변 (8개)

Mike Mki
Mike Mki 2016년 11월 29일
Dear Mr. Rick, Is it possible to create knit structure in Matlab as follows:

Junyoung Ahn
Junyoung Ahn 2020년 6월 16일
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)')
  댓글 수: 2
DARSHAN
DARSHAN 2023년 1월 8일
why should we multiply f with 100?
Walter Roberson
Walter Roberson 2023년 1월 8일
I think the intent was to give 100 samples per cycle.

댓글을 달려면 로그인하십시오.


Robert
Robert 2017년 11월 28일
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

shampa das
shampa das 2020년 12월 26일
편집: Walter Roberson 2021년 1월 31일
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 2019년 11월 1일
%% if Fs= the frequency u want,
x = -pi:0.01:pi;
y=sin(Fs.*x);
plot(y)
  댓글 수: 1
Walter Roberson
Walter Roberson 2024년 9월 20일
This should have
plot(x, y)

댓글을 달려면 로그인하십시오.


sevde busra bayrak
sevde busra bayrak 2020년 8월 24일
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')

Ranjita
Ranjita 2024년 9월 30일
clc
clear all
fs = 10000;
T=1/fs
T = 1.0000e-04
f1 = 100;
f2= 50;
L= 10000;
t = (0:L-1)*T;
x1 =sin(2*pi*f1*t)+4*cos(2*pi*f2*t)
x1 = 1×10000
4.0000 4.0608 4.1174 4.1696 4.2171 4.2598 4.2973 4.3294 4.3561 4.3770 4.3920 4.4009 4.4037 4.4000 4.3898 4.3730 4.3496 4.3193 4.2821 4.2381 4.1871 4.1292 4.0643 3.9926 3.9139 3.8284 3.7362 3.6374 3.5320 3.4202
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
figure
subplot(2,2,1)
plot(t,x1)
axis([0 0.1 -1 6]);
title('SS Function');
xlabel('time');
ylabel('magnitude');
%frequency domain conversion and plotting
Y_x1=fftshift(fft(x1));
subplot(2,1,2)
plot (-(fs/2-fs/L)-1:(fs/L):(fs/2-fs/L),abs(Y_x1))
axis([-700 700 0 max(abs(Y_x1))+10000]);
title('Magnitude spectrum of S1 Function');
xlabel('Frequency(Hz)');
ylabel('magnitude');
sgtitle('Frequency Domain Representation of S1 Function');

Steven Lord
Steven Lord 2025년 9월 23일 13:45
If you're using release R2018b or later, rather than computing sin(pi*something), I recommend using the sinpi function (and there is a corresponding cospi function.)
x = 0:0.25:2
x = 1×9
0 0.2500 0.5000 0.7500 1.0000 1.2500 1.5000 1.7500 2.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
s1 = sin(x*pi)
s1 = 1×9
0 0.7071 1.0000 0.7071 0.0000 -0.7071 -1.0000 -0.7071 -0.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
s2 = sinpi(x)
s2 = 1×9
0 0.7071 1.0000 0.7071 0 -0.7071 -1.0000 -0.7071 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Note that elements 5 and 9 of s1 and s2 are visually different. In s1 they are very close to, but not exactly equal to, 0. In s2 since we're taking the sine of exact multiples of pi (x(5) is exactly 1 and x(9) is exactly 2) we get actual 0 values.
format longg
[s1([5 9]); s2([5 9])]
ans = 2×2
1.0e+00 * 1.22464679914735e-16 -2.44929359829471e-16 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
And in this particular example from the original question:
t = [0:0.1:2*pi];
inner = 2*60*t
inner = 1×63
0 12 24 36 48 60 72 84 96 108 120 132 144 156 168 180 192 204 216 228 240 252 264 276 288 300 312 324 336 348
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
When we compare their values with the rounded version of those values using a very tight tolerance, we see that the values of inner are all very, very close to integer values. [isapprox was introduced in release R2024b.]
all(isapprox(inner, round(inner), 'verytight'))
ans = logical
1
That means that if we use sinpi all the values should be very close to 0.
a = sinpi(inner)
a = 1×63
1.0e+00 * 0 0 0 2.23223583872552e-14 0 0 4.46447167745105e-14 4.46447167745105e-14 0 0 0 0 8.9289433549021e-14 0 8.9289433549021e-14 0 0 8.9289433549021e-14 0 8.9289433549021e-14 0 0 0 1.78578867098042e-13 1.78578867098042e-13 0 0 0 1.78578867098042e-13 1.78578867098042e-13
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
maximumDifferenceFromZero = max(a, [], ComparisonMethod="abs")
maximumDifferenceFromZero =
3.57157734196084e-13
I'd say that's effectively 0 for most purposes.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by