
n = 1000;
dx = 2*pi/n;
x = -pi:dx:pi-dx;
x_left = -pi:dx:-dx;
y_left = sign(x_left);
x_right = dx:dx:pi;
y_right = sign(x_right);
y = sign(x);

y_hat = fft(y);
N_cut = 4;
clear y_hat_trunc
y_hat_trunc(1:N_cut+1) = y_hat(1:N_cut+1);
y_hat_trunc(n-N_cut:n) = y_hat(n-N_cut:n);
y_trunc = real(ifft(y_hat_trunc));
figure(1)
clf
subplot(2,1,1)
plot(x,y_trunc);
hold on
plot(x_left,y_left);
plot(x_right,y_right);
plot(x+2*pi,y_trunc);
plot(x_left+2*pi,y_left);
plot(x_right+2*pi,y_right);
plot(x-2*pi,y_trunc);
plot(x_left-2*pi,y_left);
plot(x_right-2*pi,y_right);
subplot(2,1,2)
plot(x,y_trunc);
hold on
plot(x_left,y_left);
plot(x_right,y_right);
plot(x+2*pi,y_trunc);
plot(x_left+2*pi,y_left);
plot(x_right+2*pi,y_right);
plot(x-2*pi,y_trunc);
plot(x_left-2*pi,y_left);
plot(x_right-2*pi,y_right);

N_cut = 8;
y_hat_trunc(1:N_cut+1) = y_hat(1:N_cut+1);
y_hat_trunc(n-N_cut:n) = y_hat(n-N_cut:n);
y_trunc = real(ifft(y_hat_trunc));
subplot(2,1,1)
plot(x,y_trunc);
plot(x+2*pi,y_trunc);
plot(x-2*pi,y_trunc);
subplot(2,1,2)
plot(x,y_trunc);
plot(x+2*pi,y_trunc);
plot(x-2*pi,y_trunc);

N_cut = 12;
y_hat_trunc(1:N_cut+1) = y_hat(1:N_cut+1);
y_hat_trunc(n-N_cut:n) = y_hat(n-N_cut:n);
y_trunc = real(ifft(y_hat_trunc));
subplot(2,1,1)
plot(x,y_trunc);
plot(x+2*pi,y_trunc);
plot(x-2*pi,y_trunc);
subplot(2,1,2)
plot(x,y_trunc);
plot(x+2*pi,y_trunc);
plot(x-2*pi,y_trunc);

N_cut = 16;
y_hat_trunc(1:N_cut+1) = y_hat(1:N_cut+1);
y_hat_trunc(n-N_cut:n) = y_hat(n-N_cut:n);
y_trunc = real(ifft(y_hat_trunc));
subplot(2,1,2);
plot(x,y_trunc);
plot(x+2*pi,y_trunc);
plot(x-2*pi,y_trunc);

N_cut = 20;
y_hat_trunc(1:N_cut+1) = y_hat(1:N_cut+1);
y_hat_trunc(n-N_cut:n) = y_hat(n-N_cut:n);
y_trunc = real(ifft(y_hat_trunc));
subplot(2,1,2);
plot(x,y_trunc);
plot(x+2*pi,y_trunc);
plot(x-2*pi,y_trunc);

N_cut = 24;
y_hat_trunc(1:N_cut+1) = y_hat(1:N_cut+1);
y_hat_trunc(n-N_cut:n) = y_hat(n-N_cut:n);
y_trunc = real(ifft(y_hat_trunc));
subplot(2,1,2);
plot(x,y_trunc);
plot(x+2*pi,y_trunc);
plot(x-2*pi,y_trunc);


subplot(2,1,1)
axis([-3*pi/2,3*pi/2,-1.3,1.3]);
title('S_nf for n=4,8,12','FontSize',16);
subplot(2,1,2)
axis([-.7,.7,-1.3,1.3]);
title('S_nf for n=4,8,12,16,20,24','FontSize',16);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

