\hypertarget{HDE_8c}{
\section{HDE.c File Reference}
\label{HDE_8c}\index{HDE.c@{HDE.c}}
}
Implementation of the routines to encodes a distribution of symbols using a Huffman encoding technique. Routines are available to encode both the distribution itself and the resulting Huffman table. 

{\tt \#include \char`\"{}LDT/HDE.h\char`\"{}}\par
{\tt \#include \char`\"{}LDT/HUFF.h\char`\"{}}\par
{\tt \#include \char`\"{}LDT/BFP.h\char`\"{}}\par
{\tt \#include \char`\"{}ffs.h\char`\"{}}\par
{\tt \#include \char`\"{}dprintf.h\char`\"{}}\par
{\tt \#include $<$math.h$>$}\par


Include dependency graph for HDE.c:\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=215pt]{HDE_8c__incl}
\end{center}
\end{figure}
\subsection*{Data Structures}
\begin{CompactItemize}
\item 
struct \hyperlink{struct__HistLimits}{\_\-Hist\-Limits}
\begin{CompactList}\small\item\em Min and Max indices of a histogram. \item\end{CompactList}\end{CompactItemize}
\subsection*{Defines}
\begin{CompactItemize}
\item 
\hypertarget{HDE_8c_a0}{
\#define {\bf NULL}~((void $\ast$)(0))}
\label{HDE_8c_a0}

\end{CompactItemize}
\subsection*{Typedefs}
\begin{CompactItemize}
\item 
typedef \hyperlink{struct__HistLimits}{\_\-Hist\-Limits} \hyperlink{HDE_8c_a1}{Hist\-Limits}
\begin{CompactList}\small\item\em Typedef for struct \hyperlink{struct__HistLimits}{\_\-Hist\-Limits}. \item\end{CompactList}\end{CompactItemize}
\subsection*{Functions}
\begin{CompactItemize}
\item 
\hypertarget{HDE_8c_a2}{
static \_\-\_\-inline \hyperlink{struct__HistLimits}{Hist\-Limits} {\bf hist\_\-limits\_\-find} (const unsigned short int $\ast$hist, int cnt)}
\label{HDE_8c_a2}

\item 
static \_\-\_\-inline int \hyperlink{HDE_8c_a3}{hist\_\-copy} (unsigned int $\ast$dst, const unsigned short int $\ast$src, int min, int max)
\begin{CompactList}\small\item\em Copies the core of the histogram (i.e. from the first non-zero element to the last non-zero element. \item\end{CompactList}\item 
static \_\-\_\-inline unsigned int \hyperlink{HDE_8c_a4}{huff\_\-table\_\-encode} (unsigned int $\ast$buf, unsigned int pos, const \hyperlink{struct__HUFF__code}{HUFF\_\-code} $\ast$codes, const unsigned int $\ast$freq, int nentries, int nbits\_\-min, int min)
\begin{CompactList}\small\item\em Encodes the Huffman encoding table. \item\end{CompactList}\item 
static \_\-\_\-inline unsigned int \hyperlink{HDE_8c_a5}{huff\_\-symbols\_\-encode} (unsigned int $\ast$buf, unsigned int pos, const \hyperlink{struct__HUFF__code}{HUFF\_\-code} $\ast$codes, const short int $\ast$symbols, int nsymbols)
\begin{CompactList}\small\item\em Encodes the pedestal subtracted ADC values using the specified Huffman codes. \item\end{CompactList}\item 
\hypertarget{HDE_8c_a6}{
{\bf \_\-declare} (static double calc\_\-nbits(const int $\ast$f, int cnt, int tot))}
\label{HDE_8c_a6}

\item 
unsigned int \hyperlink{HDE_8c_a7}{HDE\_\-encode\-SS} (unsigned int $\ast$buf, unsigned int pos, const unsigned short int $\ast$hist, int nbins, const short int $\ast$symbols, int nsymbols)
\begin{CompactList}\small\item\em Encodes the specified symbols {\em symbols\/} using the specified histogram of frequencies. Both the symbols and the histogram are presented as 16-bit values. \item\end{CompactList}\item 
double \hyperlink{HDE_8c_a8}{calc\_\-nbits} (const int $\ast$f, int cnt, int tot)
\begin{CompactList}\small\item\em Calculates the number of bits need to encode a sample with the specified frequency distribution. \item\end{CompactList}\end{CompactItemize}


\subsection{Detailed Description}
Implementation of the routines to encodes a distribution of symbols using a Huffman encoding technique. Routines are available to encode both the distribution itself and the resulting Huffman table. 

\begin{Desc}
\item[Author:]JJRussell - \href{mailto:russell@slac.stanford.edu}{\tt russell@slac.stanford.edu}\end{Desc}


\footnotesize\begin{verbatim}   CVS $Id: HDE.c,v 1.2 2006/09/13 17:58:41 russell Exp $
\end{verbatim}
\normalsize


\subsection{Typedef Documentation}
\hypertarget{HDE_8c_a1}{
\index{HDE.c@{HDE.c}!HistLimits@{HistLimits}}
\index{HistLimits@{HistLimits}!HDE.c@{HDE.c}}
\subsubsection[HistLimits]{\setlength{\rightskip}{0pt plus 5cm}static \_\-\_\-inline \hyperlink{struct__HistLimits}{Hist\-Limits}}}
\label{HDE_8c_a1}


Typedef for struct \hyperlink{struct__HistLimits}{\_\-Hist\-Limits}. 

cal\_\-hist\_\-limits\_\-find (const unsigned short int $\ast$hist, int cnt)

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em hist}]The target histogram \item[{\em cnt}]The total number of entries in the histogram\end{description}
\end{Desc}


\subsection{Function Documentation}
\hypertarget{HDE_8c_a8}{
\index{HDE.c@{HDE.c}!calc_nbits@{calc\_\-nbits}}
\index{calc_nbits@{calc\_\-nbits}!HDE.c@{HDE.c}}
\subsubsection[calc\_\-nbits]{\setlength{\rightskip}{0pt plus 5cm}static double calc\_\-nbits (const int $\ast$ {\em f}, int {\em cnt}, int {\em tot})}}
\label{HDE_8c_a8}


Calculates the number of bits need to encode a sample with the specified frequency distribution. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em f}]The frequency distribution \item[{\em cnt}]The count of entries in the frequency distribution \item[{\em tot}]The total number of entries in the frequency distribution\end{description}
\end{Desc}
This is based on the following formula

nbits = - TOT $\ast$ SUM \mbox{[} f\mbox{[}i\mbox{]} / TOT $\ast$ ln (f\mbox{[}i\mbox{]} / TOT) \mbox{]} = - TOT/TOT $\ast$ SUM \mbox{[} f\mbox{[}i\mbox{]} $\ast$ (ln (f\mbox{[}i\mbox{]}) - ln (TOT)) \mbox{]} = - SUM \mbox{[} f\mbox{[}i\mbox{]} $\ast$ ln (f\mbox{[}i\mbox{]} \mbox{]} + SUM \mbox{[} f\mbox{[}i\mbox{]} $\ast$ ln (TOT) \mbox{]} = - SUM \mbox{[} f\mbox{[}i\mbox{]} $\ast$ ln (f\mbox{[}i\mbox{]} \mbox{]} + TOT $\ast$ ln (TOT) \mbox{]}\hypertarget{HDE_8c_a7}{
\index{HDE.c@{HDE.c}!HDE_encodeSS@{HDE\_\-encodeSS}}
\index{HDE_encodeSS@{HDE\_\-encodeSS}!HDE.c@{HDE.c}}
\subsubsection[HDE\_\-encodeSS]{\setlength{\rightskip}{0pt plus 5cm}unsigned int HDE\_\-encode\-SS (unsigned int $\ast$ {\em buf}, unsigned int {\em pos}, const unsigned short int $\ast$ {\em hist}, int {\em nbins}, const short int $\ast$ {\em symbols}, int {\em nsymbols})}}
\label{HDE_8c_a7}


Encodes the specified symbols {\em symbols\/} using the specified histogram of frequencies. Both the symbols and the histogram are presented as 16-bit values. 

\begin{Desc}
\item[Returns:]The updated bit write position\end{Desc}
\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em buf}]The output buffer address \item[{\em pos}]The current write bit offset \item[{\em hist}]The frequency histogram \item[{\em nbins}]The number of bins in the histogram \item[{\em symbols}]The array of symbols \item[{\em nsymbols}]The number of symbols\end{description}
\end{Desc}
\hypertarget{HDE_8c_a3}{
\index{HDE.c@{HDE.c}!hist_copy@{hist\_\-copy}}
\index{hist_copy@{hist\_\-copy}!HDE.c@{HDE.c}}
\subsubsection[hist\_\-copy]{\setlength{\rightskip}{0pt plus 5cm}static \_\-\_\-inline int hist\_\-copy (unsigned int $\ast$ {\em dst}, const unsigned short int $\ast$ {\em src}, int {\em min}, int {\em max})\hspace{0.3cm}{\tt  \mbox{[}static\mbox{]}}}}
\label{HDE_8c_a3}


Copies the core of the histogram (i.e. from the first non-zero element to the last non-zero element. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em dst}]The destination histogram \item[{\em src}]The source histogram \item[{\em min}]The first non-zero histogram bin \item[{\em max}]The last non-zero histogram bin\end{description}
\end{Desc}
This routine exists solely because the HUFF build routines only take an integer (not a short integer) frequency distribution.\hypertarget{HDE_8c_a5}{
\index{HDE.c@{HDE.c}!huff_symbols_encode@{huff\_\-symbols\_\-encode}}
\index{huff_symbols_encode@{huff\_\-symbols\_\-encode}!HDE.c@{HDE.c}}
\subsubsection[huff\_\-symbols\_\-encode]{\setlength{\rightskip}{0pt plus 5cm}static \_\-\_\-inline unsigned int huff\_\-symbols\_\-encode (unsigned int $\ast$ {\em buf}, unsigned int {\em pos}, const \hyperlink{struct__HUFF__code}{HUFF\_\-code} $\ast$ {\em codes}, const short int $\ast$ {\em symbols}, int {\em nsymbols})\hspace{0.3cm}{\tt  \mbox{[}static\mbox{]}}}}
\label{HDE_8c_a5}


Encodes the pedestal subtracted ADC values using the specified Huffman codes. 

\begin{Desc}
\item[Returns:]The updated write bit position\end{Desc}
\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em buf}]The output buffer \item[{\em pos}]The position of the next bit to fill \item[{\em codes}]The Huffman codes \item[{\em symbols}]The symbols to encode \item[{\em nsymbols}]The number of symbols to encode\end{description}
\end{Desc}
\hypertarget{HDE_8c_a4}{
\index{HDE.c@{HDE.c}!huff_table_encode@{huff\_\-table\_\-encode}}
\index{huff_table_encode@{huff\_\-table\_\-encode}!HDE.c@{HDE.c}}
\subsubsection[huff\_\-table\_\-encode]{\setlength{\rightskip}{0pt plus 5cm}static \_\-\_\-inline unsigned int huff\_\-table\_\-encode (unsigned int $\ast$ {\em buf}, unsigned int {\em pos}, const \hyperlink{struct__HUFF__code}{HUFF\_\-code} $\ast$ {\em codes}, const unsigned int $\ast$ {\em freq}, int {\em cnt}, int {\em nbits\_\-min}, int {\em min})\hspace{0.3cm}{\tt  \mbox{[}static\mbox{]}}}}
\label{HDE_8c_a4}


