86
29 Sept '09 Comp30291 Section 1 1 Comp30291 Digital Media Processing Barry Cheetham [email protected] www.cs.man.ac.uk/~barry/mydocs/ Comp30291

Comp30291 D igital M edia P rocessing

  • Upload
    strom

  • View
    28

  • Download
    0

Embed Size (px)

DESCRIPTION

Comp30291 D igital M edia P rocessing. Barry Cheetham [email protected] www.cs.man.ac.uk/~barry/mydocs/Comp30291. Section 1 Introduction. Signal : time-varying measurable quantity whose variation normally conveys information. - PowerPoint PPT Presentation

Citation preview

Page 1: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 1

Comp30291Digital Media Processing

Barry Cheetham

[email protected]/~barry/mydocs/Comp30291

Page 2: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 2

• Signal: time-varying measurable quantity whose variation normally conveys information.

• Quantity often a voltage obtained from some transducer e.g. a microphone.

• Define two types of signal:

Continuous time (analogue)

Discrete time

Section 1 Introduction

Page 3: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 3

Analogue signals

• Continuous variations of voltage. • Exist for all values of t in range - to +. Examples: (i) 5sin(62.82t) : sine-wave of frequency 62.82 radians/second ( 10 Hz)

t

Voltage

0.1-0.1

5

t

Voltage

1

function'step' 0:1

