MATLAB: circular convolution using DFT

Q=Find the circular convolution of the sequences S1(n) = [1, 2,1, 2] and S2(n) = [3, 2, 1, 4]; Verify the result using DFT method.

x1=[1, 2,1, 2];
x2=[3, 2, 1, 4];
c=fcconv(x1,x2);
subplot(211);
stem(c);grid;
title('Direct');
X1=fft(x1);
X2=fft(x2);
X=X1.*X2;
x=ifft(X);
subplot(212);
stem(x);grid;
title('using DFT');

Function




function [c]=fcconv(x1,x2)
if length(x1)>length(x2)
    x2=[x2, zeros(1,length(x1)-length(x2))];
else
    x1=[x1, zeros(1,length(x2)-length(x1))];
end
l=length(x1);
n=1:l;
y(n)=0;
for k=1:l
p=(mod(n-k,l))+1;
y(n)=y(n)+ x1(k)*x2(p);
end
c=y;

0 comments: