Вы находитесь на странице: 1из 8

Sounds in Matlab Practical Comment all of the programs you write and execute and briefly explain how

each program works. Save all these programs so that they may be submitted as a portfolio for your DSP assignment. 1. Using Matlab type in the following and using your headphones play the sounds As an example, the MATLAB code to create a sine wave of amplitude A = 1, at audio frequency of 466.16 Hz (corresponds to A# in the Equal Tempered Chromatic Scale) would be: >> v = sin(2*pi*466.16*[0:0.00125:1.0]); The vector, v, now contains samples of the sine wave, starting at t=0 s, to t=1.0 s, in samples spaced 0.00125 s apart (the sampling interval Ts). This corresponds to a sampling frequency of 8 KHz, which is standard for voice grade audio channel. Now, you can either plot this sine wave; or you can hear it!!! To plot, simply do >> plot(v); To hear v, you need to convert the data contained in v to some standard audio format that can be played using a Sound Card and Speakers on your PC. One such standard audio format is called the "wav" format. Matlab provides a function called wavwrite to convert a vector into wav format and save it on disk. Do >> help wavwrite for more details. Now, to create a wav file, do >> wavwrite(v, 'asharp.wav'); (you can give any file name between the ' ' s) Now, you can "play" this .wav file called asharp.wav using any multimedia player. For example, you could use the Sound Recorder program. Start this by pointing to Start - Programs Accessories - Multimedia - Sound Recorder. Open the wav file using the File Menu and press Play. If you have installed a sound card and a pair of speakers on your PC, you should be able to hear a short note.

You can create synthetic music using MATLAB by creating and playing a sequence of short bursts of sinusoids. Use the following table for a reference of the notes Note C Frequency(Hz) 262 D 294 E 330 F 349 G 392 A 440 B 494 C 523

The following MATLAB code generates and plays the scale from middle C. 1

fs=8000; %sets the sampling frequency t=0:1/fs:0.25; %length of each note tspace=0.05; %length of pause between notes fr=2^(1/12); %the frequency ratio between neighbouring keys A4=440;%set this as a reference key to determine other keys B4=A4*fr^2; C5=A4*fr^3; C4=A4*fr^(-9); D4=A4*fr^(-7); E4=A4*fr^(-5); F4=A4*fr^(-4); G4=A4*fr^(-2); xspace=zeros(size(tspace)); %set pause x=[cos(C4*2*pi*t), xspace,cos(D4*2*pi*t),... xspace,cos(E4*2*pi*t),xspace,cos(F4*2*pi*t),xspace,cos(G4*2*pi* t),... xspace,cos(A4*2*pi*t),xspace,cos(B4*2*pi*t),xspace,cos(C5*2*pi* t)]; soundsc(x,fs);

Now the scale can be generated, the notes can be puts together to make music!!! Let's look at the following piece of music: AA EE DD C#C# EE DD (repeat first two lines once) F# F# BB C# C# EE AA B B (repeat once)

The American Standard Pitch for each of this notes is: A: 440.00 Hz A# 466.16 B: 493.88 Hz C 523.25 C#: 554.37 Hz D: 587.33 Hz D# 662.25 E: 659.26 Hz F 698.46 F#: 739.99 Hz G 783.99 G# 830.61

Assuming each note lasts for 0.5 seconds, the MATLAB m-file for creating a wav file for this piece of music would be: clear; a=sin(2*pi*440*(0:0.000125:0.5)); b=sin(2*pi*493.88*(0:0.000125:0.5)); cs=sin(2*pi*554.37*(0:0.000125:0.5)); d=sin(2*pi*587.33*(0:0.000125:0.5)); e=sin(2*pi*659.26*(0:0.000125:0.5)); fs=sin(2*pi*739.99*(0:0.000125:0.5)); line1=[a,a,e,e,fs,fs,e,e,]; line2=[d,d,cs,cs,b,b,a,a,]; line3=[e,e,d,d,cs,cs,b,b]; song=[line1,line2,line3,line3,line1,line2]; wavwrite(song,'song.wav'); HINT: Before you code the entire song, just code the first line, create a wav file, play it and make sure everything works.

2. Read the following and experiment with the commands: In MATLAB, the colon operator, :, produces a list. For example, >> -5:5 ans = -5 -4 -3 -2 -1 0 1 2 3 4 5 The default increment is 1. The increment can be changed, however, as in the example below. >> -5:2.5:-5 ans = -5.0000 -2.5000 0 2.5000 5.0000 The colon operator is often used to create row or column matrices. In the example below, the colon operator is used to create a row vector, x. This vector is then transposed into a column matrix using the ' notation.

>> x = [0:.1:1]; >> y = sin(x); >> chart = [x y] chart = 0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000 0 0.0998 0.1987 0.2955 0.3894 0.4794 0.5646 0.6442 0.7174 0.7833 0.8425

Colon notation can also be used to declare matrices, as in >> a = [0:3; 4:2:10; 7:10] a = 0 4 7 Subscripting: Often all of the data stored in a Matlab variable is not needed to perform some task such as scaling or plotting. In order to select the appropriate data from a Matlab variable, subscripts which follow the variable name must be used. The subscripts are enclosed in parenthesis ( ) and the ranges of row and column data must be separated by a comma ( , ). The row and column data ranges are denoted in three ways: 1) an integer, which tells which row or column is needed 2) two integers separated by a colon ( : ), which give the first and last rows or columns to take data from 3) a lone colon ( : ), which denotes all of the rows or columns Examples of subscripting (for these examples let A by a 10 x 10 matrix of data) A(2,5) = the data point located at the intersection of row 2 and column 5. A(1:6, 4) = a 6 x 1 vector of the data in rows 1 to 6 of column 4 A(2, 3:8) = a 1 x 6 vector of the data in columns 3 to 8 of row 2 A(1:5, 4:10) = a 5 x 7 sub-matrix of A with the data from rows 1 to 5 contained in columns 4 to 10 A(: , 3) = a 10 x 1 vector of all the data in column 3 A(7 , :) = a 1 x 10 vector of all the data in row 7 4 1 6 8 2 8 9 3 10 10

