%% Example 2.2 Sampling and aliasing
% I graph the analog signal f(x) = 2 \sin(12\pi x) + 0.25 \sin(36\pi x).

X = [0:0.001:1]; plot(X,2*sin(12*pi*X)+0.5*sin(36*pi*X))

%
% Next I sample at rate N=48 and plot.

f = 2*sin(12*pi*1/48*(0:47))+0.5*sin(36*pi*1/48*(0:47));
plot(1/48*(0:47),f,'o')
hold on
for j = 1:48
    plot([1/48*(j-1),1/48*(j-1)],[0,2*sin(12*pi*1/48*(j-1))+0.5*sin(36*pi*1/48*(j-1))],'g')
end
hold off

%
% This captures the essential information about the analog signal.
% Next I sample at rate N=24 and plot.

g = 2*sin(12*pi*1/24*(0:23))+0.5*sin(36*pi*1/24*(0:23));
plot(1/24*(0:23),g,'o')
hold on
for j = 1:24
    plot([1/24*(j-1),1/24*(j-1)],[0,2*sin(12*pi*1/24*(j-1))+0.5*sin(36*pi*1/24*(j-1))],'g')
end

%
% Here I see aliasing. The plot has been undersampled. I am getting the same plot I would get if I sample the analog signal g(x) = 1.5 \sin(12\pi x). I add its plot.

plot(X,1.5*sin(12*pi*X),'r--')