ECE 401 Fall 2021 MP4: Ideal and Practical Filters

In this file, we're going to lowpass, bandpass, and highpass filter some EEG signals. The signals we'll be using are samples provided with the MNE package. It is recommended, but not required, that you first install MME. You can do so using the instructions on the MNE homepage. On a MAC you will go to a command window, and type

conda create --name=mne --channel=conda-forge mne
conda install --name=base nb_conda_kernels

If you don't want to install MNE, you can do the whole MP without it, by simply avoiding the blocks of code marked Requires MNE below.

Data: With MNE

Here's how the data works. If you want to use MNE to visualize the data, you first need to install the mne environment as described above. Then, every time you want to run the MNE code on this page, you'll need to do the following steps:

  1. In an operating system command window, type conda activate mne, and then jupyter-notebook. That should open up your Jupyter browser, just as if you had opened it from the GUI, except it will make the following steps possible (they wouldn't be possible if you used the GUI).
  2. Open this file (mp4_notebook.ipynb).
  3. Go to the top of this screen, and find the Kernel menu. Under that menu, choose Change Kernel -> Python [conda env:mne].

Now you should be able to run the following block of code without errors:

Data: With or Without MNE

If you don't want to use MNE, you can run the following block of code to load the repackaged data. You should run the following block whether or not you're using MNE, because some of the blocks that follow will use this version of the data.

1. Browsing the Data

EEG data consists of 60 different voltage signals, recorded in parallel from different electrodes on the scalp. Here is a function that you can use to choose three of the signals, and plot both the signals and their power spectra:

Browsing the Data: With MNE

If you have installed MNE, you can use the plot_topomap function to see how the voltages are distributed over the scalp at each time step. Here's a function that plots the voltages as a function of location, for 25 different time steps:

Running it requires MNE. It requires that you have already run the MNE sample-loading code, up above, to load the info object that contains the spatial locations of the electrodes.

2. Running the MP

Now you're ready to run the MP. Each block of code, below, will first show you the distributed solution (distributed to you in the file solutions.hdf5), and will then run the code in your own file submitted.py, to test whether or not your code works.

2.1 todo_lpf_even

This function tests whether or not you can create a lowpass filter with a specified cutoff frequency, and with even length. The distributed solution contains a 200-sample lowpass filter with a cutoff of pi/4. Notice that the loglog plot doesn't quite know what to do with the aliased part of the filter, up near $\omega=2\pi$. It isn't really designed to handle the frequency responses of digital filters like this one.

If your code is working, the following block should produce exactly the same plot. If you haven't written any code yet, the following block will raise a NotImplementedError.

2.2 todo_lpf_odd

This function creates a lowpass filter with an odd length. The distributed solution contains an example with length 201, and cutoff of pi/4.

2.3 todo_h_theta

We're going to be a little sloppy, and say that all components of the signal below 7Hz are "theta-waves." In order to extract those signals, you need to design a lowpass filter with a cutoff at $\omega_L F_s/2\pi=7$Hz, where $F_s$ is the sampling frequency. The distributed solution creates a filter that's one second long.

2.4 todo_convolve

Now let's test to see if you can filter out the theta waves from the sample EEG signals. Theta waves are supposed to be particularly energetic during sleep, but the sample signal contains a lot of energy at these frequencies.

2.5 todo_h_alpha

Alpha waves (7-15Hz) are supposed to be particularly active when you are awake but at rest, e.g., when you have your eyes closed. In order to filter out alpha waves, let's create an ideal bandpass filter with cutoff frequencies of 7Hz and 14Hz.

2.6 todo_h_beta

Beta waves (15-30Hz) are supposed to be active whenever you are conscious. I find it interesting that this sample contains a lot of synchronous events (spikes that occur at the same time in different channels) in the beta band.

Many of the events that are reported in evoked-potential studies happen in the theta, alpha, and beta bands. For example, if the P150 and N300 events are of opposite polarity (positive and negative) and 150ms apart, then the fundamental frequency of that pair of events is 1/0.3=3.33Hz, which is in the theta band. Its third and fourth harmonics are in the alpha band, and its fifth through ninth harmonics are in the beta band.

2.7 todo_h_gamma

Gamma waves (above 30Hz) may contain some of the detailed modulations that distinguish different types of perceptual input. We will extract the gamma band using a highpass filter, which has two very different forms, depending on whether the filter length is odd or even:

That's all! If all of the above sections worked, try running python grade.py on the command line. If that also works, then try uploading your submitted.py file to the autograder.