Encodes the Huffman encoding table. 

\begin{Desc}
\item[Returns:]The updated write bit position\end{Desc}
\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em buf}]The output buffer \item[{\em pos}]The position of the next bit to fill \item[{\em codes}]The Huffman codes \item[{\em freq}]The frequency distribution. This is used to determine valid members. \item[{\em cnt}]The count of codes \item[{\em nbits\_\-min}]The number bits to use in encoding the minimum, {\em min\/} If 0, the minimum offset will not be encoded If -1, the number of bits will be computed from {\em min\/} itself and stored as a 5-bit number \item[{\em min}]The offset of the table. This value should be added onto the decoded symbol\end{description}
\end{Desc}
The encoding is

\begin{itemize}
\item nbits\_\-min: 5, number of bits used to encode the minimum symbol value (fixed 5 bits)\item min: nbits\_\-min, the minumum symbol value\item nbits\_\-rl : 5, number of bits needed to encode the maximum run length, if 0, then this is a 0 entropy distribution with the only value of 'min'\item rl\mbox{[}0\mbox{]} : Run length of the run of symbols\item rl\mbox{[}1\mbox{]} : Run length till the next block of symbols, if 0, then finished. Note that the finish must always be on an odd run length index.\item nbits\_\-cl : 5, number of bits needed to hold the maximum code length\item cl\mbox{[}0\mbox{]} : nbits\_\-cl, the code length of the first symbol\item cl\mbox{[}1\mbox{]} : nbits\_\-cl, the code length of the second symbol\item .\item .\item cl\mbox{[}n\mbox{]} : nbits\_\-cl, the code length of the last symbol, the number of symbols is derived from the run length decoding\end{itemize}
