\doxysection{yin.\+c}
\label{yin_8c_source}\index{src/libsphinxbase/fe/yin.c@{src/libsphinxbase/fe/yin.c}}
\textbf{ Go to the documentation of this file.}
\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ \textcolor{comment}{/*\ -\/*-\/\ c-\/basic-\/offset:\ 4;\ indent-\/tabs-\/mode:\ nil\ -\/*-\/\ */}}
\DoxyCodeLine{00002\ \textcolor{comment}{/*}}
\DoxyCodeLine{00003\ \textcolor{comment}{\ *\ Copyright\ (c)\ 2008\ Beyond\ Access,\ Inc.\ \ All\ rights\ reserved.}}
\DoxyCodeLine{00004\ \textcolor{comment}{\ *\ }}
\DoxyCodeLine{00005\ \textcolor{comment}{\ *\ Redistribution\ and\ use\ in\ source\ and\ binary\ forms,\ with\ or\ without}}
\DoxyCodeLine{00006\ \textcolor{comment}{\ *\ modification,\ are\ permitted\ provided\ that\ the\ following\ conditions}}
\DoxyCodeLine{00007\ \textcolor{comment}{\ *\ are\ met:}}
\DoxyCodeLine{00008\ \textcolor{comment}{\ *\ }}
\DoxyCodeLine{00009\ \textcolor{comment}{\ *\ 1.\ Redistributions\ of\ source\ code\ must\ retain\ the\ above\ copyright}}
\DoxyCodeLine{00010\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer.\ }}
\DoxyCodeLine{00011\ \textcolor{comment}{\ *\ }}
\DoxyCodeLine{00012\ \textcolor{comment}{\ *\ 2.\ Redistributions\ in\ binary\ form\ must\ reproduce\ the\ above\ copyright}}
\DoxyCodeLine{00013\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer\ in}}
\DoxyCodeLine{00014\ \textcolor{comment}{\ *\ \ \ \ the\ documentation\ and/or\ other\ materials\ provided\ with\ the}}
\DoxyCodeLine{00015\ \textcolor{comment}{\ *\ \ \ \ distribution.}}
\DoxyCodeLine{00016\ \textcolor{comment}{\ *\ }}
\DoxyCodeLine{00017\ \textcolor{comment}{\ *\ THIS\ SOFTWARE\ IS\ PROVIDED\ BY\ BEYOND\ ACCESS,\ INC.\ \`{}\`{}AS\ IS''\ AND\ ANY}}
\DoxyCodeLine{00018\ \textcolor{comment}{\ *\ EXPRESSED\ OR\ IMPLIED\ WARRANTIES,\ INCLUDING,\ BUT\ NOT\ LIMITED\ TO,\ THE}}
\DoxyCodeLine{00019\ \textcolor{comment}{\ *\ IMPLIED\ WARRANTIES\ OF\ MERCHANTABILITY\ AND\ FITNESS\ FOR\ A\ PARTICULAR}}
\DoxyCodeLine{00020\ \textcolor{comment}{\ *\ PURPOSE\ ARE\ DISCLAIMED.\ \ IN\ NO\ EVENT\ SHALL\ BEYOND\ ACCESS,\ INC.\ \ NOR}}
\DoxyCodeLine{00021\ \textcolor{comment}{\ *\ ITS\ EMPLOYEES\ BE\ LIABLE\ FOR\ ANY\ DIRECT,\ INDIRECT,\ INCIDENTAL,\ SPECIAL,}}
\DoxyCodeLine{00022\ \textcolor{comment}{\ *\ EXEMPLARY,\ OR\ CONSEQUENTIAL\ DAMAGES\ (INCLUDING,\ BUT\ NOT\ LIMITED\ TO,}}
\DoxyCodeLine{00023\ \textcolor{comment}{\ *\ PROCUREMENT\ OF\ SUBSTITUTE\ GOODS\ OR\ SERVICES;\ LOSS\ OF\ USE,\ DATA,\ OR}}
\DoxyCodeLine{00024\ \textcolor{comment}{\ *\ PROFITS;\ OR\ BUSINESS\ INTERRUPTION)\ HOWEVER\ CAUSED\ AND\ ON\ ANY\ THEORY\ OF}}
\DoxyCodeLine{00025\ \textcolor{comment}{\ *\ LIABILITY,\ WHETHER\ IN\ CONTRACT,\ STRICT\ LIABILITY,\ OR\ TORT\ (INCLUDING}}
\DoxyCodeLine{00026\ \textcolor{comment}{\ *\ NEGLIGENCE\ OR\ OTHERWISE)\ ARISING\ IN\ ANY\ WAY\ OUT\ OF\ THE\ USE\ OF\ THIS}}
\DoxyCodeLine{00027\ \textcolor{comment}{\ *\ SOFTWARE,\ EVEN\ IF\ ADVISED\ OF\ THE\ POSSIBILITY\ OF\ SUCH\ DAMAGE.}}
\DoxyCodeLine{00028\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00029\ }
\DoxyCodeLine{00035\ \textcolor{comment}{/*\ This\ implements\ part\ of\ the\ YIN\ algorithm:}}
\DoxyCodeLine{00036\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00037\ \textcolor{comment}{\ *\ "{}YIN,\ a\ fundamental\ frequency\ estimator\ for\ speech\ and\ music"{}.}}
\DoxyCodeLine{00038\ \textcolor{comment}{\ *\ Alain\ de\ Cheveigné\ and\ Hideki\ Kawahara.\ \ Journal\ of\ the\ Acoustical}}
\DoxyCodeLine{00039\ \textcolor{comment}{\ *\ Society\ of\ America,\ 111\ (4),\ April\ 2002.}}
\DoxyCodeLine{00040\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00041\ }
\DoxyCodeLine{00042\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/prim\_type.h"{}}}
\DoxyCodeLine{00043\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/ckd\_alloc.h"{}}}
\DoxyCodeLine{00044\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/fixpoint.h"{}}}
\DoxyCodeLine{00045\ }
\DoxyCodeLine{00046\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/yin.h"{}}}
\DoxyCodeLine{00047\ }
\DoxyCodeLine{00048\ \textcolor{preprocessor}{\#include\ <stdio.h>}}
\DoxyCodeLine{00049\ }
\DoxyCodeLine{00050\ \textcolor{keyword}{struct\ }yin\_s\ \{}
\DoxyCodeLine{00051\ \ \ \ \ uint16\ frame\_size;\ \ \ \ \ \ \ }
\DoxyCodeLine{00052\ \ \ \ \ uint16\ search\_threshold;\ }
\DoxyCodeLine{00053\ \ \ \ \ uint16\ search\_range;\ \ \ \ \ }
\DoxyCodeLine{00054\ \ \ \ \ uint16\ nfr;\ \ \ \ \ \ \ \ \ \ \ \ \ \ }
\DoxyCodeLine{00056\ \ \ \ \ \textcolor{keywordtype}{unsigned}\ \textcolor{keywordtype}{char}\ wsize;\ \ \ \ }
\DoxyCodeLine{00057\ \ \ \ \ \textcolor{keywordtype}{unsigned}\ \textcolor{keywordtype}{char}\ wstart;\ \ \ }
\DoxyCodeLine{00058\ \ \ \ \ \textcolor{keywordtype}{unsigned}\ \textcolor{keywordtype}{char}\ wcur;\ \ \ \ \ }
\DoxyCodeLine{00059\ \ \ \ \ \textcolor{keywordtype}{unsigned}\ \textcolor{keywordtype}{char}\ endut;\ \ \ \ }
\DoxyCodeLine{00061\ \ \ \ \ fixed32\ **diff\_window;\ \ }
\DoxyCodeLine{00062\ \ \ \ \ uint16\ *period\_window;\ \ }
\DoxyCodeLine{00063\ \};}
\DoxyCodeLine{00064\ }
\DoxyCodeLine{00068\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00069\ cmn\_diff(int16\ \textcolor{keyword}{const}\ *signal,\ int32\ *out\_diff,\ \textcolor{keywordtype}{int}\ ndiff)}
\DoxyCodeLine{00070\ \{}
\DoxyCodeLine{00071\ \ \ \ \ uint32\ cum,\ cshift;}
\DoxyCodeLine{00072\ \ \ \ \ int32\ t,\ tscale;}
\DoxyCodeLine{00073\ }
\DoxyCodeLine{00074\ \ \ \ \ out\_diff[0]\ =\ 32768;}
\DoxyCodeLine{00075\ \ \ \ \ cum\ =\ 0;}
\DoxyCodeLine{00076\ \ \ \ \ cshift\ =\ 0;}
\DoxyCodeLine{00077\ }
\DoxyCodeLine{00078\ \ \ \ \ \textcolor{comment}{/*\ Determine\ how\ many\ bits\ we\ can\ scale\ t\ up\ by\ below.\ */}}
\DoxyCodeLine{00079\ \ \ \ \ \textcolor{keywordflow}{for}\ (tscale\ =\ 0;\ tscale\ <\ 32;\ ++tscale)}
\DoxyCodeLine{00080\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (ndiff\ \&\ (1<<(31-\/tscale)))}
\DoxyCodeLine{00081\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00082\ \ \ \ \ -\/-\/tscale;\ \textcolor{comment}{/*\ Avoid\ teh\ overflowz.\ */}}
\DoxyCodeLine{00083\ \ \ \ \ \textcolor{comment}{/*\ printf("{}tscale\ is\ \%d\ (ndiff\ -\/\ 1)\ <<\ tscale\ is\ \%d\(\backslash\)n"{},}}
\DoxyCodeLine{00084\ \textcolor{comment}{\ \ \ \ \ \ \ tscale,\ (ndiff-\/1)\ <<\ tscale);\ */}}
\DoxyCodeLine{00085\ }
\DoxyCodeLine{00086\ \ \ \ \ \textcolor{comment}{/*\ Somewhat\ elaborate\ block\ floating\ point\ implementation.}}
\DoxyCodeLine{00087\ \textcolor{comment}{\ \ \ \ \ *\ The\ fp\ implementation\ of\ this\ is\ really\ a\ lot\ simpler.\ */}}
\DoxyCodeLine{00088\ \ \ \ \ \textcolor{keywordflow}{for}\ (t\ =\ 1;\ t\ <\ ndiff;\ ++t)\ \{}
\DoxyCodeLine{00089\ \ \ \ \ \ \ \ \ uint32\ dd,\ dshift,\ norm;}
\DoxyCodeLine{00090\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{int}\ j;}
\DoxyCodeLine{00091\ }
\DoxyCodeLine{00092\ \ \ \ \ \ \ \ \ dd\ =\ 0;}
\DoxyCodeLine{00093\ \ \ \ \ \ \ \ \ dshift\ =\ 0;}
\DoxyCodeLine{00094\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (j\ =\ 0;\ j\ <\ ndiff;\ ++j)\ \{}
\DoxyCodeLine{00095\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{int}\ diff\ =\ signal[j]\ -\/\ signal[t\ +\ j];}
\DoxyCodeLine{00096\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Guard\ against\ overflows.\ */}}
\DoxyCodeLine{00097\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (dd\ >\ (1UL<<tscale))\ \{}
\DoxyCodeLine{00098\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ dd\ >>=\ 1;}
\DoxyCodeLine{00099\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ++dshift;}
\DoxyCodeLine{00100\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00101\ \ \ \ \ \ \ \ \ \ \ \ \ dd\ +=\ (diff\ *\ diff)\ >>\ dshift;}
\DoxyCodeLine{00102\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00103\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Make\ sure\ the\ diffs\ and\ cum\ are\ shifted\ to\ the\ same}}
\DoxyCodeLine{00104\ \textcolor{comment}{\ \ \ \ \ \ \ \ \ *\ scaling\ factor\ (usually\ dshift\ will\ be\ zero)\ */}}
\DoxyCodeLine{00105\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (dshift\ >\ cshift)\ \{}
\DoxyCodeLine{00106\ \ \ \ \ \ \ \ \ \ \ \ \ cum\ +=\ dd\ <<\ (dshift-\/cshift);}
\DoxyCodeLine{00107\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00108\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00109\ \ \ \ \ \ \ \ \ \ \ \ \ cum\ +=\ dd\ >>\ (cshift-\/dshift);}
\DoxyCodeLine{00110\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00111\ }
\DoxyCodeLine{00112\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Guard\ against\ overflows\ and\ also\ ensure\ that\ (t<<tscale)\ >\ cum.\ */}}
\DoxyCodeLine{00113\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{while}\ (cum\ >\ (1UL<<tscale))\ \{}
\DoxyCodeLine{00114\ \ \ \ \ \ \ \ \ \ \ \ \ cum\ >>=\ 1;}
\DoxyCodeLine{00115\ \ \ \ \ \ \ \ \ \ \ \ \ ++cshift;}
\DoxyCodeLine{00116\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00117\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Avoid\ divide-\/by-\/zero!\ */}}
\DoxyCodeLine{00118\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (cum\ ==\ 0)\ cum\ =\ 1;}
\DoxyCodeLine{00119\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Calculate\ the\ normalizer\ in\ high\ precision.\ */}}
\DoxyCodeLine{00120\ \ \ \ \ \ \ \ \ norm\ =\ (t\ <<\ tscale)\ /\ cum;}
\DoxyCodeLine{00121\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Do\ a\ long\ multiply\ and\ shift\ down\ to\ Q15.\ */}}
\DoxyCodeLine{00122\ \ \ \ \ \ \ \ \ out\_diff[t]\ =\ (int32)(((\textcolor{keywordtype}{long}\ \textcolor{keywordtype}{long})dd\ *\ norm)}
\DoxyCodeLine{00123\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ >>\ (tscale\ -\/\ 15\ +\ cshift\ -\/\ dshift));}
\DoxyCodeLine{00124\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ printf("{}dd\ \%d\ cshift\ \%d\ dshift\ \%d\ scaledt\ \%d\ cum\ \%d\ norm\ \%d\ cmn\ \%d\(\backslash\)n"{},}}
\DoxyCodeLine{00125\ \textcolor{comment}{\ \ \ \ \ \ \ \ \ \ \ dd,\ cshift,\ dshift,\ (t<<tscale),\ cum,\ norm,\ out\_diff[t]);\ */}}
\DoxyCodeLine{00126\ \ \ \ \ \}}
\DoxyCodeLine{00127\ \}}
\DoxyCodeLine{00128\ }
\DoxyCodeLine{00129\ yin\_t\ *}
\DoxyCodeLine{00130\ yin\_init(\textcolor{keywordtype}{int}\ frame\_size,\ \textcolor{keywordtype}{float}\ search\_threshold,}
\DoxyCodeLine{00131\ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{float}\ search\_range,\ \textcolor{keywordtype}{int}\ smooth\_window)}
\DoxyCodeLine{00132\ \{}
\DoxyCodeLine{00133\ \ \ \ \ yin\_t\ *pe;}
\DoxyCodeLine{00134\ }
\DoxyCodeLine{00135\ \ \ \ \ pe\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*pe));}
\DoxyCodeLine{00136\ \ \ \ \ pe-\/>frame\_size\ =\ frame\_size;}
\DoxyCodeLine{00137\ \ \ \ \ pe-\/>search\_threshold\ =\ (uint16)(search\_threshold\ *\ 32768);}
\DoxyCodeLine{00138\ \ \ \ \ pe-\/>search\_range\ =\ (uint16)(search\_range\ *\ 32768);}
\DoxyCodeLine{00139\ \ \ \ \ pe-\/>wsize\ =\ smooth\_window\ *\ 2\ +\ 1;}
\DoxyCodeLine{00140\ \ \ \ \ pe-\/>diff\_window\ =\ ckd\_calloc\_2d(pe-\/>wsize,}
\DoxyCodeLine{00141\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ pe-\/>frame\_size\ /\ 2,}
\DoxyCodeLine{00142\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{sizeof}(**pe-\/>diff\_window));}
\DoxyCodeLine{00143\ \ \ \ \ pe-\/>period\_window\ =\ ckd\_calloc(pe-\/>wsize,}
\DoxyCodeLine{00144\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{sizeof}(*pe-\/>period\_window));}
\DoxyCodeLine{00145\ \ \ \ \ \textcolor{keywordflow}{return}\ pe;}
\DoxyCodeLine{00146\ \}}
\DoxyCodeLine{00147\ }
\DoxyCodeLine{00148\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00149\ yin\_free(yin\_t\ *pe)}
\DoxyCodeLine{00150\ \{}
\DoxyCodeLine{00151\ \ \ \ \ ckd\_free\_2d(pe-\/>diff\_window);}
\DoxyCodeLine{00152\ \ \ \ \ ckd\_free(pe-\/>period\_window);}
\DoxyCodeLine{00153\ \ \ \ \ ckd\_free(pe);}
\DoxyCodeLine{00154\ \}}
\DoxyCodeLine{00155\ }
\DoxyCodeLine{00156\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00157\ yin\_start(yin\_t\ *pe)}
\DoxyCodeLine{00158\ \{}
\DoxyCodeLine{00159\ \ \ \ \ \textcolor{comment}{/*\ Reset\ the\ circular\ window\ pointers.\ */}}
\DoxyCodeLine{00160\ \ \ \ \ pe-\/>wstart\ =\ pe-\/>endut\ =\ 0;}
\DoxyCodeLine{00161\ \ \ \ \ pe-\/>nfr\ =\ 0;}
\DoxyCodeLine{00162\ \}}
\DoxyCodeLine{00163\ }
\DoxyCodeLine{00164\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00165\ yin\_end(yin\_t\ *pe)}
\DoxyCodeLine{00166\ \{}
\DoxyCodeLine{00167\ \ \ \ \ pe-\/>endut\ =\ 1;}
\DoxyCodeLine{00168\ \}}
\DoxyCodeLine{00169\ }
\DoxyCodeLine{00170\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00171\ thresholded\_search(int32\ *diff\_window,\ fixed32\ threshold,\ \textcolor{keywordtype}{int}\ start,\ \textcolor{keywordtype}{int}\ end)}
\DoxyCodeLine{00172\ \{}
\DoxyCodeLine{00173\ \ \ \ \ \textcolor{keywordtype}{int}\ i,\ min,\ argmin;}
\DoxyCodeLine{00174\ }
\DoxyCodeLine{00175\ \ \ \ \ min\ =\ INT\_MAX;}
\DoxyCodeLine{00176\ \ \ \ \ argmin\ =\ 0;}
\DoxyCodeLine{00177\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ start;\ i\ <\ end;\ ++i)\ \{}
\DoxyCodeLine{00178\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{int}\ diff\ =\ diff\_window[i];}
\DoxyCodeLine{00179\ }
\DoxyCodeLine{00180\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (diff\ <\ threshold)\ \{}
\DoxyCodeLine{00181\ \ \ \ \ \ \ \ \ \ \ \ \ min\ =\ diff;}
\DoxyCodeLine{00182\ \ \ \ \ \ \ \ \ \ \ \ \ argmin\ =\ i;}
\DoxyCodeLine{00183\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00184\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00185\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (diff\ <\ min)\ \{}
\DoxyCodeLine{00186\ \ \ \ \ \ \ \ \ \ \ \ \ min\ =\ diff;}
\DoxyCodeLine{00187\ \ \ \ \ \ \ \ \ \ \ \ \ argmin\ =\ i;}
\DoxyCodeLine{00188\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00189\ \ \ \ \ \}}
\DoxyCodeLine{00190\ \ \ \ \ \textcolor{keywordflow}{return}\ argmin;}
\DoxyCodeLine{00191\ \}}
\DoxyCodeLine{00192\ }
\DoxyCodeLine{00193\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00194\ yin\_write(yin\_t\ *pe,\ int16\ \textcolor{keyword}{const}\ *frame)}
\DoxyCodeLine{00195\ \{}
\DoxyCodeLine{00196\ \ \ \ \ \textcolor{keywordtype}{int}\ outptr,\ difflen;}
\DoxyCodeLine{00197\ }
\DoxyCodeLine{00198\ \ \ \ \ \textcolor{comment}{/*\ Rotate\ the\ window\ one\ frame\ forward.\ */}}
\DoxyCodeLine{00199\ \ \ \ \ ++pe-\/>wstart;}
\DoxyCodeLine{00200\ \ \ \ \ \textcolor{comment}{/*\ Fill\ in\ the\ frame\ before\ wstart.\ */}}
\DoxyCodeLine{00201\ \ \ \ \ outptr\ =\ pe-\/>wstart\ -\/\ 1;}
\DoxyCodeLine{00202\ \ \ \ \ \textcolor{comment}{/*\ Wrap\ around\ the\ window\ pointer.\ */}}
\DoxyCodeLine{00203\ \ \ \ \ \textcolor{keywordflow}{if}\ (pe-\/>wstart\ ==\ pe-\/>wsize)}
\DoxyCodeLine{00204\ \ \ \ \ \ \ \ \ pe-\/>wstart\ =\ 0;}
\DoxyCodeLine{00205\ }
\DoxyCodeLine{00206\ \ \ \ \ \textcolor{comment}{/*\ Now\ calculate\ normalized\ difference\ function.\ */}}
\DoxyCodeLine{00207\ \ \ \ \ difflen\ =\ pe-\/>frame\_size\ /\ 2;}
\DoxyCodeLine{00208\ \ \ \ \ cmn\_diff(frame,\ pe-\/>diff\_window[outptr],\ difflen);}
\DoxyCodeLine{00209\ }
\DoxyCodeLine{00210\ \ \ \ \ \textcolor{comment}{/*\ Find\ the\ first\ point\ under\ threshold.\ \ If\ not\ found,\ then}}
\DoxyCodeLine{00211\ \textcolor{comment}{\ \ \ \ \ *\ use\ the\ absolute\ minimum.\ */}}
\DoxyCodeLine{00212\ \ \ \ \ pe-\/>period\_window[outptr]}
\DoxyCodeLine{00213\ \ \ \ \ \ \ \ \ =\ thresholded\_search(pe-\/>diff\_window[outptr],}
\DoxyCodeLine{00214\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ pe-\/>search\_threshold,\ 0,\ difflen);}
\DoxyCodeLine{00215\ }
\DoxyCodeLine{00216\ \ \ \ \ \textcolor{comment}{/*\ Increment\ total\ number\ of\ frames.\ */}}
\DoxyCodeLine{00217\ \ \ \ \ ++pe-\/>nfr;}
\DoxyCodeLine{00218\ \}}
\DoxyCodeLine{00219\ }
\DoxyCodeLine{00220\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00221\ yin\_read(yin\_t\ *pe,\ uint16\ *out\_period,\ uint16\ *out\_bestdiff)}
\DoxyCodeLine{00222\ \{}
\DoxyCodeLine{00223\ \ \ \ \ \textcolor{keywordtype}{int}\ wstart,\ wlen,\ half\_wsize,\ i;}
\DoxyCodeLine{00224\ \ \ \ \ \textcolor{keywordtype}{int}\ best,\ best\_diff,\ search\_width,\ low\_period,\ high\_period;}
\DoxyCodeLine{00225\ }
\DoxyCodeLine{00226\ \ \ \ \ half\_wsize\ =\ (pe-\/>wsize-\/1)/2;}
\DoxyCodeLine{00227\ \ \ \ \ \textcolor{comment}{/*\ Without\ any\ smoothing,\ just\ return\ the\ current\ value\ (don't}}
\DoxyCodeLine{00228\ \textcolor{comment}{\ \ \ \ \ *\ need\ to\ do\ anything\ to\ the\ current\ poitner\ either).\ */}}
\DoxyCodeLine{00229\ \ \ \ \ \textcolor{keywordflow}{if}\ (half\_wsize\ ==\ 0)\ \{}
\DoxyCodeLine{00230\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (pe-\/>endut)}
\DoxyCodeLine{00231\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00232\ \ \ \ \ \ \ \ \ *out\_period\ =\ pe-\/>period\_window[0];}
\DoxyCodeLine{00233\ \ \ \ \ \ \ \ \ *out\_bestdiff\ =\ pe-\/>diff\_window[0][pe-\/>period\_window[0]];}
\DoxyCodeLine{00234\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 1;}
\DoxyCodeLine{00235\ \ \ \ \ \}}
\DoxyCodeLine{00236\ }
\DoxyCodeLine{00237\ \ \ \ \ \textcolor{comment}{/*\ We\ can't\ do\ anything\ unless\ we\ have\ at\ least\ (wsize-\/1)/2\ +\ 1}}
\DoxyCodeLine{00238\ \textcolor{comment}{\ \ \ \ \ *\ frames,\ unless\ we're\ at\ the\ end\ of\ the\ utterance.\ */}}
\DoxyCodeLine{00239\ \ \ \ \ \textcolor{keywordflow}{if}\ (pe-\/>endut\ ==\ 0\ \&\&\ pe-\/>nfr\ <\ half\_wsize\ +\ 1)\ \{}
\DoxyCodeLine{00240\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Don't\ increment\ the\ current\ pointer\ either.\ */}}
\DoxyCodeLine{00241\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00242\ \ \ \ \ \}}
\DoxyCodeLine{00243\ }
\DoxyCodeLine{00244\ \ \ \ \ \textcolor{comment}{/*\ Establish\ the\ smoothing\ window.\ */}}
\DoxyCodeLine{00245\ \ \ \ \ \textcolor{comment}{/*\ End\ of\ utterance.\ */}}
\DoxyCodeLine{00246\ \ \ \ \ \textcolor{keywordflow}{if}\ (pe-\/>endut)\ \{}
\DoxyCodeLine{00247\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ We\ are\ done\ (no\ more\ data)\ when\ pe-\/>wcur\ =\ pe-\/>wstart.\ */}}
\DoxyCodeLine{00248\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (pe-\/>wcur\ ==\ pe-\/>wstart)}
\DoxyCodeLine{00249\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00250\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ I.e.\ pe-\/>wcur\ (circular\ minus)\ half\_wsize.\ */}}
\DoxyCodeLine{00251\ \ \ \ \ \ \ \ \ wstart\ =\ (pe-\/>wcur\ +\ pe-\/>wsize\ -\/\ half\_wsize)\ \%\ pe-\/>wsize;}
\DoxyCodeLine{00252\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Number\ of\ frames\ from\ wstart\ up\ to\ pe-\/>wstart.\ */}}
\DoxyCodeLine{00253\ \ \ \ \ \ \ \ \ wlen\ =\ pe-\/>wstart\ -\/\ wstart;}
\DoxyCodeLine{00254\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (wlen\ <\ 0)\ wlen\ +=\ pe-\/>wsize;}
\DoxyCodeLine{00255\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*printf("{}ENDUT!\ "{});*/}}
\DoxyCodeLine{00256\ \ \ \ \ \}}
\DoxyCodeLine{00257\ \ \ \ \ \textcolor{comment}{/*\ Beginning\ of\ utterance.\ */}}
\DoxyCodeLine{00258\ \ \ \ \ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (pe-\/>nfr\ <\ pe-\/>wsize)\ \{}
\DoxyCodeLine{00259\ \ \ \ \ \ \ \ \ wstart\ =\ 0;}
\DoxyCodeLine{00260\ \ \ \ \ \ \ \ \ wlen\ =\ pe-\/>nfr;}
\DoxyCodeLine{00261\ \ \ \ \ \}}
\DoxyCodeLine{00262\ \ \ \ \ \textcolor{comment}{/*\ Normal\ case,\ it\ is\ what\ it\ is.\ */}}
\DoxyCodeLine{00263\ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00264\ \ \ \ \ \ \ \ \ wstart\ =\ pe-\/>wstart;}
\DoxyCodeLine{00265\ \ \ \ \ \ \ \ \ wlen\ =\ pe-\/>wsize;}
\DoxyCodeLine{00266\ \ \ \ \ \}}
\DoxyCodeLine{00267\ }
\DoxyCodeLine{00268\ \ \ \ \ \textcolor{comment}{/*\ Now\ (finally)\ look\ for\ the\ best\ local\ estimate.\ */}}
\DoxyCodeLine{00269\ \ \ \ \ \textcolor{comment}{/*\ printf("{}Searching\ for\ local\ estimate\ in\ \%d\ frames\ around\ \%d\(\backslash\)n"{},}}
\DoxyCodeLine{00270\ \textcolor{comment}{\ \ \ \ \ \ \ wlen,\ pe-\/>nfr\ +\ 1\ -\/\ wlen);\ */}}
\DoxyCodeLine{00271\ \ \ \ \ best\ =\ pe-\/>period\_window[pe-\/>wcur];}
\DoxyCodeLine{00272\ \ \ \ \ best\_diff\ =\ pe-\/>diff\_window[pe-\/>wcur][best];}
\DoxyCodeLine{00273\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ wlen;\ ++i)\ \{}
\DoxyCodeLine{00274\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{int}\ j\ =\ wstart\ +\ i;}
\DoxyCodeLine{00275\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{int}\ diff;}
\DoxyCodeLine{00276\ }
\DoxyCodeLine{00277\ \ \ \ \ \ \ \ \ j\ \%=\ pe-\/>wsize;}
\DoxyCodeLine{00278\ \ \ \ \ \ \ \ \ diff\ =\ pe-\/>diff\_window[j][pe-\/>period\_window[j]];}
\DoxyCodeLine{00279\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ printf("{}\%.2f,\%.2f\ "{},\ 1.0\ -\/\ (double)diff/32768,}}
\DoxyCodeLine{00280\ \textcolor{comment}{\ \ \ \ \ \ \ \ \ \ \ pe-\/>period\_window[j]\ ?\ 8000.0/pe-\/>period\_window[j]\ :\ 8000.0);\ */}}
\DoxyCodeLine{00281\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (diff\ <\ best\_diff)\ \{}
\DoxyCodeLine{00282\ \ \ \ \ \ \ \ \ \ \ \ \ best\_diff\ =\ diff;}
\DoxyCodeLine{00283\ \ \ \ \ \ \ \ \ \ \ \ \ best\ =\ pe-\/>period\_window[j];}
\DoxyCodeLine{00284\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00285\ \ \ \ \ \}}
\DoxyCodeLine{00286\ \ \ \ \ \textcolor{comment}{/*\ printf("{}best:\ \%.2f,\ \%.2f\(\backslash\)n"{},\ 1.0\ -\/\ (double)best\_diff/32768,}}
\DoxyCodeLine{00287\ \textcolor{comment}{\ \ \ \ \ \ \ best\ ?\ 8000.0/best\ :\ 8000.0);\ */}}
\DoxyCodeLine{00288\ \ \ \ \ \textcolor{comment}{/*\ If\ it's\ the\ same\ as\ the\ current\ one\ then\ return\ it.\ */}}
\DoxyCodeLine{00289\ \ \ \ \ \textcolor{keywordflow}{if}\ (best\ ==\ pe-\/>period\_window[pe-\/>wcur])\ \{}
\DoxyCodeLine{00290\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ Increment\ the\ current\ pointer.\ */}}
\DoxyCodeLine{00291\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (++pe-\/>wcur\ ==\ pe-\/>wsize)}
\DoxyCodeLine{00292\ \ \ \ \ \ \ \ \ \ \ \ \ pe-\/>wcur\ =\ 0;}
\DoxyCodeLine{00293\ \ \ \ \ \ \ \ \ *out\_period\ =\ best;}
\DoxyCodeLine{00294\ \ \ \ \ \ \ \ \ *out\_bestdiff\ =\ best\_diff;}
\DoxyCodeLine{00295\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 1;}
\DoxyCodeLine{00296\ \ \ \ \ \}}
\DoxyCodeLine{00297\ \ \ \ \ \textcolor{comment}{/*\ Otherwise,\ redo\ the\ search\ inside\ a\ narrower\ range.\ */}}
\DoxyCodeLine{00298\ \ \ \ \ search\_width\ =\ best\ *\ pe-\/>search\_range\ /\ 32768;}
\DoxyCodeLine{00299\ \ \ \ \ \textcolor{comment}{/*\ printf("{}Search\ width\ =\ \%d\ *\ \%.2f\ =\ \%d\(\backslash\)n"{},}}
\DoxyCodeLine{00300\ \textcolor{comment}{\ \ \ \ \ \ \ best,\ (double)pe-\/>search\_range/32768,\ search\_width);\ */}}
\DoxyCodeLine{00301\ \ \ \ \ \textcolor{keywordflow}{if}\ (search\_width\ ==\ 0)\ search\_width\ =\ 1;}
\DoxyCodeLine{00302\ \ \ \ \ low\_period\ =\ best\ -\/\ search\_width;}
\DoxyCodeLine{00303\ \ \ \ \ high\_period\ =\ best\ +\ search\_width;}
\DoxyCodeLine{00304\ \ \ \ \ \textcolor{keywordflow}{if}\ (low\_period\ <\ 0)\ low\_period\ =\ 0;}
\DoxyCodeLine{00305\ \ \ \ \ \textcolor{keywordflow}{if}\ (high\_period\ >\ pe-\/>frame\_size\ /\ 2)\ high\_period\ =\ pe-\/>frame\_size\ /\ 2;}
\DoxyCodeLine{00306\ \ \ \ \ \textcolor{comment}{/*\ printf("{}Searching\ from\ \%d\ to\ \%d\(\backslash\)n"{},\ low\_period,\ high\_period);\ */}}
\DoxyCodeLine{00307\ \ \ \ \ best\ =\ thresholded\_search(pe-\/>diff\_window[pe-\/>wcur],}
\DoxyCodeLine{00308\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ pe-\/>search\_threshold,}
\DoxyCodeLine{00309\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ low\_period,\ high\_period);}
\DoxyCodeLine{00310\ \ \ \ \ best\_diff\ =\ pe-\/>diff\_window[pe-\/>wcur][best];}
\DoxyCodeLine{00311\ }
\DoxyCodeLine{00312\ \ \ \ \ \textcolor{keywordflow}{if}\ (out\_period)}
\DoxyCodeLine{00313\ \ \ \ \ \ \ \ \ *out\_period\ =\ (best\ >\ 65535)\ ?\ 65535\ :\ best;}
\DoxyCodeLine{00314\ \ \ \ \ \textcolor{keywordflow}{if}\ (out\_bestdiff)}
\DoxyCodeLine{00315\ \ \ \ \ \ \ \ \ *out\_bestdiff\ =\ (best\_diff\ >\ 65535)\ ?\ 65535\ :\ best\_diff;}
\DoxyCodeLine{00316\ }
\DoxyCodeLine{00317\ \ \ \ \ \textcolor{comment}{/*\ Increment\ the\ current\ pointer.\ */}}
\DoxyCodeLine{00318\ \ \ \ \ \textcolor{keywordflow}{if}\ (++pe-\/>wcur\ ==\ pe-\/>wsize)}
\DoxyCodeLine{00319\ \ \ \ \ \ \ \ \ pe-\/>wcur\ =\ 0;}
\DoxyCodeLine{00320\ \ \ \ \ \textcolor{keywordflow}{return}\ 1;}
\DoxyCodeLine{00321\ \}}

\end{DoxyCode}
