Sie sind auf Seite 1von 5

ECE 417NL

Experiment No. 4

AUDIO FILE CONVERSION


OBJECTIVES
1) To know how to convert audio signals from wav-, mp3- and midi-files to signals
in MATLAB.
2) To learn how to extract segments of a signal for analysis and processing.
3) To know how to convert MATLAB signals into wav-files.
INTRODUCTION
Signals stored in digital media are of various formats. For audio signals, the
common formats are wav, mp3 and midi. The signals that these types of files contain can
be imported into the MATLAB workspace with the use of appropriate conversion
functions.
Once converted, the signals become just sequences of numerical values (vectors)
at specified sampling rates. These signal vectors can be manipulated in order to prepare
certain portions of the converted signal for analysis and processing.
Create a new folder named 'Expt4' in your Z:\ drive. In this folder, store the mfile for the MATLAB code that you will implement in this experiment. The filename
should be 'group#_expt4.m'.
WAV-FILE TO MATLAB SIGNAL
Open the folder 'Instructor's Files' in your Z:\ drive. Double-click on
'whistle.wav' to play it on Windows Media Player. What sound is played? Copy the file
into your current directory in MATLAB.
Import the 'whistle' signal into MATLAB by writing the following on an m-file and
evaluating the line in the command window
[y,Fs,nBits] = wavread('whistle.wav');
What data do the variables y, Fs and nBits contain? What are the values of Fs and nBits
for the signal in 'whistle.wav'? (Note: Answers to questions must be written as comments
in the m-file.)
What function gives the number of samples of y as its output?

no_samples_y = function(y)
Create a time vector t and then plot the signal y versus time
t = [0:length(y)1]'/Fs;
figure, plot(t,y)
Explain why the last sample of y occurs at time t =

length(y) - 1
.
Fs

Label the x- and y-axes of your plot and give the plot a descriptive title.
Listen to the signal y using the soundsc function.
Using the 'Zoom' toolbar in the Figure window, zoom in on the signal to show only a few
cycles. Estimate the frequency of the 'whistle' signal by zooming in on a typical cycle of
the waveform. From the time for one cycle or period, obtain an estimate of the signal's
frequency. (Note: Remember this value of frequency. This will be compared later on
with frequency obtained from the signal's spectrum.)
Repeat the above procedure (except for the frequency estimation portion) for the file
'twinkle.wav' and the ringtone 'reklamo.wav'. In order not to overwrite on the data of
existing variables in the workspace, use another set of variables such as y1 and Fs1 for
the 'twinkle' signal, and so on.
SIGNAL EXTRACTION

Review the basic MATLAB operation of extracting a submatrix from a larger matrix by
creating the following vector
v = [3 2 5 7 4 6 8 2 1 0 4]

The value 7 is the fourth element of the vector (the index of 7 is 4) and it can be extracted
by
the_element_of_v_with_index_4 = v(4)

If it is desired to extract only the values 5 7 4 6 8 2, and name the extracted vector as
p
p = v(3:8)

Another way of determining the index range for p, besides counting, is done by plotting
the vector v against its index

figure, stem(v), grid

Locate the values 5 and 2 and look for their indices in the plot's horizontal axis.
A third way is done by
index_of_value_5 = find(v==5)
index_of_value_2 = find(v==2)
p1 = v(index_of_value_5:index_of_value_2)

Using the above discussion, do the following for the 'twinkle' signal: extract from the
original the portion that corresponds to the first two lines 'Twinkle twinkle little star, How
I wonder what you are'. To determine the index range for the segment of interest, plot
y1 against its index
figure, plot(y1)

Name the variable for the extracted signal as y1ext. Listen to the extracted signal to
verify that you have the correct segment. Plot y1ext against time t1ext in seconds.
For future use and analysis, save the data for the extracted signal in a mat-file
save twinkle_ext y1ext Fs1

Clear your workspace variables. Retrieve the saved signal by


load twinkle_ext

Use whos to check the variables that were loaded into the workspace. Verify that the
data was saved properly by plotting y1ext.
MATLAB SIGNAL TO WAV-FILE

A signal in MATLAB can be converted to a wav-file using the wavwrite


function. Type help wavwrite for the proper syntax.
To convert the extracted signal from 'twinkle' back into wav format
wavwrite(y1ext,Fs1,'twinkle_ext.wav')

Listen to the wav-file you created using Windows Media Player to verify the result.

MP3-FILE TO MATLAB SIGNAL

Listen to the 'garci.mp3' file in Windows Media Player. What type of signal do
you hear? (Disclaimer: This exercise does not promote any opinion on current political
issues. The purpose for using the above controversial file is to illustrate the relevance of
knowing how to use MATLAB for analysis of real-life signals.)
Mathworks, Inc., the maker of MATLAB, did not write a function for converting mp3files into a MATLAB signal. However, an MP3 Toolbox is freely downloadable on the
Internet. This is a good illustration of the power of MATLAB's extendibility feature. A
toolbox is just a collection of functions; if there are no MATLAB functions available for
your field of interest, you can make your own toolbox too!
Check the toolbox folder of MATLAB to verify that the MP3 Toolbox is installed.
Also, verify through the Set Path browser that the toolbox is in MATLABs search path.
Copy the garci.mp3 file into your current directory. Import the 'garci' signal into
MATLAB by
[yg,Fsg,nBitsg] = mp3read('garci.mp3');

The execution of the above line may take some time. Why? Look at the value of Fsg
and the number of samples of yg. How long is the signal duration?
Use the soundsc function to listen to the signal imported in MATLAB.
Plot the signal values against time. Why does the plot display two colors? Use the whos
command to check the size of yg. Why are there two columns?
Extract the first column and call this signal as yg1. Plot yg1 against its index. Extract
the portion of the signal where 'Hello Garci' is uttered and save the extracted signal in a
mat-file.
MIDI-FILE TO MATLAB SIGNAL

Listen to the midi-file 'sea.mid' on Windows Media Player. Describe the signal
that you hear. How do you think is such a signal produced?
As with the MP3 Toolbox, a MIDI Toolbox is freely downloadable. The latter is the
output of an excellent research in music analysis in a university in Finland. To learn
more, you can read the toolbox manual located in the folder 'Instructor's Files'.
Import 'sea.mid' into MATLAB by writing the following lines in your m-file and
evaluating on the command window

nmat = readmidi('sea.mid');
w = nmat2snd(nmat);
soundsc(w,8192)

Does the signal that MATLAB plays sound exactly the same as that played by Windows
Media Player? Why is this so?
IDEAS FOR FURTHER EXPLORATION

The course ECE 417NL covers only signals defined over a single independent
variable. Signals defined over two independent variables also abound however. An
example is the signal that we call an 'image', such as photographs or files from a digital
camera. The common digital formats are jpeg, gif and tiff. These types of files can also
be converted into matrices in MATLAB. An exploration along this line leads into the
vast field of image processing.
EXERCISES

1) What is the sampling rate for the 'reklamo' signal? Listen to the signal using a
sampling rate of only one-half the value of the original rate. Describe the result.
2) Convert the 'garci' signal in MATLAB into a wav-file. Compare the file size of
the output wav-file to the original mp3-file.
3) Another way of importing the 'whistle' signal into the MATLAB workspace is
done by double-clicking on the 'whistle.wav' filename in the Current Directory
browser. This opens an Import Wizard window. Right-click on the variable
names on this window if you want to rename them. These variables will be
created in the workspace when you click on the Finish button.
4) Describe the independent variables involved in a video signal.

/iarabuya
072105

Das könnte Ihnen auch gefallen