function pie = pie_metric(pulse_response, L, flag)
%
% Function computes Ideal Equalizer penalty 
% written by Sudeep Bhoja, BigBear Networks, June 28, 2004
% 
% pulse_response = channel pulse response
% L              = Oversampling ratio of channel pulse response
% flag           = 1 for PIE_LE, 2 for PIE_DFE
%
% 				
fb = 10.3125e9; %NRZ Baud Rate
N = 8192; 	%Number of Frequency points
f = [0:fb/(2*N):fb/2];

% From Link budget
sigma = 10^((-17-2*6)/10);

mf_out = match_filter(pulse_response, L);

if(flag == 1)
	y = 1 ./ (abs(freqz(mf_out, 1, f, fb)) + sigma);
	pie = 0.5*10*log10(mean(y)); %0.5 for E2O
else
	y = log(1 ./ (abs(freqz(mf_out, 1, f, fb)) + sigma));
	pie = 0.5*10*log10(exp(mean(y))); %0.5 for E2O
end

return

function mf_out = match_filter(pulse_response, L)
%
% pulse_response = channel pulse response
% L              = Oversampling ratio of channel pulse response
%

[mf_output lags] = xcorr(pulse_response);
% Find t = nT
ll = find(lags == 0);
phase = mod(ll,L); 
if(phase == 0) 
	phase = L;
end

% Resample match filter output at T for given phase 
mf_out = 1/L*mf_output(phase:L:end);

return

