Skip to content

Instantly share code, notes, and snippets.

@TomDLT
Last active July 11, 2016 09:45
Show Gist options
  • Save TomDLT/de4229bdc9da5d986b8ed856942a87fe to your computer and use it in GitHub Desktop.
Save TomDLT/de4229bdc9da5d986b8ed856942a87fe to your computer and use it in GitHub Desktop.
Some examples of the peak_finder function in MNE-python
import numpy as np
import matplotlib.pyplot as plt
from mne.preprocessing.peak_finder import peak_finder
def test_peak_finder(signal, thresh=None):
plt.plot(signal)
# find the maxima
peak_loc, peak_mag = peak_finder(signal, thresh=thresh, extrema=+1)
plt.scatter(peak_loc, peak_mag, color='k')
# find the minima
peak_loc, peak_mag = peak_finder(signal, thresh=thresh, extrema=-1)
plt.scatter(peak_loc, peak_mag, color='r')
plt.show()
n_points = 300
rng = np.random.RandomState(42)
# ------- test with just noise
signal = rng.randn(n_points)
test_peak_finder(signal)
# ------- test with noisy sinusoid
signal = np.sin(np.linspace(0, 20, n_points)) + 0.1 * rng.randn(n_points)
test_peak_finder(signal)
# ------- test with noisier sinusoid: weird result
signal = np.sin(np.linspace(0, 20, n_points)) + 0.3 * rng.randn(n_points)
test_peak_finder(signal)
# ------- solution: smooth it with a convolution
plt.plot(signal, alpha=0.5)
smoothing = 10
window = np.blackman(smoothing)
window /= window.sum()
signal_smooth = np.convolve(signal, window, mode='same')
test_peak_finder(signal_smooth)
# ------- solution: extreme smoothing
plt.plot(signal, alpha=0.5)
smoothing = 100
window = np.blackman(smoothing)
window /= window.sum()
signal = np.convolve(signal, window, mode='same')
test_peak_finder(signal_smooth)
# ------- test with a trend
signal = (np.linspace(0, 10, n_points)
+ np.sin(np.linspace(0, 20, n_points))
+ 0.1 * rng.randn(n_points))
test_peak_finder(signal)
# ------- solution: change threshold
test_peak_finder(signal, thresh=0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment