Code covered by the BSD License  

Highlights from
correlation between two sequences

image thumbnail

correlation between two sequences

by

 

Program to find the correlation between two sequences without using the inbuilt xcorr function

test1.m
%% Program for autocorrelation between two sequences....
%% without using the inbuilt function xcorr

clc;
clear all;
close all;
%% Inputs
% You can specify the inputs or can take them through command window
% h= input ('enter the sequence h');
% x= input ('enter the sequence x');
h=[1 1 2 3 4];
x=[1 3 5 7 9 2 4 6 8];
pp=h;qq=x;
% Plot the inputs
% subplot(3,1,1); stem(h,'m'); title ('h');ylabel('amplitude')
% %
% subplot(3,1,2); stem(x,'b'); title ('x');ylabel('amplitude')
%% calculate the cross correlation
l1= length (h); % calculate length of sequence h
l2= length (x); % calculate length of sequence x
l=abs (l1-l2); % calculate difference in lengths of sequence
if (l1 > l2)
x= [x zeros(1,l)];
else if(l2 > l)
h= [h zeros(1,l)];
    end
end 
h= [h zeros(1, max (l1, l2))];
for shift= 0:max(l1,l2); 
new_x = [zeros(1, shift) x zeros(1, (max (l1 , l2))-shift)];
y(shift+1,:)= sum(h.* new_x);
end
y=y';
% Display the correlation sum
subplot(3,1,1); stem(pp,'m'); title ('h');ylabel('amplitude')
subplot(3,1,2); stem(qq,'b'); title ('x');ylabel('amplitude')
subplot(3,1,3); stem(y,'r'); title ('cross correlation'); xlabel('time');

Contact us