% routine to illustrate the effect of subsampling on the 
% fourier transform
% Requires DFTfast

clear; figure(1); clf; figure(2); clf

a = 1;
b = 1;
n = 1023;
k=(-n:n);

% make signal

% x = (1-cos(a*k))./k.^2*b/(pi*a);
% but to avoid division by zero:

k2 = (-n:-1); cak2 = cos(a*k2);
x(1:n) = (1-cak2)./k2.^2*b/(pi*a);
x(n+2:2*n+1) = fliplr(x(1:n));
x(n+1) = b*a/(2*pi);
k1 = (-44:44);

figure(1); subplot(2,2,1); plot(x(n+1+k1));
title('The signal x(k)'); xlabel('k');
disp('A plot of the signal')
disp('... push an arbitrary key to continue'), pause
%print -deps subsam1_ex1.eps

% DTFT of the signal

N = 1024;
% mind that N > n, or there are too few points omega
% where the FFT is computed
X = DTFTfast(x,N); om = (-N:N)*pi/N;

figure(1); subplot(2,2,2); plot(om,abs(X));
axis([-pi,pi,0,b]) 

title('|X(omega)|=|FFT(x(k))|');xlabel('omega');
disp('Plot of the Fourier transform')
disp('... push an arbitrary key to continue'), pause

% subsapling 1 out of 2

xs = x(1:2:2*n+1); % subsample
Xs = DTFTfast(xs,N);
om = (-N:N)*pi/N;

figure(1); subplot(2,2,3); plot(om,abs(Xs));
axis([-pi,pi,0,b])
title('|Xs(omega)|=|FFT(x(2k))|');xlabel('omega');

disp('Plot of the Fourier transform of the subsampled signal')
disp('Note that the bandwidth is doubled, the amplitude halved')
disp('... push an arbitrary key to continue'), pause

% the  DTFT of the subsampled signal

x0 = x;
x0(2:2:2*n+1) = zeros(size(x0(2:2:2*n+1)));
X0 = DTFTfast(x0,N);
om = (-N:N)*pi/N;

figure(1), subplot(2,2,4); plot(om,abs(X0))
axis([-pi,pi,0,b])
title('|X0(omega)|=|FFT(..x(2k) 0..)|');
xlabel('omega');
%print -deps subsam1_ex2.eps

disp('Plot of the Fourier transform of the subsampled signal');
disp('               with zeros inserted')
disp('Note that the previous bandwidth halved again and repeated')
disp('In this case the repetitions do not overlap')
disp('... push an arbitrary key to continue'), pause

% repeat for higher frequency

an=2;
disp('Now repeat the same computation with a higher frequency'),
disp(['the factor is  ',int2str(an/a),' hence bandwidth is *',int2str(an/a)]),

a = an;
k=(-n:n);

% the signal

% x = (1-cos(a*k))./k.^2*b/(pi*a);
% but to avoid division by zero:

k2 = (-n:-1); cak2=cos(a*k2);
x(1:n)=(1-cak2)./k2.^2*b/(pi*a);
x(n+2:2*n+1)=fliplr(x(1:n));
x(n+1) = b*a/(2*pi);
k1=(-44:44);

figure(2);subplot(2,2,1);plot(x(n+1+k1));
title('The signal x(k)');xlabel('k');

disp('A plot of the signal')
disp('... push an arbitrary key to continue'), pause

% DTFT of signal

N = 1024;
X = DTFTfast(x,N);
om = (-N:N)*pi/N;

figure(2);subplot(2,2,2);plot(om,abs(X))
axis([-pi,pi,0,b])
title('|X(omega)|=|FFT(x(k))|');xlabel('omega');

disp('Plot of the Fourier transform')
disp('... push an arbitrary key to continue'), pause

% subsampled signal

xs = x(1:2:2*n+1); % subsample
Xs = DTFTfast(xs,N);
om = (-N:N)*pi/N;

figure(2);subplot(2,2,3);plot(om,abs(Xs));
axis([-pi,pi,0,b])
title('|Xs(omega)|=|FFT(x(2k))|');xlabel('omega');

disp('Plot of the Fourier transform of the subsampled signal')
disp('Note that the bandwidth is doubled, the amplitude halved')
disp('... push an arbitrary key to continue'), pause

% DTFT of subsampled signal

x0 = x;
x0(2:2:2*n+1) = zeros(size(x0(2:2:2*n+1)));
X0 = DTFTfast(x0,N);
om = (-N:N)*pi/N;

figure(2);subplot(2,2,4);plot(om,abs(X0))
axis([-pi,pi,0,b])
title('|Xs(omega)|=|FFT(..x(2k) 0..)|');xlabel('omega');
%print -deps subsam1_ex3.eps
xlabel('omega');
%print -deps subsam1_ex2.eps

disp('Plot of the Fourier transform of the subsampled signal');
disp('               with zeros inserted')
disp('Note that the bandwidth is NOT doubled but repeated')
disp('The repeated bands overlap and can not be separated')
disp('This is called ALIASING')

