% myclockreccost.m: draw error surfaces for cluster variance performance
% in terms of chosen pulse shape.  Modifed version of clockrecDDcost.m
% from Software Receiver Design by Johnson, Sethares & Klein.

l=10;                       % 1/2 duration of pulse shape
beta=0.5;                   % rolloff for pulse shape
m=40;                       % evaluate at m different points
ps=srrc(l,beta,m);          % make srrc pulse shape
psrc=conv(ps,ps);           % convolve 2 srrc's to get rc
psrc=psrc(l*m+1:3*l*m+1);   % truncate to same length as ps
cost1=zeros(1,m);
cost2=zeros(1,m);
cost4=zeros(1,m);
n=50000;   % experimental performance
x=zeros(1,n);
for i=1:m                   % for each offset
  pt=psrc(i:m:end);         % rc is shifted i/m of a symbol
  for k=1:n                 % do it n times
    rd=pam(length(pt),4,5); % random 4-PAM vector
    x(k)=sum(rd.*pt);       % received data point w/ ISI
  end
  err=quantalph(x,[-3,-1,1,3])-x';    % quantize to alphabet
  cost1(i)=sum(abs(x))/length(x);     % performance function
  cost2(i)=sum(abs(x).^2)/length(x);  % performance function
  cost4(i)=sum(abs(x).^4)/length(x);  % performance function
end

offset=(0:m-1)/m;
figure;
hold on;
plot(offset, cost1/max(cost1));       % normalized cost
plot(offset, cost2/max(cost2));       % normalized cost
plot(offset, cost4/max(cost4));       % normalized cost
xlabel('timing offset \tau')
ylabel('value of normalized cost functions')
legend('Absolute value', 'Squared', 'Fourth-Order')
hold off;
