MATLAB: Plotting sine wave using rad/samp

plotting

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);

Best Answer

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.
Related Question