# Explanation for a function within xcorr

4 views (last 30 days)
Big heads on 10 Aug 2022
Commented: David Goodmanson on 14 Aug 2022
Looking within the xcorr function, most of it is pretty straightforward, except for one function within xcorr called "findTransformLength".
function m = findTransformLength(m)
m = 2*m;
while true
r = m;
for p = [2 3 5 7]
while (r > 1) && (mod(r, p) == 0)
r = r / p;
end
end
if r == 1
break;
end
m = m + 1;
end
With no comments, i fail to understand what this function is meant to acheive and what is the significance of p = [2 3 5 7]. Why those numbers specifically? Why not take a fixed FFT size instead?

David Goodmanson on 12 Aug 2022
Edited: David Goodmanson on 12 Aug 2022
Hi big,
rather than puzzle this out in place, it seemed easier to look at the output of the function for the first hundred values of m. The answer seems to be: the smallest value that is
(a) greater than or equal to 2*m, and
(b) contains only the prime factors 2,3,5,7 to various powers.
This is done to take advantage of the speed of the fft in those circumstances.
n = 100;
a = zeros(n,1);
for k = 1:n
a(k) = findTransformLength(k);
end
[(1:n)' a]
function m = findTransformLength(m)
(as you have it)
end % extra 'end' required to delineate a function at the bottom of a script file
##### 2 CommentsShowHide 1 older comment
David Goodmanson on 14 Aug 2022
Hi big,
I believe this is done for reasons of speed. If the length of the array is factored into primes, and one or more of the primes are large, then the fft is slow. When the length is the product of small prime factors, then the fft is faster, typically by a factor of 5 to 10.