% file:      	upsampling_3.m, (c) Matthew Roughan, Thu Aug  5 2004
%
N = 32;
x = (1:N)/N;
f1 = 3.4;
y1 = sin(2*pi*f1*x);
K = 3;
upsample = zeros(1,length(y1)*K);
upsample(K*(1:length(y1))) = y1;
x_u = (1:K*N)/(K*N);
z = fft(upsample);
z(N/2+1:end-N/2+1) = 0; %%% filter the data (using perfect low-pass in freq. domain).
y3 = real(ifft(z));

figure(3)
subplot(2,1,1);
hold off
plot([x_u; x_u], [zeros(size(y3)); y3],'b', 'linewidth', 3);
hold on
plot(x_u, y3, 'bo', 'linewidth', 4);
set(gca, 'linewidth', 3, 'fontsize', 18);
ylabel('signal');
set(gca, 'ylim', [-1.2 1.2]/K, 'xlim', [0 max(x)], 'xtick', [], 'ytick', []);

subplot(2,1,2);
hold off
plot([-K*N/2:K*N/2-1; -K*N/2:K*N/2-1], [zeros(1, K*N); abs(fftshift(z))], 'b', 'linewidth', 4);
hold on
plot(-K*N/2:K*N/2-1, abs(fftshift(z)), 'bo', 'linewidth', 3);
YY = get(gca,'ylim');
plot([-K*N/2 -N/2 -N/2 N/2 N/2 K*N/2], [0 0 max(abs(z)) max(abs(z)) 0 0], 'r', 'linewidth', 3);
set(gca, 'linewidth', 3, 'fontsize', 18);
set(gca, 'xlim', [-K*N/2 K*N/2], 'ytick', []);
ylabel('power');
xlabel('frequency');

set(gcf, 'PaperUnits', 'centimeters', 'PaperPosition', [0 0 30 15])
print('-depsc', 'Plots/upsampling_2.eps');


