Lab 4

Matlab: Basic Filtering

A filter is a system through which a signal can be passed to change the amplitude and phase of any sinusoidal component of that signal. Four main basic filter types exist:
1) a low-pass filter, which lets low frequencies pass through but cuts out high frequencies (also called a high-cut filter).
2) a high-pass (or low-cut) filter, which lets high frequencies pass through but cuts out low frequencies.
3) a band-pass filter, which allows frequencies through in some band in the middle of the spectrum, but filters out high and low frequencies.
4) a notch (or band-stop) filter, which allows all frequencies through except frequencies within some band in the middle of the spectrum.
The frequency at which a filter starts reducing the response (or around which the pass or stop band is centered for band-pass and notch filters) is called the cutoff frequency.

We’re going to design some basic filters just to give you some practice filtering audio signals.

First, we need to create a signal we can filter. A good signal to use for this is random noise. This will also give us a chance to add a new function to our toolbox for generating noise, which can be coupled with sounds you’ve already generated to deepen your sonic palette - many sounds can be modelled as a combination of tonal components and noise (often filtered noise) components.

Design a function that generates white noise:

[output] = generateNoise(fs, duration)
fs: sampling rate
duration: length of noise signal in seconds

White noise can be generated by simply creating a stream of random numbers between -1.0 and 1.0 (although we know by now it’s best to avoid -1.0 and 1.0; if you want to include an amplitude argument in your generateNoise(), good on you!). A handy Matlab function for generating random numbers is the randn() function.

Plot the spectrum of your white noise signal using your getSpectrum() function from last lab. Try different lengths / durations. What do you see in the spectrum for your noise signal? Why does white noise make a good signal for testing filters? Answer in script comments.

Next look up the Matlab functions butter and filter. You’ll see that the outputs of one can be used as arguments for the other. These vectors (B and A by convention) are the coefficients of the filter transfer function numerator and denominator (if you want to learn more, here’s a good place to start, or take Music 320A/B and / or EE 264). Use these two functions to design and implement the following filters for the given signals:

  • White noise low-pass filtered with a cutoff frequency of 800 Hz.
  • White noise high-pass filtered with a cutoff frequency of 1500 Hz.
  • A short clip of a song of your choice band-pass filtered with parameters of your choice.
  • A short clip of a song of your choice notch filtered with parameters of your choice.

Matlab Deliverables:

Submit your generateNoise() function and test script code. Also submit spectrum plots and .wav files of the four filtered signals listed above. Remember audiowrite() can be used to generate .wav files from signal vectors.

Game Development: Biofeedback

For this lab you will develop a game that incorporates some element of biofeedback. The Neurosky Mindwave headsets are provided as a bio-sensor to use for this lab, but you are welcome to use other bio-sensor devices to provide the biofeedback for your game. The focus here is not to use biofeedback as a control element (like the controllers in the last lab), but rather for the game state to react to the player’s state in some compelling way that strengthens the gameplay feedback loop.

Getting Started with the Mindwave and Unity

The Neurosky Mindwave Mobile is a consumer-level EEG headset that you can use to access basic EEG data in Unity. It uses bluetooth, so make sure the computer your group uses has bluetooth capability. If not, email Mark and we can get you set up with a bluetooth dongle.

First, make sure your Mindwave device has a good battery - turn it on and make sure that a little blue light is flashing next to the power switch. If this isn’t the case, you need a new AAA battery - talk to Mark if you need one.

Next, go to this page to walk through setting up your Unity project to work with the Mindwave.

Here’s a quick rundown of what you’ll need to do to set up:

  • Download the developer tools
  • Import their Unity Package into your project
  • Make sure bluetooth on your computer is turned on
  • Launch the TGC Installer in the ThinkGear Connector folder in the developer tools
  • Launch ThinkGear Connector
  • In your unity project, drag the NeuroSkyTGCController onto the game hierarchy
  • Add the DisplayData.cs script as a component on your main camera
  • On the Display Data script component, change the size of the Signal Icons list to 5, and choose/drag any five 2D textures for each of the new fields there (there are included default textures you can use).
  • Hit play! The meditation and attention values should update on screen. To use these values to control the game, access the meditation1 and attention1 variables in the Display Data script.

Once you have the Mindwave (or another biofeedback sensor) generating data, create a Unity game that utilizes this data to affect the game state in some way. Remember that (especially with the Mindwaves) it will be nearly impossible for the player to actually control their current state in response to the game. Instead, you should design your game as a system that reacts to whatever state the player is in.

Try to incorporate elements you’ve learned from previous labs into your game, including alternate controllers / input, unique / original sounds, etc. to create a fun and innovative experience!

Also, now that we’ve learned about spectrums in Matlab, you can also leverage this knowledge in Unity. You can call GetSpectrumData() on any AudioSource object to get frequency / spectrum information from that source’s audio stream for the current game frame. Rather than return a vector of values, however, GetSpectrumData() populates an array that you’ll need to pre-allocate before calling, which has to have a length that is a power of 2 between 64 to 8192 inclusive. Syntax for allocating a float array of length 256 in C# looks like this:
float [] myArray = new float [256];
It’s best to allocate the array once - declare it with your script variables and allocate it in Awake() or Start(). You can then populate your array using GetSpectrumData() like this:
mySource.GetSpectrumData(myArray, 0, FFTWindow.Hanning);
0 is the channel - could also be 1 for stereo sources but 0 is safest. The third argument is the window applied to our time samples, which we haven’t talked about. You can use any of the other window types you like, but will probably get the best results with the Hanning or Blackman windows. We’ll go over GetSpectrumData() in class also in case anything’s unclear.

Game Design Deliverables:

Submit links to your pitch and playtest videos (details on the Lab Overview page if you’ve forgotten). You do not need to submit the actual Unity project.

Both individual Matlab and Group Game Development portions of Lab 4 are due Tuesday, May 9th by 6:00 PM. We will play your biofeedback games during class that evening.

Lecture

Fridays, 10:30 AM - 12:20 PM
CCRMA Classroom (Knoll 217)

Lab

Tuesdays, 6:00 - 7:50 PM
CCRMA Classroom (Knoll 217)

Office Hours

Monday 6-8 PM
Friday 1-2 PM
CCRMA Ballroom

Questions

Post on Piazza

Instructors

Poppy Crum
Instructor
poppy(at)stanford(dot)edu

Mark Hertensteiner
Teaching Assistant
hert(at)stanford(dot)edu