Sine plot at a period T over a 20 second interval

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)

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

@Walter Roberson, many thanks for detailed response!
If I want something like this saying, 'A matlab code for sin(πt) sampled at a period T over a 20 second interval. Initially choosing a sample period that is small compared to the period of the wave', what changes should I be targetting in my exisiting code?
correction to what I wrote above: your existing code does pi radians per second but Hz requires 2*pi radians per second per Hz.
But that doesn't actually matter directly. It appears that you are being asked to deal with a 1/2 Hz signal.
I want to create as shown in the attachement using sin(pi*t) over a 20 second interval. I have to initially choose the sample period small compared to the period of the waveform. The two plots have different sample times.
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)
And what about the triangular wave? What parameter should I change in order to get the same?
first peak to second peak appears to be 10 seconds and you pass through the bottom, so suppose you sample at 1/5 hz
So here is my code for 1/5 frequency
t=0:0.2:20;
y=sin(pi*t);
figure(1)
plot(t,y)
But I am not getting the triangular wave that I am supposed to.
That is not 1/5th hz, that is 5 hz.
I am new to this so please bear with me.
Here are the changes.
t=0:5:20;
y=sin(pi*t);
figure(1)
plot(t,y)
I have attached my output as well.
Thanks alot. You are awesome!
Taking this ahead, I want to compute the fft of the original signal where timesamp was t = 0:0.01:20. For the same signal, how do I convert the time axis to frequency axis since fft has to be plotted against frequency and not time.
Here is what I have come up with for plotting FFT vs time.
clear
clc
t=0:0.01:20;
y=sin(t*pi);
f = fft(y);
figure(1)
plot (t,abs(f))
I need to find out what frequencies and what part of the FFT that is useful.
This is the information that I have, 'The first peak of the FFT magnitude should occur at a frequency of 0.5 Hz because that is the frequency of the waveform. What scale factor must you apply to the horizontal axis so that the axis shows the frequency in Hz? As a hint, determine the largest and smallest frequencies that can be represented in the duration of the time array. Apply this scale and reproduce your plot. Notice that the frequency spectrum is symmetric about the center. It turns out that information from the latter half of the curve is redundant. This is an example of what is termed frequency folding or aliasing. If L is the length of the frequency array, exclude all information beyond the middle of the array and plot only the first half with the proper frequency scale on the horizontal axis. Can you predict what frequency corresponds to the middle of the array based on the sample rate and number of samples? Provide a succinct formula for the frequency scaling.' The Figure attached illustrates the results of this for a particular sample rate.
See the first example in the documentation for fft()

Sign in to comment.

Asked:

on 10 Mar 2021

Commented:

on 10 Mar 2021

Community Treasure Hunt

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

Start Hunting!