%-----------------------------------------------------------------
%     *** 2.161 Signal Processing - Continuous and Discrete  ***
%                      Tutorial MATLAB Function
%
%   fftx -  Tutorial FFT routine to demonstrate the Radix-2 FFT with
%              decimation in time.
%
%   Source:  Class handout: The Fast Fourier Transform
%
%   Usage:   Fout = fftx(f, k)
%            where  f  - time domain data set ()real or complex
%                   k  - size of the data set k = log_2(N)
%
%   Author:   D. Rowell
%   Revision: 1.0 10-1-2007
%
%---------------------------------------------------------------
%
function Fout = fftx(f,ksize)
N = 2^ksize;
% Move the input data to the output array
Fout=f;
%
% Perform the "bit-reversed" re-ordering of the input data
%
MR = 0;
for M = 1:N-1
    L = N/2;
    while MR + L > N-1
       L = L/2;
    end
    MR = mod(MR,L) + L;
    if MR >= M
        temp       = Fout(M+1);   %swap the data points
        Fout(M+1)  = Fout(MR+1);
        Fout(MR+1) = temp;
    end
end
%
% Now perform the column operations
%
L = 1;
while L < N
    ISTEP = 2*L;
    for K = 1:L
        W = exp(-i*pi*(K-1)/L);
         for P = K:ISTEP:N
             Q = P + L;       % P & Q are the two points to be updated
             WBm     = W*Fout(Q);  
             Fout(Q) = Fout(P) - WBm;  % Q identifies the "odd" block.
             Fout(P) = Fout(P) + WBm;  % P identifies "even" block,
         end
    end
         L = ISTEP;
end