function [t,f0,pvx] = ptrack_wu(d,sr)
% [t,f0,pvx] = ptrack_wu(d,sr)
%    Run the WuWang03 pitch tracker on the waveform d (at sampling
%    frequency sr).
%    Returns three 1D vectors.  
%      t is the sample times at which pitches are estimated 
%          (typically uniformly spaced every 0.010 sec).  
%      f0 is the corresponding pitch value in Hz (which can be 
%          zero for unvoiced frames). 
%      pvx is the corresponding probability of voicing for each
%          frame (between 0 and 1).  Generally, a threshold of 0.5
%          is the best guess for actual voiced-unvoiced threshold.
%    This is supposed to be a generic framework for pitch trackers,
%    but in this case we'll run Wu
% 2013-02-02 Dan Ellis dpwe@ee.columbia.edu

if ~isdeployed() && exist('wu_main','file') ~= 2
  addpath('wu_v1.0')
end

dispflag = 0;
uvpctile = 50; % 61;  % percentile of probs for unvoiced state
[f0, logprob] = wu_main(d,sr,dispflag,uvpctile);

% Wu uses 10 ms frames
t = [0:0.01:(0.01*(length(f0)-1))];

% something like pvx from full array of likelihoods
epvw = exp(logprob/10);  %/10 is a hack
nepvw = (epvw./repmat(sum(epvw),size(epvw,1),1));
% sqrt is a hack, makes it look likely
pvx = sqrt(max(nepvw));
