
figure(1)
clf;
set(gcf,'doublebuffer','on')
clear all;

%grid size
n = 128;
%time steps
nt = 60;
%Generate a random frequency domain grid
s = sum(100*clock);
rand('seed',s);
freq = (rand(n,n,nt)-.5) + i* (rand(n,n,nt)-.5);  
%and zero the DC level
freq (n/2,n/2,nt/2) = 0;

%lowest spatial frequency -- units are cycles/frame width
lowcut = 4;
%highest spatial frequency
hicut = 10;
%time bandwidth
tcut = 3 ;
freq(:,:,1:nt/2-tcut) = 0; 
freq(:,:,nt/2+tcut:nt) = 0; 

% make a matrix with entries proportional to the freq
%for use in filtering in freq domain
[x,y] = meshgrid(1:n,1:n);
dist = sqrt( (x-n/2).^2 + (y-n/2).^2 ) ;
    
for j=1:nt
    %band-pass filter the spatial frequency
    freq(:,:,j) = freq(:,:,j) .* ( (dist>=lowcut) & (dist<=hicut) );
end

freq = fftshift(freq);

%3D inverse FFT
frames = abs(ifftn(freq));

for j=1:nt
    %and scale it to 0-1 range
    surface = frames(:,:,j);
    surface = (surface - min(min(surface)))/(max(max(surface)) - min(min(surface)));
    
    %Convert to a rgb (truecolor) image
    dis = cat(3,surface, surface, surface);
    %display the grayscale image
    imshow(dis);
    truesize(gcf);
    colormap(gray);
    mov(j) = getframe; 
%     imwrite(dis,...
%         ['c:\my documents\bigdata\FourierMovie\frame',...
%             num2str(j,'%03d'),'.tif'])
end
    
movie2avi(mov,'c:\my documents\bigdata\FourierMovie1.avi','fps',30,...
    'compression','cinepak', 'quality',100)

%figure(2):clf
%movie(mov,5,30);