---
title: "SVS QA"
output: html_document
params:
  data: ""
  label: ""
---

```{r setup, include=FALSE}
library(spant)
library(ggplot2)
knitr::opts_chunk$set(echo = FALSE)
theme_set(theme_bw())
```

## `r params$label`

### Mean spectra
```{r, fig.width=10}
par(mfcol = c(1, 2))
plot(zf(params$data$mean_corr), xlim = c(4, 0.2), restore_def_par = FALSE,
     main = "Freq & phase corrected")
plot(zf(params$data$mean_uncorr), xlim = c(4, 0.2), main = "Uncorrected")
```

### Quality metrics
Median single shot SNR : `r round(params$data$summary_diags['ss_median_spec_snr'], 1)`

Linewidth of mean spectrum (post FP correction) : `r round(params$data$summary_diags['mean_corr_spec_lw'], 3)` ppm

SNR of mean spectrum (post FP correction) : `r round(params$data$summary_diags['mean_corr_spec_snr'], 1)`

Linewidth of mean spectrum (no correction) : `r round(params$data$summary_diags['mean_uncorr_spec_lw'], 3)` ppm

SNR of mean spectrum (no correction) : `r round(params$data$summary_diags['mean_uncorr_spec_snr'], 1)`

Frequency shift range : `r round(params$data$summary_diags['shift_hz_range'], 1)` Hz

Dynamic lipid fluctuation range : `r round(100 * params$data$summary_diags['dlfr'], 1)`% tCr intensity

### Dynamic frequency shifts
```{r, fig.width=10}
diag_table <- params$data$diag_table
    ggplot(diag_table, aes(x = time_sec, y = shifts_hz)) + geom_line() + 
      xlab("Time (s)") + ylab("Shift (Hz)")
```

### Dynamic linewidths
```{r, fig.width=10}
diag_table <- params$data$diag_table
p <- ggplot(diag_table, aes(x = time_sec, y = lw_ppm)) + geom_line() +
  xlab("Time (s)") + ylab("tNAA linewidth (ppm)") +
  theme(legend.position = "none")
if (!anyNA(diag_table$lw_ppm)) {
  p <- p + geom_line(aes(x = time_sec, y = lw_ppm_smo), linewidth = 1.5,
                     colour = "red")
}
print(p)
```

### Spectrograms
```{r, fig.width=10}
corr_spec   <- crop_spec(params$data$corrected)
uncorr_spec <- crop_spec(params$data$uncorrected)
par(mfcol = c(1, 2))
image(corr_spec, xlim = c(4, 0.2), restore_def_par = FALSE,
      main = "Freq & phase corrected", plot_dim = "time_sec", vline = NULL,
      vline_col = "red", vline_lty = 1)
image(uncorr_spec, xlim = c(4, 0.2), main = "Uncorrected",
      vline = NULL, plot_dim = "time_sec", vline_col = "red", vline_lty = 1)
```

### Mean subtracted spectrograms
```{r, fig.width=10}
par(mfcol = c(1, 2))
image(params$data$mrs_mean_sub, xlim = c(4, 0.2), restore_def_par = FALSE,
      main = "Smoothed", plot_dim = "time_sec")
image(params$data$mrs_mean_sub_bc, xlim = c(4, 0.2),
      main = "Smoothed and baseline corrected", plot_dim = "time_sec")
```