CS1371 Introduction to Computing for Engineers

advertisement

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.

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.

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.

Intensity Levels

Source

Normal Breathing

Rustling Leaves

Soft Whisper

Mosquito

Quiet Office

Normal Conversation

Busy Street Traffic

Factory

Vacuum Cleaner

Train

Walkman at Maximum Level

Rock Concert

Machine Gun

Military Jet Takeoff

Rocket Engine

Decibels

90

100

110

120

130

50

60

70

80

150

180

0

10

20

30

40

Description

Threshold of Hearing

Barely Audible

Quiet

Constant Exposure

Endangers Hearing

Threshold of Pain

Perforated Eardrum

Sound Recording and Playback

 Methods to store and reproduce sound is a continual process for high quality

 Phonograph

 Magnetic tape

 Digital recording

Record

 A sound will be collected as a vector

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

(intensity)

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)

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

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

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

Music

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

 To plot, simply type:

>> plot(v);

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

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)

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)

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

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

Sound

 SOUND:

 One dimensional function of changing airpressure in time

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

Sound

 The SHAPE of the curve defines the sound character

Sound

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

Flute SINGLE FUNCTION !

Brass

String

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 ?

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

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.

Fourier

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)= a

0

/2 + Σ k=1..n

a k cos(kx) + b k sin(kx)

Fourier Coefficients

Fourier

 The set of a k

, b TOTALLY defines the k

CURVE synthesized !

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

COEFFICIENTS !

SAWTOOTH Function

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

1/n *sin (n* π*x)

3

4

Freq sin cos

1 1 0

2 1/2 0

1/3 0

1/4 0

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

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)

Fast Fourier Transform

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

 Decompose a signal into it's sinusoids

Functionality of the fft

Examples

Download