oephys2nix/doc/samplerates.qmd

183 lines
6.6 KiB
Plaintext

---
title: Differences between sample rates
format:
html:
toc: true
toc-title: Contents
code-block-bg: true
code-block-border-left: "#31BAE9"
code-line-numbers: true
highlight-style: atom-one
link-external-icon: true
link-external-newwindow: true
eqn-number: true
---
### 1. General Idea
The two aquisition systems have a different default sampling rate and currently
there is a delay and maybe this is due to the different sampling rates.
`open-ephys` has a sample-rate of 30 kHz and `relacs` one of 20 kHz. In this
test we have two different recordings with one where the open-epyhs has 30 kHz
and the other with 20 kHz.
### 2. Loading the data
```{python}
from pathlib import Path
import rlxnix as rlx
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import scipy.signal as signal
import numpy as np
# Path to test recording with different samplerate open-epyhs 30kHz and relacs 20kHz
dataset_path_diff_fs = Path("../oephys2nix/test/Test1/2025-10-08-aa-invivo-2-recording.nix")
relacs_path_diff_fs = Path("../oephys2nix/test/Test1/2025-10-08-aa-invivo-2_relacs/2025-10-08-aa-invivo-2.nix")
# Path to test recording with same samplerate open-epyhs 20kHz and relacs 20kHz
dataset_path_same_fs = Path("../oephys2nix/test/Test2/2025-10-08-ab-invivo-2-recording.nix")
relacs_path_same_fs = Path("../oephys2nix/test/Test2/2025-10-08-ab-invivo-2_relacs/2025-10-08-ab-invivo-2.nix")
dataset_diff_fs = rlx.Dataset(str(dataset_path_diff_fs))
relacs_diff_fs = rlx.Dataset(str(relacs_path_diff_fs))
dataset_same_fs = rlx.Dataset(str(dataset_path_same_fs))
relacs_same_fs = rlx.Dataset(str(relacs_path_same_fs))
repro_diff_fs_d = dataset_diff_fs.repro_runs("FileStimulus_1")[0].stimuli[2]
repro_diff_fs_r = relacs_diff_fs.repro_runs("FileStimulus_1")[0].stimuli[2]
repro_same_fs_d = dataset_same_fs.repro_runs("FileStimulus_1")[0].stimuli[2]
repro_same_fs_r = relacs_same_fs.repro_runs("FileStimulus_1")[0].stimuli[2]
#sinus, t = repro_diff_fs_d.trace_data("sinus")
#sinus_r, t_r = repro_diff_fs_r.trace_data("V-1")
stimulus_diff_oe, t_diff = repro_diff_fs_d.trace_data("stimulus")
stimulus_diff_re, t_diff_r = repro_diff_fs_r.trace_data("GlobalEFieldStimulus")
stimulus_same_oe, t_same = repro_same_fs_d.trace_data("stimulus")
stimulus_same_re, t_same_r = repro_same_fs_r.trace_data("GlobalEFieldStimulus")
```
### 3. Samples in the different recordings for one stimulus
```{python}
#| echo: False
print(f"Samples open-epyhs [30 kHz] for one trial: {stimulus_diff_oe.shape}")
print(f"Samples relacs for one trial: {stimulus_diff_re.shape}")
print(f"Samples open-epyhs [20 kHz] for one trial: {stimulus_same_oe.shape}")
print(f"Samples relacs for one trial: {stimulus_same_re.shape}")
```
### 4. Plotting first trial
Here we plot the different output stimulus, different sample rates
```{python}
#| echo: False
x_lim = 0.05
fig = make_subplots( rows=2, cols=1, shared_xaxes=True, subplot_titles=("Different fs [30 khz and 20 kHz]", "Same fs [20kHz]"))
fig.add_trace( go.Scattergl(x=t_diff_r[t_diff_r<x_lim],
y=stimulus_diff_re[t_diff_r<x_lim],
showlegend=False, line_color="blue",
mode="markers+lines"), row=1, col=1)
fig.add_trace( go.Scattergl(x=t_diff[t_diff<x_lim],
y=stimulus_diff_oe[t_diff<x_lim],
showlegend=False,
line_color="red", mode="markers+lines"), row=1, col=1)
fig.add_trace( go.Scattergl(x=t_same_r[t_same_r<x_lim],
y=stimulus_same_re[t_same_r<x_lim],
name="GlobalStimulus (relacs)", line_color="blue",
mode="markers+lines") , row=2, col=1)
fig.add_trace( go.Scattergl(x=t_same[t_same<x_lim],
y=stimulus_same_oe[t_same<x_lim],
name="GlobalStimulus (open-ephys)",
line_color="red", mode="markers+lines"),row=2, col=1)
fig.update_layout(
template="plotly_dark",
height=400,
legend=dict(
bgcolor="rgba(0,0,0,0)",
bordercolor="#444",
borderwidth=0,
font=dict(color="#e5ecf6"),
orientation="h",
yanchor="bottom",
y=1.06,
xanchor="right",
x=0.72,
)
)
fig.update_xaxes(range=[0, 0.01])
```
### 5. Lags in recodings
```{python}
# resample to 20 kHz
stimulus_diff_oe_resampled = signal.resample(stimulus_diff_oe, len(stimulus_same_re))
correlation_diff = signal.correlate(stimulus_diff_oe_resampled, stimulus_diff_re, mode="full")
lags_diff = signal.correlation_lags(stimulus_diff_oe_resampled.size, stimulus_diff_re.size, mode="full")
lag_diff = lags_diff[np.argmax(correlation_diff)]
correlation_same = signal.correlate(stimulus_same_oe, stimulus_same_re, mode="full")
lags_same = signal.correlation_lags(stimulus_same_oe.size, stimulus_same_re.size, mode="full")
lag_same = lags_same[np.argmax(correlation_same)]
print(f"The lag in with different sampling rates is {lag_diff}, and with the same sample rate is {lag_same}")
```
```{python}
#| echo: False
fig = make_subplots( rows=2, cols=1, shared_xaxes=True, subplot_titles=("Different fs [30 khz and 20 kHz]", "Same fs [20kHz]"))
fig.add_trace( go.Scattergl(x=t_diff_r[t_diff_r<x_lim],
y=np.roll(stimulus_diff_re[t_diff_r<x_lim], lag_diff),
line_color="blue",
showlegend=False,
mode="markers+lines"), row=1, col=1)
fig.add_trace( go.Scattergl(x=t_diff[t_diff<x_lim],
y=stimulus_diff_oe[t_diff<x_lim], showlegend=False,
line_color="red", mode="markers+lines"), row=1,
col=1)
fig.add_trace( go.Scattergl(x=t_same_r[t_same_r<x_lim],
y=np.roll(stimulus_same_re[t_same_r<x_lim], lag_same),
name="GlobalStimulus (relacs)", line_color="blue",
mode="markers+lines") , row=2, col=1)
fig.add_trace( go.Scattergl(x=t_same[t_same<x_lim],
y=stimulus_same_oe[t_same<x_lim],
name="GlobalStimulus (open-ephys)",
line_color="red", mode="markers+lines"),row=2, col=1)
fig.update_layout(
template="plotly_dark",
height=400,
legend=dict(
bgcolor="rgba(0,0,0,0)",
bordercolor="#444",
borderwidth=0,
font=dict(color="#e5ecf6"),
orientation="h",
yanchor="bottom",
y=1.06,
xanchor="right",
x=0.72,
)
)
fig.update_xaxes(range=[0, 0.01])
```
### 6. Conculsion
Lags of simuliar magnitude exists in both recordings therefor the sample rate is not the problem!