Professional Documents
Culture Documents
EE-20141
LAB SESSION 4
To study discrete-time correlation and apply it to real data to observe the correlation between two signals.
n = [0:49];
ph1 = 0; ph2 = 0;
x = sin(2*pi*0.1*n + ph1);
origin_x = 1;
nx = [1:length(x)]-origin_x;
y = sin(2*pi*0.1*n + ph2);
origin_y = 1; ny = [1:length(y)]-origin_y;
norm_corr = rxy/max(abs(rxy));
perct_corr = norm_corr*100;
figure;
subplot(4,1,1);
stem(nx,x,'filled','b');
xlabel('Time index (n)'); ylabel('Amplitude');
xlim([nx(1)-1 nx(end)+1]);
title('Signal x(n)'); grid
subplot(4,1,2);
stem(ny,y,'filled','r');
xlabel('Time index (n)'); ylabel('Amplitude');
xlim([ny(1)-1 ny(end)+1]);
title('Signal y(n)'); grid;
subplot(4,1,3);
stem(l,rxy,'filled','k');
xlabel('Lag index (l)'); ylabel('Correlated Output');
title('Correlation'); grid;
subplot(4,1,4);
stem(l,norm_corr,'filled','k');
xlabel('Lag index (l)');
ylabel('Normalized Correlated Output');
title('Normalized Correlation');
grid;
KINZA PERVEZ
EE-20141
LAB SESSION 4
TASK#1:
Changing Line 4 only from the above code:
ph1 = 0; ph2 = pi/2;
KINZA PERVEZ
EE-20141
LAB SESSION 4
OBSERVATION:
TASK#2:
Changing Line 4 only from the above code:
ph1 = 0; ph2 = pi;
OBSERVATION:
TASK#3:
The term auto in autocorrelation refers to the correlation of the signal with itself, instead of being
correlated with some other data or signal.
To achieve the similarity, we can put ph1 = ph2:
Changing Line 4 only from the above code:
ph1 = pi/2; ph2 = pi/2;
KINZA PERVEZ
EE-20141
LAB SESSION 4
OBSERVATION:
TASK#4:
clear all; close all;clc;
n = [0:49];
ph1 = 0; ph2 = pi/2;
x = sin(2*pi*0.1*n + ph1);
origin_x = 1; nx = [1:length(x)]-origin_x;
y = sin(2*pi*0.1*n + ph2);
origin_y = 1; ny = [1:length(y)]-origin_y;
%L.H.S.
[rxy lxy]=xcorr(x,y);
[maxR1 indR1] = max(rxy);
disp(['The correlation for x and y at lag zero is:'num2str(rxy(lxy==0))'.']);
disp(['The maximum correlation is at lag ' num2str(lxy(indR1)) '.']);
norm_corr1=rxy/max(abs(rxy));
perct_corr1=norm_corr1*100;
%R.H.S.
[ryx lyx]=xcorr(y,x);
[maxR2 indR2] = max(ryx);
disp(['The correlation for y and x at lag zero is:'num2str(ryx(lyx==0))'.']);
KINZA PERVEZ
EE-20141
LAB SESSION 4
norm_corr2=ryx/max(abs(ryx));
perct_corr2=norm_corr2*100;
figure
subplot(3,2,1)
stem(nx,x,'filled','b')
xlabel('Time index (n)'); ylabel('Amplitude');
xlim([nx(1)-1 nx(end)+1]);
title('Signal x(n)'); grid;
subplot(3,2,2)
stem(ny,y,'filled','r');
xlabel('Time index (n)'); ylabel('Amplitude');
xlim([ny(1)-1 ny(end)+1]);
title('Signal y(n)'); grid;
subplot(3,2,3)
stem(lxy,rxy,'filled','r');
xlabel('Lag index (lxy)'); ylabel('Correlated Output');
title('Correlation b/w x and y'); grid;
subplot(3,2,4)
stem(lyx,ryx,'filled','b');
xlabel('Lag index (lyx)'); ylabel('Correlated Output');
title('Correlation b/w y and x'); grid;
subplot(3,2,5)
stem(lxy,norm_corr1,'filled','b');
xlabel('Lag index (lxy)'); ylabel('Normalized Correlated Output')
title('Normalized Correlation');grid;
subplot(3,2,6)
stem(lyx,norm_corr2,'filled','r');
xlabel('Lag index (lyx)'); ylabel('Normalized Correlated Output');
title('Normalized Correlation'); grid;
KINZA PERVEZ
EE-20141
LAB SESSION 4
OBSERVATION:
TASK#5:
clear all; close all; clc;
n = [0:49];
ph1 = 0; ph2 = pi/2;
x = sin(2*pi*0.1*n + ph1);
origin_x = 1;
nx = [1:length(x)]-origin_x;
y = sin(2*pi*0.1*n + ph2);
origin_y = 1;
ny = [1:length(y)]-origin_y;
z = fliplr(y);
origin_z = 1;
nz = [1:length(z)]-origin_z;
rxz =conv(x,z);
nrxz = [nx(1)+nz(1): nx(end)+nz(end)];
figure
subplot(3,1,1)
stem(nx,x,'filled','b');
xlabel('Time index (n)');
ylabel('Amplitude');
xlim([nx(1)-1 nx(end)+1]);
title('Signal x(n)'); grid;
subplot(3,1,2),
stem(nz,z,'filled','r');
xlabel('Time index (n)');
ylabel('Amplitude')
xlim([nz(1)-1 nz(end)+1])
title('Signal z(n)'); grid
subplot(3,1,3)
stem(nrxz,rxz,'filled','k')
xlabel('Lag index (l)');
ylabel('Correlated Output')
title('Correlation'); grid
KINZA PERVEZ
EE-20141
LAB SESSION 4
OBSERVATION:
Correlation can be accomplished through the convolution operation, which involves first folding y(n) and
then convolution with x (n).
TASK#6:
filename = ('voltages.xlsx');
sheet = 1;
VoltageA = 'A1:A26'; VoltageB = 'B1:B26' VoltageC = 'C1:C26';
A = xlsread(filename,sheet,VoltageA)
A = A.'; nA= [0:length(A)-1];
B = xlsread(filename,sheet,VoltageB)
B = B.'; nB= [0:length(B)-1];
C = xlsread(filename,sheet,VoltageC)
C = C.'; nC= [0:length(C)-1];
x1=input('Select data: 1 for VoltageA, 2 for VoltageB, 3 for VoltageC: ');
x2=input('Select data: 1 for VoltageA, 2 for VoltageB, 3 for VoltageC: ');
y=0;
if x1==1 && x2==1
[rxy l]=xcorr(A,A); [maxR indR] = max(rxy);
disp(['The correlation at lag zero is: ' num2str(rxy(l==0)) '.']);
disp(['The maximum correlation is at lag ' num2str(l(indR)) '.']);
elseif x1==1 && x2==2
[rxy l]=xcorr(A,B); [maxR indR] = max(rxy);
disp(['The correlation at lag zero is: ' num2str(rxy(l==0)) '.']);
disp(['The maximum correlation is at lag ' num2str(l(indR)) '.']);
elseif x1==1 && x2==3
[rxy l]=xcorr(A,C); [maxR indR] = max(rxy);
dips(['The correlation at lag zero is: ' num2str(rxy(l==0)) '.']);
disp(['The maximum correlation is at lag ' num2str(l(indR)) '.']);
elseif x1==2 && x2==1
[rxy l]=xcorr(B,A); [maxR indR] = max(rxy);
disp(['The correlation at lag zero is: ' num2str(rxy(l==0)) '.']);
disp(['The maximum correlation is at lag ' num2str(l(indR)) '.']);
elseif x1==2 && x2==2
KINZA PERVEZ
EE-20141
LAB SESSION 4