close all;

% filter prototypes

fp = 1e5;
wp = 2*pi*fp;

f = logspace(4, 7, 400);
w = 2*pi*f;
flin = linspace(0, 2*fp, 400);
wlin = 2*pi*flin;

[ N, wn ] = buttord(wp, 10*wp, 3, 80, 's');
[b, a] = butter(N, wn, 's');
but = tf(b, a);

[ N, wn ] = cheb2ord(wp, 10*wp, 3, 80, 's');
[b, a] = cheby2(N, 80, wn, 's');
cheby4 = tf(b, a);

[ N, wn ] = ellipord(wp, 3*wp, 5, 80, 's');
[b, a] = ellip(N, 5, 80, wn, 's');
elliptic = tf(b, a);

[b, a] = besself(4, 0.76*1.52*wp);
bessel4 = tf(b, a);

[b, a] = besself(6, 0.75*1.75*wp);
bessel6 = tf(b, a);

[b, a] = besself(8, 0.74*1.95*wp);
bessel8 = tf(b, a);

% allpass (from E. Swanson)

fp1 = 14523;    Q1 = 0.56707;
fp2 = 30495;    Q2 = 1.9341;

fp1 = 5*fp1;    fp2 = 5*fp2;
s = tf('s');

w1 = 2*pi*fp1;  w2 = 2*pi*fp2;

allpass1 = (1 - s/(w1*Q1) + (s/w1)^2) / (1 + s/(w1*Q1) + (s/w1)^2);
allpass2 = (1 - s/(w2*Q2) + (s/w2)^2) / (1 + s/(w2*Q2) + (s/w2)^2);
allpass = allpass1 * allpass2;
cheby4allpass = cheby4*allpass;

% simulate response to random input
figure;
Ts = 1/1e5;
H  = elliptic;
[ t, x, in, out ] = sim('L05_eye_sim', 15*Ts); % , simset('maxstep', Ts/10));
[tt, in]  = stepify(t, in);
plot(tt, in, t, out);                   % display results
legend('input', 'output');
fixfig;

% eye diagram;
figure;
Tinterval = 5*Ts;
H  = elliptic;
[ t, x, in, out ] = sim('L05_eye_sim', 500*Ts , simset('maxstep', Ts/40));
out = out / max(abs(out));

N = sum(t <= Tinterval);
while N > 2
    % plot N datapoints
    plot(t(1:N), out(1:N));
    hold on;
    % chop off already plotted data
    t = t(N:end);
    t = t - Tinterval;
    out = out(N:end);
    N = sum(t < Tinterval);
end
xlabel('Time');
title('Elliptic');
hold off;

% eye diagram;
figure;
Tinterval = 5*Ts;
H  = cheby4;
[ t, x, in, out ] = sim('L05_eye_sim', 500*Ts , simset('maxstep', Ts/40));
out = out / max(abs(out));

N = sum(t <= Tinterval);
while N > 2
    % plot N datapoints
    plot(t(1:N), out(1:N));
    hold on;
    % chop off already plotted data
    t = t(N:end);
    t = t - Tinterval;
    out = out(N:end);
    N = sum(t < Tinterval);
end
xlabel('Time');
title('4th Order Chebychev');
hold off;

% eye diagram;
figure;
Tinterval = 5*Ts;
H  = bessel4;
[ t, x, in, out ] = sim('L05_eye_sim', 500*Ts , simset('maxstep', Ts/40));
out = out / max(abs(out));

N = sum(t <= Tinterval);
while N > 2
    % plot N datapoints
    plot(t(1:N), out(1:N));
    hold on;
    % chop off already plotted data
    t = t(N:end);
    t = t - Tinterval;
    out = out(N:end);
    N = sum(t < Tinterval);
end
xlabel('Time');
title('4th Order Bessel');
hold off;

% eye diagram;
figure;
Tinterval = 5*Ts;
H  = cheby4allpass;
[ t, x, in, out ] = sim('L05_eye_sim', 500*Ts , simset('maxstep', Ts/40));
out = out / max(abs(out));

N = sum(t <= Tinterval);
while N > 2
    % plot N datapoints
    plot(t(1:N), out(1:N));
    hold on;
    % chop off already plotted data
    t = t(N:end);
    t = t - Tinterval;
    out = out(N:end);
    N = sum(t < Tinterval);
end
xlabel('Time');
title('4th Order Chebychev with 4th Order Allpass');
hold off;

% eye diagram;
figure;
Tinterval = 5*Ts;
H  = bessel8;
[ t, x, in, out ] = sim('L05_eye_sim', 500*Ts , simset('maxstep', Ts/40));
out = out / max(abs(out));

N = sum(t <= Tinterval);
while N > 2
    % plot N datapoints
    plot(t(1:N), out(1:N));
    hold on;
    % chop off already plotted data
    t = t(N:end);
    t = t - Tinterval;
    out = out(N:end);
    N = sum(t < Tinterval);
end
xlabel('Time');
title('8th Order Bessel');
hold off;