0:0)( (ii)

t

ttu

Graph of voltage against time gives continuous 'waveform' :

Page 4: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 4

‘Sound’• Variation in air pressure

• May be generated by musical instruments or a human voice.

• E.g. by a piano string vibrating or your vocal cords vibrating

• Local pressure variation travels thro’ the air to your ear or a microphone.

• There it causes a ‘diaphragm’ to vibrate.

• Microphone produces a continuous voltage proportional to the variation in air pressure.

• Graph of voltage against time is ‘sound waveform’.

Page 5: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 5

Segment of telephone speech: ‘Its close to Newcastle’

0 0.5 1 1.5-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

closetoNewcastle.pcm

time in seconds

volta

ge

Page 6: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 6

16 s segment of violin music

0 2 4 6 8 10 12 14 16-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1cap4th.wav (mono)

time in seconds

volta

ge

Page 7: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 7

50ms segment of music: violin note

0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8Violin note before sampling

time in seconds

volta

ge

What is frequency of note being played? Ans 16 cycles in 0.05 s 320 Hz

Page 8: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 8

Discrete-time signal • Exists only at discrete points in time. • Often obtained by sampling an analogue signal,

i.e. measuring its value at discrete points in time. • Sampling points separated by equal intervals of T seconds.• Given analogue signal x(t), x[n] = value of x(t) when t = nT.• Sampling process produces a sequence of numbers:

{ ..., x[-2], x[-1], x[0], x[1], x[2], ..... } • Referred to as {x[n]} or ' the sequence x[n] '. • Sequence exists for all integer n in the range - to .

Page 9: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 9

Examples of discrete time signals:

(i) {..., -4, -2, 0, 2, 4, 6, ....}

sequence whose nth element, x[n], is defined by : x[n] = 2n.

Underline sample corresponding to n = 0.

(ii) { ..., -4.75, -2.94, 0, 2.94, 4.75, 4.76, ...}

sequence with x[n] = 5 sin(62.82t) with t=nT and T=0.01.

(iii) { ..., 0, ..., 0. 0, 1, 1, 1, ..., 1, ...}

“ unit step ” sequence whose nth element is:

0 : n < 0

u[n] = 1 : n 0

Page 10: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 10

n

x[n]

1 2 3 4

-3 -2 -12 -

-2-

Graphs of discrete time signals

For Example (i):

Page 11: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 11

n

Graphs of discrete time signals

x[n]For Example (ii):

1 2 3 4

-3 -2 -1

3 -

-3

Page 12: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 12

n

x[n]

1 2 3 4

-3 -2 -1

1 -

-1-

Graphs of discrete time signals

For Example (iii):

Page 13: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 13

Speech sampled at 8kHz with 16 bits per sample

0 2000 4000 6000 8000 10000 12000-3

-2.5

-2

-1.5

-1

-0.5

0

0.5

1

1.5

2 x 104 closetoNewcastle.pcm

sample index n

16-b

it bi

nary

num

ber

Page 14: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 14

45 ms segment of music sampled at 22.05 kHz

Frequency of note 19/1000 = 0.019 cycles/sample = 0.019 *22050 = 419 Hz

Page 15: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 15

50 ms segment of music sampled at 22.05 kHz

0 100 200 300 400 500 600 700 800 900 1000 1100-3

-2

-1

0

1

2

3 x 104 Violin note sampled at 22050 Hz

sample index n

16-b

it bi

nary

num

ber

What’s the frequency now, folks?

Page 16: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 16

Voiced telephone speech sampled at 16000 Hz

Short ( 100ms) segment of a vowel. [1ms = 1/1000 second]

Vowels are approximately periodic.

What is the pitch of the voice here?

Ans: 9 cycles in 1/10 s i.e. 90 Hz - probably male speech

0 500 1000 1500-2

-1.5

-1

-0.5

0

0.5

1

1.5

2x 10

4

n

16-b

it bi

nary

num

ber

Page 17: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 17

• Discrete time signals often generated by ADC devices.

• Produce binary numbers from sampled voltages or currents.

• Accuracy determined by 'word-length ' of ADC device, i.e. number of bits available for each binary number.

• Quantisation: Truncating or rounding sampled value to nearest available binary number

• Resulting sequence of quantised numbers is a digital signal. . • i.e. discrete time signal with each sample digitised for arithmetic processing.

Digital signal

Page 18: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 18

Signal Processing

• Analogue signals "processed" by circuits consisting of resistors,

capacitors, transistors etc. • Digital signals "processed" using programmed computers,

microcomputers or special purpose digital hardware. • Examples of the type of processing that may be carried out are:

(i) amplification or attenuation.

(ii) filtering out some unwanted part of the signal.

(iii) rectification : making waveform purely positive.

(iv) multiplying by another signal.

Page 19: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 19

‘Real time’ & ‘non-real time’ processing

’Real time' processing:

A mobile phone contains 'DSP' processor fast & powerful enough to perform the mathematical operations required to process digitised speech as it is being received.

‘Non-real time’ processing:• A PC can perform DSP processing on a stored recording of music.• It can take as much time as it needs to complete this processing. • Useful in its own right; e.g for MP3 compression. • Also used to 'simulate' software for real time DSP systems before

they are built into special purpose hardware. • Simulated DSP systems tested with stored segments of speech/music.

Page 20: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 20

Introduction to ‘MATLAB’

•High level computing language for matrix operations. •Widely used for studying signal processing & comms. •No need to declare variables in advance. •Each variable may be array with real or complex elements.•No distinction between reals & integers

Page 21: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 21

Examples of MATLAB statements

A = [ 1 2.8 3.2 4 5 6 7.7 8 ] ;

X = [ 1 2 3 4 ] ;

Y = [ 1 2 3 4 ] ;

Z = 3 ;

W = 4 + 5.5j ; % Comment

Page 22: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 22

clear all;T = 0.01; % sampling interval (seconds)for n=1:80 s(n) = 5*sin(2*pi*10*n*T); end;plot (s);

Generating, storing & plotting signals

Generate & plot 80 samples of a 10 Hz sine-wave of amplitude 5, sampled at 100 Hz:

0 10 20 30 40 50 60 70 80-5

0

5

Page 23: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 23

Signals in MATLABSegments in row or column ‘vector’ arrays: n x 1 or 1 x n matrices. Previous example produces row vector sTo produce column vector s containing sine-wave segment

for n = [1:100]’ s(n) = sin(n*pi/10)

end;

Another (preferred) way of defining column vector s:

n=[1:100]’ ; % 100x1 col vector with entries 1,2,...,100s(n) = sin(n*pi/10) % For each entry of n, calculate value.

Page 24: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 24

x = [ 1.1, 2, 3.3, 4, 5 ] defines row vector x = [ 1.1; 2; 3.3; 4; 5 ] defines column vector. Easier to write than:

x = [ 1.1 2 3.3 4 5 ]

Row-vectors & column-vectors

Page 25: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 25

Transpose

• x.' is transpose of x. (Note the .' symbols). • If x is col vector, x.' will be row vector. • Note dot apostrophe (.') rather than simply apostrophe ('). • Omitting dot will give “complex conjugate transpose”. • Index of first element of a row or column vector is always one. • Sometimes inconvenient since a signal starts at time zero. • Frequency spectrum normally starts at frequency zero. • Consider adopting following statements:

for n = 0 : 1023 x(1+n) = sin(n*pi/10)

end;• Preserves natural definition of n as time-index.

Page 26: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 26

Command line mode• Run MATLAB by clicking on icon & type in statements. • If in doubt about any command, e.g. ‘plot’ type: help plot

<return>> 4 + 5>> X=4;Y=5; X+Y>> X*Y>>X/Y>> bench (Tells you how fast your computer is)>> for n = 0 : 99>> x(1+n) = sin(n*pi/10)>> end;>> plot(x)

Page 27: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 27

• More convenient to have statements in script file e.g. myprog.m.• Just a text file often referred to as an "M-file".• To execute the statements in "myprog.m" just type• "myprog" • MATLAB has a text editor. • Paths must be set to a "work" directory where the "M-files" will be stored.

Scripting ‘m-files’

Page 28: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 28

Digital media file formats

• Confusing number of different ways of storing speech, music, images & video in digital form. Consider just six:

• ‘*.pcm’ or ‘*.raw’: binary file without header often used for uncompressed ‘narrowband’speech sampled at 8 or 16 kHz.

• ‘*.wav’: binary file with header often used uncompressed high-fidelity stereo sound (music or speech) sampled at 44.1 kHz.

• ‘*.mp3’ for mp3 compressed music or hi-fi speech.

• ‘*.tif’ files for images with lossless compression.

• ‘*jpg’ files for images with lossy compression

• ‘*.avi’ audio/video interleaved files.

Page 29: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 29

MATLAB functions for reading/writing *.pcm

% Open *.pcm file of 8 kHz sampled speech,16 bits/sample

IFid=fopen('operamp.pcm','rb');

inspeech = fread(IFid, 'int16');

L = length(inspeech); % number of samples

scale = max(abs(inspeech)) + 1;

sound(outspeech/scale,8000,16); % listen to the speech

outspeech = inspeech / 2; % reduce by 6 dB

OFid = fopen(‘opout.raw’, ‘wb’);

fwrite(OFid,outspeech, ‘int16’);

fclose('all');

Page 30: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 30

MATLAB functions for reading/writing *.wav

clear all; close all; % closes all graphs etc.

[inmusic, Fs, Nbits] = wavread('cap4th.wav');

S=size(inmusic); % inmusic may be mono or stereo

L=S(1); % number of mono or stereo samples

if S(2) ==2 disp(‘music is stereo’); end;

sound(inmusic,Fs,Nbits);

outmusic = inmusic /sqrt(2); % reduce amplitude by 3 dB

wavwrite(outmusic, Fs, Nbits,’outfile.wav’);

fclose (‘all’); % closes all files

Page 31: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 31

Another way of saving/loading arrays

• Does not produce files that can be read by wave-editors, though it can produce text files.

• To save an array called "b" in a disk file called "bits.dat":

save bits.dat b -ascii• Omitting ‘-ascii’ creates a more compact ‘non-ascii’ binary

file.• To read contents of ascii file ‘bits.dat’ into array called ‘bits’:

load bits.dat -ascii

Page 32: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 32

Frequency spectrum & sampling

• A sine-wave has a ‘frequency’( f ) in Hz or 2f radians/second.

• Sine-waves do not sound very nice.

• Speech & music need of lots of sine-waves added together.

• Harmonically related, i.e. f, 2f, 3f, 4f, 5f, ...

• Other sounds approximated as sum of lots of very small sine-waves.

• Any type of signal has a ‘frequency spectrum’.

• Most energy in speech is within 300 Hz to 3400 Hz (3.4 kHz).

• Energy in music that we can hear is in range 50 Hz to 20 kHz.

Page 33: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 33

Spectral analysis of a sine-wave

Powerspectral density

f Hz

F

x(t)=Asin(2Ft) is sine-wave of frequency F Hz

All its power is concentrated at F Hz.

Its power spectrum is infinitely high line at f=F.

Draw upward arrow & call it an ‘impulse’.

Its ‘strength’ is A2/2 watt/Hz for Asin(2Ft)

Can make length of line strength but must put arrow on top.

A2/2

Page 34: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 34

Spectral analysis of musical note

Powerspectral density

f Hz

F

•‘Fundamental’ sine-wave at F Hz & lots of sine-wave ‘harmonics’ at 2F, 3F, etc.•F is ‘note’: 440 Hz for ‘A’, 266 Hz for ‘mid C’•What is the note being played here? 910 Hz ( Just above ‘top A’).

20 kHz

Page 35: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 35

Spectral analysis of analogue signals

• Spectral analysis of periodic sound before it is sampled is tricky because of the upward arrows.

• They are infinitely high!!• We plotted their ‘strengths’, not their amplitudes.• Things become easier when we spectrally analyse

digitised sound (later)!

Page 36: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 36

‘Sampling Theorem’

• If a signal has all its spectral energy below B Hz, & is sampled at

Fs Hz where Fs ≥ 2B, it can be reconstructed exactly from the

samples.

• Speech below 3400 Hz can be sampled at 6800 Hz (in practice 8

kHz).

• Music can be sampled at 40 kHz (in practice 44.1 kHz).

• When we process a sampled signal in MATLAB & sampling rate

was Fs Hz, we can only observe frequencies in range 0 to Fs/2.

Page 37: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 37

‘Aliasing’

• What happens if you sampled a speech or music signal at Fs & it

has some spectral energy above Fs/2 ?

• Leads to a form of distortion known as ‘aliasing’ which sounds

very bad.

• Must filter off all spectral energy at & above Fs/2 Hz before

sampling at Fs.

Page 38: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 38

MATLAB Signal Processing toolbox

Set of functions for: (i) carrying out operations on signal segments stored in vectors (ii) evaluating the effect of these operations, and (iii) computing parameters of digital filters etc.

Two commonly used operations are:

• Digital filtering• Spectral analysis

Page 39: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 39

Digital filtering• Low-pass digital filters, high-pass and other categories

• Remove or enhance frequency components of a sampled signal.

• Low-pass filter aims to ‘filter out’ spectral energy above cut-off frequency Fc, keeping all spectral energy below Fc unchanged.

• Only consider frequencies up to Fs/2 .

Gain

fFc

1

Fs/2

‘Gain’ frequency-response graph for ideal low-pass digital filter.

Page 40: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 40

Ideal high-pass digital filter

• Aims to ‘filter out’ spectral energy below cut-off frequency Fc, keeping all spectral energy above Fc unchanged.

• Only consider frequencies up to Fs/2 .

Gain

fFc

1

Fs/2

‘Gain’ frequency-response graph for ideal high-pass digital filter:

Page 41: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 41

Ideal band-pass digital filter

• Aims to ‘filter out’ spectral energy below frequency FL, & above FH keeping all spectral energy between FL & FH unchanged.

• Only consider frequencies up to Fs/2 .

‘Gain’ frequency-response graph for ideal band-pass digital filter:

Gain

f

FL

1

Fs/2FH

Page 42: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 42

Ideal band-stop digital filter

• Aims to ‘filter out’ spectral energy between FL, & FH keeping all spectral energy below FL & above FH unchanged.

• Only consider frequencies up to Fs/2 .

‘Gain’ frequency-response graph for ideal band-stop digital filter:

Page 43: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 43

Ideal band-stop digital filter

• Aims to ‘filter out’ spectral energy between FL, & FH keeping all spectral energy below FL & above FH unchanged.

• Only consider frequencies up to Fs/2 .

Gain

f

FL

1

Fs/2

‘Gain’ frequency-response graph for ideal band-stop digital filter:

FH

Page 44: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 44

IIR & FIR digital filters

• There are 2 types of digital filter:

Infinite impulse response (IIR) type.

Finite impulse response (FIR) type.

• Either can be low-pass, high-pass, band-pass etc.

• MATLAB can design both types by many different methods.

Page 45: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 45

GAIN

FREQUENCY

1

fC

Gain-response of typical IIR low-pass filter in practice

•Previous graph shown ‘ideal response’.

•Referred to as ‘brick-wall’.

•In practice cut-off is more gradual as illustrated here.

Fs/2

Page 46: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 46

GAIN (dB)

FREQUENCY

0

fC

Same gain-response now in deciBels

• Plot 20*log10(GAIN) against freq

• Absolute gain of 0 is 0 dB.

• Absolute gain of 0 is - dB.

-10

-20

-40

-30

Fs/2

Page 47: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 47

Decibel scale

• Gain is often converted to deciBels (dB)

• Gain in dB = 20 log 10 (Absolute Gain)

dB Absolute

0 1

6 2

-6 1/2

3 2

20 10

40 100

Page 48: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 48

y = filter(a, b, x) ; % Pass x thro’ digital filter to produce y. Coefficients in vectors a and b determine the filter’s effect. For an FIR filter, b=1.

The filter has a ‘transfer function’ which is: a(1) + a(2)z-1 + a(3)z-2 + ...H(z) =

1 + b(2)z-1 + b(3)z-2 + ...

freqz(a,b); % Display gain & phase responses of digital filters:- % Note the 'zoom' function.

[a,b] = butter(n, fC/(Fs/2)); % Get a & b for nth order low-pass filter.

Some of the signal processing functions provided

Page 49: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 49

[a,b] = butter(n, fc/(Fs/2), ‘high’); % High-pass Butt IIR.[a,b] = butter(n, [fL fU]/(Fs/2) ); % Band-pass Butt IIR.[a,b] = butter(n, [fL fU]/(Fs/2),’stop’); % Band-stop Butt IIR

[a,b] = cheby1(n, Rp,fc/(Fs/2) ); % Low-pass Chebychev type 1. [a,b] = cheby2(n, Rs,.. ); % Low-pass Chebychev type 2.

a = fir1(n, fc/(Fs/2)); % Get 'a' coeffs of FIR low-pass filter order n. % Cut-off is fc.

a = fir1(n, (fL, fU]/(Fs/2) ); % FIR band-pass filter design.Many more exist.

More functions for designing digital filters

Page 50: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 50

Specifying the cut-off frequencies

• This is a bit cumbersome.• If the cut-off is Fc, must enter:

Fc / (Fs/2)

i.e. divide by half the sampling frequency!

Page 51: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 51

To illustrate for IIR :

• Design 4th order "Butterworth type" IIR low-pass filter with Fc = 1000, Fs=8kHz.

[a,b] = butter(4, 1000/4000 ) ;

freqz(a,b); % plot gain & phase responses

• Implement it by applying it to signal in array x :

y = filter(a,b,x);

Page 52: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 52

To illustrate for FIR:

• Design & implement 40th order FIR low-pass digital filter with cut-off frequency 1 kHz • Apply it to signal in array x.

%To design: a = fir1(40, 1 / 4 ) ; freqz ( a , 1) ; % plot gain & phase

% To implement: y = filter(a, 1, x );

Page 53: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 53

clear all;%Input speech from a file:-fs = 8000; % sampling rate in HzIFid=fopen('operamp.pcm','rb');Inspeech = fread(IFid, 'int16');

%Design FIR digital filter:-fc = 1000; % cut-off frequency in Hz[a b] = fir1(20, fc/(0.5*fs) ); freqz(a,b);

%Process speech by filtering:-Outspeech = filter(a, b, Inspeech);

% Output speech to a fileOFid=fopen('newop.pcm','wb');fwrite(OFid, Outspeech, 'int16'); fclose('all');

MATLAB Demo1: Filtering speech

Page 54: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 54

Speech file: ‘operamp.pcm’

• This may be downloaded from

www.cs.man.ac.uk/~barry/mydocs/Comp30192

Page 55: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 55

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-1000

-800

-600

-400

-200

0

Normalized Frequency (1 Fs / 2)

Ph

ase

(d

eg

ree

s)

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-100

-80

-60

-40

-20

0

Normalized Frequency (1 Fs/2)

Ma

gn

itud

e (

dB

)

Graphs produced by ‘freqz’ in previous slide

4kHz

Page 56: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 56

Normalized Frequency (1 Fs/2)

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

-100

-80

-60

-40

-20

0M

ag

nitu

de

(d

B)

Gain-response from previous slide

4kHz

Page 57: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 57

IFid=fopen('newop.pcm','rb');speech = fread(IFid,'int16');scale = max(abs(speech))+1;SOUND(speech/scale,8000,16); fclose('all');

MATLAB Demo2: Listen to speech in a file

•To listen to the filtered speech as produced by MATLAB Demo1, run the following program.

•Effect of the lowpass filtering should be to make speech sound ‘muffled’

• Higher frequencies have been filtered out.

Page 58: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 58

Spectral analysis• Fast Fourier Transform (FFT) is famous algorithm for determining frequency content of sampled signals.

• Given a signal segment of N samples stored in array x:

X = fft(x) ;

• gives complex vector X of N samples of frequency spectrum of x. plot ( abs(X[1:N / 2] ) ;

• plots 'magnitude spectrum' of x from 0 Hz to Fs/2.

Page 59: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 59

FFT analyse 200 samples of 5Hz sine-wave

clear all; close all;

N=200;

Fs = 100; % sampling frequency (Hz)

for n=0:N-1

x(1+n) = 100*cos(2*pi*5*n/Fs) ;

end;

figure(1); plot(x);

X=fft(x)/(N/2); % Dividing by N/2 gives correct scaling for graph.

figure(2); plot(abs(X(1:N/2)),'*-');

grid on;

Page 60: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 60

Plot of 5Hz sine-wave segment sampled at 100 Hz

0 20 40 60 80 100 120 140 160 180 200-100

-80

-60

-40

-20

0

20

40

60

80

100

Sample index n

Value

Page 61: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 61

Spectral graph from fft

0 10 20 30 40 50 60 70 80 90 1000

10

20

30

40

50

60

70

80

90

100

Frequency-index (0:100 0:Fs/2)

FF

T m

agni

tude

50Hz

5 Hz

Page 62: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 62

Analyse sum of 2 sine-waves

clear all;

N=200; Fs = 100;

for n=0:N-1

x(1+n) = 100*cos(2*pi*5*n/Fs) + 50*sin(2*pi*10*n/Fs);

end;

figure(1); plot(x);

X=fft(x)/(N/2);

figure(2); plot(abs(X(1:N/2)),'*-'); grid on;

Page 63: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 63

Plot of sum of 2 sine-waves

0 20 40 60 80 100 120 140 160 180 200-150

-100

-50

0

50

100

150

Sample-index n

Vol

tage

Page 64: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 64

Spectral graph from FFT for 2 sine-waves

0 10 20 30 40 50 60 70 80 90 1000

10

20

30

40

50

60

70

80

90

100

Frequency index (100 Fs/2) 50Hz

FF

T m

agni

tude

Page 65: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 65

50 ms segment of music sampled at 22.05 kHz

0 100 200 300 400 500 600 700 800 900 1000 1100-3

-2

-1

0

1

2

3x 10

4 Violin note sampled at 22050 Hz

sample index n

16

-bit

bin

ary

nu

mb

er

Page 66: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 66

FFT magnitude spectrum of music segment

0 50 100 150 200 250 300 350 400 450 500 5500

10

20

30

40

50

60

70

80

Violin note: magnitute FFT spectrum

frequency-index

ma

gn

itu

de

550 Fs/2 = 11025 Hz

Page 67: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 67

Same FFT spectrum in dB against frequency- also frequency axis scaled to show Hz

0 2000 4000 6000 8000 100000

5

10

15

20

25

30

35

40Violin note FFT spectrum in dB

frequency-Hz

dB

Page 68: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 68

Decimation (down sampling)

• If a signal is sampled at 44.1 kHz, FFT gives spectrum with frequency range 0 to 22.05 kHz.

• You may only want to see spectrum from 0 to 2kHz.• Down-sampling by a factor 10 makes Fs = 4.41 kHz. • FFT then gives spectrum in range 0 to 2.205Hz.• To down-sample x by factor 10:

xd = decimate(x, 10);• Creates new array xd of length 1/10 th of that of x.• FFT analyse xd rather than x.• ‘decimate’ works by low-pass filtering then re-sampling.

Page 69: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 69

Understanding decimation

[a b] = butter(8, (Fs/24)/(Fs/2) );

xf = filter(a,b,x); %filter off all components above Fs/20 Hz

% Now re-sample at Fs/10 Hz:

for n=1:length(xf)/10

xd(n) = xf(n*10); % Omit 9 out of 10 samples

end;

We can easily write our own decimation procedure:

But it’s quicker to use the MATLAB function ‘decimate’

Page 70: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 70

Spectrograms• FFT can be used to analyse 20 to 30 ms of speech or music. • Over longer periods of time, spectral distribution will be changing.• We often want to analyse this change.• A related function is ‘specgram’ • Plots a whole succession of FFT spectral graphs showing how the

distribution of frequencies is changing over time. • Result is a ‘spectrogram’ which plots spectral distribution (using

colour) against time.• Demonstrated on next slide.

Page 71: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 71

Spectrograms for 4 Chinese words

Time

Fre

qu

en

cy

Spectrogram L1

0 0.1 0.2 0.3 0.4 0.5 0.60

500

1000

1500

2000

Time

Fre

qu

en

cy

Spectrogram L2

0 0.1 0.2 0.3 0.4 0.5 0.6 0.70

500

1000

1500

2000

Time

Fre

qu

en

cy

Spectrogram L3

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.90

500

1000

1500

2000

Time

Fre

qu

en

cy

Spectrogram L4

0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.50

500

1000

1500

2000

Page 72: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 72

clear all;fs = 8000; %sampling rate in HzT = 1/fs; % sampling interval (seconds)% Generate 10000 samples of 500 Hz sine-wave:-for n=1:10000 s(n) = 4000 * sin(2 * pi * 500 * n * T);end;% Store in a non-formatted (binary) file:-OFid=fopen('newsin.pcm','wb');fwrite(OFid, s, 'int16'); fclose('all');

MATLAB demo: Stores sine-wave segment in pcm file

Page 73: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 73

for n=1:10000 Outsin(n) = 2*Insin(n);end;

for n=1:10000 Outsin(n)=abs(Insin(n));end;

Outsin = abs(Insin);

Outsin = 2*Insin;

Using MATLAB more efficiently

Slow Faster

Page 74: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 74

MATLAB Exercise 1: Modulation

Generate 320 samples of a 50 Hz sine wave sampled at 8kHz & multiply this by a 1kHz sine-wave sampled at 8kHz. Plot resulting waveform

MATLAB Exercise 2: Simulate POTS speech quality

A ‘plain old fashioned telephone’ transmits speech between 300Hz & 3 kHz. Design & implement a digital filter to allow effect of this band-width restriction to be assessed by listening.

Page 75: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 75

MATLAB Exercise 3

• File ‘noisyviolin.wav’ contains music sampled at 22.05 kHz. • It is corrupted by:

‘high-pass’ random noise above 6 kHz

and a sine-wave around 4 kHz.• Design & implement 2 digital filters to remove the random

noise and the tone.• Listen to the ‘cleaned up music’

Page 76: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 76

clear all; fs = 8000; T=1/fs;for n=1:320 s(n) = sin(2*pi*50*n*T);end;figure(1); plot (s); for n=1:320 smod(n) = s(n)*sin(2*pi*1000*n*T);end;figure(2); plot (smod);

Solution to MATLAB Exercise 1

0 50 100 150 200 250 300 350-1-0.8-0.6-0.4-0.2

00.20.40.60.8

1

Page 77: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 77

% A solution for MATLAB Exercise 2clear all;%Input speech from a file:-fs = 8000; % sampling rate in HzIFid=fopen('operamp.pcm','rb');Inspeech = fread(IFid, 'int16');

%Design FIR band-pass digital filter:-fU = 3000; % upper cut-off freq HzfL = 300; % lower cut-off freq[a b] = fir1(100,[fL/(fs/2) fU/fs/2]); freqz(a,b);

%Process speech by filtering:-Outspeech = filter(a, b, Inspeech);

% Output speech to a fileOFid=fopen('newop.pcm','wb');fwrite(OFid, Outspeech, 'int16'); fclose('all');

Page 78: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 78

Solution to MATLAB exercise 3

clear all; close all;

[noisymusic,Fs,bits] = wavread('noisyviolin.wav');

S=size(noisymusic); disp(sprintf('Size of array = %d, %d', S(1),S(2)));

L=S(1); % length of vector

wavplay(noisymusic,Fs); % Listen to noisy violin

figure(1); plot(noisymusic); title('noisyviolin.wav (mono)');

[a1 b1] = butter(10,(Fs/4)/(Fs/2)); figure(2); freqz(a1,b1);

cleanmusic=filter(a1,b1,noisymusic); % Low-pass filter

[a2 b2] = butter(4,[3800 4200]/(Fs/2),'stop'); figure(3); freqz(a2,b2);

cleanmusic = filter (a2,b2,cleanmusic); %Band-stop filter

scale=max(abs(cleanmusic));

wavplay(cleanmusic/scale,Fs); figure(4); plot(cleanmusic/scale);

Page 79: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 79

• Real time DSP often uses 'fixed point' microprocessors since they consume less power & are less expensive than 'floating point' devices.

• A fixed point processor deals with all numbers as integers.

• Numbers often restricted to a word-length of only 16 bits.

• Overflow can occur with disastrous consequences to sound quality.

• If we avoid overflow by scaling numbers to keep them small in amplitude, we lose accuracy as quantisation error is larger proportion of the value.

• Fixed point DSP programming can be quite a difficult task.

Fixed point arithmetic

Page 80: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 80

• When we use a PC for non real time DSP, we have floating point

operations available with word-lengths that are larger than 16 bits. • This makes the task much easier. • When simulating fixed point real time DSP on a PC, we can

restrict programs to integer arithmetic, & this is useful for testing.• Can do this with MATLAB

Floating point arithmetic

Page 81: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 81

Advantages of digital signal processing• Signals are generally transmitted & stored in digital form so it

makes sense to process them in digital form also.• DSP systems can be designed & tested in simulation using

PCs. • Accuracy pre-determined by word-length & sampling rate.• Reproducible: every copy of system will perform identically.• Characteristics will not drift with temperature or ageing.• Availability of advanced VLSI technology.• Systems can be reprogrammed without changing hardware. • Products can be updated via Internet.• DSP systems can perform highly complex functions such as adaptive filtering

speech recognition.

Page 82: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 82

Disadvantages of DSP• Can be expensive especially for high bandwidth signals

where fast analogue/digital conversion is required.

• Design of DSP systems can be extremely time-consuming & a highly complex and specialised activity.

• Power requirements for digital processing can be high in battery powered portable devices.

• Fixed point processing devices are less power consuming but ability to program such devices is valued & difficult skill.

• There is a shortage of computer science & electrical engineering graduates with the knowledge & skill required.

Page 83: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 83

Summary of Course Objectives

1. Significance of DSP for multi-media, storage and comms. Introduction to MATLAB 2. Fundamental concepts: linearity etc3. Digital filters & their application to sound & images Low-pass digital filters etc. Butterworth low-pass gain response approximation.4. FIR & IIR type digital filter design & implementation.5. MATLAB for design, simulation & implementation of DSP.6. Real time implementation of DSP (fixed point arith).7. A/D conversion for DSP8. FFT and its applications9. Speech and music compression (CELP, MP3, etc)

Page 84: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 84

Background knowledge needed

Basic maths ( complex numbers, Argand diagram, modulus & argument, differentiation, sum of an arithmetic series, factorisation of quadratic equation, De Moivre's Theorem, exponential function, natural logarithms).

Experience with any high-level computer programming

language.

Page 85: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 85

Recommended Book

1. S.W. Smith, "Scientist and Engineer's Guide to Digital Signal Processing " California Tech. Publishing, 2nd ed., 1999, available complete at: http://www.dspguide.com/

Page 86: Comp30291 D igital  M edia P rocessing

29 Sept '09 Comp30291 Section 1 86

Problems1. Why do we call continuous time signals ‘analogue’ ?2. When could you describe a SIGNAL as being linear? 3 Give a formula for an analogue signal which is zero until t=0 and

then becomes equal to a 50 Hz sine wave of amplitude 1 Volt.4. Given that u(t) is an analogue step function, sketch the signal

u(t)sin(2.5t).5. If u(t)sin(5t) is sampled at intervals of T=0.1 seconds, what discrete

time signal is obtained? What is the sampling frequency?6. What is difference between a discrete time signal & a digital signal?7. Define (i) amplification, (ii) filtering, (iii) rectification and (iv)

modulation.8. What is meant by “fixed point” & “floating point”?9. Explain the terms “analogue filter” and “digital filter”.10. What is meant by the term “real time” in DSP?11. There are many applications of DSP where the digital signal being

processed is not obtained by sampling an analogue signal. Give an example.