% Demonstration of sampling theorem and negative frequencies.

% Constants
FREQUENCY = 21
SAMPLING_RATE = 24

% Calculations
t = 0 : (1 / (SAMPLING_RATE * 144)) : 1;
u = 0 : (1 / SAMPLING_RATE) : 1;
xt1 = cos(2 * pi * FREQUENCY * t);
xu1 = cos(2 * pi * FREQUENCY * u);
xt2 = cos(2 * pi * (FREQUENCY + SAMPLING_RATE) * t);
xu2 = cos(2 * pi * (FREQUENCY + SAMPLING_RATE) * u);
xt3 = cos(2 * pi * (FREQUENCY - SAMPLING_RATE) * t);
xu3 = cos(2 * pi * (FREQUENCY - SAMPLING_RATE) * u);


% Plots
figure;

subplot(6, 1, 1);
plot(t, xt1);
title(sprintf('cos(%d)', FREQUENCY));

subplot(6, 1, 2);
stem(u, xu1);
title(sprintf('cos(%d) sampled at %d Hz', ...
              FREQUENCY, ...
              SAMPLING_RATE));

subplot(6, 1, 3);
plot(t, xt2);
title(sprintf('cos(%d)', FREQUENCY + SAMPLING_RATE));

subplot(6, 1, 4);
stem(u, xu2);
title(sprintf('cos(%d) sampled at %d Hz', ...
              FREQUENCY + SAMPLING_RATE, ...
              SAMPLING_RATE));

subplot(6, 1, 5);
plot(t, xt3);
      title(sprintf('cos(%d)', FREQUENCY - SAMPLING_RATE));

subplot(6, 1, 6);
stem(u, xu3);
title(sprintf('cos(%d) sampled at %d Hz', ...
              FREQUENCY - SAMPLING_RATE, ...
              SAMPLING_RATE));

