i want to speed up my code for Fourier Transform .how can i do it??
Show older comments
I Am taking selective frames from a video and applying 3D FFT. I want to reduce the execution time of the 3D FFT & IFFT. How can I achieve this, e.g. by using vectorization or preallocation?
i have added my code
%take selective frames from a video
for
t=1:100:T; %T is no of frames
f1= read(video,t); %read frames from video file video
f2=rgb2gray(f1);
I(:,:,t)=f2; %I is for storing database of each frame
end
%apply 3-d Fourier Transform on frames
Phase=zeros(400,400,20);%memory preallocate
mag=zeros(400,400,20);%memory preallocate
Phasestruct=zeros(400,400,20);%memory preallocate
magstruct=zeros(400,400,20);%memory preallocate
for s=1:100:T
for u=1:r1 %r1 and c1 are dimensions of frame
for v=1:c1
dft=0;
mag(u,v,s)=0;
phase(u,v,s)=0;
for t=1:100:T
for x=1:r1
for y=1:c1
v1=exp(((-1j)*2*pi*(x-1)*(u-1))/r1);
v2=exp(((-1j)*2*pi*(y-1)*(v-1))/c1);
v3=exp(((-1j)*2*pi(t-1)*(s-))/(T-31));
dft=dft+ I(x,y,t)*((v1)*(v2)*(v3))/(r1*c1*(T));
dft1=(fftshift(dft));
mag(u,v,s)=mag(u,v,s)+log(1+abs(dft1));%magnitude spectrum
phase(u,v,s)=phase(u,v,s)+exp(1j*angle(dft));%phase spectrum
end
end
end
end
end
magstruct(s)=struct('magnitude',mag(:,:,s));%use of structure for storing magnitude data
phasestruct(s)=struct('phase',phase(:,:,s));%use of structure for storing phase data
end
%reconstruct frames from phase spectrum only using 3-d Inverse Fourier Transform
idft=zeros(400,400,20);%memory preallocate
idft2=zeros(400,400,20); %memory preallocate
reconstruct=zeros(1000)%memory preallocate
for t=1:100:T
for x=1:r1
for y=1:c1
idft(x,y,t)=0;
for s=1:100:T
for u=1:r1
for v=1:c1
z1=exp(((1j)*2*pi*(x-1)*(u-1))/r1);
z2=exp(((1j)*2*pi*(y-1)*(v-1))/c1);
z3=exp(((1j)*2*pi*(t-1)*(s-1))/(T));
idft(x,y,t)=idft(x,y,t)+ (phasestruct(s).phase(u,v))*((z1)*(z2)*(z3)); %as i will reconstruct frames from phase spectrum only
idft2(x,y,t)=idft(x,y,t).*conj(idft(x,y,t));
end
end
end
end
end
reconstruct(t)=struct('reconstructed',idft(:,:,t));
end
/* 1.when i run it ,it takes almost 3-4 hours for getting output results..
2.when i used direct matlab function "fftn" instead of six "for" loops for 3d fft formula, it gives error as "unsupported dimensions" at line "f1= read(video,t);".
3.when i used memory preallocation it gives error at line " phasestruct(s)=struct('phase',phase(:,:,s));" as "cannot covert from double to structure".
so i'm totally confused..so please help me so that i can get output results within few minutes .......thanks */
2 Comments
dpb
on 22 Apr 2014
Please format your code legibly first so can read it...HINT: don't put blank lines between code lines--that starts the wyswig formatting over again.
Suggestions
1) Use the profiler to find the bottlenecks
2) Show the error on preallocation in context; don't make us guess...
ramdas patil
on 23 Apr 2014
Accepted Answer
More Answers (0)
Categories
Find more on Spectral Measurements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!