From 9b742131612ce7d4cc105683a079f2b7063727da Mon Sep 17 00:00:00 2001 From: David Protzman Date: Thu, 7 Apr 2022 13:56:09 -0400 Subject: [PATCH] Added normalized crosscorrelation function This is to help Octave handle the MATLAB scripts --- matlab/normalized_xcorr.m | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 matlab/normalized_xcorr.m diff --git a/matlab/normalized_xcorr.m b/matlab/normalized_xcorr.m new file mode 100644 index 0000000..62fdd5d --- /dev/null +++ b/matlab/normalized_xcorr.m @@ -0,0 +1,19 @@ +%% Computes the normalized cross correlation of two vectors +% Used https://www.researchgate.net/post/How-can-one-calculate-normalized-cross-correlation-between-two-arrays +% as the reference for this implementation +function [score] = normalized_xcorr(window_one, window_two) + assert(length(window_one) == length(window_two), "Windows must be equal length"); + assert(isrow(window_one) || iscolumn(window_one), "Windows must be row/column vectors"); + assert(isrow(window_two) || iscolumn(window_two), "Windows must be row/column vectors"); + + % Make both windows zero mean + window_one = window_one - mean(window_one); + window_two = window_two - mean(window_two); + + % Cross correlate and get the average + xcorr_value = sum(window_one .* conj(window_two)) / length(window_one); + + % Final step in normalization + score = xcorr_value / sqrt(var(window_one) * var(window_two)); +end +