Monday, October 26, 2009

Convolution Matlab Code with FFT and IFFT

Matlab has own convolution code; but this code is use the fft and ifft and thanks to Fourier Transform properties make own convolution algorithm.

function y=newconv(x,h)
sizeX=size(x);
if(sizeX(1)>sizeX(2))
    x=x';
end
sizeH=size(h);
if(sizeH(1)>sizeH(2))
    h=h';
end
M=length(x);
N=length(h);
y=zeros(1,M+N-1);
NewLength=M+N-1;

fftx=fft(x,NewLength);
ffth=fft(h,NewLength);
k=fftx.*ffth;
y=ifft(k);
plot(y,'*');hold;
plot(conv(x,h),'red');
legend('newconv','conv')

No comments:

Post a Comment