Pages

Friday, December 10, 2010

MATLAB PROGRAM FOR CROSS-CORRELATION (DIGITAL SIGNAL PROCESSING)


x1=input('Enter first seq=');
x2=input('Enter second seq=');
n1=length(x1);
n2=length(x2);
N=max(n1,n2);
%padding zeros at the end of smaller sequence
if n2>n1
    x1=[x1 zeros(1,N-n1)];
    x3=x2;x2=x1;x1=x3;
elseif (n1>n2)
    x2=[x2 zeros(1,N-n2)];
end
x3=x2;
y=[];
%shifting and multiplying
for n=1:N
    y(n)=0;
    for j=1:N
        y(n)=y(n)+x1(j)*x2(j);
    end
    x=x2(N);
   for i=N-1:-1:1%loop for shifting and adding zeros
       x2(i+1)=x2(i);
   end
   x2(1)=0;
   y(n)=y(n)/N
   
end
n=1:N; %plotting crosscorrelation output
disp('output seq of cross corr=');
disp(y);
pause;
stem(n,y);
grid on;
grid minor;
xlabel('time index n');
ylabel('amplitude y');
title('cross correlation of sequence x1 and x2');

No comments:

Post a Comment