y_left = (x_left+pi).*(x_left-1/2);
y_right = -(x_right+1/2).*(x_right-pi);
for j=1:length(x)
    if x(j) <=0
        y(j) = (x(j)+pi)*(x(j)-1/2);
    else
        y(j) = -(x(j)+1/2)*(x(j)-pi);
    end
end

y_hat = fft(y);
N_cut = 4;
clear y_hat_trunc
y_hat_trunc(1:N_cut+1) = y_hat(1:N_cut+1);
y_hat_trunc(n-N_cut:n) = y_hat(n-N_cut:n);
y_trunc = real(ifft(y_hat_trunc));
figure(2)
clf
subplot(2,1,1)
plot(x,y_trunc);
hold on
plot(x_left,y_left);
plot(x_right,y_right);
plot(x+2*pi,y_trunc);
plot(x_left+2*pi,y_left);
plot(x_right+2*pi,y_right);
plot(x-2*pi,y_trunc);
plot(x_left-2*pi,y_left);
plot(x_right-2*pi,y_right);
subplot(2,1,2)
plot(x,y_trunc);
hold on
plot(x_left,y_left);
plot(x_right,y_right);
plot(x+2*pi,y_trunc);
plot(x_left+2*pi,y_left);
plot(x_right+2*pi,y_right);
plot(x-2*pi,y_trunc);
plot(x_left-2*pi,y_left);
plot(x_right-2*pi,y_right);

N_cut = 8;
y_hat_trunc(1:N_cut+1) = y_hat(1:N_cut+1);
y_hat_trunc(n-N_cut:n) = y_hat(n-N_cut:n);
y_trunc = real(ifft(y_hat_trunc));
subplot(2,1,1)
plot(x,y_trunc);
plot(x+2*pi,y_trunc);
plot(x-2*pi,y_trunc);
subplot(2,1,2)
plot(x,y_trunc);
plot(x+2*pi,y_trunc);
plot(x-2*pi,y_trunc);

N_cut = 12;
y_hat_trunc(1:N_cut+1) = y_hat(1:N_cut+1);
y_hat_trunc(n-N_cut:n) = y_hat(n-N_cut:n);
y_trunc = real(ifft(y_hat_trunc));
subplot(2,1,1)
plot(x,y_trunc);
plot(x+2*pi,y_trunc);
plot(x-2*pi,y_trunc);
subplot(2,1,2)
plot(x,y_trunc);
plot(x+2*pi,y_trunc);
plot(x-2*pi,y_trunc);

N_cut = 16;
y_hat_trunc(1:N_cut+1) = y_hat(1:N_cut+1);
y_hat_trunc(n-N_cut:n) = y_hat(n-N_cut:n);
y_trunc = real(ifft(y_hat_trunc));
subplot(2,1,2);
plot(x,y_trunc);
plot(x+2*pi,y_trunc);
plot(x-2*pi,y_trunc);

N_cut = 20;
y_hat_trunc(1:N_cut+1) = y_hat(1:N_cut+1);
y_hat_trunc(n-N_cut:n) = y_hat(n-N_cut:n);
y_trunc = real(ifft(y_hat_trunc));
subplot(2,1,2);
plot(x,y_trunc);
plot(x+2*pi,y_trunc);
plot(x-2*pi,y_trunc);

N_cut = 24;
y_hat_trunc(1:N_cut+1) = y_hat(1:N_cut+1);
y_hat_trunc(n-N_cut:n) = y_hat(n-N_cut:n);
y_trunc = real(ifft(y_hat_trunc));
subplot(2,1,2);
plot(x,y_trunc);
plot(x+2*pi,y_trunc);
plot(x-2*pi,y_trunc);

subplot(2,1,1)
axis([-pi,pi,-4,4]);
title('S_nf for n=4,8,12','FontSize',16);
subplot(2,1,2)
axis([-.7,.7,-4,4]);
title('S_nf for n=4,8,12,16,20,24','FontSize',16);

figure(1)
print -dps Gibbs1.ps
figure(2)
print -dps Gibbs2.ps