3. Read the following descriptions of various File Formats Digital Audio Files An audio file has two main parts: a header and the audio data. The header is used to store information about the file, including the resolution, sampling rate and type of compression. Often a wrapper is used to add features, such as license management information or streaming capability, to a digital audio file.

The format of a digital audio file refers to the type of audio data within the file. The file type refers to the structure of the data within the file. It is common for the same format to be used by more than one file type. For example, the PCM format is found in both WAV and AIFF files. Table 1 - Common Digital Audio Formats Type Extensions AIFF (Mac) .aif, .aiff AU (Sun/Next) .au CD audio (CDDA) N/A MP3 .mp3 Windows Media Audio .wma QuickTime .qt Codec *PCM *u-law PCM MPEG Audio Layer-III Proprietary (Microsoft) Proprietary (Apple Computer) Proprietary (Real Networks) *PCM

RealAudio .ra, ram WAV .wav * Can be used with other codecs.

WAV WAV is the default format for digital audio on Windows PCs. WAV files are usually coded in PCM format, which means they are uncompressed and take up a lot of space. WAV files can also be coded in other formats, including MP3. AIFF and AU AIFF is the default audio format for the Macintosh, and AU is the default format for SUN systems. Both of these formats are supported on most other platforms and by most audio applications. Each of these formats can be compressed, but compression sometimes creates compatibility problems with other platforms.

Streaming Audio Streaming audio avoids many of the problems of large audio files. Instead of having to wait for the entire file to download, you can listen to the sound as the data arrives at your computer. Streaming audio players store several seconds worth of data in a buffer before beginning playback. The buffer absorbs the bursts of data as they are delivered by the Internet and releases it at a constant rate for smooth playback. Many digital audio formats can be streamed by wrapping them in a streaming format, such as Microsofts ASF (Active Streaming Format), which can be used to stream MS Audio, MP3 and other formats. Table 2 - Streaming Audio Systems Type Primary Format Windows Media Audio / Windows Media Active Streaming Format Technologies (ASF) Icecast (open source) MP3 QuickTime QuickTime RealSystem RealAudio SHOUTcast MP3

Developer Microsoft The Icecast Team Apple Computer RealNetworks Nullsoft

Standard Formats Standard formats make it easier for software developers and equipment manufacturers to produce products that are less costly and more compatible with each other. The compatibility provided by standard formats helps assure consumers that their music and equipment wont become obsolete. Cassette tapes, compact discs and PCM are examples of standard audio formats that benefit both consumers and manufacturers. PCM PCM (Pulse Code Modulation) is a common method of storing and transmitting uncompressed digital audio. Since it is a generic format, it can be read by most audio applicationssimilar to the way a plain text file can be read by any word-processing program. PCM is used by Audio CDs and digital audio tapes (DATs). PCM is also a very common format for AIFF and WAV files.

PCM is a straight representation of the binary digits (1s and 0s) of sample values. When PCM audio is transmitted, each 1 is represented by a positive voltage pulse and each 0 is represented by the absence of a pulse. Figure 2 shows how binary data is converted to a PCM signal. 4. Enter and run the following code snippets 6

To load an audio (PCM) file, Matlab provides the function wavread(). e.g. road = wavread(road.wav);

If the sampling frequency at which the sound was recorded is required, which is normally the case, as reproduction of the original will be impossible without it, then type: [road,fs] = wavread(road.wav); Where road is the variable to store the sound signal and fs is the variable which stores the sampling frequency (in this case it is the same as that on a music CD fs = 44,100 samples/second). The array road now contains the stereo sound data and fs is the sampling frequency. Remember to give the complete location of the wav file if it is not in your current directory. For e.g., if the wav file is located in my documents folder, you write [x,fs] = wavread(C:\My Documents\myfile.wav); Also, note that the address of the file has to be within apostrophes. To play a wave file at the sampling frequency wavplay(road, fs); To see the size of road type: size(road); The left and right channel signals are the two columns of the road array are: left = road(:,1); right = road(:,2); Next, plot the data using the following Matlab commands: time = (1/44100)*length(left); t=linspace(0,time,length(left)); plot(t,left); xlabel(time(sec)); ylabel(relative signal strength); n = length(x) returns the size of the longest dimension of x. If x is a vector, this is the same as its length. time = (1/44100)*2000; t=linspace(0,time,2000); plot(t,left(1:2000)); xlabel(time(sec)); ylabel(relative signal strength); Another audio format is the .au file format used by SUN. These files are read in using: 7

[lunch,fs2] = auread(lunch.au); soundsc(lunch,fs2); To save an array as a .wav file, use wavwrite(). Use auwrite() for .au format output. Reverse Playing To play the sound backwards, simply reverse the order of the numbers in the arrays. Type in the following commands: y=[1:2:3:4:5] y2=flipud(y) Note that flipud stands for flip upside down which flips your array y and stores the inverted array in a new array called y2. Trying it on one of the sound arrays we have: left2=flipud(left); soundsc(left2,fs) Changing the speed The sampling frequency fs is the time which elapses between each sample (T=1/fs). If the song is played with more or less time between each sample than was originally there when recorded, there will be some interesting results. soundsc(hootie,fs) soundsc(hootie,fs/1.5) soundsc(hootie,fs*1.5)

Вам также может понравиться