% Created 2023-11-12 Sun 13:27
% Intended LaTeX compiler: pdflatex
\documentclass[colorlinks,aspectratio=169]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{color}
\usepackage{listings}
\usepackage{pdfpages}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{xspace}
\usepackage{libertine}
\usepackage{fontawesome}
\mode<beamer>{\usetheme{Boadilla}}
\setbeamertemplate{navigation symbols}{}
\setbeamerfont{alerted text}{series=\bfseries}
\setbeamercolor{alerted text}{fg=blue}
\usecolortheme{default}
\usefonttheme{serif}
\usefonttheme{professionalfonts}
\lstloadlanguages{[ISO]C++}
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.50,0,0.82}
\definecolor{codeorange}{rgb}{0.95,0.3,0.1}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\lstdefinestyle{mystyle}{
commentstyle=\color{codegreen},
keywordstyle=\color{codeorange},
numberstyle=\tiny\color{codegray},
stringstyle=\color{codepurple},
basicstyle=\footnotesize,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2
}
\lstset{style=mystyle}
\usepackage{siunitx}
\usetheme{default}
\author{Brett Viren}
\date{\today}
\title{Sample rate conversion for DUNE FD VD TPC ADC}
\hypersetup{
 pdfauthor={Brett Viren},
 pdftitle={Sample rate conversion for DUNE FD VD TPC ADC},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 29.1 (Org mode 9.6.6)}, 
 pdflang={English}}
\begin{document}

\maketitle

\begin{frame}[label={sec:org969662d}]{Context}
\begin{block}{DUNE FD VD is split into two halves}
\begin{enumerate}
\item Top Detector Electronics (TDE)

\item Bottom Detector Electronics (BDE)
\end{enumerate}
\end{block}

\begin{block}{ADC sampling parameters}
\begin{center}
\begin{tabular}{|r|r|r|r|}
sampling & period (ns) & rate (MHz) & ADC (bits)\\[0pt]
\hline
FD VD TDE & 512 & 1.953125 & 14\\[0pt]
\hline
FD VD BDE & 500 & 2.000000 & 14\\[0pt]
\hline
everyone else & 500 & 2.000000 & 12\\[0pt]
\hline
\end{tabular}
\end{center}
\end{block}
\end{frame}

\begin{frame}[label={sec:orgb11d19f}]{To do: official VD sampling numbers}
VD CDR is somewhat unclear
\begin{itemize}
\item says, ``12 bit ADC, 2 MHz or 14 bit ADC, 2.5 MHz or more''
\item talks about downsampling from 2.5 MHz+ in FPGA.
\item table 7.2: 14 bit ADC, 1.953125 MHz

\alert{Must find a definitive and public reference!}
\end{itemize}
\end{frame}

\begin{frame}[label={sec:org17a5c93},fragile]{Problems with different sample rates in DUNE FD VD TDE vs BDE}
 \begin{itemize}
\item Either \alert{same readout time} (\(\approx \qty{4}{ms}\)) \texttt{XOR} \alert{same \#samples} (\(N \approx\) 8k), can not have both.
\item Sloppy matching of 3D clusters across top/bottom boundary.
\item Two separate field response calculations despite same CRP design.
\item Two \texttt{tick} values floating around the configuration.
\end{itemize}
\end{frame}

\begin{frame}[label={sec:org0f7ffc6}]{Solution: Apply \alert{sample rate conversion} (SRC) methods}
\[F=\frac{1}{T} \to F'=\frac{1}{T'},\ R\equiv \frac{F'}{F} = \frac{T}{T'}\]

