\section{Uncertainties}
\label{891_2:sec:uncertainty}

Uncertainties in our derived parameters ($\tau_L$, $Z_L$, $A_V$) come
from a number of sources: the precision and accuracy of the data
(e.g., random errors and flux calibration, respectively); degeneracies
between age, metallicity, and extinction; and systematic uncertainty
in our interpretation of the results of the fit. In the context of our
DFK basis set, the latter consists of the assignment of the effective
age of each template, which then propagates into the mass- or
light-weighted mean age and metallicity.  In this section we quantify
the magnitude of these uncertainties, first looking at the coupling
between random errors in the data and covariance in age, metallicity,
and extinction.

\subsection{Stochastic Uncertainty and Covariance}
\label{891_2:sec:fit_err}

\begin{figure*}
  \centering
  \includegraphics[width=0.48\textwidth]{891_2/figs/MC_covarcont_below.pdf}
  \includegraphics[width=0.48\textwidth]{891_2/figs/MC_covarcont_above.pdf}
  \caption[Covariance between uncertainties in $\tau_L$, $Z_L$, and
    $A_V$]{\fixspacing\label{891_2:fig:MC_covar}Derived parameters for
    all iterations of the Monte Carlo noise perturbations for all
    apertures. To remove real differences in values between apertures
    the values for each aperture are zeroed to the mean of the 100
    noise perturbations. \emph{left:} Data for apertures below
    \val{0.4}{kpc}. \emph{right:} Data for apertures above
    \val{0.4}{kpc}. The contour levels are 0.5\%, 10\%, 40\%, 70\%,
    and 90\%.}
\end{figure*}

Stochastic uncertainty in ($\tau_L$, $Z_L$, $A_V$) that arises during
full spectral fitting is driven by two effects: uncertainty in the
true shape of our galaxy SED and degeneracies between age, extinction,
and metallicity. The uncertainty of the SED of NGC 891 is already
quantified; it is exactly the error spectrum computed as part of the
data reduction described in Chapter \ref{chap:891_1} and accounts for
uncertainty in wavelength calibration, sky subtraction, and flux
calibration.

To quantify uncertainties caused by degeneracies in the fit parameters
we construct a set of 100 Monte Carlo iterations of each galaxy
spectrum. For each iteration the galaxy flux at a specific wavelength,
$f(\lambda)$, is perturbed by an amount randomly chosen from a
normally distributed set of values with a width equal to the error
spectrum computed during data reduction. Thus, the $k$th Monte Carlo
spectrum is $f_k'(\lambda) = f(\lambda) + r(e(\lambda))$, where
$r(\sigma)$ represents a normal distribution with width $\sigma$ and
$e(\lambda)$ is the galaxy error spectrum. Each of the $f_k'(\lambda)$
spectra are then fit with the identical method and SSP library
(\S\ref{891_2:sec:final_SSP}). The ``true'' value of the fit
parameters is then taken to be the mean over all 100 iterations while
the total fit uncertainty (i.e., stochastic plus degeneracies) is
defined by an ellipse in ($\tau_L$,$Z_L$,$A_V$) space that contains
68\% of all Monte Carlo iterations (i.e., a 68\% phase-space
confidence interval). The reported uncertainties for each individual
parameter are the projection of the limits of this ellipse onto the
specific parameter. These uncertainties are shown in Figure
\ref{891_2:fig:MC_covar}.

The decision to perform Monte Carlo perturbations on our data rather
than the best fit model is similar to the approach taken by PIPE3D
\citep{Sanchez16}, and we feel it yields important
information. Namely, it allows us to quantify how any systematic
differences between our data and the models couple with random
uncertainties in the data. In other words, it fully encapsulates all
sources of uncertainty that affect the final fits.

