Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix to PSP metric to account for unbiased cross-correlation #33

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions src/cedalion/sigproc/quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ def psp(
# the first sample in the window. Setting the stride size to the same value as the
# window length will result in non-overlapping windows.
windows = amp.rolling(time=nsamples).construct("window", stride=nsamples)

windows = windows.fillna(1e-6)
fs = amp.cd.sampling_rate

psp = np.zeros([len(windows["channel"]), len(windows["time"])])

# Vectorized signal extraction and correlation
sig = windows.transpose("channel", "time", "wavelength", "window").values
psp = np.zeros((sig.shape[0], sig.shape[1]))
lags = np.arange(-nsamples + 1, nsamples)

for w in range(sig.shape[1]): # loop over windows
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use sig.sizes['time'] for clarity

sig_temp = sig[:,w,:,:]
Expand All @@ -118,26 +119,30 @@ def psp(
)

# FIXME assumes 2 wavelengths
norm_factor = [
np.sqrt(np.sum(sig_temp[ch, 0, :] ** 2) * np.sum(sig_temp[ch, 1, :] ** 2))
for ch in range(sig.shape[0])
]

corr /= np.tile(norm_factor, (corr.shape[1],1)).T

for ch in range(sig.shape[0]):
window = signal.windows.hamming(len(corr[ch,:]))
f, pxx = signal.periodogram(
corr[ch, :],
window=window,
nfft=len(corr[ch, :]),
fs=fs,
scaling="spectrum",
)

psp[ch, w] = np.max(pxx)
corr = corr /(nsamples - np.abs(lags))

nperseg = corr.shape[1]
window = np.hamming(nperseg)
window_seg = corr * window

fft_out = np.fft.rfft(window_seg, axis=1)
psd = (np.abs(fft_out) ** 2) / (fs * np.sum(window ** 2))
freqs = np.fft.rfftfreq(nperseg, 1/fs)

# for ch in range(sig.shape[0]):
# window = signal.windows.hamming(len(corr[ch,:]))
# f, pxx = signal.welch(
# corr[ch, :],
# window=window,
# nfft=len(corr[ch, :]),
# fs=fs,
# scaling="density",
# )

psp[:, w] = np.max(psd, 1)

# keep dims channel and time

psp_xr = windows.isel(wavelength=0, window=0).drop_vars("wavelength").copy(data=psp)

# Apply threshold mask
Expand Down
Loading