SUNY Geneseo Department of Computer Science


Lesson 3—Vectors

CSci 120, Spring 2014

Prof. Doug Baldwin

Complete by Monday, February 10
Or Wednesday, February 12

Purpose

This lesson develops your ability to work with vectors in Matlab.

Background

Vectors in Matlab are essentially lists of data. This makes vectors one of the most powerful and frequently used features of Matlab. This lesson introduces you to vectors by using them to build tables of the values of functions. The lesson ends by using these tables to introduce you to something called Nyquist’s theorem, which is a surprising result in its own right and a foundation of digital media and signal processing (i.e., Nyquist’s theorem plays an important role in your iPod, streaming video, etc.) If you like interesting surprises, do not look up Nyquist’s theorem until you get to the end of the exercise!

The basic Matlab concepts you will need are covered in the following places. Read or watch each of these before your recitation on Friday (February 7).

You will probably also find it useful to know that Matlab’s comparison operators (i.e., <, <=, >, >=, ==, ~=) can be applied to two vectors of the same size, much like addition and subtraction can. Used this way, these operators produce a result vector containing 1 wherever the comparison holds between corresponding elements of the two operands, and 0 wherever the comparison does not hold. For example,

[1 3 4] < [4 3 1]

produces the vector

[1 0 0]

Similarly, the non-short-circuit logical operators & and | can also be applied to vectors (these are variations on the && and || operators you learned about in Lesson 2; unfortunately those operators cannot be applied to vectors).

Exercise

Use Matlab expressions and variables to solve each of the following sets of problems.

If you have heard of “loops” in programming, note that you do not need them for this exercise, and in fact may not use them. One of the distinctive things about Matlab is that many calculations that would require loops in other languages can be carried out as single expressions involving vectors in Matlab.

Problem 1: Warm-Up

Create a vector that serves as a “square root table” for the numbers 1 through 10. In other words, the vector should contain the square roots of the integers from 1 through 10, with element i of the vector holding √i.

Create a vector that serves as a square root table for 19 equally-spaced numbers between 1 and 10 (not all of these numbers will be integers, of course). You can think of this table as a a series of “samples” of the square root function over the interval 1 to 10.

Problem 2: Shifting Vectors

Write and test Matlab expressions that shift vectors one place up or down. Shifting an n-element vector v up means creating a new n-element vector whose first element is 0 and whose remaining elements are the first n - 1 elements of v. For example, shifting up

2 4 6 8 10

produces the new vector

0 2 4 6 8

I call this shifting up because the effect is to place (most of) the elements of v one position higher in the new vector than they are in v. Similarly, shifting down places the last n - 1 elements of v one position lower in the new vector than they were in v, and fills the vacated position with 0. For example, shifting down

2 4 6 8 10

produces

4 6 8 10 0

Problem 3: Sampling a Periodic Function

In this exercise, you will use some of the ideas developed in the first two problems to sample and analyze a periodic function.

You can think of this problem as developing an “algorithm” for sampling a periodic function and computing the function’s frequency from the samples. An algorithm is basically a process for solving some problem by carrying out a set of steps (you’ll see a more precise, formal, definition later in this course), so here are the steps you need:

Step 1

Create a vector holding 60 equally spaced values between 0 and 2π (inclusive). It will be extremely useful later if you define a variable holding the upper bound for this interval (i.e., 2π for now) and write the expression to generate the 60 equally spaced values in terms of this variable.

Step 2

Create a second vector that holds the values of the function sin(6x), where the x values come from the vector created in step 1. Note that another way of thinking about this vector is that it contains 60 samples of sin(6x), where the samples are taken at the values in the vector from step 1.

Step 3

Count the number of cycles in the table from step 2. You can do this by counting the number of “peaks” in the table, where a peak is defined to be a value that is greater than the value immediately before it, and greater than or equal to the value immediately after it (this asymmetry between “greater” on one side and “greater than or equal” on the other handles the remote but not impossible chance that the sampling hits two x values equally spaced on either side of the actual maximum in the sine function; the definition ensures that such pairs count as exactly one peak; also note that later in this course we will look at more general and exact ways to find maxima of functions).

Be sure to write a Matlab expression (or expressions) that count cycles, don’t do it by hand by reading through the table.

Step 4

The frequency of the function is simply the number of cycles divided by the length of the interval you sampled over. Since the interval is implicitly measured in radians (because that’s the unit Matlab’s sin function works in), your frequency will be in units of cycles per radian. (As a test of your work, the function sin(6x) goes through 6 complete cycles between 0 and 2π, so you should have 6 cycles, and a frequency of slightly under 1 cycle per radian.)

Once you have completed steps 1 through 4, repeat them for x values ranging over intervals of 0 to 4π, 0 to 8π, and 0 to 16π. In all cases, take 60 samples over each interval. Note that if you defined the expression for the 60 equally spaced values in terms of a variable that holds the upper end of the interval, you can just change the value of this variable and then recall and re-execute your expressions for steps 1, 2, 3, and 4. Because the function you are sampling is always sin(6x), you should expect to count more peaks as the intervals get longer, but always end up with the same frequency. Is this what actually happens?

Now go look up Nyquist’s theorem and a signal-processing phenomenon called “aliasing.” In your own words, explain what Nyquist’s theorem says and how it explains what you saw in this exercise. Can you explain informally why Nyquist’s theorem is true, based on your experience sampling the sin(6x) function and seeing it alias?

Follow-Up

In your study group’s first face-to-face meeting with me following your “Complete By” date above, I will look over your solutions to the problems, ask you any further questions I have, and answer any questions from you. I may also ask you to demonstrate some of your answers in Matlab. Please bring short written solutions to each problem to the meeting (diary output from Matlab showing you solving the problems would be one way to do this). This will speed the meeting along, but because we will also be talking face-to-face, the written answers don’t have to be long or elaborate.

If you aren’t already signed up for a study group meeting during the week following this recitation, please sign up. Make the meeting 15 minutes long, and try to make it at a time all members of your study group can attend. If no such time exists, make it at a time the greatest number of members can attend.