Saturday, February 20, 2010

Correlation and Convolution

**************************************
%covolution of two sequences &
%comparison with conv command
**************************************
x=[1 4 2 4 1 1];
h=[1 2 3 4 5];
len1=length(x);
len2=length(h);
len=len1+len2-1;
a=fliplr(h);
for i=1:len
c(i)=0;
for j=1:len1
if j>i
continue;
end
if(len2-i+j)<=0
continue;
end
c(i)=c(i)+(x(j)*a(len2-i+j));
end
end
k=1:len;
conv_op1=c(1:len)
subplot(2,1,1),stem(k,conv_op1);
title('Without using "conv" command')
conv_op2=conv(x,h)
subplot(2,1,2),stem(k,conv_op2)

**************************************
% correlation using Convolution
**************************************
N = 96;
n = 1 : N;
x = cos(0.25 *pi *n);
rx = conv(x,fliplr(x));
k = -28 : 28;
subplot(5,1,1);
stem(k,rx(68 : 124));
xlabel(' log index');
ylabel(' Amplitude');
title(' clean signal ACF');
w=rand(1,N)-0.5;
y=x+w;
ry=conv(y,fliplr(y));
subplot(5,1,2);
stem (k,ry(68 : 124));
xlabel(' log index');
ylabel(' Amplitude');
title (' noisy signal ACF');
rw=conv(w,fliplr(w));
subplot(5,1,3);
stem(k,rw(68 : 124));
xlabel(' log index');
ylabel(' Amplitude');
title(' noise ACF');
rxw=conv(x,fliplr(w));
subplot (5,1,4);
stem (k,rxw(68 : 124));
xlabel(' log index');
ylabel(' Amplitude');
title (' clean signal and noise CCF');
rxy=conv(x,fliplr(y));
subplot (5,1,5);
stem (k,rxy(68 : 124));
xlabel(' log index');
ylabel(' Amplitude');
title (' clean and noisy signal CCF');

**************************************
% correlation and Correlation coefficient
**************************************
N = 96;
n = 1 : N;
x = cos(0.25 *pi *n);
rx = xcorr(x);
corrcoef(x)
k = -28 : 28;
subplot(5,1,1);
stem(k,rx(68 : 124));
xlabel(' log index');
ylabel(' Amplitude');
title(' clean signal ACF');
w=rand(1,N)-0.5;
y=x+w;
ry=xcorr(y);
corrcoef(y)
subplot(5,1,2);
stem (k,ry(68 : 124));
xlabel(' log index');
ylabel(' Amplitude');
title (' noisy signal ACF');
rw=xcorr(w);
corrcoef(w)
subplot(5,1,3);
stem(k,rw(68 : 124));
xlabel(' log index');
ylabel(' Amplitude');
title(' noise ACF');
rxw=xcorr(x,w);
corrcoef(x,w)
subplot (5,1,4);
stem (k,rxw(68 : 124));
xlabel(' log index');
ylabel(' Amplitude');
title (' clean signal and noise CCF');
rxy=xcorr(x,y);
corrcoef(x,y)
subplot (5,1,5);
stem (k,rxy(68 : 124));
xlabel(' log index');

3 comments:

Unknown said...

thanks very much for programs.I was searching for LPF,HPF,PSF programs and got into your blog.Thanks again for sharing.

Regards
Varun

Unknown said...
This comment has been removed by a blog administrator.
Swanirbhar said...

u r welcome