function tf = tfdist_af(sig)
% tfdist_af -- Alias-Free Generalized Discrete Time-Frequency Distribution
%  Usage
%    aftf = tfdist_af(sig)
%  Inputs
%    sig     1-d signal of dyadic length n=2^J
%  Outputs
%    aftf    complex-valued matrix representing the alias-free generalized
%            time-frequency distribution of zero-extended signal with rows
%            corresponding to frequencies and columns corresponding to times
%
%  Side Effects
%    Image Plot of the alias free TF distribution
%
%  See Also
%   WigDist, AFWigDist, specgram, imagephaseplane
%
%  References
%   Jechang Jeong and William J. Williams,
%   "Alias-Free Generalized Discrete-Time Time-Frequency Distribution
%   IEEE Transactions on Signal Processing", vol. 40, pp. 2757-2765
%

sig = sig(:);
n   = length(sig);
f   = [zeros(n,1); sig; zeros(n,1)];
aftf = zeros(n);
zerosn = zeros(n,1);

for t=1:n,
    x = zerosn;
    for k = 0:(n/2-1),
        %even indices
        if k == 0,
            x(2*k+1) = f(n + t) * f(n+t);
        else
            ix = (-k):k;
                tplus    = n + t + k - ix;
            tminus   = n + t - k - ix;
            ff = f(tplus) .* f(tminus);
            b = 1/((2*k+1)*.54 + .46 * ...
                real((exp(i*2*pi*(k+1)/(2*k))-exp(-i*pi))/ ...
                (1 - exp(i*pi/k))));
            x(2*k+1) = b*(.54+.46*cos(2*pi*ix/(2*k))) * ff;
        end

        %odd indices
        ix = (-k):(k+1);
        tplus = n + t + k - ix + 1;
        tminus   = n + t - k - ix;
        ff = f(tplus) .* f(tminus);
        b = 1/((2*k+2)*.54 + .46 * ...
            real((exp(i*2*pi*(k+2)/(2*k+1)) ...
            -exp(-i*2*pi*k/(2*k+1))) * exp(-i*pi/(2*k+1))/ ...
            (1 - exp(i*2*pi/(2*k+1)))));
        x(2*k+2) = b*(.54+.46*cos(2*pi*(ix-.5)/(2*k+1)))*ff;
    end
    aftf(:,t) = 2 * real(fft(x));
end

abstf = abs(aftf);
tfmax = max(max(abstf));
tfmin = min(min(abstf));
colormap(1-gray(256))
%colormap(jet)
image(linspace(0,1,n),linspace(0,1,n),256*(abstf-tfmin)/(tfmax-tfmin));
axis('xy')
%title('Alias Free Time-Frequency Distribution');
xlabel('Time')
ylabel('Frequency')

%
% Copyright (c) 1994-5, Shaobing Chen
%
    
    
%   
% Part of WaveLab version .701
% Built Tuesday, January 30, 1996 8:25:59 PM
% This is Copyrighted Material
% For copying permissions see copying.m
% Comments? e-mail wavelab@playfair.stanford.edu
%   
    
   
% Auto name mapping to DOS conventions  Wednesday, January 31, 1996 5:36:19 PM
