1
0

utils.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import numpy as np
  2. import mne
  3. def upsample_events(events, upsample_interval=500):
  4. # Upsample events every 500 sample points
  5. events_new = []
  6. for e_ in events:
  7. for i in range(0, e_[1] - upsample_interval + 1, upsample_interval):
  8. events_new.append([e_[0] + i, 0, e_[-1]])
  9. return np.array(events_new)
  10. def extend_signal(raw, frequencies, freq_band):
  11. """ Extend a signal with filter bank using MNE """
  12. raw_ext = np.vstack([
  13. bandpass_filter(raw, l_freq=f - freq_band, h_freq=f + freq_band)
  14. for f in frequencies]
  15. )
  16. info = mne.create_info(
  17. ch_names=sum(
  18. list(map(lambda f: [ch + '-' + str(f) + 'Hz'
  19. for ch in raw.ch_names],
  20. frequencies)), []),
  21. ch_types=['ecog'] * len(raw.ch_names) * len(frequencies),
  22. sfreq=int(raw.info['sfreq'])
  23. )
  24. return mne.io.RawArray(raw_ext, info)
  25. def bandpass_filter(raw, l_freq, h_freq, method="iir", verbose=False):
  26. """ Band-pass filter a signal using MNE """
  27. return raw.copy().filter(
  28. l_freq=l_freq,
  29. h_freq=h_freq,
  30. method=method,
  31. verbose=verbose
  32. ).get_data()