Aqua Phoenix
     >>  Lectures >>  Matlab 2  


2.2 Introduction - Practial Foundation - Vectors

For a practical example using vectors, we will now look at a one dimensional data structure, namely sound. A sound wave is merely a collection of values that, when graphed in the x/y plane, exhibits a large number of sine waves at different heights (amplitudes) and different periods.

To load a wave sound file, we issue the command: authoritah=wavread('authoritah.wav'); Analogous to function imread, wavread returns the content of the wave file in terms of numbers. In the previous command, this vector of numbers is assigned to variable authoritah.

We can play back the sound file by issuing the command: wavplay(authoritah) . Note that the file is played back too fast, i.e. at a higher frequency than what the file was recorded in. To change the playback frequency, we merely pass the correct frequency to the function: wavplay(authoritah, 8000) . The sound file is now played back at frequency 8000 Hz = 8 KHz.

Just as we have evaluated the size of the image, we can find out the size of this sound file: size(authoritah) . This sound file is a data structure of size 16565*1, which essentially means that it is a vector with 16565 elements.

Figure 2.20
Click image to enlarge, or click here to open
When plotting the wave form with plot(authoritah) , we can roughly make out words and syllables by the concentrated high amplitudes.

Figure 2.21
Click image to enlarge, or click here to open
While multiplication of an image with a scalar resulted in a change in brightness/contrast, multiplication of a wave form with a scalar results in a change of amplitude (volume). Since the data type of the wave form vector is double, we need not convert it:

authoritahAmp = authoritah*4; wavplay(authoritahAmp, 8000);

Figure 2.22
Click image to enlarge, or click here to open
We can extract portions of the wave form by indexing into the vector, e.g. the individual words:

wordYou=authoritah(1:2400); wordWill=authoritah(2000:4400); wordRespect=authoritah(4500:8000); wordMy=authoritah(8000:9000); wordAuthoritah=authoritah(9800: length(authoritah)); and then play and plot them, e.g.:

wavplay(wordAuthoritah, 8000); plot(wordAuthoritah);

Figure 2.23
Click image to enlarge, or click here to open
Once all of the words have been isolated from the sound file, they can be re-ordered to form a new, while not necessarily senseful sentence:

authoritah2=[wordMy; wordAuthoritah; wordWill; wordRespect; wordYou]; wavplay(authoritah2, 8000);

Figure 2.24
Click image to enlarge, or click here to open
Finally, we can generate primitive wave forms, e.g. a beep, by repeating a sine wave of some constant period:

beepsound1=sin([0:0.3:pi*2000]); wavplay(beepsound1, 8000);

Figure 2.25
Click image to enlarge, or click here to open