Lab 3

Matlab: Spectrum Analysis

In the Matlab portion of this lab, we’re going to perform some basic spectral analysis using the Discrete Fourier Transform. You may recall from the last lab that we said we could create any sound by adding together sinusoids, and sinusoids are the most basic building blocks of sound. Here we are going to create a function that takes any audio signal and tells us the sinusoids that create that sound.

Create the following function:
[Y, F] = getSpectrum(input, fs)
Input variables:
input = any input signal
fs = sampling rate
Outputs:
Y = amplitudes of sinusoidal components
F = corresponding frequencies for amplitudes in Y

Notice this function has two outputs: Y and F. Matlab functions can output an array of variables, which is why the return values must be listed in square brackets. Y is a list of amplitudes of sinusoids spaced evenly from 0 Hz to fs/2 Hz non-inclusive, i.e. [0:fs/2). F contains the frequencies that correspond to each amplitude in Y.

Take a look at the documentation for the Matlab function fft(y,N). This is the fast Fourier transform, which takes a signal from the time domain into the frequency domain, outputting complex values for individual frequency components (sinusoids) of a given input signal. We are interested in the magnitude of these complex numbers, corresponding to the amplitudes of the sinusoids, which you can get with abs(). N is the number of sinusoids that fft() will return, spaced from (0:fs] - this corresponds to the size of the FFT window. The higher the value of N, the higher the frequency resolution of the spectrum. If N is larger than the number of samples in the input signal, the signal will be zero-padded, meaning zeros will be added to the end of the signal. A good number to use for N is the next power of two larger than the length of the input signal (check out nextpow2()).

For real signals (like audio signals), the magnitude of the first and second halves of the values returned by the FFT will be symmetrical, or even - you can think of the second half corresponding to negative frequencies. In other words, the absolute value of the output of fft(y,N) is a list of the amplitudes of sinusoidal components evenly spaced from 0 Hz to fs/2 Hz, and then from -fs/2 to 0 Hz, respectively. The negative frequency component amplitudes are always going to be the same as the positive frequency components, as long as we are not feeding complex numbers into the fft() function. So we really only care about the first half of the values returned by fft(), since the second half is redundant for real signals.

So we know what F should be - N/2 values evenly spaced from (0:fs/2-fs/N) - and we know that Y will be the first half of the output from abs(fft(y,N)). F and Y can be conveniently used to plot the results of your getSpectrum() funtion - F as the x axis and Y as the y axis (this will look like plot(F,Y)).

Matlab Deliverables:

Submit your getSpectrum() function along with a short (3-5 seconds) .wav sample of your choice (don’t use anything you’ve generated, it should be a music or audio sample), and a plot of the output of your getSpectrum() function with the .wav sample as the input signal. Make sure to label your plot! Here’s an example of how to do this:

plot(F,Y);
title(‘Spectrum of example.wav’);
xlabel(‘Frequency (Hz)’);
ylabel(‘Amplitude’);

Game Development: Modified / Augmented Breakout

The game development portion of this lab consists of two parts. The first part you will complete individually, while the second part will be completed in groups.

Solo: Getting Started with Unity

Unity is a game creation engine: a giant collection of tools that handles graphics, audio, controller input, and routines for various scripts and such. Think of it as GameSalad on steroids. If you haven’t done so already, you can find the link to install Unity on the course Resources page. You don’t need to select additional target platforms when you install unless you know you want to develop for them (you can always add them later also). You will have to create an account with Unity before you can start working with it. Select the Personal and Educational options when presented with them.

After you download and install Unity, go through this Breakout tutorial independently. Breakout is a classic game (the original was played on the Atari 2600 with a paddle controller) that you may know by the name of Brickbreaker. It’s kind of like Pong, except instead of playing against someone else, you are playing against an endless army of evil bricks.

After you finish the tutorial, change one small thing about the game. It could be the color scheme, or the amount of bricks, or the scoring mechanism, or add your own Matlab-generated sounds! In your submission for the assignment, include a short document that describes what you did to personalize your Breakout along with a few screenshots or link to a short video that shows it off.

Group Breakout, Augmented

Now that you have a basic understanding of the fundamentals of using Unity, build a basic game that uses a specific controller. This game could build off of one of your Breakout demos. Think about what your controller offers you in terms of input - what are the different ways a player can interface with the controller? Are there any real-world gestures that the controller accepts as input that we can translate to an in-game mechanism, giving the player a greater sense of physical presence in the game?

We have Leap motion sensors and a couple Kinects you can use for controllers, or if you have something else in mind feel free to use it! I also have a Tobii eye tracker that only works with Windows machines if someone wants to use it (I can help you set this up). Standard game controllers (like an XBox controller) don’t count as ‘controllers’ for this assignment.

Here are a some links for the Leap Motion (How To, Cool Examples) and Kinect (How To and Another One, Cool Examples).

Game Design Deliverables:

Once again, submit links to your (hopefully standard by now) pitch and playtest videos (details on the Lab Overview page if you’ve forgotten).

We’ve extended the due dates for this lab due to our schedule, so the solo Breakout Demos will be due Tuesday April 24th by class time and the group Augmented Games will be due Friday April 27th by Midnight.

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