\begin{block}{We need an \alert{upsampled interpolation}}
\begin{itemize}
\item Sample period: \(T=\qty{512}{ns} \to T'=\qty{500}{ns},\ R = \frac{512}{500} = 1.024\)
\item Nyquist frequency: \(\qty{0.9765625}{MHz} \to \qty{1.000000}{MHz}\)
\item Nyquist-Shannon sampling theorem tells us, \emph{``You can reconstruct original signal exactly.''}
\end{itemize}
\end{block}


\begin{block}{Any interpolation produces \alert{artifacts} at some level.}
\begin{itemize}
\item Must demonstrate validity.
\end{itemize}
\end{block}
\end{frame}

\begin{frame}[label={sec:org631734b}]{Historical sample rate conversion (SRC)}
Schafer, R.W. and Rabiner, L.R., Proc. IEEE, 1973.

\begin{quote}
An irrational SRC ratio can be approximated by a rational ratio \(R\) to arbitrary accuracy.
\end{quote}

Rational interpolation = integral upsample + integral downsample.

\begin{itemize}
\item \alert{integral upsample} \(N \to N^{iu} = LN\)
\item \alert{integral downsample}  \(N \to N^{id} = N/M\)
\item \alert{rational interpolation} \(N \to N^{ri} = RN,\ R\equiv \frac{L}{M}\)
\end{itemize}

Here, all natural numbers, \(N,N^x,L,M \in \mathbb{N}\) and rational \(R\).
\end{frame}


\begin{frame}[label={sec:org56c11d7}]{Integral upsampling \(N \to N^{iu} = LN\)}
\begin{block}{Procedure}
\begin{enumerate}
\item Apply \(DFT\)
\item \alert{Insert} \((L-1)N\) zero-amplitude high frequency samples
\item Apply \(DFT^{-1}\).
\end{enumerate}
\end{block}

\begin{block}{}
Insertion is at the Nyquist frequency and must preserve Hermitian symmetry.
\begin{itemize}
\item half of the samples inserted above ``most positive'' frequency, half below ``most negative''.
\end{itemize}
\end{block}
\end{frame}

\begin{frame}[label={sec:org4a18eba}]{Integral downsampling \(N \to N^{id} = N/M\)}
\begin{block}{Procedure}
\begin{enumerate}
\item Select every \(M^{th}\) sample.
\end{enumerate}
\end{block}

\begin{block}{}
\begin{itemize}
\item Aliasing effect if signal bandwidth \(>F^{id}_{nyquist}\)
\item No practical problem if \(\frac{N}{M} \notin \mathbb{N}\).
\begin{itemize}
\item Ignore that there may be \(< M-1\) original samples past the last resample.
\end{itemize}
\end{itemize}
\end{block}
\end{frame}

\begin{frame}[label={sec:orgfcdeaa1}]{Rational interpolation}
How to assure \(\frac{LN}{M}\) is an integer?

Either we get ``lucky'' or we must ``over upsample'', eg:

\[T=\qty{512}{ns}, T'=\qty{500}{ns}, N=9600, \frac{LN}{M} = \frac{128\cdot 384}{5}\]

Upsample by another factor of 5: \(L \to L' = 5*L = 5\cdot 128\).

The conventional method then needs \(DFT^{-1}\) of size \(n = 5 \cdot 128 \cdot 9600 = 6144000\) followed by \(5\times\) larger downsampling, \(M \to M' = 5*M\).
\end{frame}


\begin{frame}[label={sec:org0d42cbe}]{Optimization}
If we allow freedom to choose \(N\) we may be able to minimize \(L\) and \(M\) while keeping \(\frac{LN}{M} \in \mathbb{N}\).

We want to shift the Nyquist frequency by:

\[ \Delta F= F_{nyquist} - F'_{nyquist} = \frac{T-T'}{2TT'}\]

And we want that shift to be covered by an integral number \(n\) of bins of size \(\delta F = \frac{1}{NT}\).

So, we want \(n\cdot\delta F = \Delta F\) which is solved by:

\[ N = \frac{2nT'}{T-T'} = \frac{n}{3}\cdot 250\]

With the numbers plugged in for the DUNE VD problem.

Thus for \(N\) any multiple of 250, we can assure the downsampling will be exactly integral.
\end{frame}

\begin{frame}[label={sec:orgea3b79f}]{Exact integer downsampling \(N \to N^{id} = N/M \in \mathbb{N}\)}
When downsampling is exact, performing in frequency space is possible and advantageous.

\begin{block}{Procedure}
\begin{enumerate}
\item Apply \(DFT\)
\item \alert{Remove} \((M-1)N\) high frequency samples
\item Apply \(DFT^{-1}\)
\end{enumerate}
\end{block}

\begin{block}{}
This implicitly applies a \alert{perfect low-pass filter}.
\begin{itemize}
\item Though, that doesn't matter when \(R>1\).
\end{itemize}

Similar to insertion for upsampling, we must remove half the samples at the
``most positive'' and half at the ``most negative'' part of the spectrum.
\end{block}
\end{frame}

\begin{frame}[label={sec:org4893d6d}]{Optimized procedure}
\begin{enumerate}
\item Calcualte desired \(N\) and \(n\).
\item Truncate or pad original signal to \(N\)
\item Apply \(DFT\)
\item Insert \(2n\ (R>1)\) or remove \(2n\ (R<1)\) frequency samples.
\item Apply \(DFT^{1}\)
\item Fix normalization
\end{enumerate}
\end{frame}


\begin{frame}[label={sec:org0f718b7}]{Normalization - conserve Parseval energy and ionization charge}
\begin{block}{\(DFT\) is not normalized, \(DFT^{-1}\) is normalized by \(\frac{1}{N'}\)}
But we change \(N \to N' = \frac{LN}{M}\) while ading no spectral energy.
Need renormalize by \(\frac{N'}{N} = R\)
\end{block}

\begin{block}{ADC is an integral of current over time}
Normally, a sample is the value of a function at a point.  But, an ADC sample is
an integral of current over the sample period.  Interpolating changes the integration bounds.  We must also renormalize by \(\frac{T'}{T} = \frac{1}{R}\).
\end{block}

\begin{block}{So, it seems we do nothing!}
But, do check this is correct and that Parseval energy is conserved: \(\sum_i^N |x_i|^2\).
\end{block}
\end{frame}

\begin{frame}[label={sec:orgfdffa53}]{Validation}
\begin{block}{Why?}
\begin{itemize}
\item For upsampling \((R>1)\) the method is ``valid'' by construction in that it is a lossless transformation (Nyquist-Shannon sampling theorem).

\item However, any interpolation produces artifacts.

\item And, we should illustrate the method and confirm it is bug free.
\end{itemize}
\end{block}

\begin{block}{Things to check}
\begin{itemize}
\item Generate ``truth'' waveforms and sample at 512 and 500ns.
\item Perform signal rate conversion.
\item Check Parseval energy conservation.
\item Plot interval and frequency space signals at each step.
\item Visual check for artifacts
\item \ldots{}
\end{itemize}
\end{block}
\end{frame}
\end{document}