フィルターのクリア

Normalized cross-correlation function

19 ビュー (過去 30 日間)
Manolis Michailidis
Manolis Michailidis 2015 年 9 月 18 日
コメント済み: devi sowjanya 2021 年 3 月 6 日
Hello, i am trying to write a normilized cross-correlation method function , but i can't complete it. Here are the details of the formula :
My code is
x=rand(1,1000);
N=length(x); %// length of signal
n1=128; %// length of window
xf=framing(x,n1,n1/2,rectwin(n1)); %this function frames the signal i will get xf(128,14)
win_num=size(xf,2);
for col=1:win_num
for m=1:n1+1
for n=1:n1-m
ccor(m,col)=sum(xf(n+m,col)*(xf(n,col)))/ sqrt(sum(xf(n)^2)*sum(xf(n+m)^2) );
end
end
end
Note that i want to see the correlation values betwen -1 and 1 like in the figure but instead i get
.
  1 件のコメント
devi sowjanya
devi sowjanya 2021 年 3 月 6 日
Can you please provide the framing function code

サインインしてコメントする。

採用された回答

Kirby Fears
Kirby Fears 2015 年 9 月 21 日
編集済み: Kirby Fears 2015 年 9 月 21 日
Hi Manolis,
To directly replicate what the equations say (shifting indices with +1), the calculation would look more like this:
x=rand(1,1000);
N=length(x); %// length of signal
n1=128; %// length of window
xf=framing(x,n1,n1/2,rectwin(n1)); %this function frames the signal i will get xf(128,14)
win_num=size(xf,2);
% M0=???
ccor=NaN(M0+1,win_num);
for col=1:win_num
for m=1:M0+1,
[term1,term2,term3]=deal(0);
for n=1:N-m,
term1=term1+xf(n,col)*xf(n+m,col);
term2=term2+xf(n,col)^2;
term3=term3+xf(n+m,col)^2;
end
ccor(m,col)=term1/sqrt(term2*term3);
end
end
You would just need to explicitly specify M0. This calculation can be simplified as follows:
x=rand(1,1000);
N=length(x); %// length of signal
n1=128; %// length of window
xf=framing(x,n1,n1/2,rectwin(n1)); %this function frames the signal i will get xf(128,14)
win_num=size(xf,2);
% M0=???
ccor=NaN(M0+1,win_num);
for col=1:win_num
for m=1:M0+1,
term1=sum(xf(1:N-m,col).*xf(1+m:N,col));
term2=sum(xf(1:N-m,col).^2);
term3=sum(xf(1+m:N,col).^2);
ccor(m,col)=term1/sqrt(term2*term3);
end
end
I don't have the toolbox required to run this code, so it may have bugs. Hope this helps.
  1 件のコメント
Manolis Michailidis
Manolis Michailidis 2015 年 9 月 29 日
Thank you i finally solved it, for M0=n1-1 shifts i had to replace in the loops N with n1 and it works. I get the results i want , from the start i wanted the correlation to be biased it the region [-1 1] and that's what i get. So thanks a lot for your effort.

サインインしてコメントする。

その他の回答 (2 件)

Kirby Fears
Kirby Fears 2015 年 9 月 18 日
編集済み: Kirby Fears 2015 年 9 月 18 日
Hi Manolis,
You are looping n and m by starting at 1, but in the formula you posted the sum starts at 0. You will need to correct your calculations to include the 0 terms in the summation. You should also consider pre-allocating "ccor" to save yourself some computational time.
You may have access to the xcorr function which calculates cross correlations. It has several options to control normalization.
Hope this helps.
  2 件のコメント
Manolis Michailidis
Manolis Michailidis 2015 年 9 月 18 日
Hello, indeed the index starts from 0 but matlab's indexing is from 1 so i considered that in mind. The pre-allocating is not my primary concern , on the other hand i know the xcoor(...,'coeff') can do it , but i wanted to write my own function , so do you have any ideas where is the error? thank you.
Kirby Fears
Kirby Fears 2015 年 9 月 21 日
Indeed the loop variables are only used as indices, so you are correct. My mistake. I assumed the loop variables were also used as numeric values in the summand. I'm writing another answer below.

サインインしてコメントする。


Image Analyst
Image Analyst 2015 年 9 月 29 日
Do you have the Image Processing Toolbox? If so, why not just use the built-in function normxcorr2()?
  1 件のコメント
Manolis Michailidis
Manolis Michailidis 2015 年 9 月 29 日
編集済み: Manolis Michailidis 2015 年 9 月 29 日
Yes i have it, i was just curious and wanted to confirm the result with the matlabs, anyway thank you i will take a look at the function you mentioned. It's always nice to learn something new.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by