Lecture 10

Chapter 13 - Sights and Sounds

Synopsis
We have now seen all of the primary data types, control structures, and programming paradigms within MATLAB. From this point on, we will be looking into more nuanced features that will make your data analysis jobs easier. In this lecture, we will cover the first section of Chapter 13 - Sights and Sounds. Topics will include audio file handling, image file handling, and the digitization of continuous signals.


Daily Quiz

Quiz 17


Audio Files

In the physical world, sound propagates through pressure waves. The frequency with which these pressure waves hit our eardrum determines the pitch of the sound we hear. We can digitize these sounds by measuring, or sampling, the pressure waves at a given frequency over time. What results is a vector of sampled values and an associated frequency. These two components make up the bulk of any audio file. MATLAB provides a convenient method for reading audio files with the audioread function.

1
[y, Fs] = audioread(filename)

The audioread function returns the sampled data y and the sampling frequency Fs. As mentioned above, y is a vector of values that represents a particular series of pressure waves. Fs is the rate at which the values of y were sampled. The dimensions of y are mxn, where m is equal to the duration of the recorded sound multiplied by the sampling frequency (Fs) and n is equal to the number of audio channels (typically one for mono and two for stereo).

If you would like MATLAB to play the audio data via your device's audio output, you can use the sound function. This can be a convenient way to play a notification sound (ex. chim or beep) when your program requires user attention.

1
sound(y, Fs)

Image Files

Similar to audio files, images are digital representations of a continuous visual signal. Images are typically stored in one of three forms--grayscale, RGB or indexed. Non-color, or grayscale, images are represented as 2D matrices where each element of the matrix corresponds to a picture element, or pixel. The value for each pixel indicates the intensity, or brightness, with lower values being darker.

A color image can be stored in RGB or indexed form. RGB is similar to grayscale; however, different 2D matrices are used to represent each color channel--red, green, and blue. The color channels are stored as one 3D matrix with the dimensions mxnx3, where m is the height of the image in pixels and n is the width of the image in pixels.

Alternatively, color images can be stored in indexed form where two different 2D matrices are used to represent the color of each pixel. The first matrix is a px3 colormap representing all of the colors possible for that image (there are p possible colors). The second matrix is an mxn index matrix that indicates the corresponding colormap row for each pixel.

The values within the RGB matrix and colormap matrix can be one of the following data types: uint8, uint16 or double. Most often, images are recorded as 8-bit and use the uint8 scale of values from 0 to 255. To represent the spectrum of colors, we use the values 0-255 in different combinations of three for red, green, and blue. In this way, we get the following color encodings:

Red         - (255,   0,   0)
Green       - (  0, 255,   0)
Blue        - (  0,   0, 255)
Yellow      - (255, 255,   0)
Magenta     - (255,   0, 255)
Cyan        - (  0, 255, 255)
Dark Yellow - (128, 128,   0)

As you can see, many different colors can be produced by simply varying the value for red, green, and blue. In fact, there are exactly 256*256*256 = 16777216 possible colors for an 8-bit image.

MATLAB provides several key functions for working with images. Here are a few that you will find most useful:

1
2
3
4
5
6
7
8
9
% reading an image
img = imread(filename)

% writing an image
imwrite(img, filename)

% displaying an image
imshow(img)
image(img)

Final Words

Today we learned how to store and access digitized versions of continuous audio and visual signals in MATLAB. These functions should give you a taste of what is possible with MATLAB. but only scratch the surface of the libraries available to process such data. You can find a wealth of additional information on MATLAB's documentation site, and on the MATLAB forum.