DSP Correlation On Matlab
DSP Correlation On Matlab
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;
1. Now modify the phase of the second signal to pi/2 (it will make it cosine) and observe the
correlation at lag zero.
3. Check for auto-correlation (ph1 = ph2) that the lag zero value gives the m energy of the signal.
At phase 2 = pi/2:
The correlation at lag zero is: 4.9127e-15.
Signal x(n)
1
-1
0 5 10 15 20 25 30 35 40 45 50
amount of correlation
Time index (n) observed at lag 2
Signal y(n)
1 caused by 90 degrees
Amplitude
0 phase difference.
-1
0 5 10 15 20 25 30 35 40 45 50
Time index (n)
Correlated Output
Correlation
50
-50
Normalized Correlated Output
-1
-50 -40 -30 -20 -10 0 10 20 30 40 50
Lag index (l)
At phase 2 = pi:
Signal x(n)
1
Amplitude
0
Comments: The two
-1
0 5 10 15 20 25 30 35 40 45 50 signals correlates
Time index (n)
perfectly at lag 5 as both
Signal y(n)
1
the signals are at origin
Amplitude
0
at lag 5. The negative
-1
0 5 10 15 20 25 30 35 40 45 50 sign shows both the
Time index (n) signals are 180 degrees
Correlated Output
Correlation
50 apart.
0
-50
Normalized Correlated Output
-1
-50 -40 -30 -20 -10 0 10 20 30 40 50
Lag index (l)
At Phase 1 = Phase 2:
Signal x(n)
1
Amplitude
0
Comments: Both the
-1
0 5 10 15 20 25 30 35 40 45 50 signals are similar so
Time index (n)
the correlates perfectly
Signal y(n)
1
at any lag.
Amplitude
-1
0 5 10 15 20 25 30 35 40 45 50
Time index (n)
Correlated Output
Correlation
50
-50
Normalized Correlated Output
-1
-50 -40 -30 -20 -10 0 10 20 30 40 50
Lag index (l)
10
origin_x = 1;
nx = [1:length(x)]-origin_x; 0
xlabel('Lag index'); 10
ylabel('Correlated rxy 0
Output') -10
title('Correlation')
-20
grid on;
subplot(2,1,2) -30
-50 -40 -30 -20 -10 0 10 20 30 40 50
stem(c2,ryx,'filled','g') Lag index
Tasks Lab Session 4
Comments: Although correlation looks like
doing convolution but it does not hold the
xlabel('Lag index');, commutative property as can be seen in
ylabel('Correlated ryx Output')
output waveforms
title('Correlation')
sgtitle('Commutative doesnt hold')
grid on;
5. Modify the code, such that the correlation is obtained using convolution command.
% rxy(n)=x(n)*y(-n)
clc;clear all;close all;
n = [0:49]; 30
Using Convolution
ph1 = 0;
20
ph2 = 0;
Correlated Output
x = sin(2*pi*0.1*n + ph1); 10
origin_x = 1; 0
nx = [1:length(x)]-origin_x;
-10
y = sin(2*pi*0.1*n + ph2);
origin_y = 1; -20
ny = [1:length(y)]-origin_y; -30
z=fliplr(y); -50 -40 -30 -20 -10 0 10 20 30 40 50
Lag index
rxy_1=conv(x,z);
[rxy_2 l]=xcorr(x,y); 30
Using Correlation
subplot(2,1,1)
20
stem(l,rxy_1,'filled','k')
Correlated Output
xlabel('Lag index'); 10
ylabel('Correlated Output') 0
title('Using Convolution')
-10
grid on;
subplot(2,1,2) -20
stem(l,rxy_2,'filled','m') -30
-50 -40 -30 -20 -10 0 10 20 30 40 50
xlabel('Lag index');
Lag index
ylabel('Correlated Output')
title('Using Correlation') Comments: The correlation code is modified using a flip
sgtitle('Correlation using Convolution') command in the code. So the correlation becomes
grid on;
exactly like convolution as can be veified from the output
6. Calculate correlation between voltages of any two phases of a 10HP motor using the data given
below. First use Ms. Excel to copy data and then calculate correlation.
clc;clear all;close all;
[numbers, strings, raw] = xlsread('D:\Quickaccess\
5th sem\DSP\Lab4Task6');
numbers_1 = numbers(:,1);
numbers_3 = numbers(:,3);
org_num1 = 1;
num1_x = [ 1:length(numbers_1) ] - org_num1 ;
orgin_num3 = 1;
num3_x = [ 1:length(numbers_3) ] - orgin_num3 ;
[values,indices] = xcorr(numbers_1,numbers_3);
[maxVal,position] = max(values);
disp(['The correlation at lag zero is: '
num2str(values(indices==0)) '.']);
disp(['The maximum correlation is at lag '
num2str(indices(position)) '.']);
normalized_correlation=values/max(abs(values));
percent_correlation=normalized_correlation*100;
Tasks Lab Session 4
Comments: Correlation between the phase A and phase C was observed. The correlation is
maximum at starting indices but decreases as we move higher up the order.