%aliasing_effekt
%script to demonstrate the aliasing-effect of low sample rates with a
%sinus curve.
% It is part of the Matlab course for PhD Students 2010-2011:
% http://www.sinnesphysiologie.uni-oldenburg.de/50246.html
% written by Jutta Kretzberg, last modification 12.12.2010

%% generate original curve with 1000 samples between 0 and 50*pi
t1000=linspace(0,1,1001);           % "time"
x1000=linspace(0,20*pi,1001);       % x value for the sine wave
y1000=sin(x1000);                   % sine signal
plot(t1000,y1000,'b-')              % plot a line
xlabel('sec')
ylabel('amplitude')
title('continuous signal')
hold on

pause                               % wait for user key press

plot(t1000,y1000,'b^')              % plot individual data points       
title('Sampling mit 1000Hz')
hold off

pause


%% Down-sampling to half of the data points

t500=t1000(1:2:end);
x500=x1000(1:2:end);
y500=y1000(1:2:end);
plot(t1000,y1000,'b-',t500,y500,'r^')
title('Sampling with 500Hz')
hold on

pause

plot(t500,y500,'r^-')
title('Reconstructed with 500Hz')
hold off

pause

%% Down-sampling to 1/10 of the data points

t100=t1000(1:10:end);
x100=x1000(1:10:end);
y100=y1000(1:10:end);
plot(t1000,y1000,'b-',t100,y100,'r^')
xlabel('sec')
ylabel('Pegel')
title('Sampling with 100Hz')
hold on

pause

plot(t1000,y1000,'b-',t100,y100,'r^-')
title('Reconstructed with 100Hz')
hold off

pause

%% Down-sampling to 1/20 of the data points

t50=t1000(1:20:end);
x50=x1000(1:20:end);
y50=y1000(1:20:end);
plot(t1000,y1000,'b-',t50,y50,'r^')
xlabel('sec')
ylabel('Pegel')
title('Sampling with 50Hz')
hold on

pause

plot(t1000,y1000,'b-',t50,y50,'r^-')
title('Reconstructed with 50Hz')
hold off

pause

%% Down-sampling to 1/20 of the data points

t20=t1000(1:50:end);
x20=x1000(1:50:end);
y20=y1000(1:50:end);
plot(t1000,y1000,'b-',t20,y20,'r^')
xlabel('sec')
ylabel('Pegel')
title('Sampling with 20Hz')
hold on

pause

plot(t1000,y1000,'b-',t20,y20,'r^-')
title('Reconstructed with 20Hz')
hold off

pause

%% Down-sampling to 1/75 of the data points

t13=t1000(1:75:end);
x13=x1000(1:75:end);
y13=y1000(1:75:end);
plot(t1000,y1000,'b-',t13,y13,'r^')
xlabel('sec')
ylabel('Pegel')
title('Sampling with 13.3Hz')
hold on

pause

plot(t1000,y1000,'b-',t13,y13,'r^-')
title('Reconstructed with 13.3Hz')
hold off

pause


%% Down-sampling to 1/100 of the data points

t10=t1000(1:100:end);
x10=x1000(1:100:end);
y10=y1000(1:100:end);
plot(t1000,y1000,'b-',t10,y10,'r^')
xlabel('sec')
ylabel('Pegel')
title('Sampling with 10Hz')
hold on

pause

plot(t1000,y1000,'b-',t10,y10,'r^-')
title('Reconstructed with 10Hz')

hold off
