% - Fast Fourier Transform [8FFT] 
% Univ. of the Ryukyus, Okinawa, Japan

function [y,s1,s2,s3]=myfft8(x)
% internal buffer memory and output memory
s1 = zeros(8,1);
s2 = zeros(8,1);
s3 = zeros(8,1);
y  = zeros(8,1);
% stage 1
for n0=0:1
    for n1=0:1
        %k0=0
        s1(n0+2*n1+0+1)=x(n0+2*n1+1) + x(n0+2*n1+4+1);
        %k0=1
        s1(n0+2*n1+4+1)=(x(n0+2*n1+1) - x(n0+2*n1+4+1))*exp(-2j*pi/8*(n0+2*n1));
    end
end
% stage 2
for n0=0:1
    for k0=0:1
        %k1=0
        s2(n0+0+4*k0+1)=s1(n0+0+4*k0+1) + s1(n0+2+4*k0+1);
        %k1=1
        s2(n0+2+4*k0+1)=(s1(n0+0+4*k0+1) - s1(n0+2+4*k0+1))*exp(-2j*pi/4*n0);
    end
end
% stage 3
for k1=0:1
    for k0=0:1
        %k2=0
        s3(0+2*k1+4*k0+1)=s2(0+2*k1+4*k0+1) + s2(1+2*k1+4*k0+1);
        %k2=1
        s3(1+2*k1+4*k0+1)=s2(0+2*k1+4*k0+1) - s2(1+2*k1+4*k0+1);
    end
end
% reorder
for k0=0:1
   for k1=0:1
      for k2=0:1
         y(k0+2*k1+4*k2+1)=s3(k2+2*k1+4*k0+1);
      end;
   end;
end
end
