320 likes | 471 Views
PROCESSING SOUNDS. CMPS1371 Introduction to Computing for Engineers. 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.
E N D
PROCESSING SOUNDS CMPS1371Introduction to Computing for Engineers
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.
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]);
Now, we can either plot this sine wave; or we can hear it!!! To plot, simply type: >> plot(v); Music
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 air-pressure in time Pressure Pressure Time t Time t
Sound If the function is periodic, we perceive it as sound with a certain frequency (else it’s noise). The frequency defines the pitch. Pressure Pressure Time t Time t
Sound The SHAPE of the curve defines the sound character String Flute Flute String Flute Flute Brass Brass
Sound Listening to an orchestra, you can distinguish between different instruments, although the sound is a SINGLE FUNCTION ! Flute 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.
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 akcos(kx) + bksin(kx) Fourier Fourier Coefficients
Fourier • The set of ak, bk 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 !
SAWTOOTH Function f(x) = ½ - 1/π * Σn 1/n *sin (n*π*x) Freq sin cos 1 1 0 2 1/2 0 3 1/3 0 4 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