0% found this document useful (0 votes)
9 views

DSP Correlation On Matlab

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

DSP Correlation On Matlab

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab 4 Tasks

Code for tasks 1-3:


clear all;
close all;
clc;
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;
[rxy l]=xcorr(x,y);
[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)) '.']);
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;

1. Now modify the phase of the second signal to pi/2 (it will make it cosine) and observe the
correlation at lag zero.

2. Modify the phase again to ‘pi’ and observe.

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.

The maximum correlation is at lag 2.


Tasks Lab Session 4

Signal x(n)
1

Amplitude Comments: Very little


0

-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

-50 -40 -30 -20 -10 0 10 20 30 40 50


Lag index (l)
Normalized Correlation
1

-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

-50 -40 -30 -20 -10 0 10 20 30 40 50


Lag index (l)
Normalized Correlation
1

-1
-50 -40 -30 -20 -10 0 10 20 30 40 50
Lag index (l)

The correlation at lag zero is: -25.

The maximum correlation is at lag 5.


Tasks Lab Session 4

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

-50 -40 -30 -20 -10 0 10 20 30 40 50


Lag index (l)
Normalized Correlation
1

-1
-50 -40 -30 -20 -10 0 10 20 30 40 50
Lag index (l)

The correlation at lag zero is: 25.

The maximum correlation is at lag 0.

4. Observe that the commutative property does not hold.


%rxy!=ryx
clear all;close all;clc;
n = [0:49];
phi_1 = 0; Correlation
30
phi_2 = 0; 20
x = sin(2*pi*0.1*n + phi_1);
Correlated rxy Output

10
origin_x = 1;
nx = [1:length(x)]-origin_x; 0

y = cos(2*pi*0.1*n + phi_2); -10


origin_y = 1; -20
ny = [1:length(y)]-origin_y;
-30
[rxy , c1]=xcorr(x,y); -50 -40 -30 -20 -10 0 10 20 30 40 50
[ryx , c2]=xcorr(y,x); Lag index
figure, Correlation
30
subplot(2,1,1)
20
stem(c1,rxy,'filled','g')
Correlated ryx Output

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

subplot(5,1,1),stem(num1_x,numbers_1,'filled','k'),xlabel('Time index (n)');


ylabel('Voltage'),xlim([num1_x(1)-1 num1_x(end)+1]),
title('Voltage A'),grid on;
subplot(5,1,2),stem(num3_x,numbers_3,'filled','r'), xlabel('Time index (n)');
ylabel('Voltage'),xlim([num3_x(1)-1 num3_x(end)+1]),title('Voltage C')
grid on;
subplot(5,1,3),stem(indices,values,'filled','g'),xlabel('Indices');
ylabel('Correlation Output'),xlim([indices(1)-1 indices(end)+1])
title('Correlation'), grid on;
subplot(5,1,4), stem(indices,normalized_correlation,'filled','b');
xlabel('Indices'), ylabel('Normalized Correlation Output')
xlim([indices(1)-1 indices(end)+1]), title('Normalized Correlation')
grid on;
subplot(5,1,5), stem(indices,percent_correlation,'filled','Color',[0 1 1]);
xlabel('Indices'), ylabel('Percentage Correlation Output')
xlim([indices(1)-1 indices(end)+1]), title('Percentage Correlation')
grid on;

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.

You might also like