Plotting sine wave using rad/samp

3 views (last 30 days)
Vinci
Vinci on 30 Aug 2018
Commented: Sven on 30 Aug 2018
If I need to plot a sine wave sampled at 8kHz for 4 seconds with a frequency of pi/10 rad/samples, would this be the right way to go about it?
Fs = 8000; % Sampling frequency
stopTime = 4; % Run time (seconds)
dr = pi/10; % Radians per sample
stopSample = Fs * stopTime; % Total samples
totalRad = (Fs * stopTime * dr); % Total radians for run time
r = 0:dr:totalRad; % Rad step for run time
% Generate sine wave
x = sin(r*dr);

Accepted Answer

Sven
Sven on 30 Aug 2018
Almost correct. You need to change your r vector to get the correct time vector, because you only want to plot the sine wave for 4 seconds.
r = linspace(0,stopTime,stopSample);
linspace creates a vector with evenly spaced points between 0 and stopTime. stopSample is the number of points. The plotted data is now not even a fourth of a full sine wave which makes sense, because your frequency is 0.05 Hz ( pi/10/(2*pi) ) which makes a full sine wave take 20 seconds.
  2 Comments
Vinci
Vinci on 30 Aug 2018
How does the sample rate of 8000 effect the frequency? I thought it would be: 8000*(pi/10)/(2*pi) = 400Hz
Sven
Sven on 30 Aug 2018
The sample rate does not affect the frequency itself. A sample rate of 8000 Hz just says, that you get 8000 data points per second or 1 data point every 125us.
If you are sampling a frequency of 1 Hz, a full periode takes 1 second, so you have 8000 data points per cycle, which lets you see a perfectly round sine wave if plotted. If the frequency for example is 4000 Hz, a periode takes only 250us and you only get 2 data points per cycle.
There is also something called the nyquist frequency which is half the sampling frequency, in your case 4000 Hz. This is the maximum frequency you can detect when doing an FFT of your sampled data, because you need at least 2 data points per cycle for that.
I hope that helps you to understand.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!