% 
% Aliasing Reduction in Clipped Signals
% Fabian Esqueda, Stefan Bilao and Vesa Välimäki
% Aalto University, Dept. of Signal Processing and Acoustics
% 
% MATLAB code used to implement Fig. 7
% 
% August 2015
% 
%%
close all; clear all; clc;

% INITIAL SETUP
step = 1/1000;
d = 0:step:1-step;
dl = length(d);
z = zeros(1,4*dl);

% SETUP FIGURE
figure(1)
set(1, 'Color', 'w');
set(1,'units','normalized','outerposition',[0 0 0.38 0.6])
fontsize = 16;
fontname = 'Times';
markersize = 16;
linewidth = 2;


% PLOYSINC
polySINC_L = d;
polySINC_R = -d + 1;
polySINC = [polySINC_L polySINC_R];

% PLOT POLYSINC
subplot(2,2,1), plot(dl+1:3*dl,polySINC, 'k','LineWidth',2);
set(gca,'fontsize',fontsize,'fontname',fontname);
xlabel({'Time (s)','(a)'}');
set(gca,'XTick', [1, dl 2*dl 3*dl 4*dl]); 
set(gca,'XTicklabel',{'-2T', '-T', '0', 'T', '2T'});
set(gca,'YTick', [0, 0.5 1]); 
set(gca,'YTicklabel',{'0', '0.5', '1'});
axis([1 2*length(polySINC) -0.2 1.2])
xL = xlim;
line(xL, [0 0], 'color','k');  %x-axis

% PLOYBLEP
polyBLEP_L = d.^2/2;
polyBLEP_R = -d.^2/2 + d + 1/2;
polyBLEP = [polyBLEP_L polyBLEP_R];

% PLOT POLYBLEP
subplot(2,2,2), plot(dl+1:3*dl,polyBLEP, 'k','LineWidth',2);
set(gca,'fontsize',fontsize,'fontname',fontname);
xlabel({'Time (s)','(b)'}');
set(gca,'XTick', [1, dl 2*dl 3*dl 4*dl]); 
set(gca,'XTicklabel',{'-2T', '-T', '0', 'T', '2T'});
set(gca,'YTick', [0, 0.5 1]); 
set(gca,'YTicklabel',{'0', '0.5', '1'});
axis([1 2*length(polySINC) -0.2 1.2])
xL = xlim;
line(xL, [0 0], 'color','k');  %x-axis

% PLOYBLAMP
polyBLAMP_L = d.^3/6;
polyBLAMP_R = -(d.^3/6) + (d.^2/2) + (d/2) + (1/6);
polyBLAMP = [polyBLAMP_L polyBLAMP_R];

% PLOT POLYBLAMP & TRIVIAL RAMP
subplot(2,2,3), plot(dl+1:3*dl,polyBLAMP, 'k','LineWidth',2), hold on;
set(gca,'fontsize',fontsize,'fontname',fontname);
set(gca,'XTick', [1, dl 2*dl 3*dl 4*dl]); 
set(gca,'XTicklabel',{'-2T', '-T', '0', 'T', '2T'});
set(gca,'YTick', [0, 0.5 1]); 
set(gca,'YTicklabel',{'0', '0.5', '1'});
xlabel({'Time (s)','(c)'}');
axis([1 2*length(polySINC) -0.2 1.2]);
xL = xlim;
line(xL, [0 0], 'color','k');  %x-axis
% TRIVIAL RAMP
line([2000 3200],[0 1.2],'color','k','LineWidth',2,'LineStyle','--');
line([1 2000],[0 0],'color','k','LineWidth',2,'LineStyle','--');


% POLYBLAMP RESIDUAL
polyBLAMP_L = d.^3/6;
polyBLAMP_R = -(d.^3/6) + (d.^2/2) - (d/2) + (1/6);
polyBLAMP = [polyBLAMP_L polyBLAMP_R];

% PLOT POLYBLAMP RESIDUAL
subplot(2,2,4), plot(dl+1:3*dl,polyBLAMP, 'k','LineWidth',2);
set(gca,'fontsize',fontsize,'fontname',fontname);
set(gca,'XTick', [1 dl 2*dl 3*dl 4*dl]); 
set(gca,'XTicklabel',{'-2T', '-T', '0', 'T', '2T'});
set(gca,'YTick', [0, 1/6]); 
set(gca,'YTicklabel',{'0', '1/6'});
xlabel({'Time (s)','(d)'}');
axis([1 2*length(polySINC) -0.03 0.2])
xL = xlim;
line(xL, [0 0], 'color','k');  %x-axis

% print -deps2 Fig-polyBLAMP-steps-N1

