Experiment: Correlation
Code: Autocorrelation
clc;
close all;
clear all;
% two input sequences
x = input ('enter input')
subplot(1,2,1);
stem(x);
xlabel('n');
ylabel('x(n)');
title('Input Sequence');
% auto correlation of input sequence
z=xcorr(x,x);
disp('The values of z are =');disp(z);
subplot(1,2,2);
stem(z);
xlabel('n');
ylabel('z(n)');
title('auto correlation of input sequence');
command window
Output:
Code: cross correlation by convolution
clc;
clear all;
close all;
function [y,n] = sigshift(x,n1,n0)
n=n1+n0;
y=x;
end;
function[y,n]=sigfold(x,m)
n=-fliplr(m);
y=fliplr(x);
end;
function [y,k] = convolution_sum(nx, x, nh, h)
kmin = min(nx)+min(nh);
kmax = max(nx)+max(nh); % same as the kmin
k = kmin:kmax; % index vector k
y = conv(x,h); % convolution of x and h
end
nx = 0:15; % defining time index
x = 0.05*nx.^2; %defining a signal n
% y = x[n−5]
n0 = 5;
[y, ny] = sigshift(x, nx, n0);
% calculating cross−correlation using convolution
 % rxy = y[n]*x[−n]
[x, nx] = sigfold(x, nx);
[rxy, nxy] = convolution_sum(nx, x, ny, y);
stem(nxy, rxy);
Output: