% Phase Distortion

clc             % clear the command window
clear           % clear all variables
close all       % close any existing windows
format compact	% prevent extra blank lines in the output

t = 0 : 0.01 : 2;
n = 1 : 50;

B(n) = 4 ./(pi*(2*n-1));
dt(n) = 0.05 * (1 - exp(-(2*n-1)));

sum = 0;
for n = 1 : 50
    sum = sum + B(n)*sin((2*n-1)*2*pi*(t-dt(n)));
end
F_lag = sum;
sum = 0;
for n = 1 : 50
    sum = sum + B(n)*sin((2*n-1)*2*pi*(t+dt(n)));
end
F_lead = sum;

% Plot square wave with high-frequency components lagging
plot (t, F_lag)
title ('high-frequency components lagging');
xlabel('t');
ylabel('F_lag(t)');

% Plot square wave with high-frequency components leading
figure      % open new figure window (to prevent previous from being lost)
plot (t, F_lead)
title ('high-frequency components leading');
xlabel('t');
ylabel('F_lead(t)');

disp 'Hit Enter to close all windows and quit'
pause
close all