---
output:
  pdf_document: default
  html_document:
    df_print: paged
---

```{r , include=FALSE}
library(mosaic) # load the mosaic package for later use
library(jpeg)

```

# Exam exercise for Module 4: Low pass filter

```{r, fig.width=8, echo=FALSE, fig.align='center'}
url <- "https://asta.math.aau.dk/static-files/asta/img/Lab-opstilling.jpg"
z <- tempfile()
download.file(url, z, mode = "wb")
grid::grid.raster(jpeg::readJPEG(z))
invisible(file.remove(z))
```
\begin{center}{\begin{large}{\bf Setup in lab.}\end{large}}\end{center}


### First order low pass filter

```{r, fig.width=4, echo=FALSE, fig.align='center'}
url <- "https://asta.math.aau.dk/static-files/asta/img/RC-circuit.jpg"
z <- tempfile()
download.file(url, z, mode = "wb")
grid::grid.raster(jpeg::readJPEG(z))
invisible(file.remove(z))
```

We shall study data associated with the displayed circuit.

The fundamental characteristics associated with the circuit is:
\newcommand{\mRt}{\mbox{R_true}}
\newcommand{\mCt}{\mbox{C_true}}
\newcommand{\mft}{\mbox{f_c_true}}


- $R$ is the resistance measured in Ohm
- $C$ is the capacitor measured in Farad
- $f_c$ is the 3dB cut-off frequency measured in Hertz


In theory the relation between these quantities is

$$f_c=\frac{1}{2\pi RC}$$

### Data

Peter Koch has spent quite some time in the lab producing data.

For different combinations of nominal $R$ and $C$ values Peter has measured $R$, $C$ and $f_c$.

Nominal $R$ values:

- 1, 1.1, 4.75 and 5.9 kOhm

Nominal $C$ values:

- 47, 56, 68, 330, 470, 560 and 680 nF

The 3 parameters have been measured for each combination of nominal values , that is, 4 resistor measurements, 7 capacitor measurements and 
4x7=28 measurements of frequency.


Load data:

```{r }
load(url("https://asta.math.aau.dk/datasets?file=RC_data.RData"))
head(RC_data)
```

On a logarithmic scale the model is:


- `log(fc_true)=-log(2*pi)-log(R_true)-log(C_true)`

where true refers to that this is the exact value of the parameter.

Actually we are measuring

- `log(fc_measured)=log(fc_true)+fc_error`
- `log(R_measured)=log(R_true)+R_error`
- `log(C_measured)=log(C_true)+C_error`


1) Show that:

- `log(fc_measured)=log_fc_predict+R_error+C_error+fc_error`
- where `log_fc_predict= -log(2*pi)-log(R_measured)-log(C_measured)`

2) Argue that if the meters have no systematic errors, then 

- `(log_fc_predict,log(fc_measured))` should vary around the identity line

Calculate and plot these points.

Argue that the plot calls for a linear calibration of `log(fc_measured)`.

3) Fit a simple linear regression of `log(fc_measured)` on `log_fc_predict`

Argue that

- the intercept is significantly different from zero.
- the slope is significantly different from one.

Meaning that meters must have systematic errors.

4) In the light of your conclusions do a calibration of `log(fc_measured)` and call it `log_fc_corrected`.

Do a plot of `(log_fc_predict,log_fc_corrected)`

5) The data version of the RC-model is now

- `log_fc_corrected=-log(2*pi)-log(R_measured)-log(C_measured)+error`

Fit a multiple regression of `log_fc_corrected` on `log(R_measured)`and `log(C_measured)` and test the hypotheses

- intercept is equal to  `-log(2*pi)`
- slope of `log(R_measured)`  is equal to -1.
- slope of  `log(C_measured)` is equal to -1.

6) If the fitted model is called `fit`, then the residuals is obtained by `resid(fit)`.

Do a `qqnorm` plot of the residuals and a Shapiro-Wilks test of normality of the residuals.

What is your conclusion?
