# Error implementing Harmonic Product Spectrum algorithm

I'm trying to find the fundamental frequencies present in a piano recording using MATLAB. These are the steps I've followed;

1. Find the envelop of the signal

2. Find the note onsets

3. perform FFT between each onset

4. Harmonic product spectrum.

It's when I try to implement the HPS algorithm that I face a "dimensions don't agree" error. This is the whole code that implements the HPS algorithm .

**% In harmonic prodcut spectrum, you downsample the fft data several times and multiply all those with the original fft data to get the maximum peak.
%HPS
seg_fft = seg_fft(1 : size(seg_fft,1)/2 );
seg_fft = abs(seg_fft);

%HPS: downsampling
for i = 1:length(seg_fft)
seg_fft2(i,1) = 1;
seg_fft3(i,1) = 1;
seg_fft4(i,1) = 1;
%   seg_fft5(i,1) = 1;
end
for i = 1:floor((length(seg_fft)-1)/2)
seg_fft2(i,1) = (seg_fft(2*i,1) + seg_fft((2*i)+1,1))/2;
end
for i = 1:floor((length(seg_fft)-2)/3)
seg_fft3(i,1) = (seg_fft(3*i,1) + seg_fft((3*i)+1,1) + seg_fft((3*i)+2,1))/3;
end

for i = 1:floor((length(seg_fft)-3)/4)
seg_fft4(i,1) = (seg_fft(4*i,1) + seg_fft((4*i)+1,1) + seg_fft((4*i)+2,1) + seg_fft((4*i)+3,1))/4;
end

%HPS, PartII: calculate product
p1 = (seg_fft3)  .* (seg_fft4);
p2 = p1.* (seg_fft2);
p3 = p2.* (seg_fft);

HPS, PartIII: find max
[f_y1,I] = max(p3)

for c = 1 : length(p3)
if(p3(c,1) == f_y1)
index = c;
end
end

% Convert that to a frequency
f_y(h) = (index / NFFT) * FS;

h = 1;
h=h+1;
f_y = abs(f_y)';

end**


Before implementing p3 = p2.* (seg_fft); the sizes of seg_fft, seg_fft2, seg_fft3, seg_fft4 all have the same dimensions of 16384 1. Then when I DO implement p3 = p2.* (seg_fft); the size of seg_fft changes to 8192 1 while the sizes of rest remain at 16384 1 thus causing an error in multiplication as the dimensions aren't the same.

I'm really confused as to why this keeps happening and nothing I try seems to work out. Would really REALLY appreciate some help here... If someone could fix this code it'd be a GREAT help... Thanx in advance.. I'm real desperate here......