cmps1371 introduction to computing for engineers processing sounds

32
CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Upload: carlie-watchorn

Post on 16-Dec-2015

224 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

CMPS1371Introduction to

Computing for Engineers

PROCESSING SOUNDS

Page 2: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

The Physics Of Sound

Why do we hear what we hear?

Sound is made when something vibrates.

The vibration disturbs the air around it. This makes changes in air pressure. These changes in air pressure move through

the air as sound waves.

Page 3: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Sound Volume

The louder a sound, the more energy it has. This means loud sounds have a large amplitude.

Think about what an amplifier does: it makes sounds louder. It is the amplitude that relates to how loud sound is.

Page 4: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Sound Pitch

All sound is made by things vibrating. The faster things vibrate, the higher the pitch of the sound produced.

The vibrations being more frequent mean the frequency of the wave increases.

Page 5: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Intensity Levels

Source Decibels Description

0 Threshold of H earingN orm al B reathing 10 B arely A udibleR us tling Leaves 20S oft W hisper 30M osquito 40Quiet O ff ice 50 QuietN orm al C onversation 60B usy S treet Traff ic 70Fac tory 80V acuum C leaner 90 C ons tant E xposureTrain 100 E ndangers H earingW alkm an at M ax im um Level 110R ock C oncert 120 Threshold of P ainM achine Gun 130M ilitary Jet Takeoff 150R ocket E ngine 180 P erforated E ardrum

Page 6: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Sound Recording and Playback

Methods to store and reproduce sound is a continual process for high quality Phonograph Magnetic tape Digital recording

Page 7: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Record

A sound will be collected as a vector

The vector will provide signals over time to represent the frequency (pitch) and amplitude (intensity)

Page 8: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Sound Function

SOUND function will play the vector as sound. sound(y,Fs) sends the signal in vector Y (with sample

frequency FS) out to the speaker on platforms that support sound.

sound(y) plays the sound at the default sample rate of 8192 Hz.

sound(y,Fs,bits) plays the sound using BITS

bits/sample if possible. Most platforms support BITS=8 or 16.

Example: load laughter

sound(y,Fs)plot(y)

Page 9: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Read and Write Sound Files

y = wavread(FILE) reads a wave file specified by the string FILE,

returning the sampled data in y

wavwrite(y,Fs,NBITS,WAVEFILE) writes data Y to a Windows WAVE file

specified by the file name WAVEFILE, with a sample rate of FS Hz and with NBITS number of bits (default Fs = 8000 hz, NBITS = 16 bits)

For audio files use: auread auwrite

Page 10: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Making Music with MATLAB

Before we actually start making music, let's revise a few AC waveform basics. Consider the sine wave shown in the figure below:

The sine wave shown here can be described mathematically as:

v = A sin(2π f t)

where A is the Amplitude (varying units), f is the frequency (Hertz) and t is the time (seconds).

T is known as the time period (seconds) and T=1/f

Page 11: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Music

Sound waves are created when a waveform is used to vibrate molecules in a material medium at audio frequencies (300 Hz <= f <= 3 kHz).

Example: the MATLAB code to create a sine wave of

amplitude A = 1, at audio frequency of 466.16 Hz (corresponds to A#) would be:

>> v = sin(2*pi*466.16*[0:0.00125:1.0]);

Page 12: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Now, we can either plot this sine wave; or we can hear it!!!

To plot, simply type:

>> plot(v);

Music

Page 13: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Music

To hear v, we need to convert the data to some standard audio format

Matlab provides a function called wavwrite to convert a vector into wav format and save it on disk.

>> wavwrite(v, 'asharp.wav'); you can give any file name

Page 14: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Music

Now, we can "play" this wav file called asharp.wav using any multimedia player. wavfunction returns 3 variables:

Vector signal Sampling frequency Number of bits

>> [y, Fs, bits] = wavread('asharp.wav');>> sound(y, Fs)

Page 15: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Music

Now that we can make a single note, we can put notes together and make music!!!

Let's look at the following piece of music:

A A E E F# F# E E D D C#C# B B A A E E D D C# C# B B (repeat once) (repeat first two lines once)

Page 16: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Music

The American Standard Pitch for each of these notes is:

A: 440.00 Hz B: 493.88 Hz C#: 554.37 Hz D: 587.33 Hz E: 659.26 Hz F#: 739.99 Hz

Page 17: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Music

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');

Page 18: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Sound

SOUND: One dimensional function of changing air-

pressure in timeP

ress

ure

Time t

Pre

ssur

e

Time t

Page 19: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Sound

If the function is periodic, we perceive it as sound with a certain frequency (else it’s noise).

The frequency defines the pitch.

Time t Time t

Pre

ssur

eP

ress

ure

Page 20: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Sound

The SHAPE of the curve defines the sound character

Flute String

Brass

FluteFlute StringFlute

Brass

Page 21: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Sound

Listening to an orchestra, you can distinguish between different instruments, although the sound is a

SINGLE FUNCTION !Flute

String

Brass

Page 22: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Sound

If the sound produced by an orchestra is the sum of different instruments, could it be possible that there are BASIC SOUNDS, that can be combined to produce every single sound ?

Page 23: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Sound

The answer (Charles Fourier, 1822):

Any function that periodically repeats itself can be expressed as the sum of sines/cosines of different frequencies, each multiplied by a different coefficient

Page 24: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Fourier

…A function…can be expressed as the sum of sines/cosines…

What happens if we add sine and cosine ?

a * sin(ωt) + b * cos(ωt)

= A * sin(ωt + φ)

Adding sine and cosine of the same frequency yields just another sine function with different phase and amplitude, but same frequency.

Page 25: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Any function that periodically repeats itself…

To change the shape of the function, we must add sine-like functions with different frequencies.

As a formula:

f(x)= a0/2 + Σ

k=1..n a

kcos(kx) + b

ksin(kx)

Fourier

Fourier Coefficients

Page 26: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Fourier

The set of ak, b

k TOTALLY defines the

CURVE synthesized !

We can therefore describe the SHAPE of the curve or the CHARACTER of the sound by the (finite ?) set of FOURIER COEFFICIENTS !

Page 27: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

SAWTOOTH Function

f(x) = ½ - 1/π * Σn 1/n *sin (n*π*x)

01/44

01/33

01/22

011

cossinFreq

Page 28: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

The Problem

Given an arbitrary but periodically one dimensional function (e.g. a sound), can you tell the FOURIER COEFFICIENTS

to construct it ?

The answer (Charles Fourier):Yes

Page 29: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Fast Fourier Transform

MATLAB - function fft:

Input: A vector, representing the discrete function

Output: The Fourier Coefficients as vector of scaled imaginary numbers

We can analyze the frequency content of sound using the Fast Fourier Transform (fft)

Page 30: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Fast Fourier Transform

"Fourier transform" goes from time domain to the frequency domain

Decompose a signal into it's sinusoids

Page 31: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Functionality of the fft

Page 32: CMPS1371 Introduction to Computing for Engineers PROCESSING SOUNDS

Examples