%+++++++++++++++++
%% {\bf MAB: above par. may be a bit redundant with intro par. for the
%%   section, but I would keep the intro par. as is and modify here
%%   accordingly. Pay attention to your use of the term ``shape of the
%%   galaxy data.'' I think you might mean the ``shape of the spectral
%%   energy distribution'' but it is important to be clear if you mean
%%   low or high frequency. I think low frequency is relevant in terms of
%%   flux calibration errors and couples most directly to uncertainties
%%   in age and extinction. The higher-frequency errors associated with
%%   shot noise or sky-subtraction are more likely to impact age and
%%   metallicity.  It might be useful to make these distinctions and
%%   discuss here. Also you should note if flux-cal uncertainties have or
%%   have not been included in the analysis, and if not, why not, or why
%%   we don't care.}

The primary degeneracy in our fit parameters is the well known
age/metallicity degeneracy \citep[see, for
example,][]{Oconnel76,Aaronson78,Worthey94,dePaz02}, whereby a mix of
younger, more metal-rich populations can be spectrally
indistinguishable from a mix of older, more metal-poor
populations. This correlation is especially relevant to our work given
our attempt to simultaneously fit SSPs with multiple metallicities and
ages.

Secondary degeneracies exist between extinction and both age and
metallicity, the amplitudes of which are dependent on the overall age
of the model spectrum. In models with a high fraction of young SSPs
extinction is strongly correlated with age. This is due to the fact
that the stars dominating the blue-visible portion of the spectrum in
these populations are so hot that any metals present will be
sufficiently ionized that their contribution to stellar atmospheric
opacity is negligible. Thus the only way to change the slope of the
continuum is to either vary age or attenuation.

%% Furthermore, in the absence of extinction
%% older populations are redder due to cooler temperatures and increased
%% line blanketing, but younger populations can be extincted to match,
%% over large wavelength ranges, the general continuum shape of old
%% populations. For single populations this effect is minimal; absorption
%% lines can differentiate different population ages independent of
%% continuum shape, but in composite galaxy spectra made from the
%% superposition of many SSPs (i.e., our spectral fits) the amount of
%% light coming from old populations can be decreased (although not
%% eliminated) be increasing the extinction.

In intermediate-aged populations metal absorption lines make up a key
component of the opacity in stellar atmospheres and a higher
concentration of metals absorption lines will ``redden'' the spectrum
through increased line-blanketing at bluer wavelengths \citep[see,
e.g., Figure 4 in ][]{Vazdekis10}. In this regime there is a strong
negative correlation between metallicity and extinction because the
model spectrum can be reddened by increasing the strength of these
metal lines. The left panel of Figure \ref{891_2:fig:MC_covar} shows
this effect in the ($A_V$, $Z_L$) plane. As we will see in
\S\ref{891_2:sec:SFH}, below \val{0.4}{kpc} most of the measured light
comes from intermediate populations where the correlation between
$A_V$ and $Z_L$ is strongest.

There is a limit to this effect, however; the very oldest SSPs are
cool enough that there is significant metal-line reddening regardless
of the overall metallicity (at least within the range of metallicities
considered here) and increasing the total metallicity will not
appreciably change the shape of the continuum. This effect can be seen
in the right panel of Figure \ref{891_2:fig:MC_covar}; there is very little
correlation between $Z_L$ and $A_V$ in regions dominated by the Old
DFK bin.

%% A secondary degeneracy exists between metallicity and extinction. A
%% pristine spectrum can be reddened either through the presense of
%% wavelength dependent attenuation (i.e., classical reddening) or
%% through the addition of strong metal absorprion lines. Importantly,
%% there is an age dependence to this effect; young populations are so
%% hot that any metals present will be fully ionized and therefore won't
%% contribute to the total opacity so the slope of the continuum is only
%% weakly dependent on metallicity. In intermediate-aged and old
%% populations, on the other hand, metal absorption lines make up a key
%% component of the opacity in stellar atmospheres and a higher
%% concentration of metals will ``redden'' the spectrum by removing
%% black-body continuum from strong absorption features blueward of
%% \val{\asim 5000}{\AA} (e.g., Fe, Mg, G-band, Ca). %Figure here?

%++++++++++++++++++++++++
%% {\bf MAB: You have everything right here, but the paragraph needs some
%%   re-arrangement. I would lead by saying that age and Z both play off
%%   $A_V$, but the relative importance depends on age. I.e., at young
%%   ages (probably $<$ 0.5-1 Gyr) age reigns supreme in wagging $A_V$
%%   and vice versa. For older ages, it's Z vs $A_V$. I think this will
%%   set the stage for your covariance plots latter. ``through the
%%   addition of strong metal absorprion lines'' should become ``through
%%   increased line-blanketing in stellar photospheres at bluer
%%   wavelengths due to strengthened metal absorption lines.'' You
%%   actually say this latter. Like I said, the words are there! I
%%   wouldn't call out the strong lines in terms of the line-blanketing
%%   effect, but rather the forest of weak lines that supreses the real
%%   continuum to a redder pseudo-continuum level. I think Conroy shows
%%   this pseudo-continuum concept in his review, to which you could
%%   refer.}

%% Finally, there can be degeneracies between age and extinction. In the
%% absence of extinction older populations are redder due to cooler
%% temperatures and increased metal absorption, but younger populations
%% can be extincted to match, over large wavelength ranges, the general
%% continuum shape of old populations. For single populations this effect
%% is minimal; absorption lines can differentiate different population
%% ages independent of continuum shape, but in composite galaxy spectra
%% made from the superposition of many SSPs (i.e., our spectral fits) the
%% amount of light coming from old populations can be decreased (although
%% not eliminated) be increasing the extinction.

%++++++++++++++++++
%% {\bf MAB: I think you can wrap this paragraph into the previous one
%%   along the lines noted. Also, since we've mentioned that there is an
%%   age-Z degeneracy, it is probably worth stating that the coarse age
%%   differentiation for when $A_V$ is degenerate with age vs when it is
%%   more degenerate with $Z$ is sufficiently large that the age-$Z$
%%   degeneracy is no significant in altering the discussion.}

%+++++++++++++++++
%% {\bf MAB: You want to say something about the approach of using the
%%   data instead of the models for the MC here. (a) This requires you
%%   smooth the data and models (say how much); but (b) it allows you to
%%   determine how any systematics differences between data and models
%%   couples to random errors in the data -- somthing that can't be done
%%   from doing MCs of the models. And just so we aren't attacked by the
%%   Spanish Armada....I think there is a reference to Sanchez who does
%%   the same. We must reference if so.}

\begin{figure*}
  \centering
  \includegraphics[width=0.31\textwidth]{891_2/figs/fit_uncertainty_MLWA.pdf}
  \includegraphics[width=0.31\textwidth]{891_2/figs/fit_uncertainty_MLWZ.pdf}
  \includegraphics[width=0.31\textwidth]{891_2/figs/fit_uncertainty_TAUV.pdf}
  \caption[Distributions of fit uncertainties in $\tau_L$, $Z_L$,
    $A_V$]{\fixspacing\label{891_2:fig:fit_err_hist}Distribution of
    parameter uncertainties in age, metallicity, and extinction
    derived from full spectral fitting.  The uncertainties are 68\%
    confidence limits on all three parameters simultaneously based on
    the Monte Carlo perturbations described above.}
\end{figure*}

As shown in Figure \ref{891_2:fig:MC_covar}, the majority of apertures
our quantities of interest show the expected degeneracies discussed
above (negative age/metallicity correlation, negative age/extinction
correlation, and negative metallicity/extinction correlation) however
there are large group of apertures that exhibit a \emph{positive}
correlation between age and metallicity (i.e., increasing the
metallicity leads to fits that have older ages). This seemingly
backward trend is reasonable (and expected) in the context of how the
reddening affects different aged populations. Apertures with a
positive age/metallicity relation are concentrated below the age break
at \val{0.4}{kpc} (Chapter \ref{chap:891_1}) and therefore have a high
fraction of total flux contributed by young SSPs (although,
importantly, I2 and Old populations still contribute the majority of
the light, a la \S\ref{891_2:sec:SFH}). As discussed above, young SSPs
have a continuum shape that is only weakly dependent on metallicity,
so reddening is achieved by increasing $A_V$. Thus, as the fraction of
Young SSPs increases more extinction is needed to redden the models to
the level of the data. This increase in total extinction (for a given
aperture) then requires the I2 and Old SSPs to have lower
metallicities to counteract the extinction-induced reddening. This
decreases the total metallicity, $Z_L$, because (as shown in
\S\ref{891_2:sec:SFH}) I2 and Old SSPs still contribute the majority
of the light at all points in the galaxy. Thus, the positive
age/metallicity correlation at small heights is the result of the fact
that metallicity is a weighted average that is mostly dominated by
older SSPs, and that a single extinction value, $A_V$, is applied to
all SSPs in a particular fit.

%% {\bf MAB: I am not sure I buy your conclusion, so I recommend we redo
%%   this starting with the parts we do agree on, and then get to the
%%   more challenging bits. So start with a first-order description of
%%   Fig \ref{891_2:fig:MC_covar}, as follows:

%% $\bullet$ $A_V$-age: negative at all heights; stronger at lower $z$

%% $\bullet$ $A_V$-Z: negative at hi-$z$; flat at low-$z$.

%% $\bullet$ Z-age: more weakly correlated at low $z$, with a hint that
%%   it is a postive correlation; strongly negative correlation at
%%   hi-$z$.  Concerning the hint, see comment in figure caption. This is
%%   likely more than a hint but you need to discuss significance of
%%   contour levels.

%% Then I would turn first to discuss the r.h. panels for $z>0.4$ kpc for
%% the older pops and describe how it makes sense. The big deal is
%% age-metallicity; well known. But note there is a little degeneracy in
%% AV and age, although apparently none between AV and Z which refutes
%% our line-blanketing spit-balling earlier in the text.

%% Then I would turn to the less-intuitive situation at low heights where
%% ages are lower. (You should either or both hark back to Paper I to
%% quote what you think are the characteristic ages in the $z<$0.4 kpc
%% and $z>0.4$ kpc bins, or compute the average from later sections in
%% the paper.  Note in text or caption.)

%% Here's what I don't buy (yet):

%% ``...young SSPs have a continuum shape that is only weakly dependent
%% on metallicity, so reddening is achieved by increasing $A_V$. This
%% increase in total extinction (for a given aperture) requires older
%% SSPs to have lower metallicities to counteract the extinction-induced
%% reddening. Thus, the positive age/metallicity correlation at small
%% heights is the result of the fact that metallicity is a weighted
%% average that is mostly dominated by older SSPs, and that a single
%% extinction value, $A_V$, is applied to all SSPs in a particular fit.''

%% The underlying premise is this:

%% ``metallicity is a weighted average that is mostly dominated by older
%% SSPs''

%% But it is not correct. The metallicity is light-weighted, so if the
%% population is young $Z_L$ is a measure of the metallicity of the young
%% stars.

%% Here's my theory:

%% ``because young SSPs have a continuum shape that is only weakly
%% dependent on metallicity, variations in the stellar population age and
%% $A_V$ both serve to be the primary ways to redden the continuum shape.
%% Consequently these model parameters are strongly negatively
%% correlated.  When increases in model age overly serve to fit the
%% reddened continuum, to compensate for the increased metal line
%% strength one might expect the model metallicy to diminish. In fact the
%% opposite is seen, i.e. surprisingly age and metallicity are {\it
%%   positively} correlated. We believe this is an artifact of having
%% adopted a constant extinction value for all age templates. A more
%% realistic model would tend to apply more extinction to the younger
%% populations. As a consequence, when the ratio of young to old stars is
%% over-estimated, this diminishes the model metal-line equivalent
%% widths, and to compensate the model metallicity must increase.''

%% If this is what you mean, then it is just that your wording is
%% confusing or unclear.

%% }

The fact that a purely random perturbation of our data results in fits
that clearly show the expected astrophysical degeneracies indicates
that these degeneracies are the primary source of fitting
uncertainty. In other words, uncertainty in our SSP fits is driven
mainly by similarities between different model SSPs; random flux
errors in a given wavelength channel are only a secondary source of
fitting uncertainty.

%% {\bf MAB: The hard question then is why did we bin to such high
%% S/N?}

%% ADE comment: The binning was based on theory. Here is the
%% reality. Better to be in this case than a situation where data
%% uncertainty drives our total uncertainty.

We compute total fitting uncertainty using the method described above
separately for each aperture. The values thus derived constitute all
sources of uncertainty that arise from the simultaneous measurement of
the three parameters. Figure \ref{891_2:fig:fit_err_hist} shows the
distribution of uncertainties in age, metallicity, and
extinction. These uncertainties are on the order of 5\% for most
apertures, but the distribution of uncertainty in metallicity is
broader than either $\tau_L$ or $A_V$ and can reach \asim 30\% in the
worst cases.

%+++++++++++++++
%% {\bf MAB: You want to emphasize that ``total'' here includes the
%%   degeneracies, i.e., these are not marginalized, but are the
%%   uncertainties in simultaneous measurement of all three parameters.

%--------------
%% {\bf $>>>$ How do these uncertainties compare to what you predicited in
%%   Paper I?}

\subsection{Systematic Uncertainty in DFK Ages}
\label{891_2:sec:sys_err}

Computing a weighted age (e.g., Equation \ref{891_2:eq:MLWA}) for a
superposition of model SSPs requires assigning a specific age to each
individual SSP, which, in turn, requires making an assumption about
the star formation history (SFH) across the range of ages spanned by
the SSP age bin (see Table \ref{891_2:tab:dfk}). Different SFHs have
minimal impact on the youngest SSPs due to their narrow age range, but
the oldest SSPs span a range of over \val{7}{Gyr} and thus the age
assigned to this bin can vary by the same magnitude, depending on the
assumed SFH. Because of this, the choice of SFH creates systematic
uncertainty in the computed value of $\tau_L$. This uncertainty is
primarily a consequence of the fact that old stellar spectra are
essentially constant over a large age range. In other words, these old
populations do not provide enough information in their spectral shape
to yield precisely defined ages.

%+++++++++++++++++
%% {\bf MAB: Here I think it is worth reminding the reader that this
%%   problem of the older DFK bins isn't due to a bad choice on our or
%%   M15's part, but rather that the width of the bins reflects our
%%   ignorance, or rather, where we do not have information.}

To quantify this systematic uncertainty we compute $\tau_L$ as derived
from 100 Monte Carlo SFHs. For each Monte Carlo iteration we assign
each DFK bin an age that is randomly selected from a uniform
distribution bounded by the limits of the age bin given in Table
\ref{891_2:tab:dfk}. These ages correspond to $t_i$ in Equation
\ref{891_2:eq:MLWA}, which allows us to compute a different $\tau_L$ for
each iteration. It is important to note that $t_i$ is the only
quantity that changes between iterations; the specific SSP weights
($W_{L,i,j}$) are held constant at their best fit values. The standard
deviation of $\tau_L$ across all 100 Monte Carlo iterations quantifies
the systematic uncertainty that results from assuming a SFH.

By choosing $t_i$ completely randomly from within the corresponding
age bin we allow completely unconstrained SFHs. The drawback of this
approach is an almost certain over-estimation of the true systematic
uncertainty, but the benefit is that we are not required to make
assumptions about the general form of the SFH. In this way, our
computation captures not only our lack of knowledge about the specific
shape of SFH, but also our lack of knowledge about the general form of
SFH (e.g., bursty versus smooth).

\begin{figure}
  \centering
  \includegraphics[width=0.45\textwidth]{891_2/figs/sys_uncertainty_hist.pdf}
  \includegraphics[width=0.45\textwidth]{891_2/figs/sys_uncertainty.pdf}
  \caption[Distribution of systematic uncertainties in
    $\tau_L$]{\fixspacing\label{891_2:fig:sys_err}\emph{left:}
    Distribution of systematic uncertainty that arises from assuming a
    star formation history. The uncertainties are the standard
    deviation of 100 values derived from a random assignment of ages
    to the SSP basis set. \emph{right:} The same systematic
    uncertainties as a function of $\tau_L$.}
\end{figure}

The left panel Figure \ref{891_2:fig:sys_err} shows the distribution
of systematic uncertainties in $\tau_L$. In the majority of our
apertures this uncertainty is \asim 20\%, but there is a tail with
high fractional uncertainties that extends up to 50\%. As shown in the
right panel this tail comes from apertures with $\tau_L < \val{\asim
  5}{Gyr}$. As we will see in \S\ref{891_2:sec:SFH} I2 and O
populations make up the largest contributions to the total light in
\emph{all} apertures, even those with a significant fraction of
younger populations, and it is these older populations that have the
largest systematic uncertainty due the large range of ages they
represent. Thus in all apertures the systematic uncertainty is
dominated by the presence of old populations. Figure
\ref{891_2:fig:MLWA_sys_err} shows these uncertainties in the context
of the age gradients presented in \S\ref{891_2:sec:rz}; the magnitude
of these uncertainties still allows us to determine broad, smooth
trends over large ranges in height, but it becomes difficult to make
statements about the detailed age structure of NGC 891.

%% given the magnitude of these uncertainties it is
%% difficult to make strong statements on the detailed structure and
%% trends in age or any other measured quantity.

%++++++++++++++
%% {\bf MAB: Spell out the computation for me. You have 100 sets
%% of DFK ages. For each aperture you take the best fit DFK weights,
%% compute 100 values of $\tau_L$, and then take the mean
%% and standard deviation of these values. That ratio is plotted in the figure.}

\begin{figure}
  \centering
  \includegraphics[width=\columnwidth]{891_2/figs/MLWA_sys_err.pdf}
  \caption[Systematic $\tau_L$ uncertainties in relation to our data]
          {\fixspacing\label{891_2:fig:MLWA_sys_err}$\tau_L$ as a
            function of height above the midplane showing the
            magnitude of the systematic uncertainties that arise from
            assuming a star formation history.}
\end{figure}

Despite this bleak picture, we urge the reader to take solace in the
fact that the systematic uncertainties presented above are an extreme
upper limit and ignore NGC 891 as a coherent galaxy. In other words,
it is unreasonable to assume that the SFH within NGC 891 varies on
time scales greater than a dynamical timescale (\val{\asim
  1}{Gyr}). Thus, when assessing trends in $\tau_L$, $Z_L$, or $A_V$ we
can say with confidence that points near each other in phase space
likely come from the same underlying population(s), despite the large
systematic uncertainties. It is only on large physical and time scales
that these uncertainties become relevant. Furthermore, in
\S\ref{891_2:sec:SFH} we find evidence that the general form of SFH is
mostly invariant over the portion of NGC 891 we measure. For these
reasons we choose not to include systematic uncertainties in the final
results shown in \S\ref{891_2:sec:results}, where we draw conclusions about
the detailed, small-scale structure of NGC 891. If we were to compare
the global results for NGC 891 presented here to other galaxies with
potentially different SFH then the systematic uncertainties would be
relevant.

%------------------
%% {\bf MAB: I agree with everything you say here, but I would word it
%%   differently, emphasizing earlier that Figure \ref{891_2:fig:sys_err} is
%%   the worst case of the worst case.}

%% ADE comment. Not sure what you mean here, I think it does a good
%% job as it is.


\chapter{Introduction}
\label{cha:introduction}

In today's medical world, more and more data is stored using Electronic Health Records. Each time a patient goes to a hospital, doctor or receives lab results, those events are stored in the patient's Electronic Health Record. The medical world and governments are interested in EHRs as they can for example provide new insights into disease trajectories, drug treatments, medical costs, or the link between demographics and certain diseases. \\

Due to the increased use of EHRs, a new research area has emerged, namely the area of Electronic Health Record Analytics. EHR analytics is an active research field as a lot of different problems need to be solved, like different codings, privacy, interpretation, and large amounts of data. At the moment various research groups are working on utilizing EHRs to find medical patterns with several methods like querying, statistics, data mining, and artificial intelligence approaches. \\
The methods we mentioned vary from simple to very complex. In the field of machine learning algorithms, a limited amount of research is done on EHRs, mainly using out-of-the box tools. \\
The focus point of this thesis is: applying advanced machine learning algorithms to find patterns in EHRs. \\

An EHR of a patient can be seen as a time series, namely a sequence of EHR events such as visits to the doctor. In this thesis we make the analogy between sentences of words and sequences of EHR events. Based on this analogy, we propose novel techniques which are generalizations of the Word2Vec approach, a technique typically used in linguistic analysis \cite{w2vOriginal:article}. \\
To enable this, we use several preprocessing methods. One of these methods is a newly proposed disease code mapping between two standards namely MedDRA and ICD-10. This mapping is used to categorize diagnoses and to make our validation process possible.\\
We also categorize our EHR events into more general events. For example, a bruise on your right leg and a bruise on your left leg should both be categorized under a bruises event. \\

We introduce generalized Word2Vec. This generalized Word2Vec makes it possible to apply Word2Vec on medical data. \\
To make these approaches possible for large-scale medical datasets, we apply the generalization concept on DeepWalk. DeepWalk makes it possible to generate a smaller dataset from the original dataset and then apply a Word2Vec approach on this smaller dataset for performance reasons. \\
Besides the exploration on generalizing Word2Vec approaches, we also improve Word2Vec by tackling one of its shortcomings. This shortcoming of Word2Vec is that it is unable to handle unseen instances once the model has been built. We combine a k-nearest neighbors method with Word2Vec and make an estimation of the correlation to other diagnoses for the unseen instance. \\

We compare the model from our Word2Vec methods applied on a OSIM2 dataset to the results of the currently largest study on Danish EHRs \cite{Brunak:article}. The OSIM2 dataset is a simulated dataset generated by an organization named OMOP. \\
To make it possible to apply our methods on the OSIM2 dataset, we use the above mentioned disease code mapping. \\

Within the limitations of our validation method, we conclude that our model does match the Danish results well depending on how the matching is defined. Especially since we use several estimations such as the disease code mapping, different datasets, and categorization. \\

The structure of this thesis is as follows. In chapter \ref{cha:context}, we describe the general field of EHR and EHR analytics. We also introduce the relevant state of the art. In chapter \ref{cha:background}, we provide the background needed to understand our generalized Word2Vec approaches and afterwards describe our novel techniques. In chapter \ref{cha:implementation}, we describe how we make a model of the OSIM2 dataset using our proposed techniques and how to validate our model. In chapter \ref{cha:futureWork}, we mention some possible improvements and extensions left to explore.

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "thesis"
%%% End:


\chapter{Modelling} \label{ch:model}
Most of the modeling work has been performed on the data since there is no component meta-model to conform to. However, as a proof-of-concept exercise, it will be reported a conceptual example of how a model for this controller may look like.

\section{Data model}
Data modeling is partially supported in UBX in the sense that only a M0 and M1 model can be expressed. In the details, each data port defined in i-blocks and c-blocks have a predefined data-type; UBX only defines primitives data-types like integers and floating-points numbers and characters. Additional and composed types can be defined however; MicroBLX is not a mature project and therefore data-types are hand-written and stored in the \texttt{types} directory of the \texttt{ubx-control} project, but the idea behind data-types models is that an abstract meta-model should be instead be defined by the user, for example it is possible to do so using the JSon language, and leave to a tool-chain the dirty work of generating the language-specific headers files defining those data-types. Vice-versa it should be possible from the data instance to go back to its model, thats why all the data types defined have a meta data field that should be used to store the URI of the relative model.

\subsection{State data model}
The data meta-model of the state is a probability function representing an estimate of the real state of the system. This formulation is too general for being used with a Kalman filter, but may be useful if a more advanced estimator would be introduced, for example a particle filter. In this particular application we simply suppose to have a probability function representable with a Gaussian function, therefore a vector and a covariance matrix have been used to represent the state.\\
The state vector is a composition of the position vector and its first and second derivative and the orientation quaternion and its first and second derivatives (with a total size of 21 elements). The covariance matrix is accordingly a 21$\times$21 elements matrix.

\subsection{Sensor data model}
The sensor data model is similar to the state data model in the sense that is composed by a vector of arbitrary length (specified in an integer variable) and its covariance matrix. Please note that while the covariance matrix is a property of the sensor data only, the sensor model needed by the Kalman filter require the knowledge of both the state and the sensor data types, therefore it is specified as an input of the filter, not a property of the sensor data.

\subsection{Set-point and control action model}
The set-point and the control action can be represented using just a vectors: in this study case the set-point vector is a vector with the same structure used to represent the state, but no covariance matrix is necessary; the control action is a composition of the scalar thrust and the vector torque to be generated by the propellers.

\section{Filter model}
The meta-model of the extended Kalman filter is a Baysian estimator, multiple filters conforms to this type of filter like, for example, the particle filter. It is important to conform to the meta-model since conceptually it could be possible to have an automated model transformation tool able to select alternative filters to replace the original one and a supervisor able to switch the filter node on-line whenever necessary.\\
As example, think to have a controller that usually flies outdoor relaying on a GPS signal filtered by a Kalman filter for position estimation, but also embedded with a particle filter for indoor map-based navigation; it could be possible to have a software supervisor able to detect the feasibility of switching the GPS Kalman filter with the map-based particle filter and able to hot-swap those filters, thus allowing the vehicle to adapt the control system to the environment. For this to be possible, the supervisor should be able to perform model-transformation of both the data model and the filter model, thats why whenever this will be possible it will be necessary to have models of the components. A scheme in the following page shows conceptually the structure of the filter model.

\begin{figure}[ht]
\label{figmodel}
\begin{adjustbox}{addcode={\begin{minipage}{\width}}{\caption{The Kalman filter model and meta-model.}\end{minipage}},rotate=90,center}

\includegraphics[scale=.6]{ekf_general}

\end{adjustbox}
\end{figure}


\section{Cournot competition}


\documentclass{article}
\usepackage{verbatim}
\newcommand{\uwind}{u_{\textrm{wind}}}
\newcommand{\vwind}{v_{\textrm{wind}}}
\newcommand{\wwind}{w_{\textrm{wind}}}
\newcommand{\thetam}{\theta_{\textrm{motor}}}
\newcommand{\rcmd}{r_{\textrm{cmd}}}
\newcommand{\drcmd}{{\dot r}_{\textrm{cmd}}}
\newcommand{\ddrcmd}{{\ddot r}_{\textrm{cmd}}}
\newcommand{\gammacmd}{\gamma_{\textrm{cmd}}}
\newcommand{\acmd}{a_{\textrm{cmd}}}
\newcommand{\ncmd}{n_{\textrm{cmd}}}

\newcommand{\radius}{\ell}
\newcommand{\dradius}{\dot \radius}
\newcommand{\radiuscmd}{\radius_\textrm{cmd}}
\newcommand{\dradiuscmd}{\dot \radius_\textrm{cmd}}
\newcommand{\ddradius}{\ddot \radius}
\newcommand{\ddradiuscmd}{\ddot \radius_\textrm{cmd}}
\newcommand{\omegaradius}{\omega_\radius}
\newcommand{\zetaradius}{\zeta_\radius}

\newcommand{\xturnstart}{x_{\textrm{turn,start}}}
\newcommand{\xturnstop}{x_{\textrm{turn,stop}}}
\newcommand{\chiturn}{\chi_{g,\textrm{turn}}}
\newcommand{\rturn}{l_{\textrm{turn}}}

\newcommand{\cchi}{c_\chi}
\newcommand{\cgamma}{c_\gamma}
\newcommand{\ceta}{c_\eta}
\newcommand{\cphi}{c_\phi}
\newcommand{\ctheta}{c_\theta}
\newcommand{\cpsi}{c_\psi}
\newcommand{\calpha}{c_\alpha}
\newcommand{\cbeta}{c_\beta}

\newcommand{\thetamotor}{\theta_{\textrm{motor}}}

\newcommand{\Fcmd}{F_\textrm{cmd}}
\newcommand{\Fcmdnom}{F_\textrm{cmd,0}}
\newcommand{\Fcmdmin}{F_\textrm{cmd,min}}
\newcommand{\Fcmdmax}{F_\textrm{cmd,max}}
\newcommand{\Ftether}{F_{\textrm{tether}}}
\newcommand{\Ftetherx}{F^b_{\textrm{tether},x}}
\newcommand{\Ftetherz}{F^b_{\textrm{tether},z}}
\newcommand{\tensioncmd}{F_\textrm{tether,cmd}}
\newcommand{\tensioncmdmin}{F_\textrm{tether,cmd,min}}

\newcommand{\schi}{s_\chi}
\newcommand{\sgamma}{s_\gamma}
\newcommand{\seta}{s_\eta}
\newcommand{\sphi}{s_\phi}
\newcommand{\stheta}{s_\theta}
\newcommand{\spsi}{s_\psi}
\newcommand{\salpha}{s_\alpha}
\newcommand{\sbeta}{s_\beta}

\newcommand{\ycmd}{y_\textrm{cmd}}
\newcommand{\dycmd}{\dot y_\textrm{cmd}}

\newcommand{\alphacmd}{\alpha_\textrm{cmd}}

\newcommand{\phicmd}{\phi_\textrm{cmd}}
\newcommand{\phicmdmin}{\phi_\textrm{cmd,min}}
\newcommand{\phicmdmax}{\phi_\textrm{cmd,max}}
\newcommand{\thetacmd}{\theta_\textrm{cmd}}
\newcommand{\psicmd}{\psi_\textrm{cmd}}
\newcommand{\dpsicmd}{\dot \psi_\textrm{cmd}}

\newcommand{\pcmd}{p_\textrm{cmd}}
\newcommand{\qcmd}{q_\textrm{cmd}}
\newcommand{\rcmd}{r_\textrm{cmd}}

\newcommand{\Mmotor}{M_\textrm{motor}}
\newcommand{\Nmotor}{N_\textrm{motor}}

\newcommand{\mtether}{m_\textrm{tether}}

\newcommand{\sat}{\textrm{sat}}

\usepackage{amsmath}
\usepackage{tikz}

\usetikzlibrary{arrows}

\tikzstyle{block} = [draw, fill=blue!20, rectangle,
    minimum height=3em, minimum width=6em]
\tikzstyle{sum} = [draw, fill=blue!20, circle, node distance=1cm]
\tikzstyle{input} = []
\tikzstyle{output} = [coordinate]
\tikzstyle{pinstyle} = [pin edge={to-,thin,black}]

\begin{document}

\begin{table}
\centering
\caption{Notation}
\begin{tabular}{|l|l|l|}
\hline
Symbol & Meaning & Units\\
\hline
$x,y,z$ & Components of kite position in trans-in coordinates & m \\
$\uwind,\vwind, \wwind$ & Components of the wind vector in trans-in coordinates & m/s\\
$p,q,r$ & Body resolved roll, pitch and yaw rate. & m/s \\
$V_g$ & Inertial velocity vector & m/s \\
$\gamma_g$ & Inertial climb angle & rad \\
$\chi_g$ & Inertial course angle & rad \\
$V$ & Airspeed & m/s \\
$\gamma$ & Aerodynamic climb angle & rad \\
$\chi$ & Aerodynamic course angle & rad \\
$\radius$ & Radial position & m \\
$\eta$ & Elevation angle & rad \\
$\gamma_t$ & Tangent climb angle & rad \\
$m$ & Kite mass & kg \\
$\mtether$ & Tether mass & kg \\
$\bar q$ & Dynamic pressure at the kite & Pa \\
$\thetamotor$ & Pitch angle of thrust line to body x axis & rad \\
$\phi$ & Roll angle & rad \\
$\theta$ & Pitch angle & rad \\
$\psi$ & Yaw angle & rad \\
$\Ftether$ & Tether force vector & N \\
\hline
\end{tabular}
\end{table}
The functions $\sin, \cos,$ and $\tan$ of an angle $a$ are abbreviated:
\[
s_a = \sin(a), \quad c_a = \cos(a), \quad t_a = \tan(a).
\]
Rotation matrices around the primary axes are defined by:
\begin{equation*}
R_x(\phi) = \begin{bmatrix}
1 & 0 & 0 \\
0 & \cphi & \sphi \\
0 & -\sphi & \cphi
\end{bmatrix}, \:
R_y(\theta) = \begin{bmatrix}
\ctheta & 0 & -\stheta \\
0 & 1 & 0 \\
\stheta & 0 & \ctheta
\end{bmatrix}, \:
R_z(\psi) = \begin{bmatrix}
\cpsi & \spsi & 0 \\
-\spsi & \cpsi & 0 \\
0 & 0 & 1
\end{bmatrix}.
\end{equation*}
The matrix which rotates a vector from a frame ($a$) to a frame ($b$)
is denoted $R_a^b$.

\section{Kinematics and Conventions}
The trans-in frame is defined as having its origin at the
ground-station ($g$-frame) origin.  The $z$ axis of the trans-in frame
points down, and the $x$ axis points away from the kite at the point
where trans-in begins.  The kite's attitude can be expressed as a
rotation between the trans-in frame ($t$) and the body-frame ($b$).
This can also be expressed in terms of Euler angles:
\begin{eqnarray}
R_t^b &=& R_x(\phi) R_y(\theta) R_z(\psi) \\
&=& \begin{bmatrix}
\ctheta \cpsi & \ctheta \spsi & -\stheta \\
(-\cphi\spsi + \sphi \stheta \cpsi) & (\cphi\cpsi + \sphi\stheta\spsi) & \sphi \ctheta \\
(\sphi\spsi + \cphi \stheta \cpsi) & (-\sphi\cpsi + \cphi \stheta \spsi) & \cphi\ctheta
\end{bmatrix}.\notag
\end{eqnarray}

The kite's velocity can be written in terms of its velocity $V_g$, a
geometric course angle $\chi_g$, and a geometric climb angle $\gamma_g$:
\begin{eqnarray}
\dot x &=& V_g \cos(\gamma_g) \cos(\chi_g), \\
\dot y &=& V_g \cos(\gamma_g) \sin(\chi_g), \\
\dot z &=& -V_g \sin(\gamma_g).
\end{eqnarray}
It can also be written in terms of the airspeed ($V$), aerodynamic
course angle ($\chi$), aerodynamic climb angle ($\gamma$) and the wind
components:
\begin{eqnarray}
\dot x &=& V \cgamma \cchi + \uwind, \\
\dot y &=& V \cgamma \schi + \vwind, \\
\dot z &=& -V \sgamma + \wwind.
\end{eqnarray}
The relative velocity can be written in terms of the airspeed,
angle-of-attack ($\alpha$) and angle-of-sideslip ($\beta$):
\begin{equation}
\begin{bmatrix}
\dot x - \uwind \\
\dot y - \vwind \\
\dot z - \wwind
\end{bmatrix} = R_b^t \begin{bmatrix}
V \calpha \cbeta \\
V \sbeta \\
V \salpha \cbeta\end{bmatrix}.
\end{equation}

The current trans-in controller implementation makes use of
cylindrical coordinates in terms of a radial coordinate ($\radius$) and
elevation angle ($\eta$):
\begin{equation}
x = - \radius \ceta, \qquad z = - \radius \seta.
\end{equation}
From this we have:
\begin{eqnarray}
\dradius &=& \frac{x \dot x + z \dot z}\radius =
 -V\ceta\cchi\cgamma + V \seta \sgamma - \ceta \uwind - \seta \wwind \\
&=& -V_g\ceta\cos(\chi_g)\cos(\gamma_g) + V_g \seta \sin(\gamma_g)\\
\radius \dot \eta &=& \frac{ x \dot z - \dot x z}\radius =
V\ceta\sgamma + V \seta\cgamma\cchi
-\ceta\wwind + \seta\uwind.
\end{eqnarray}

\subsection{Frequent Approximations}
This sections lists approximations that are used in the controller
derivations below.
\begin{description}
\item[A1.] $\vwind \approx 0$.  This relies on the trans-in frame $x$
  axis being pointed into the wind, and there being limited change in
  wind direction.
\item[A2.] $\wwind \approx 0$.  This assumption treats updrafts and
  downdrafts as disturbances.
\end{description}
The following assumptions are made after the initial pitch-forward is
complete.
\begin{description}
\item[A3.] $\alpha, \beta, \psi, \phi$ are all small angles.
\item[A4.] $\theta \approx \gamma + \alpha$.  In outer loop
  calculations this is sometimes simplified to $\theta \approx \gamma$.
  This relies on the fact that:
\begin{eqnarray}
-\sgamma &=&
-\stheta\calpha\cbeta + \sphi\ctheta\sbeta + \cphi\ctheta\cbeta\salpha \\
&\approx& -\stheta\calpha + \ctheta\salpha = \sin(\alpha - \theta),
\end{eqnarray}
which approximates $\phi$ and $\beta$ as small angles.
\item[A5.] $V\sbeta \approx \dot y - V\ctheta\spsi$, which follows
  from (A1) and (A3):
\begin{eqnarray}
\frac{\dot y}V &=& \ctheta \calpha \cbeta \spsi +
(\cphi\cpsi + \sphi\stheta\spsi)\sbeta + \\
&& (-\sphi\cpsi + \cphi\stheta\spsi)\cbeta\salpha + \frac{\vwind}V\\
&\approx& \ctheta \spsi + \sbeta.
\end{eqnarray}
\end{description}

\subsection{Approximating Aerodynamic Climb Angle}
The trans-in controller approximates the aerodynamic climb angle $\gamma$ via:
\begin{equation}
\hat \gamma = \sin^{-1}\left(-\frac{\dot z}{V}\right).
\end{equation}
This uses assumption (A2).  Here the airspeed is derived from the
Pitot tube's dynamic pressure port and the vertical velocity is
derived from inertial sensors fused with GPS.

\section {Attitude Control}
\begin{figure}
\centering
\includegraphics[width=\textwidth]{attitude_loop.png}
\caption{Block diagram for the longitudinal outer loop.  Not shown:
  integrator saturations, integrator holds, and pitch forward logic.}
\end{figure}
The attitude controller receives roll, pitch and yaw command
($\phicmd$, $\thetacmd$, and $\psicmd$ resp.).  This is resolved into
a commanded attitude:
\begin{equation}
R_t^c := R_x(\phicmd) R_y(\thetacmd) R_z(\psicmd),
\end{equation}
where $\thetacmd = \gamma + \alphacmd.$ This is used to calculate
a rotation axis:
\begin{equation}
R^c_b = R_t^c R_b^t = \exp([-a_{bc} \times]).
\end{equation}
\subsection{Pitch Forward Manuver}
The objective of the pitch-forward manuver attempts to orient the kite
so as to break stall while controlling the pitch rate.  The target
pitch angle is $\gamma$.  The desired change in pitch
angle is calculated as $\Delta \theta_0 = \gamma - \theta$ at the
moment that trans-in begins.The duration of the pitch-forward $T$ is
set to:
\begin{equation}
T = \textrm{max}\left(
\frac{-2 \Delta\theta_0}{q_{\textrm{max}}},
\sqrt{\frac{-2 \pi \Delta\theta_0}{\dot q_{\textrm{max}}}}
 \right)
\end{equation}
This duration enforces either a maximum commanded pitch rate (when
$\Delta \theta_0$ is very negative) or a maximum pitch acceleration.
If $\Delta \theta_0$ is greater than a threshold, $T$ is taken to be zero.

A pitch and pitch rate trajectory are generated to be tracked:
\begin{eqnarray}
\thetacmd(t) &=& \theta_0 + \frac{\Delta\theta_0}{T} \left (
t - \frac{T}{2\pi} \sin\left(\frac{2\pi}T t\right)
\right), \\
\qcmd(t) &=& \frac{\Delta\theta_0}{T} \left(1 - \cos\left(\frac{2 \pi}T t\right)\right).
\end{eqnarray}
Here $t = 0$ at the beginning of trans-in, $\theta_0$ is the initial
pitch attitude estimate.  These commands are cross-faded to the outer
loop commands $t$ range from $\frac{T}2$ to $T$.

\subsection{Longitudinal Attitude Control}
The longitudinal controller is defined by:
\begin{eqnarray}
\dot i_\alpha &=& \alphacmd - \alpha \\
\Mmotor &=& K_{\Mmotor ay} (\hat y \cdot a_{bc}) + K_{\Mmotor q}(\qcmd - q) + K_{\Mmotor i_\alpha} i_\alpha,\\
\delta_e &=& k_{\delta_e\alphacmd} \alphacmd + K_{\delta_e ay} (\hat y \cdot a_{bc}) + K_{\delta_e q}(\qcmd - q) + K_{\delta_e i_\alpha} i_\alpha.
\end{eqnarray}
The integrator is held at zero until the following
conditions are met:
\begin{enumerate}
\item The pitch-forward is complete.
\item The airspeed is above a minimum threshold.
\item The angle-of-attack is below a threshold.
\end{enumerate}
The integrator is also saturated.
\subsection{Lateral Attitude Control}
The lateral controller is defined by:
\begin{eqnarray}
\dot i_{ax} &=& \hat x \cdot a_{bc}, \\
\Nmotor &=& K_{\Nmotor a} (\hat z \cdot a_{bc}) + K_{\Nmotor r}(\rcmd - r), \\
\delta_a &=& K_{\delta_a ax} (\hat x \cdot a_{bc})
+ K_{\delta_a az} (\hat z \cdot a_{bc})
+ K_{\delta_a p}(\pcmd - p)\\
&&+ K_{\delta_a r}(\rcmd - r)
+ K_{\delta_a i_{ax}} i_{ax}\notag\\
\delta_r &=& K_{\delta_r ax} (\hat x \cdot a_{bc})
+ K_{\delta_r az} (\hat z \cdot a_{bc})
+ K_{\delta_r p}(\pcmd - p)\\
&&+ K_{\delta_r r}(\rcmd - r)
+ K_{\delta_r i_{ax}} i_{ax}.\notag
\end{eqnarray}
During the pitch-forward, $K_{\delta_a ax}, K_{\delta_a az},
K_{\delta_r ax},$ and $K_{\delta_r az}$ are held at zero.  The
integrator is held at zero until the following conditions are met:
\begin{enumerate}
\item The pitch-forward is complete.
\item The airspeed is above a minimum threshold.
\item The angle-of-attack is below a threshold.
\end{enumerate}
The integrator is also saturated.

\section{Lateral Control}
\begin{figure}
\centering
\includegraphics[width=\textwidth]{lateral_outer_loop.png}
\caption{Block diagram for the lateral outer loop.  Not shown:
  low airspeed logic and dependence of $\omega_y$ on $V$.}
\end{figure}

The lateral position and velocity errors are used to calculate a
desired lateral acceleration:
\begin{eqnarray}
\acmd &=& \omega_y^2 (\ycmd - y) + 2 \zeta_y \omega_y (\dycmd - \dot y),
\end{eqnarray}
where $\omega_y = \min\left(V / \ell_y,
\omega_{y,\textrm{min}}\right)$ and $\zeta_y$ specify a desired
natural frequency and damping ratio for the position response.  The
reference length $\ell_y$ captures the fact that greater turn
authority is available when airspeed is higher and may be required if
ground speed is also increased (e.g. in low wind flights). The expected
lift is computed from the current aerodynamic climb angle assuming a
steady-state climb:
\begin{eqnarray}
L_\textrm{expected} &\approx& W \cgamma,
\end{eqnarray}
where $W = (m + \mtether) g$, and this is used to compute the roll-angle command:
\begin{eqnarray}
\phicmd &=& \sat\left(\frac{m\acmd}{W \cgamma}, \phicmdmin, \phicmdmax\right).
\end{eqnarray}

In addition to the roll command, a feed-forward yaw command
($\psicmd$) is applied:
\begin{equation}
\psicmd = \sin^{-1}\left(\frac{\dycmd}{V\cgamma}\right),
\end{equation}
based on approximations (A4) and (A5).

For turn coordination, a feed-forward yaw-rate is applied:
\begin{equation}
\dpsicmd = \frac{g \cgamma}V \tan(\phicmd).
\end{equation}
This is based on Stevens and Lewis, Equation 3.6-7 making the
approximation that $\gamma \approx \theta$.  Assuming zero roll and
pitch rate, this command is mixed into body-rate commands (see Stevens and Lewis Eq 1.3-21):
\begin{eqnarray}
\pcmd &=& -\sin(\gamma) \dpsicmd \\
\rcmd &=& \cos(\gamma) \dpsicmd,
\end{eqnarray}
again using the approximation $\theta \approx \gamma$.

\section{Longitudinal Outer Loop}
\begin{figure}
\centering
\includegraphics[width=\textwidth]{longitudinal_outer_loop.png}
\caption{Block diagram for the longitudinal outer loop.  Not shown:
  tension control and low airspeed logic.}
\end{figure}

The objectives of the controller, in order of priority are:
\begin{enumerate}
\item[(a)] Command angle-of-attack within limits.
\item[(b)] Maintain airspeed above a minimum threshold.
\item[(c)] Regulate the radial position $\radius$ to a command $\radiuscmd$.
\item[(d)] Ensure sufficient tension is applied to cause the
  transition to crosswind flight.
\end{enumerate}

The longitudinal outer loop is designed based on the kinematics of the
kite and the point mass model of the kite dynamics:
\begin{eqnarray}
m V \dot \gamma &\approx& T(V) \sin(\alpha + \thetam) + \bar q A C_L - mgc_\gamma - \Ftetherz,\\
m \dot V &\approx& T(V) \cos(\alpha + \thetam) - \bar q A C_D - mg \sgamma - \Ftetherx.
\end{eqnarray}
The force balance here is resolved in the wind frame and $R_b^w
\approx I$ is used based on assumption (A3).

\subsection{Radial Position and Aerodynamic Climb Angle Control}
The lateral controller assumes that $\chi \approx 0$ and $\chi_g
\approx 0$ so that:
\begin{equation}
\begin{bmatrix}
m V \dot \gamma\\
m\dot V
\end{bmatrix}
=
\begin{bmatrix}
\cos(\gamma_g - \gamma) & \sin(\gamma_g - \gamma) \\
-\sin(\gamma_g - \gamma) & \cos(\gamma_g - \gamma)
\end{bmatrix}
\begin{bmatrix}
m V_g \dot \gamma_g\\
m\dot V_g
\end{bmatrix}
\end{equation}
For radial control we assume that $\dot V \approx 0$.

The trans-in controller attempts to enforce a smooth approach the
tether sphere by commanding:
\begin{eqnarray}
\dradiuscmd &=& \frac{\omegaradius}{2\zetaradius}(\radiuscmd - \radius),\\
\ddradius &=& 2 \zetaradius \omegaradius (\dradiuscmd - \dradius),
\end{eqnarray}
where $\omegaradius$ and $\zetaradius$ are the desired natural frequency and
damping ratio of the response.

The steady-state airspeed of the kite is determined primarily by the
thrust level $T(V)$ and the aerodynamic climb angle ($\gamma$).  To
maintain sufficient airspeed, the controller transforms the desired
radial velocity into a desired aerodynamic climb angle, which is
saturated.  Approximating $\chi \approx 0$ yields:
\begin{eqnarray}
\dradius &\approx& -V \ceta \cgamma + V \seta \sgamma  - \ceta \uwind - \seta \wwind\\
&=& -V \sin(\gamma_t - \gamma) - \ceta \uwind - \seta \wwind,
\end{eqnarray}
where $\gamma_t = \frac{\pi}2 - \eta$ is the tangent climb angle.
From this we take:
\begin{eqnarray}
\gammacmd &=& \gamma_t + \sin^{-1}\left(\frac{\dradiuscmd - \dradius}V
- \sin(\gamma_t - \gamma)\right).
\end{eqnarray}
This is then used to calculate a normal acceleration command:
\begin{equation}
\ncmd = 2 \zetaradius \omegaradius V (\gammacmd - \gamma) - (\ell \dradius)^2 / \radiuscmd / cos(\gamma_t - \gamma).
\end{equation}
Please see comments in \begin{tt}control/trans\_in/trans\_in\_longitudinal.c\end{tt}
for more detailed analysis.

\subsection{Tension Control}
An additional tether tension command:
\begin{equation}
\tensioncmd = \tensioncmdmin
+ \max\left (0, \bar q - \frac{1}2 \rho V_{\textrm{min}}^2\right) AC_{L_{\textrm{max}}}
\end{equation}
is applied if the conditions:
\begin{itemize}
\item The radial position error, $\radiuscmd - \radius$, is sufficiently small.
\item The elevation angle, $\eta$, exceeds a threshold.
\end{itemize}
both hold.  Otherwise, $\tensioncmd = \Ftetherz$.

\subsection{Normal Force Control}
The normal force control attempts to generate a force:
\begin{equation}
\Fcmd = m \ncmd + \tensioncmd - \Ftetherz
\end{equation}
along the negative wind z-axis (i.e. in the direction that lift acts).

Given a normal acceleration command $\ncmd$ and tension command
$\tensioncmd$, the acceleration control computes a desired
angle-of-attack and flap command.  A point-mass model of the kite is
used to compute the minimum and maximum normal forces that can be expected:
\begin{flalign}
\Fcmdnom =&\: \bar q A C_{L_0} - \Ftetherz - mg\cgamma + T(V) \sin(\thetamotor),\\
\Fcmdmin =&\: \Fcmdnom + (\bar q A C_{L_\alpha} + T(V) \cos(\thetamotor)) \alpha_{\textrm{min}} + q A C_{L_{\delta_f}} \delta_{f,\textrm{max}},\\
\Fcmdmax =&\: \Fcmdnom + (\bar q A C_{L_\alpha} + T(V) \cos(\thetamotor)) \alpha_{\textrm{max}}+ q A C_{L_{\delta_f}} \delta_{f,\textrm{max}}.
\end{flalign}
We then take:
\begin{eqnarray}
\alphacmd &=&
\frac{\Fcmd - \Fcmdmin}{\Fcmdmax - \Fcmdmin} \alpha_{\textrm{cmd,max}}
+\frac{\Fcmdmax - \Fcmd}{\Fcmdmax - \Fcmdmin} \alpha_{\textrm{cmd,min}}, \\
\delta_f &=&
\frac{\Fcmd - \Fcmdmin}{\Fcmdmax - \Fcmdmin} \delta_{f,\textrm{max}}
+\frac{\Fcmdmax - \Fcmd}{\Fcmdmax - \Fcmdmin} \delta_{f,\textrm{min}},
\end{eqnarray}
with $\Fcmd$ saturated between $[\Fcmdmin, \Fcmdmax]$.

A feed-forward pitch rate is calculated based on desiring $\dot \alpha
\approx q - \dot \gamma \approx 0$.  This is computed as:
\begin{equation}
\qcmd = \frac{\Fcmdnom + (q A C_{L_\alpha} + T(V) \cos(\thetamotor)) \alphacmd + q A C_{L_{\delta_f}} \delta_f}{mV}.
\end{equation}

%% \section{Turn Control}
%% {\bf TODO}
%% The turn starts at $\xturnstart$ and ends at $\xturnstop$:
%% \begin{equation}
%% \xturnstop = \xturnstart + \rturn \sin(|\chiturn|).
%% \end{equation}
%% \begin{equation}
%% \ycmd = \begin{cases}
%% \tan(\chiturn)(x - \xturnstop) + \rturn d(1-\cos(\chiturn)) & x > \xturnstop
%% & \xturnstop \geq x > \xturnstart\\
%% 0 & x < \xturnstart
%% \end{cases}
%% \end{equation}

\end{document}


\section{Bilingual contact center}
\label{sec:Bilingual}

This example represents a contact center serving an English and
a Spanish population.  On each weekday, contacts of each type arrive
following a Poisson process with rate $100/$h from~8AM to~9AM, and
$50/$h from~9AM to~5PM.
7 English-speaking, 7 Spanish-speaking, and 4
bilingual agents are available to serve contacts.  The contact
center provides 40 phone lines to accommodate contacts.

When a contactor
arrives, the router randomly selects a free agent or queues the
contact if there is no free agent.
Patience times are i.i.d.\ exponential variables
with mean 2~minutes.  I.i.d.\ service times follow the uniform distribution,
ranging from 3~minutes to
7~minutes for English contacts, and from 4~minutes to 6~minutes for
Spanish contacts.

If abandonment occurs, with probability~$0.75$,
the contactor retries to join an agent after 20~minutes.
Arena Contact Center Edition supports a generalization of this concept
called \emph{contact backs}.  Whenever a contactor exits the system, he
has the possibility to enter back with a certain probability, after a
possibly random time.  In the call center terminology, contact backs
after abandonment are denoted \emph{retrials} whereas contact backs
after service are named \emph{returns}.  This differs from the concept
of returns, i.e., agents recontacting customers, implemented in Arena
Contact Center Edition.

The program estimates the service level for contact types~$k=0$
and~$1$,
and the service level over
all contact types, as well as the occupancy ratio for agent
groups~$i=0,1,2$ and the ratio over all agent groups.
Let $\SgK{k}(s)$ be the number of served contacts of
type~$k$ having waited in queue for less than $s$, and let $\AK{k}$
be the total number of arrivals of type~$k$.
The \emph{service level for type~$k$} is defined by
\begin{equation}
\frac{\E[\SgK{k}(s)]}{\E[\AK{k}]}.\label{eq:slarenak}
\end{equation}
The occupancy ratio in group~$i$ is defined by~(\ref{eq:occi}).
Since
\begin{eqnarray*}
S&=&\sum_{k=0}^{K-1} \SK{k},\\
A&=&\sum_{k=0}^{K-1} \AK{k},\\
N(t)&=&\sum_{i=0}^{I-1} N_i(t),\\
\Nb(t)&=&\sum_{i=0}^{I-1} \Nb[i](t),\\
&\mbox{etc.,}&
\end{eqnarray*}
equations~(\ref{eq:slarena}) and~(\ref{eq:occ}) can be used for
the overall service level and occupancy ratio.

The program given on Listing~\ref{lst:Bilingual} simulates the above
model and computes required statistics.

\lstinputlisting[
caption={A simulation program for a bilingual contact center},%
emph={create,main,MyRouter},label=lst:Bilingual
]
{Bilingual.java}

The program uses a helper class called
\texttt{Contact\-Center} providing convenience static methods for
initializing components of the contact center.

In real-life models, contact types and agent groups have names.
However, for efficiency, to distinguish contact types and agent
groups, the library uses numerical identifiers rather than strings.
On the other hand, replacing names with numerical identifiers reduces the
readability of the program and the produced statistical reports.
To make the program clearer, constants are associated
with each identifier.  The arrays \texttt{TYPENAMES} and
\texttt{GROUPNAMES} contain names for contact types and agent groups
being used for statistical reports.

In this model, we are interested in statistics for each contact type
separately as well as for all contact types.  In the program, many
scalars are then replaced by arrays of $K+1$ elements, and lists of
tallies are used instead of tallies for easier statistical collecting
and reporting.
Each list of tally has a \emph{global name} corresponding to a type of
performance measure, e.g., service level.  Each element in a list of
tallies also has its own
\emph{local name} corresponding to a particular measure, e.g., the service
level for a contact type, or the occupancy ratio of the agents in a a group.
The list of tallies are constructed using a \texttt{create} method
that also initializes the global and local names, and set
reporting options to show confidence intervals.

The model in Arena Contact Center Edition
is made of two parent groups which create two waiting
queues for contacts.  A \emph{parent group} is a set of agent groups
defining
a selection rule for the agents and assigning a preference for each
contact type.  To implement the parent group efficiently, a
queue is created for each contact type and the contact selection is
implemented in the router.

This contact center simulator requires a custom router to implement the
random selection of agents as well as the contact back mechanism.  This
router, represented by the \texttt{My\-Router} class,
extends \texttt{Single\-FIFO\-Queue\-Router} used in
section~\ref{sec:SimpleMSK}.   For this model, the type-to-group map
indicates that an English contact must be served by an English-speaking
or a bilingual agent, in that order.  A Spanish contact must be
served by a
Spanish-speaking or a bilingual agent.  The group-to-type map assigns
contact types to agents:  an English-speaking agent can serve English
people only whereas a Spanish-speaking agent can serve Spanish people.
The bilingual agents are generalists since they can serve any contact
type in this system.

The agent selection rule of the chosen router is not appropriate,
since we do not want specialists to have priority over
generalists in this particular example.
To implement random agent selection, the
\texttt{select\-Agent} method is thus overridden.
This method is called by the router when a contact arrives
and must be assigned an agent.  It selects an agent, starts the
service, and returns a reference to the end-service event.
The \texttt{select\-Uniform} helper method manages the random
selection automatically:  each agent group is assigned a probability
of selection given by the number of free agents in the group over the
total number of free agents capable of serving the contact.   If no
group contains free agents, a
\texttt{null} agent group reference is returned by
\texttt{select\-Uniform}, and \texttt{select\-Agent} returns
\texttt{null} to indicate the router that the contact must be queued.
Otherwise, the service of the contact is started.

When an agent becomes free, the
router tries to pull contacts from waiting queues.  In this example,
the contact selection rule of the inherited router does not need to be
customized.  For specialists, the
appropriate waiting queue is checked for a new contact which is removed and
served.  For generalists, both waiting queues are checked and the
contact with the longest waiting time is removed.

Further customization is needed to support retrials,
by overriding the \texttt{dequeued} method in
the router.  First, if the effective dequeue type is~0, the method returns
immediately because this is not an abandoning contact; the dequeue
type~0 is reserved for contacts being removed from the queue to be
served.  If the dequeue type is~1,
with some probability, a \texttt{Contact\-Back\-Event} is
scheduled after a fixed delay.  The contact then leaves the system,
using the
\texttt{exit\-Dequeued} method.  It is important to remove the contact from
the system in order to free its associated phone line.  When the
contact-back event occurs, the \texttt{new\-Contact} method is called
with the \texttt{Contact} object.
This is exactly what is performed by an
arrival process broadcasting a new contact to the registered
router.

However, some internal checks are performed
to avoid contacts entering multiple times in the system, which would
produce wrong simulation results.  When a contact exits the system, the
router sets an internal exit indicator
that must be cleared
before the contact can enter the router again.

To count the number of retrials, we need to define a subclass of
\texttt{Contact} with a new field being updated when retrial
occurs.  The subclass \texttt{My\-Contact} must be
referred to in \texttt{My\-Contact\-Factory} to get the appropriate
instances and typecasts are often needed in listeners such as
\texttt{My\-Contact\-Measures}.

% The \texttt{My\-Contact} subclass in the example defines a field named
% \texttt{serial\-Version\-UID}, because the class inherits the
% serializable property of \texttt{Contact}.  Although this is not
% necessary for this example, since contacts are not serialized here,
% the Java documentation recommends the association of a fixed serial
% version UID with each serializable class.
% Declaring this field can sometimes be useful to avoid warnings from
% the Java compiler.

As we can see in the program,
using arrays requires more work than scalars.
Of course, they must be constructed when declared or in the constructor of
the simulator.
Then, at the beginning of each replication, in the
\texttt{simulate\-One\-Day} method, each element must be initialized
separately.  Finally, in the exited-contact listener, events must be
counted in
two elements of the arrays.  When the event concerns a contact of
type~$k$, the index~$k$ of the arrays must be updated in addition to
index~$K$ for the measures for all types.
In addition, in \texttt{add\-Obs}, estimating the occupancy ratio
requires
$\int_0^T \Nb[i](t)\ dt$ and
$\int_0^T N_i(t)\ dt$ to be obtained for each agent group separately.

\texttt{report} is used to format
reports about each contact type or agent group, depending on the type
of performance measure.  The list report contains the
same information as the ordinary report, but, as shown on
Listing~\ref{res:Bilingual}, it is more condensed and
more appropriate for related performance measures.

\lstinputlisting[caption={Results of the program \texttt{Bilingual}},%
label=res:Bilingual,language={}
]{Bilingual.res}


\section{Simulation examples}

In the end, the purpose of this project is to produce animations for computer graphics, and the
last word must be given to the human observer who watches these animations. I would like to claim
that the results are of a quality similar to what an animator might create, and that they are
realistic in the sense that they do not violate the laws of physics or anatomy. However, such a
claim is difficult to defend convincingly in print. I have therefore compiled a video of several
example animations, and I urge the reader to watch it to gain a more personal impression of this
project's results. The video can be found in the 3D graphics section of my web site
\texttt{http://martin.kleppmann.de/}. The following sections describe how each of the simulated
systems was implemented and point out noteworthy features.

\subsection{Pendulum systems}

The simplest physical system in the demonstration video is
the animation of a double pendulum (a rigid pendulum with a second one
attached to its end), and its extension to three, eight and 25 segments. The double pendulum is
a commonly studied system in physics; although it does not have an exact analytic solution, an
approximate solution can be found for small angles. In this case, one finds that the resulting
movement is the sum of two simple harmonic oscillations at different frequencies, resulting in
a mysterious-looking swinging movement.

The double, triple and eight-part pendulums are set up in a very similar way. Each segment is
modelled as a rigid cylinder with constant density. The top end of the top segment is held in
place by a `nail' constraint. Each pair of adjacent segments is connected by a ball-and-socket
joint. Initially, all segments are at rest, the first segment is rotated by 45 degrees
anticlockwise from the equilibrium position, and all other segments hang straight down.

The simulation does not employ an XML input file; instead, the objects representing the bodies
and the constraints are directly created by the Java test case. The simulation results were
exported to Blender and rendered using the internal renderer and orthographic projection.
I also performed a Fourier transform on the results of the double pendulum simulation, which
indicated very clearly that there were two main frequency components.

The 25-segment pendulum is an attempt to simulate rope, and it is considerably more ambitious.
It is modelled as a cylindrical mesh bound to a chain of 25 bones. The ground is a separate mesh,
and in the simulation its position is fixed by three `nail' constraints. This input data is
represented as an XML file. Collision detection is performed on basis of the meshes, thus the
rope can collide both with itself and with the ground.

The joints between adjacent segments of the rope are of ball-and-socket type, and their rotation
is limited to a maximum of 15 degrees about each axis. The si\-mu\-la\-ted rope is therefore very
stiff. I am not entirely sure how realistic I should consider this simulation; its behaviour looks
strange at first, but this is mainly due to the assumptions put into the model. I believe that
the simulation correctly obeys the model, but the model would have to be refined in order
to produce genuinely realistic-looking rope.

\subsection{Newton's cradle}

\begin{figure}[p]
\centerline{\includegraphics[width=60mm,height=45mm]{figures/cradle1} \hspace{5mm}
            \includegraphics[width=60mm,height=45mm]{figures/cradle2}}\vspace{5mm}
\centerline{\includegraphics[width=60mm,height=45mm]{figures/cradle3} \hspace{5mm}
            \includegraphics[width=60mm,height=45mm]{figures/cradle4}}
\caption{Animation of Newton's cradle.\label{sampleCradle}}
\end{figure}

The next section shows Newton's cradle in a range of different animations. For full elasticity,
various combinations of one, two and three balls in simultaneous motion are demonstrated, and all
effects observed in a real toy cradle are reproduced. I also simulate what happens if collisions
are not fully elastic: all balls gradually begin to swing, and the sequence of collisions becomes
much more complicated.

Newton's cradle is modelled as five spherical bodies, each with its centre of mass at the centre
of the ball (i.e.\ the wire connecting it to the frame is assumed to be massless). The joint to
the frame is modelled as a `nail' constraint, and the frame itself does not exist in the
simulation. Collision detection is performed not on the basis of the meshes (since these are
polyhedral approximations and not exact spheres), but using a special type of sphere/sphere
constraint which computes its behaviour using the positions of the centres of the balls and their
radii.

For the simulation to work accurately, a very low penetration tolerance must be set. The different
examples are simulated in exactly the same way, independent of the number of balls moving or the
elasticity of collisions.

\subsection{Falling boxes}

\begin{figure}[p]
\centerline{\includegraphics[width=60mm,height=45mm]{figures/boxes1} \hspace{5mm}
            \includegraphics[width=60mm,height=45mm]{figures/boxes2}}\vspace{5mm}
\centerline{\includegraphics[width=60mm,height=45mm]{figures/boxes3} \hspace{5mm}
            \includegraphics[width=60mm,height=45mm]{figures/boxes4}}
\caption{Animation of ten boxes falling onto a table.\label{sampleBoxes}}
\end{figure}

The third section demonstrates eleven interacting rigid bodies: ten boxes falling onto a table.
The simulation is performed at two different elasticities: one close to what one would expect in
reality ($\varepsilon = 0.2$), and one very bouncy as contrast ($\varepsilon = 1.0$).

Each of the ten boxes is modelled as a symmetric hollow body. All boxes and the table are
specified as meshes in an XML input file. The table is held in place by three `nail' constraints.
These simulations exhibit a range of different collision types: each example starts with a
vertex/face collision when the first box hits the table, and is followed by a sequence of
vertex/face, edge/edge and compound collisions. Whenever a collision cannot be clearly classified
as either vertex/face or edge/edge, the algorithm searches for a plane which contains as closely
as possible the line of intersection between the two colliding meshes. This plane is then used as
the contact plane for computing the impulses and resting contact forces.

The fully elastic version of this simulation looks unrealistic because it behaves as though the
boxes were made of extremely bouncy rubber. The version with elasticity 0.2 is much closer to what
one might expect in a real-life scene, but the contrast between the two is interesting. At the
end of the low-elasticity simulation, several boxes can be seen in a frictionless glide over the
surface of the table. They cannot penetrate the table due to a resting contact force.

\subsection{Alfred falling down stairs}

The final two sections show a humanoid model fall down a straight staircase and a spiral staircase
respectively. I created these simulations to demonstrate a situation which did not require control
of muscular forces (which are hard to control in a simulation~\cite{Green:91}) but were
nevertheless interesting.

The simulation on straight stairs and on the spiral staircase are set up in a very similar way.
Each scene consists of two bodies, a polygon model of the staircase and Alfred himself. In each
case, the staircase is held in place by three `nail' constraints\footnote{It seems intuitively
bizarre that one might make a whole staircase immobile by only three nails, but in the simulation
the magnitude of the forces is, of course, almost irrelevant!}. Alfred is an articulated body
as described in section~\ref{softwareTools}.

All meshes and skeletons are defined in an XML input file. All joints' rotation is restricted in
a way which approximates the anatomical reality. Nonetheless the body sometimes enters poses which,
although they are not impossible, look rather uncomfortable. This is because a pose does not
gradually become more uncomfortable as the limit is approached, but action takes place only at
the limit. The articulated body model would have to be extended to accommodate a notion of
``comfort'' or elastic limits.

For purposes of collision detection, the shape of the Alfred mesh is approximated by 252 little
spheres. The staircase is not approximated. Thus this simulation does not make use of the usual
vertex/face and edge/edge collisions; instead, sphere/face and sphere/edge collisions occur
(on contact with the staircase), and sphere/sphere collisions (on contact between different parts
of the articulated body, e.g.\ the hand against the chest). I found the use of spheres to be the
most reliable technique for such a complicated mesh. Constraint functions for these types of
collision can be derived fairly easily and handled using the usual algorithms for resting and
colliding contact as described in section~\ref{collisionHandling}.

The camera movement and lighting was set up in Blender. After importing the simulation results,
the animation was rendered using Blender's internal renderer/raytracer. Some of the mesh
deformations look strange; this is unrelated to the simulation, but must be blamed on my lack of
3D art skills!

\begin{figure}[p]
\centerline{\includegraphics[width=60mm,height=45mm]{figures/stairs1} \hspace{5mm}
            \includegraphics[width=60mm,height=45mm]{figures/stairs2}}\vspace{5mm}
\centerline{\includegraphics[width=60mm,height=45mm]{figures/stairs3} \hspace{5mm}
            \includegraphics[width=60mm,height=45mm]{figures/stairs4}}
\caption{Animation of Alfred falling down a set of stairs.\label{sampleStairs}}
\end{figure}

\begin{figure}[p]
\centerline{\includegraphics[width=60mm,height=45mm]{figures/spiral1} \hspace{5mm}
            \includegraphics[width=60mm,height=45mm]{figures/spiral2}}\vspace{5mm}
\centerline{\includegraphics[width=60mm,height=45mm]{figures/spiral3} \hspace{5mm}
            \includegraphics[width=60mm,height=45mm]{figures/spiral4}}
\caption{Animation of Alfred tumbling down a spiral staircase.\label{sampleSpiral}}
\end{figure}


\documentclass[10pt, aspectratio=169]{beamer}
\usefonttheme{professionalfonts}
%\usetheme{CambridgeUS}
%
% Choose how your presentation looks.
%
% For more themes, color themes and font themes, see:
% http://deic.uab.es/~iblanes/beamer_gallery/index_by_theme.html
%
\mode<presentation>
{
  \usetheme{default}      % or try Darmstadt, Madrid, Warsaw, ...
  \usecolortheme{beaver} % or try albatross, beaver, crane, ...
  \usefonttheme{default}  % or try serif, structurebold, ...
  \setbeamertemplate{navigation symbols}{}
  \setbeamertemplate{caption}[numbered]
}

\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{array}  % for table column M
\usepackage{makecell} % to break line within a cell
\usepackage{verbatim}
\usepackage{graphicx}
\usepackage{epstopdf}
\usepackage{amsfonts}
\usepackage{xcolor}
%\captionsetup{compatibility=false}
%\usepackage{dsfont}
\usepackage[absolute,overlay]{textpos}
\usetikzlibrary{calc}
\usetikzlibrary{pgfplots.fillbetween, backgrounds}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows}
\usetikzlibrary{pgfplots.groupplots}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{plotmarks}

\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=newest}
%\pgfplotsset{plot coordinates/math parser=false}

\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,
    urlcolor=cyan,
}

%%
\def\EXTERNALIZE{1} % for externalizing figures
\input{header.tex}

\title[EE 264]{Changing the Sampling Rate in DSP}
\author{Jose Krause Perin}
\institute{Stanford University}
\date{July 11, 2017}

\begin{document}

\begin{frame}
  \titlepage
\end{frame}

\begin{frame}{Last lecture}
\begin{itemize}
	\item Sampling a continuous-time signal results in replicas of the spectrum at multiples of the sampling frequency $\Omega_s$ (or $2\pi$ of the normalized frequency $\omega$)
	\item A band-limited signal has highest frequency $\Omega_N$ ($X_c(j\Omega) = 0, |\Omega| > \Omega_N$)
	\item If a band-limited signal is oversampled ($\Omega_s > 2\Omega_N$) there'll be gaps between the spectrum replicas
	\item If the signal is undersampled ($\Omega_s < 2\Omega_N$) the spectrum replicas will overlap resulting in aliasing distortion
	\item We can perfectly reconstruct a signal from its samples, provided that there is no aliasing and that we use the ideal lowpass filter as reconstruction filter
	\item In practice, we use different reconstruction filters, since the ideal lowpass filter is unfeasible.
	\item Oversampling relaxes the reconstruction filter specifications
	\item In theory, we can perform any LTI continuous-time filtering in discrete-time (in DSP), provided that there is no aliasing and that we use the ideal reconstruction filter
\end{itemize}
\end{frame}

%
\begin{frame}{Today's lecture}

\begin{itemize}
	\item Downsampling and decimation
	\item Upsampling and interpolation
	\item Noninteger rate change
	\item Multi-rate processing
\end{itemize}
\end{frame}

%
\section{Downsampling and Decimation}
\begin{frame}{Downsampling}

Downsampling by an integer factor $M$ is equivalent to sampling the discrete-time signal $x[n]$ with sampling period $M$.

\begin{center}
	\resizebox{0.6\linewidth}{!}{\input{figs/downsampling_block.tex}}
\end{center}

To understand what happens in the frequency domain, we can think that we're sampling the original continuous-time signal $x_c(t)$ with sampling period $T_d = MT$:
\begin{equation*}
x_d[n] = x[Mn] = x_c(nMT)
\end{equation*}

\end{frame}

%
\begin{frame}{Downsampling: frequency domain interpretation}
\fontsize{9pt}{7.2}\selectfont
%\vspace{-1cm}
Sampling $x_c(t)$ with sampling period $T_d = MT$ results in
\begin{align*}
	X_d(e^{j\omega}) &= \frac{1}{T_d}\sum_{k = -\infty}^{\infty} X_c\Big[j\Big(\frac{\omega}{T_d} - \frac{2\pi k}{T_d}\Big)\Big] \tag{spectrum replicas appear with period $\Omega_s = 2\pi/T_d$} \\
	&= \frac{1}{MT}\sum_{k = -\infty}^{\infty} X_c\Big[j\Big(\frac{\omega}{MT} - \frac{2\pi k}{MT}\Big)\Big] \tag{$T_d = MT$} \\
	&= \frac{1}{MT}\sum_{m = 0}^{M-1}\sum_{l = -\infty}^{\infty} X_c\Big(j\Big(\frac{\omega}{MT} - \frac{2\pi l}{T} - \frac{2\pi m}{MT}\Big)\Big) \tag{change of variables: $k = m + lM$} \\
	&= \frac{1}{M}\sum_{m = 0}^{M-1}\tikz[baseline]{\node[fill=blue!20,anchor=base] (t1) {$\displaystyle\frac{1}{T}\sum_{l = -\infty}^{\infty} X_c\Big(j\Big(\frac{\omega - 2\pi m}{MT} - \frac{2\pi l}{T}\Big)\Big)$};} \tag{rearranging} \\
	&= \frac{1}{M}\sum_{m = 0}^{M-1} X(e^{j\omega^{\prime}})\Big|_{\omega^{\prime} = \frac{\omega-2\pi m}{M}} \tag{$\tikz[baseline]{\node[fill=blue!20,anchor=base] {$^{~}$};}$ is equivalent to $X(e^{j\omega^{\prime}})$ for $\omega^{\prime} = \frac{\omega-2\pi m}{M}$}
\end{align*}

\textbf{Conclusion:} $X(e^{j\omega})$ is stretched by a factor of $M$ ($\omega/M$), and there will be replicas of the spectrum with period $2\pi/M$.
\end{frame}

%
\begin{frame}{Downsampling: frequency domain interpretation}
Example of downsampling with $M = 2$ i.e., $T_d = 2T$.

\textbf{Impulse sampling interpretation:}
\begin{center}
	\resizebox{0.65\linewidth}{!}{\input{figs/downsampling_freq_domain_ct.tex}}
\end{center}
\onslide<3|handout:1>{After downsampling spectrum replicas appear with period $2\pi/T_d$}
\end{frame}

%
\begin{frame}{Downsampling: frequency domain interpretation}
Same example of downsampling with $M = 2$ i.e., $T_d = 2T$.

\textbf{Discrete-time interpretation:}
\begin{center}
	\resizebox{0.65\linewidth}{!}{\input{figs/downsampling_freq_domain.tex}}
\end{center}
\onslide<3|handout:1>{To obtain the spectrum we discrete time, we just need to use the change of variables $\omega = \Omega T$ and $\omega = \Omega T_d$}
\end{frame}

%
\begin{frame}{Downsampling: frequency domain interpretation}
Downsampling may lead to spectrum overlapping (\textbf{aliasing distortion}).

Example of downsampling with $M = 3$ i.e., $T_d = 3T$.
\begin{center}
	\resizebox{0.65\linewidth}{!}{\input{figs/downsampling_freq_domain_aliasing.tex}}
\end{center}
\end{frame}

%
\begin{frame}{Decimation}
Similarly to sampling, it is common to employ an \textbf{anti-aliasing filter} before downsampling in order to minimize aliasing.

Pre-filtering followed by downsampling is called \textbf{decimation}.

\begin{center}
	\resizebox{0.85\linewidth}{!}{\input{figs/decimation_block.tex}}
\end{center}
\vspace{-0.5cm}
\begin{align}
x[n] \Longleftrightarrow & X(e^{j\Omega T}) = \frac{1}{T}\sum_{k = -\infty}^\infty X_c\Big(j\Big(\Omega - \frac{2\pi k}{T}\Big)\Big) \tag{from sampling} \\
\tilde{x}[n] \Longleftrightarrow & \tilde{X}(e^{j\Omega T}) = H(e^{j\Omega T})X(e^{j\Omega T}) \tag{LTI output} \\
x_d[n] = \tilde{x}[nM] \Longleftrightarrow & \frac{1}{M}\sum_{m = 0}^{M-1} \tilde{X}(e^{j(\Omega T - 2\pi T)/M}) \tag{downsampling by $M$}
\end{align}

\end{frame}

%
\begin{frame}{Decimation: frequency domain interpretation}
Decimation with $M = 3$ i.e., $T_d = 3T$.
\begin{center}
	\resizebox{0.6\linewidth}{!}{\input{figs/decimation_freq_domain.tex}}
\end{center}
\onslide<4|handout:1>{Now the spectrum replicas do not overlap after downsampling by 3.}
\end{frame}

%
\section{Upsampling and Interpolation}
\begin{frame}{Interpolation}
Interpolation is used to increase the sampling rate by an integer factor $L$.

\begin{center}
	\resizebox{0.6\linewidth}{!}{\input{figs/upsampling_block.tex}}
\end{center}
\vspace{-0.2cm}
\begin{equation*}
x_e[n] = \begin{cases}
x[n/L], & 0, \pm L, \pm 2L, \ldots \\
0, & \text{otherwise}
\end{cases}\tag{after upsampling}
\end{equation*}

\pause
In the frequency domain:
\begin{align*}
X_e(e^{j\omega}) &= \sum_{n = -\infty}^{\infty} x_e[n]e^{-j\omega n} \tag{definition of DTFT} \\
&= \sum_{n = 0, \pm L, \ldots} x[n/L]e^{-j\omega n} \tag{from $x_e[n]$ equation above} \\
&= \sum_{k = -\infty}^{\infty} x[k]e^{-j\omega kL} \tag{change of variable: $k = n/L$} \\
&= X(e^{j\omega L}) \tag{from DTFT equation with $\omega L$}
\end{align*}
\end{frame}

%
\begin{frame}{Interpolation: frequency domain interpretation}
Example of interpolation with $L = 2$
\begin{center}
	\resizebox{0.55\linewidth}{!}{\input{figs/upsampling_freq_domain.tex}}
\end{center}
\end{frame}

%
\begin{frame}{Practical interpolation filters}
\begin{itemize}
	\item Similarly to what we saw in reconstruction (D-to-C), the ideal lowpass filter is not practical. Hence, we must use practical interpolation filters such as ZOH, linear interpolator, or cubic spline.
	\item \textbf{One important difference:} The interpolation filter used in reconstruction to convert from discrete-time to continuous-time was an analog filter (a continuous-time filter). The interpolation filter used for upsampling is realized in discrete-time (in DSP). Therefore, we have more flexibility.
\end{itemize}
\end{frame}

%
\begin{frame}{Example of interpolation}
\begin{itemize}
	\item The original signal has maximum frequency $\Omega_N = 1$ rad/s.
	\item From the Nyquist-Shannon theorem, we need $\Omega_s > 2\Omega_N$ in order to be able to achieve perfect reconstruction. Or equivalently, $T < \pi$ s.
	\item Sampling period is $T = \pi$
\end{itemize}

\begin{center}
	\resizebox{0.6\linewidth}{!}{\input{figs/interpolation_example.tex}}
\end{center}
\onslide<4|handout:1> {Truncated sinc filter had 11 coefficients, while ZOH had 2 and linear interpolator had 3.}
\end{frame}

%
\begin{frame}{Example of interpolation}
Same example as before, but now sampling period is $T = 0.4\pi$.

\begin{center}
	\resizebox{0.7\linewidth}{!}{\input{figs/interpolation_example_oversampled.tex}}
\end{center}
\onslide<4|handout:1> {Truncated sinc filter had 11 coefficients, while ZOH had 2 and linear interpolator had 3.}
\end{frame}

%
\begin{frame}<beamer:1|handout:1>
%\fontsize{9pt}{7.2}\selectfont
\begin{itemize}
	\item In the first example the continuous-time signal was sampled at the Nyquist rate, whereas in the second example the continuous-time signal was oversampled by 2.5.
	\item In both cases, we upsample by a factor of 2 and use practical reconstruction filters: ZOH, a linear interpolator, and a truncated sinc with 11 samples.
	\item The ZOH filter only has two coefficients, the linear interpolator has three coefficents, and the truncated sinc has 11 coefficients.
	\item Although we use the same filters in both examples, the interpolated sequences are much closer to the original continuous-time signal in the second example. This illustrates how oversampling can help the interpolation filters.
	\item In these examples, the linear interpolator offers that best performance vs complexity trade-off, as it achieves performance close to the truncated sinc, but only uses 3 samples.
	\item Even with high oversampling, we see that the truncated sinc filter didn't achieve perfect reconstruction. This is a consequence of the \textbf{Gibbs phenomenon}, discussed in lecture 1. Recall that a truncated sinc will produce a DTFT that is different from the ideal lowpass filter. Specifically, the DTFT of the truncated sequence will have oscillations, which will affect the signal and will not suppress the spectrum replicas centered at multiples of $2\pi$. Using an even larger sequence (sinc with more samples) would not help much, since the ripples would only become more rapid, but their amplitude would not decrease.
\end{itemize}
\end{frame}

\begin{frame}{In Matlab}

Sampled signal with period $T$\\
\texttt{>> T = 0.5*pi} \\
\texttt{>> t = -20:T:20} \\
\texttt{>> x = cos(t/2) - sin(t) + cos(t/2-pi/4) - sin(t/4-deg2rad(154)); {\color{matlabcomment} \% Sampled signal}} \\

Upsample\\
\texttt{>> xu = upsample(x, L)  {\color{matlabcomment} \% Upsample}} \\

Interpolation filters \\
\texttt{>> hZOH = [1 1]  {\color{matlabcomment} \% ZOH}} \\
\texttt{>> hlin = [1/2 1 1/2] {\color{matlabcomment} \% Linear interpolator}} \\
\texttt{>> hsinc = sinc(-(5:5)/2) {\color{matlabcomment} \% truncated sinc with 11 samples}} \\

Interpolate \\
\texttt{>> yzoh = filter(hZOH, 1, xu)} \\
\texttt{>> ylin = filter(hlin, 1, xu)} \\
\texttt{>> ysinc = filter(hsinc, 1, xu)} \\
\texttt{{\color{matlabcomment} \% since these filters are FIR we could also have used the conv command}}
~\\

Before plotting we need to remove the group delay introduced by the filters (more on this next week)
\end{frame}

%
\section{Non-Integer Rate Change}
\begin{frame}{Interpolation/Decimation by a non-integer factor}
\begin{itemize}
	\item We have seen how to increase the sampling period by an integer factor $M$ and how to decrease the sampling period by an integer factor $L$
	\item By cascading interpolation and decimation we can change the sampling period by a non-integer factor $M/L$.
\end{itemize}

\begin{block}{Cascading interpolation and decimation}
\begin{center}
	\resizebox{0.85\linewidth}{!}{\input{figs/non-integer_rate_change_diagram.tex}}
\end{center}
\end{block}
\vspace{-0.5cm}
\onslide<2|handout:1>{
\begin{block}{Equivalent diagram}
\begin{center}
	\resizebox{0.55\linewidth}{!}{\input{figs/non-integer_rate_change_equivalent_diagram.tex}}
\end{center}
\end{block}
}
\end{frame}

\begin{frame}{Example}
	\only<1-3|handout:1>{
	\begin{itemize}
		\item Let's combine the examples we saw earlier with $L = 2$ and $M = 3$. Recall that with $M = 3$, there would be aliasing if we didn't use the anti-aliasing filter.
		\item The filter cutoff is $\min(\pi/2, \pi/3) = \pi/3$.
	\end{itemize}
	}
	\only<4-|handout:2>{
		continuing...
	}
	\begin{center}
		\resizebox{0.65\linewidth}{!}{\input{figs/non-integer_rate_change_freq_domain.tex}}
	\end{center}
	\only<5-|handout:2>{
		The resulting signal has sampling period $MT/L = 3T/2$. Note that aliasing was prevented by selecting the cutoff frequency $\min(\pi/2, \pi/3) = \pi/3$.
	}
\end{frame}

%
\section{Multi-rate processing}
\begin{frame}{Multirate processing}
	In practice, it is common to have parts of the system operating at one sampling rate and other parts operating at a different sampling rate.
	\begin{itemize}
		\item Interchanging filtering and downsampling
		\item Interchanging filtering and upsampling
		\item Multi-stage decimation
		\item Multi-stage interpolation
		\item Polyphase decomposition
	\end{itemize}

\end{frame}

\begin{frame}{Interchanging filtering and downsampling}
	These two systems are equivalent i.e., $y_1[n] = y_2[n]$
	\begin{center}
		\resizebox{0.6\linewidth}{!}{\input{figs/interchange_filter_downsampling.tex}}
	\end{center}

	To move the filter before downsampling by $M$, we must stretch its frequency response by a factor of $M$: $H(z^M) \xrightarrow{z = e^{j\omega}} H(e^{j\omega M})$.
\end{frame}

\begin{frame}
	\textit{Proof:}

	Staring with the second system: $X_2(e^{j\omega}) = H(e^{j\omega M})X(e^{j\omega})$. Now we can apply the equation for downsampling to obtain $Y_2(e^{j\omega})$
	\begin{align*}
	Y_2(e^{j\omega}) &= \frac{1}{M}\sum_{m= 0}^{M-1} X_2(e^{j(\omega/M - 2\pi m)}) \\
	&=\frac{1}{M}\sum_{m= 0}^{M-1} X(e^{j(\omega/M - 2\pi m)})H(e^{j(\omega - 2\pi m)}) \\
	&=H(e^{j\omega})\frac{1}{M}\sum_{m= 0}^{M-1} X(e^{j(\omega/M - 2\pi m)}) \tag{periodicity of the DTFT $\implies H(e^{j(\omega - 2\pi m)}) = H(e^{j\omega})$} \\
	&=H(e^{j\omega})\tikz[baseline]{\node[fill=blue!20,anchor=base] {$\displaystyle\frac{1}{M}\sum_{m= 0}^{M-1} X(e^{j(\omega/M - 2\pi m)})$};} \\
	&=H(e^{j\omega})\tikz[baseline]{\node[fill=blue!20,anchor=base] {$X_1(e^{j\omega})$};} = Y_1(e^{j\omega})
	\end{align*}
\end{frame}

\begin{frame}
\textbf{Comments on stability:}

Note that if $H(z)$ is a rational $z$-transform with poles $\{p_1, p_2, \ldots, p_N\}$ and zeros $\{z_1, z_2, \ldots, z_R\}$:
\begin{equation*}
H(z) = \frac{b_0}{a_0}z^{N-R}\frac{(z-z_1)(z-z_2)\ldots(z-z_R)}{(z-p_1)(z-p_2)\ldots(z-p_N)}
\end{equation*}
Then $H(z^M)$ will be
\begin{equation*}
H(z^M) = \frac{b_0}{a_0}z^{M(N-R)}\frac{(z^M-z_1)(z^M-z_2)\ldots(z^M-z_R)}{(z^M-p_1)(z^M-p_2)\ldots(z^M-p_N)},
\end{equation*}
with poles $\{\sqrt[M]{p_1}, \sqrt[M]{p_N}, \ldots, \sqrt[M]{p_N}\}$, and zeros $\{\sqrt[M]{z_1}, \sqrt[M]{z_N}, \ldots, \sqrt[M]{z_R}\}$.

If $H(z)$ is stable and \textbf{causal}, the poles of $H(z^M)$ will lie \underline{inside the unit circle}, and therefore $H(z^M)$ will also be stable.

Similarly, if $H(z)$ is \textbf{anti-causal} and stable, the poles of $H(z^M)$ will lie \underline{outside the unit circle}, and therefore $H(z^M)$ will also be stable.

\end{frame}

\begin{frame}{Interchanging filtering and interpolation}
	These two systems are equivalent i.e., $y_1[n] = y_2[n]$
	\begin{center}
		\resizebox{0.5\linewidth}{!}{\input{figs/interchange_filter_interpolation.tex}}
	\end{center}
	\textit{Proof:}

	Top diagram:
	\begin{align*}
	Y_1(e^{j\omega}) = X_1(e^{j\omega L}) = X(e^{j\omega L})H(e^{j\omega L})
	\end{align*}

	Bottom diagram:
	\begin{align*}
	Y_2(e^{j\omega}) = X_2(e^{j\omega})H(e^{j\omega L}) = X(e^{j\omega L})H(e^{j\omega L})
	\end{align*}
\end{frame}

\begin{frame}{Multi-stage decimation}
	Suppose we want to decimate by a factor $M = 20$. The cutoff frequency of the lowpass filter would be $\pi/20$.
	\begin{equation*}
	\text{Sharp  filter} \implies \text{long impulse response} \implies \begin{array}{c}
	\text{higher complexity} \\
	\text{higher cost} \\
	\text{higher power consumption}
	\end{array}
	\end{equation*}

	\pause
	It's more efficient to use several decimation stages
	\begin{center}
		\resizebox{0.45\linewidth}{!}{\input{figs/multistage_decimation_block.tex}}
	\end{center}
	\pause

	Interchanging filter and downsampling results in the equivalent system:

	\begin{center}
	\resizebox{0.45\linewidth}{!}{
		\begin{tikzpicture}[->, >=stealth, shorten >= 0pt, draw=black!50, node distance=3.5cm, font=\sffamily]
			\tikzstyle{node}=[circle,fill=black,minimum size=2pt,inner sep=0pt]
			\tikzstyle{block}=[draw=black,rectangle,fill=none,minimum size=1.5cm, inner sep=0pt]

			\node[node] (xc) {};
			\node[block, right=1cm of xc, text width = 2.5cm, align= center] (AA) {$H_1(z)H_2(z^{M_1})$};
			\node[block, right of=AA, text width = 2cm, align= center] (DSP) {$~~(M_1M_2)$};
			\draw[-latex, shorten >= 15pt, shorten <= 15pt, line width=0.75pt] ($(DSP.north)-(17pt, 0)$) -- ($(DSP.south)-(17pt, 0)$) {};
			\coordinate (yc) at ($(DSP.east)+(1cm, 0)$) {};

			\path (xc) edge (AA);
			\path (AA) edge (DSP);
			\path (DSP) edge (yc);

			\node[above = 0mm of xc, text width = 1cm, align=center] {$x[n]$};
			\node[above = 0mm of yc, text width = 1cm, align=center] {$y[n]$};
		\end{tikzpicture}
	}
	\end{center}

	\begin{itemize}
		\item The equivalent downsampling factor is $M = M_1M_2$.
		\item Design $H_1(z)$ and $H_2(z)$ so that $H_1(z)H_2(z^{M_1})$ has the desired frequency response.
	\end{itemize}
\end{frame}

\begin{frame}{Multi-stage interpolation}
	The same rationale applies to interpolation
	\begin{center}
		\resizebox{0.6\linewidth}{!}{\input{figs/multistage_interpolation_block.tex}}
	\end{center}
	Interchanging filter and downsampling results in the equivalent system:

	\begin{center}
		\resizebox{0.6\linewidth}{!}{
			\begin{tikzpicture}[->, >=stealth, shorten >= 0pt, draw=black!50, node distance=3.5cm, font=\sffamily]
			\tikzstyle{node}=[circle,fill=black,minimum size=2pt,inner sep=0pt]
			\tikzstyle{block}=[draw=black,rectangle,fill=none,minimum size=1.5cm, inner sep=0pt]

			\node[node] (xc) {};
			\node[block, right=1cm of xc, text width = 2cm, align= center] (DSP) {$~~(L_1L_2)$};
			\draw[-latex, shorten >= 15pt, shorten <= 15pt, line width=0.75pt] ($(DSP.south)-(17pt, 0)$) -- ($(DSP.north)-(17pt, 0)$) {};
			\node[block, right of=DSP, text width = 2.5cm, align= center] (AA) {$H_1(z^{L_2})H_2(z)$};

			\coordinate (yc) at ($(AA.east)+(1cm, 0)$) {};

			\path (xc) edge (DSP);
			\path (DSP) edge (AA);
			\path (AA) edge (yc);

			\node[above = 0mm of xc, text width = 1cm, align=center] {$x[n]$};
			\node[above = 0mm of yc, text width = 1cm, align=center] {$y[n]$};
			\end{tikzpicture}
		}
	\end{center}

	\begin{itemize}
		\item The equivalent upsampling factor is $L = L_1L_2$.
		\item Design $H_1(z)$ and $H_2(z)$ so that $H_1(z^{L_2})H_2(z)$ has the desired frequency response.
	\end{itemize}
\end{frame}

\begin{frame}{Polyphase decomposition}
	What if the filter is placed before downsampling?
	\begin{center}
		\resizebox{0.6\linewidth}{!}{\input{figs/filter_downsampling.tex}}
	\end{center}
	\begin{itemize}
		\item To interchange filter and downsampling in this case, we'd need to express $H(z)$ as some $G(z^M)$. Generally not easy.
		\item \textbf{Practical problem:} this implementation wastes computation. All samples of the output of $H(z)$ are calculated, but only 1 out of $M$ is used after downsampling.
		\item If $H(z)$ is FIR of length $N$, there are $N$ multiplications per sample. Downsampling by $M$ discards $M-1$ samples every $M$ samples.
	\end{itemize}
\end{frame}

\begin{frame}{Polyphase decomposition}
	We can decompose any given sequence $h[n]$ into $M$ subsequences such that
	\begin{equation*}
	h_k[n] = \begin{cases}
	h[n+k], & n~\text{integer multiple of } M \\
	0, & \text{otherwise}
	\end{cases}, k = 0, 1, \ldots, M-1
	\end{equation*}

	It follows that
	\begin{equation*}
	h[n] = \sum_{k = 0}^{M-1} h_k[n-k] \Longleftrightarrow H(z) = \sum_{k = 0}^{M-1} H_k(z)z^{-k}
	\end{equation*}
\end{frame}

\begin{frame}{Polyphase decomposition}
	Example of decomposition with $M = 2$

	\begin{center}
	\resizebox{0.6\linewidth}{!}{\input{figs/polyphase_decomposition_time_domain.tex}}
\end{center}
\end{frame}

\begin{frame}{Polyphase decomposition}
	We can downsample $h_k[n]$ in order to discard the zero samples
	\begin{equation*}
	e_k[n] = h_k[Mn] \Longleftrightarrow E_k(z^M) = H_k(z) \tag{upsamling by M}
	\end{equation*}

	The subsequences $e_k[n]$ are called the \textbf{polyphase components} of $h[n]$
	\begin{center}
		\resizebox{0.5\linewidth}{!}{\input{figs/polyphase_decomposition_e_time_domain.tex}}
	\end{center}
\end{frame}

\begin{frame}{Polyphase decomposition}
	How to recover $h[n]$ from $e_0[n], \ldots, e_{M-1}[n]$?

	\begin{enumerate}
		\item Upsample $e_k[n]$ by $M$, and we're back with $h_k[n]$
		\item Delay by $k$ and add
	\end{enumerate}

	In terms of the $z$-transform:
	\begin{equation*}
	H(z) = \sum_{k=0}^{M-1} E_k(z^M)z^{-k}
	\end{equation*}

	\begin{center}
		\resizebox{0.6\linewidth}{!}{\input{figs/polyphase_decomposition_diagram.tex}}
	\end{center}
\end{frame}

\begin{frame}{Polyphase decimation}
	Back to the original problem: how to interchange filter and downsampling?
	\begin{center}
		\resizebox{0.5\linewidth}{!}{\input{figs/filter_downsampling.tex}}
	\end{center}
	Using the polyphase decomposition of $H(z)$
	\begin{center}
		\def\DOWN{1}
		\resizebox{0.8\linewidth}{!}{\input{figs/polyphase_decomposition_diagram.tex}}
	\end{center}
\end{frame}

%
\begin{frame}{Polyphase decimation}
	First interchange sum and downsampling:
 	\begin{center}
	 	\resizebox{0.9\linewidth}{!}{\input{figs/polyphase_decomposition_diagram2.tex}}
 	\end{center}
\end{frame}

%
\begin{frame}{Polyphase decimation}
	Now it is easy to interchange $E_k(\tikz[baseline]{\node[fill=blue!20,anchor=base] {$z^M$};})$ with downsampling resulting in the filters $E_k(\tikz[baseline]{\node[fill=blue!20,anchor=base] {$z$};})$
	\begin{center}
		\resizebox{0.9\linewidth}{!}{\input{figs/polyphase_decomposition_diagram3.tex}}
	\end{center}
\textbf{Computation:} Each polyphase filter $E_k(z)$ requires $N/M$ multiplications, which are realized at the lower rate (higher sampling period) $TM$.

Similarly for polyphase interpolation (Textbook section 4.7.5)

\end{frame}

%
\begin{frame}{Summary}
\begin{itemize}
	\item Downsampling by an integer factor $M$ stretches the discrete-time spectrum by a factor $M$ and causes replicas of the spectrum to appear at $2\pi/M$. The amplitude of the spectrum is attenuated by $M$
	\item It's often easier to think of downsampling as sampling the original continuous-time signal with a sampling period $T_d = MT$
	\item Anti-aliasing filtering followed by downsampling is called decimation
	\item Upsampling by an integer factor $L$ compresses the discrete-time spectrum by a factor $L$. The interpolation filter is assumed to have gain $L$, so the spectrum amplitude is scaled by $L$
	\item We can achieve non-integer sampling rate changes by cascading interpolation and decimation stages
	\item For large downsampling/uspsampling factors, it's generally more efficient to realized multistage decimation/interpolation
	\item Polyphase decomposition allows efficient implementation of filtering followed by downsampling and upsampling followed by filtering.
\end{itemize}
\end{frame}

\end{document}