### 3.20 How to sample a sin signal and plot it?

Sample a sin signal of one second period at 8 times its frequency.

Mathematica

period   = 1; (*sec*)
f        = 1/period;
fs       = 8*f; (*sample at 8 times*)
Ts       = 1/fs; (*sampling period*)
nSamples = Round[period/Ts];
x        = Array[# &, nSamples, {0, period - Ts}]; (*linspace*)
signal   = {#, Sin[2 Pi f #]} & /@ x;
text     = MapIndexed[Text[ToString@First[#2], #1, {2, 0}] &, signal];

Show[
ListPlot[signal, PlotStyle -> Red, Filling -> Axis, Frame -> True,
FrameLabel -> {{"f(t)", None}, {"time (sec)", "sampled sin at f2=20 hz"}},
Epilog -> text, PlotTheme -> "Classic",
],
Plot[Sin[2 Pi f t], {t, 0, period}, PlotTheme -> "Classic"],
BaseStyle -> 10, PlotRange -> {{0, period}, {-1.1, 1.1}}]



Matlab

clear all; close all;
period   = 1;
f        = 1/period;
fs       = 8*f;
Ts       = 1/fs;
nSamples = round(period/Ts);
x        = linspace(0,period-Ts,nSamples);
signal   = sin(2*pi*f*x);

stem(x,signal)
for i = 1:length(x)
text(x(i),sign(signal(i))*0.1+signal(i),num2str(i))
end
hold on;
x  = linspace(0,period,10*nSamples);
plot(x,sin(2*pi*f*x))
ylim([-1.2,1.2]);