fooof-tools / Development

Development ideas for parameterizing neural power spectra.
0 stars 0 forks source link

Explore KDE for overlapping gaussians #1

Open TomDonoghue opened 4 years ago

TomDonoghue commented 4 years ago

Copied over from https://github.com/fooof-tools/fooof/issues/30, original post by @voytek:

Okay, the overlapping oscillations issue is really annoying. As much as I want to avoid an iterative procedure that requires more than one call to the gaussian multi-fitting function, we may need to, in some cases.

My proposed solution is to take the gaussian output from from FOOOF--which may have many noisy overlapping gaussian fits--and do kernel density estimation on that output. It looks like this on the right:

KDE from Wikipedia

This integrates those overlapping gaussians, and then we can try running FOOOF on that, again.

seaborn has some nice KDE code that only has a scipy dependency, so it doesn't change our dependencies at all.

from scipy import stats, integrate

support = np.linspace(np.min(frequency_vector), np.max(frequency_vector), np.size(frequency_vector))
bandwidth = 1.06 * x.std() * x.size ** (-1 / 2.)
kernels = []
for i in x:
    kernel = stats.norm(i, bandwidth).pdf(support)
    kernels.append(kernel)

density = np.sum(kernels, axis=0)
density /= integrate.trapz(density, support)