% Project, Spectral analysis 532
% Ethnicity '98
% DATA of length 4800, samplingfreq 8000 Hz

function n = power_find(word, Model)

global wsize wlag window powlen

% --- Create power for word ---
xx=reshape(word,1,4800); xx=filter([1 -15/16],1,xx); xx=xx/max(xx);
for k=0:powlen
  yy=window.*xx(wlag*k+1:wlag*k+wsize);
  power_word(k+1) = sqrt(sum(yy.^2))/1.4;
end;


% --- Compare ---
for wordnr = 1:5;

% --- Do Peak-shifting ---
%  [m, t1] = max(Model(wordnr,:));
%  [m, t2] = max(power_word);

%  if t1 < t2
%    power_comp = [power_word(t2-t1+1:powlen) zeros(1,(t2-t1)+1)];
%  else 
%    power_comp = [zeros(1, t1-t2) power_word(1:powlen-(t1-t2)+1)];
%  end;
% ------------------------
power_comp = power_word;

  e(wordnr,:) = abs(Model(wordnr,:)-power_comp).^2;
  err(wordnr) = sum(e(wordnr,:));
end;

[y, n] = min(err);

e(5,:) = abs(Model(5,:)-power_comp).^2;

figure(2)
subplot(311); plot(Model(n,:)); 
axis([0 powlen 0 1]); title('The Guessed word - Model'); ylabel('Power');
subplot(312); plot(power_word); 
axis([0 powlen 0 1]); title('The Searched word'); ylabel('Power');
subplot(313); plot(e(n,:));
axis([0 powlen 0 1]); title('Difference'); ylabel('Power');




















