Sine plot at a period T over a 20 second interval

9 views (last 30 days)
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
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
Ketan Shende on 10 Mar 2021
Edited: Walter Roberson on 10 Mar 2021
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.
Walter Roberson
Walter Roberson on 10 Mar 2021
See the first example in the documentation for fft()

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!