Sine plot at a period T over a 20 second interval
Show older comments
Write a MATLAB script (m) file that builds an array e(k) which consists of the waveform sin(πt) sampled at a period T over a 20 second interval. Initially choose a sample period that is small compared to the period of the waveform.
My code is as follows.
clear all
clc
T=20;
freq = 1/20;
t=0:0.01:20;
y=sin(t*pi);
figure(1)
plot(t,y)
xticks(0:2:20);
grid on
grid minor
Answers (1)
Walter Roberson
on 10 Mar 2021
T=20;
ok. But you never use T.
freq = 1/20;
Is the 20 a coincidence? But it doesn't matter, as you never use freq. Note by the way that a "freq"uency of 1/20 would mean that one period requires 20 seconds.
t=0:0.01:20;
That is a frequency of 1/0.01 which is 100 Hz.
y=sin(t*pi);
Only if your t is in radians per second instead of samples per second.
If you want 1 Hz thenyou need to complete 1 period in 1 second. 1 period is 2*pi radians. Therefore over 1 second you need to complete 2*pi radians. Your current code completes 1 radian per second instead of 2*pi radians in 1 seconds.
13 Comments
Ketan Shende
on 10 Mar 2021
Walter Roberson
on 10 Mar 2021
correction to what I wrote above: your existing code does pi radians per second but Hz requires 2*pi radians per second per Hz.
Walter Roberson
on 10 Mar 2021
But that doesn't actually matter directly. It appears that you are being asked to deal with a 1/2 Hz signal.
Ketan Shende
on 10 Mar 2021
Walter Roberson
on 10 Mar 2021
Sample rate for this purpose is the difference between adjacent values in t, which would be determined by the increment you use in
t=0:0.01:20
^^^^
replace that with the sample interval (which is 1 divided by the sampling frequency)
Ketan Shende
on 10 Mar 2021
Walter Roberson
on 10 Mar 2021
first peak to second peak appears to be 10 seconds and you pass through the bottom, so suppose you sample at 1/5 hz
Ketan Shende
on 10 Mar 2021
Walter Roberson
on 10 Mar 2021
That is not 1/5th hz, that is 5 hz.
Ketan Shende
on 10 Mar 2021
Fs = 2/5;
t=0:1/Fs:20;
y=sin(pi*t);
plot(t,y)
Ketan Shende
on 10 Mar 2021
Edited: Walter Roberson
on 10 Mar 2021
Walter Roberson
on 10 Mar 2021
See the first example in the documentation for fft()
Categories
Find more on Waveform Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!