Fundamentals of DSP -Part 1 - KAIST

37
GCT535: Sound Technology for Multimedia Fundamentals of DSP - Part 1 Juhan Nam

Transcript of Fundamentals of DSP -Part 1 - KAIST

Page 1: Fundamentals of DSP -Part 1 - KAIST

GCT535: Sound Technology for Multimedia

Fundamentals of DSP - Part 1

Juhan Nam

Page 2: Fundamentals of DSP -Part 1 - KAIST

Goals

● Introducing digital audio effects

● The fundamentals of digital signal processingβ—‹ Linear time-invariant (LTI) systemβ—‹ FIR / IIR Filtersβ—‹ Convolutionβ—‹ Impulse responseβ—‹ Frequency response

Page 3: Fundamentals of DSP -Part 1 - KAIST

Introduction

● Once we record or synthesize sound, we modify the acoustic characteristics using various digital audio effects β—‹ Loudness: volume, compressor, limiter, noise gateβ—‹ Timbre: filters, equalizer, distortion, modulation, chorus, flanger, vocoderβ—‹ Pitch: pitch correction, harmonizer, vibratoβ—‹ Duration: time scale modification, resampling β—‹ Spatial effect: panning, delay, reverberation, binaural (HRTF)

Input OutputDigital Audio Effects

Page 4: Fundamentals of DSP -Part 1 - KAIST

Introduction

● Filters and delaysβ—‹ Bi-quads filters: lowpass, bandpass, highpass, equalizersβ—‹ Comb filter (delayline): delay/echo, chorus, flanger, reverbβ—‹ Convolution: resampling, HRTF, reverberation (measured impulse responses)

● Non-linear processingβ—‹ Modulation: amplitude, frequencyβ—‹ Distortion and dynamic range control: compressor, limiter, noise gate

● Block-based processingβ—‹ Pitch shifting and Time-scale modification

Page 5: Fundamentals of DSP -Part 1 - KAIST

Filters and Delays

● Take the input signal π‘₯ 𝑛 as a sequence of numbers and returns the output signal 𝑦 𝑛 as another sequence of numbers

● Perform a combination of three mathematic operations upon the inputβ—‹ Multiplication: 𝑦 𝑛 = 𝑏 % π‘₯ 𝑛○ Delay: 𝑦 𝑛 = π‘₯ 𝑛 βˆ’π‘€β—‹ Summation: 𝑦 𝑛 = π‘₯ 𝑛 + π‘Ž % 𝑦 𝑛 βˆ’ 𝑀

Input OutputFiltersor Delays

π‘₯ 𝑛 𝑦 𝑛

Page 6: Fundamentals of DSP -Part 1 - KAIST

Filters and Delays

● Bi-quad filterβ—‹ Two delay elements for input or outputβ—‹ The delayed output (β€œfeedback”) causes resonanceβ—‹ Rooted from analog circuits (R-L-C)

𝑧!"

π‘₯ 𝑛 +

𝑧!" 𝑏"

𝑏#

𝑏$

π‘₯ 𝑛 βˆ’ 1

π‘₯ 𝑛 βˆ’ 2

𝑧!"

𝑦 𝑛

𝑧!"-π‘Ž"

-π‘Ž#

𝑦 𝑛 βˆ’ 1

𝑦 𝑛 βˆ’ 2

𝑦 𝑛 = 𝑏$ + π‘₯ 𝑛 + 𝑏" + π‘₯ 𝑛 βˆ’ 1 + 𝑏# + π‘₯ 𝑛 βˆ’ 2 βˆ’ π‘Ž" + 𝑦 𝑛 βˆ’ 1 βˆ’ π‘Ž# + 𝑦 𝑛 βˆ’ 2

Page 7: Fundamentals of DSP -Part 1 - KAIST

Filters and Delays

● Comb filterβ—‹ Use a delayline β—‹ Feedforward is used for chorus/flanger and feedback is for echo/reverbβ—‹ Rooted from magnetic tape recording

𝑦 𝑛 = π‘₯ 𝑛 + 𝑔 + π‘₯ 𝑛 βˆ’ 𝑀

𝑧!%

π‘₯ 𝑛 𝑦 𝑛+

Feedforward Comb Filter

𝑦 𝑛 = π‘₯ 𝑛 + 𝑔 + 𝑦(𝑛 βˆ’ 𝑁)

𝑧!&

π‘₯ 𝑛 𝑦 𝑛+

𝑔

Feedback Comb Filter

𝑔

Page 8: Fundamentals of DSP -Part 1 - KAIST

Filters and Delays

● Convolution filterβ—‹ Conduct the convolution operation with an impulse response of a systemβ—‹ The impulse response is often measured from natural objects

β–  HRTF: human ears β–  Reverberation: rooms

𝑦 𝑛 = 𝑏$ + π‘₯ 𝑛 + 𝑏" + π‘₯ 𝑛 βˆ’ 1 + 𝑏# + π‘₯ 𝑛 βˆ’ 2 +β‹―+ 𝑏% + π‘₯ 𝑛 βˆ’ 𝑀

β„Ž(𝑛)= [𝑏$, 𝑏", 𝑏#… , 𝑏%]

Page 9: Fundamentals of DSP -Part 1 - KAIST

Linear Time-Invariant Filters

● They are called Linear Time-Invariant (LTI) filters in the context of digital signal processing

● Linearityβ—‹ Scaling: if π‘₯ 𝑛 β†’ 𝑦 𝑛 , then a % π‘₯ 𝑛 β†’ a % 𝑦 𝑛○ Superposition: if π‘₯! 𝑛 β†’ 𝑦! 𝑛 and π‘₯" 𝑛 β†’ 𝑦" (n), then π‘₯! 𝑛 + π‘₯" 𝑛 β†’

𝑦! 𝑛 + 𝑦" 𝑛

● Time-Invarianceβ—‹ If π‘₯ 𝑛 β†’ 𝑦 𝑛 , then π‘₯ 𝑛 βˆ’ 𝑁 β†’ 𝑦 𝑛 βˆ’ 𝑁 for any 𝑁○ This means that the system does not change its behavior over time

Page 10: Fundamentals of DSP -Part 1 - KAIST

Linear Time-Invariant Filters

● Non-linear filtersβ—‹ 𝑦 𝑛 = π‘₯ 𝑛 "

● Time-variant filtersβ—‹ 𝑦 𝑛 = 𝑛 % π‘₯ 𝑛 + π‘₯(𝑛 βˆ’ 1)

● Modulation β—‹ Amplitude or frequency modulation generates new sinusoidal components

● Dynamic range compressionβ—‹ Input gain is a function of the input: 𝑦 𝑛 = 𝑓(π‘₯ 𝑛 ) % π‘₯ 𝑛

Page 11: Fundamentals of DSP -Part 1 - KAIST

Linear Time-Invariant System

● Remember that sinusoids are eigenfunctions of linear systemβ—‹ The input sinusoids changes in amplitude and phase while preserving the

same frequencyβ—‹ No new sinusoidal components are created

LinearSystem

𝑒#$%𝐴(πœ”)𝑒#&($)𝑒#$%

Amplitude Response

Phase Response

Page 12: Fundamentals of DSP -Part 1 - KAIST

The Simplest Lowpass Filter

● Difference equation: 𝑦 𝑛 = π‘₯ 𝑛 + π‘₯(𝑛 βˆ’ 1)

● Signal flow graph

𝑧!"

π‘₯ 𝑛 𝑦 𝑛+

β€œDelay Operator”

Page 13: Fundamentals of DSP -Part 1 - KAIST

The Simplest Lowpass Filter: Sine-Wave Analysis

● Measure the amplitude and phase changes given a sinusoidal signal input

Page 14: Fundamentals of DSP -Part 1 - KAIST

The Simplest Lowpass Filter: Frequency Response

● Plot the amplitude and phase change over different frequencyβ—‹ The frequency sweeps from 0 to the Nyquist rate

Page 15: Fundamentals of DSP -Part 1 - KAIST

The Simplest Lowpass Filter: Frequency Response

● Mathematical approachβ—‹ Use complex sinusoid as input: π‘₯ 𝑛 = 𝑒#$)

β—‹ Then, the output is: 𝑦 𝑛 = π‘₯ 𝑛 + π‘₯ 𝑛 βˆ’ 1 = 𝑒#$) + 𝑒#$()*!) = 1 + 𝑒*#$ % 𝑒#$)

= 1 + 𝑒*#$ % π‘₯(𝑛)

β—‹ Frequency response: 𝐻 πœ” = 1 + 𝑒*#$ = 𝑒#!" + 𝑒*#

!" 𝑒*#

!" = 2cos($

")𝑒*#

!"

β—‹ Amplitude response: 𝐻(πœ”) = 2 cos $"

β—‹ Phase response: ∠𝐻 πœ” = βˆ’$"

Page 16: Fundamentals of DSP -Part 1 - KAIST

The Simplest Highpass Filter

● Difference equation: 𝑦 𝑛 = π‘₯ 𝑛 βˆ’ π‘₯(𝑛 βˆ’ 1)

● Signal flow graph

𝑧!"

π‘₯ 𝑛 +

βˆ’1

𝑦 𝑛

Page 17: Fundamentals of DSP -Part 1 - KAIST

The Simplest Highpass Filter: Frequency Response

● Plot the amplitude and phase change over different frequencyβ—‹ The frequency sweeps from 0 to the Nyquist rate

Page 18: Fundamentals of DSP -Part 1 - KAIST

Finite Impulse Response (FIR) System

● Difference equation𝑦 𝑛 = 𝑏+ % π‘₯ 𝑛 + 𝑏! % π‘₯ 𝑛 βˆ’ 1 + 𝑏" % π‘₯ 𝑛 βˆ’ 2 +β‹―+ 𝑏, % π‘₯ 𝑛 βˆ’ 𝑀

● Signal flow graph

𝑧!"

π‘₯ 𝑛 +

𝑧!"

𝑧!"

...

𝑏"

𝑏#

𝑏$

𝑏%

π‘₯ 𝑛 βˆ’ 1

π‘₯ 𝑛 βˆ’ 2

π‘₯ 𝑛 βˆ’π‘€

𝑦 𝑛

Page 19: Fundamentals of DSP -Part 1 - KAIST

Impulse Response

● The system output when the input is a unit impulseβ—‹ π‘₯ 𝑛 = 𝛿 𝑛 = 1, 0, 0, 0, … β†’ 𝑦 𝑛 = β„Ž(𝑛)= [𝑏+, 𝑏!, 𝑏"… , 𝑏,] (for FIR

system)

● Characterizes the digital system as a sequence of numbersβ—‹ A system is represented just like audio samples!

π‘₯ 𝑛 = 𝛿 𝑛 𝑦 𝑛 = β„Ž(𝑛)β„Ž 𝑛

Input OutputLTISystem

Page 20: Fundamentals of DSP -Part 1 - KAIST

Examples: Impulse Response

● The simplest lowpass filterβ—‹ h 𝑛 = 1, 1

● The simplest highpass filterβ—‹ h 𝑛 = 1,βˆ’1

● Moving-average filter (order=5)

β—‹ h 𝑛 = !-, !-, !-, !-, !-

Page 21: Fundamentals of DSP -Part 1 - KAIST

Convolution

● The output of LTI digital filters is represented by the convolution operation between π‘₯ 𝑛 and β„Ž 𝑛

● Examplesβ—‹ The simplest lowpass filter β—‹ 𝑦 𝑛 = 1, 1 βˆ— π‘₯ 𝑛 = 1 % π‘₯(𝑛) + 1 % π‘₯(𝑛 βˆ’ 1) = π‘₯ 𝑛 + π‘₯(𝑛 βˆ’ 1)

𝑦 𝑛 = π‘₯ 𝑛 βˆ— β„Ž 𝑛 = :'(!)

)

π‘₯ 𝑖 + β„Ž 𝑛 βˆ’ 𝑖 = :'(!)

)

β„Ž(𝑖) + π‘₯(𝑛 βˆ’ 𝑖)

This is more practical expression when the input is an audio streaming

Page 22: Fundamentals of DSP -Part 1 - KAIST

Proof: Convolution

● Method 1β—‹ The input is represented as the sum of weighted and delayed impulses units

β–  π‘₯ 𝑛 = π‘₯$, π‘₯", π‘₯#… , π‘₯% = π‘₯$ + 𝛿 𝑛 + π‘₯" + 𝛿 𝑛 βˆ’ 1 + π‘₯# + 𝛿 𝑛 βˆ’ 2 +β‹―+ π‘₯% +𝛿 𝑛 βˆ’ 𝑀

β—‹ By the linearity and time-invariance β–  𝑦 𝑛 = π‘₯$ + β„Ž 𝑛 + π‘₯" + β„Ž 𝑛 βˆ’ 1 + π‘₯# + β„Ž 𝑛 βˆ’ 2 +β‹―+ π‘₯% + β„Ž 𝑛 βˆ’ 𝑀 =

βˆ‘'($% π‘₯(𝑖) + β„Ž(𝑛 βˆ’ 𝑖)

Page 23: Fundamentals of DSP -Part 1 - KAIST

Proof: Convolution

● Method 2β—‹ The impulse response can be represented as a set of weighted impulses

β–  β„Ž 𝑛 = 𝑏$, 𝑏", 𝑏#… , 𝑏% = 𝑏$ + 𝛿 𝑛 + 𝑏" + 𝛿 𝑛 βˆ’ 1 + 𝑏# + 𝛿 𝑛 βˆ’ 2 +β‹―+ 𝑏% +𝛿 𝑛 βˆ’ 𝑀

β—‹ By the linearity, the distributive property and π‘₯ 𝑛 βˆ— 𝛿 𝑛 βˆ’ π‘˜ = π‘₯(𝑛 βˆ’ π‘˜)β–  𝑦 𝑛 = 𝑏$ + π‘₯ 𝑛 + 𝑏" + π‘₯ 𝑛 βˆ’ 1 + 𝑏# + π‘₯ 𝑛 βˆ’ 2 +β‹―+ 𝑏% + π‘₯ 𝑛 βˆ’ 𝑀 =

βˆ‘'($% β„Ž(𝑖) + π‘₯(𝑛 βˆ’ 𝑖)

Page 24: Fundamentals of DSP -Part 1 - KAIST

Properties of Convolution

● Commutative: π‘₯ 𝑛 βˆ— β„Ž" 𝑛 βˆ— β„Ž# 𝑛 = π‘₯ 𝑛 βˆ— β„Ž# 𝑛 βˆ— β„Ž" 𝑛

● Associative: (π‘₯ 𝑛 βˆ— β„Ž" 𝑛 ) βˆ— β„Ž# 𝑛 = π‘₯ 𝑛 βˆ— (β„Ž" 𝑛 βˆ— β„Ž# 𝑛 )

● Distributive: π‘₯ 𝑛 βˆ— β„Ž" 𝑛 + β„Ž# 𝑛 = π‘₯ 𝑛 βˆ— β„Ž" 𝑛 + π‘₯ 𝑛 βˆ— β„Ž# 𝑛

Page 25: Fundamentals of DSP -Part 1 - KAIST

Example: Convolution

● Given π‘₯ 𝑛 = π‘₯$, π‘₯", π‘₯#, … , π‘₯% and β„Ž 𝑛 = β„Ž$, β„Ž", β„Ž#β—‹ 𝑦 𝑛 = βˆ‘./+, β„Ž(𝑖) % π‘₯(𝑛 βˆ’ 𝑖)β—‹ 𝑦 0 = β„Ž(0) 0 π‘₯(0)β—‹ 𝑦 1 = β„Ž 0 0 π‘₯ 1 + β„Ž(1) 0 π‘₯(0)β—‹ 𝑦 2 = β„Ž 0 0 π‘₯ 2 + β„Ž 1 0 π‘₯ 1 + β„Ž 2 0 π‘₯ 0β—‹ 𝑦 3 = β„Ž 0 0 π‘₯ 3 + β„Ž 1 0 π‘₯ 2 + β„Ž 2 0 π‘₯ 1β—‹ 𝑦 4 = β„Ž 0 0 π‘₯ 4 + β„Ž 1 0 π‘₯ 3 + β„Ž 2 0 π‘₯ 2β—‹ 𝑦 5 = β„Ž 0 0 π‘₯ 5 + β„Ž 1 0 π‘₯ 4 + β„Ž 2 0 π‘₯ 3β—‹ 𝑦 6 = β„Ž 1 0 π‘₯ 5 + β„Ž 2 0 π‘₯ 4β—‹ 𝑦 7 = β„Ž 2 0 π‘₯ 5

● The size of transient region is equal to the number of delay operators

● If the length of π‘₯ 𝑛 is 𝑀 and the length of β„Ž 𝑛 is 𝑁, then the length of 𝑦 𝑛 is 𝑀 +𝑁 βˆ’ 1.

Transient state

Steady state

Transient state

Page 26: Fundamentals of DSP -Part 1 - KAIST

Demo: Convolution

Page 27: Fundamentals of DSP -Part 1 - KAIST

Example: Convolution Reverb

● Convolution Reverb

Room Impulse Response

Lobby

Church

Page 28: Fundamentals of DSP -Part 1 - KAIST

A Simple Feedback Lowpass Filter

● Difference equation: 𝑦 𝑛 = π‘₯ 𝑛 + π‘Ž 3 𝑦(𝑛 βˆ’ 1)

● Signal flow graphβ—‹ When π‘Ž is slightly less than 1, it is called β€œLeaky Integrator”

𝑧!"

π‘₯ 𝑛 𝑦 𝑛+

π‘Ž

Page 29: Fundamentals of DSP -Part 1 - KAIST

A Simple Feedback Lowpass Filter: Impulse Response

● Impulse response: exponential decaysβ—‹ 𝑦 0 = π‘₯ 0 = 1β—‹ 𝑦 1 = π‘₯ 1 + π‘Ž % 𝑦 0 = π‘Žβ—‹ 𝑦 2 = π‘₯ 2 + π‘Ž % 𝑦 1 = π‘Ž"

β—‹ 𝑦 3 = π‘₯ 3 + π‘Ž % 𝑦 2 = π‘Ž"

β—‹ …

● Stability Issue! β—‹ If π‘Ž < 1, the filter output converges (stable)β—‹ If π‘Ž = 1, the filter output oscillates (critical)β—‹ If π‘Ž > 1, the filter output diverges (unstable)

β„Ž(𝑛)=[1, π‘Ž, π‘Ž#, π‘Ž*, … ]

Page 30: Fundamentals of DSP -Part 1 - KAIST

A Simple Feedback Lowpass Filter: Sine-Wave Analysis

● Measure the amplitude and phase changes given a sinusoidal signal input

π‘Ž = 0.9

Transient state Steady state Transient state

Page 31: Fundamentals of DSP -Part 1 - KAIST

A Simple Feedback Lowpass Filter: Frequency Response

● More dramatic change than the simplest lowpass filter (FIR)β—‹ Phase response is not linear

𝑦 𝑛 = π‘₯ 𝑛 + 0.9 0 𝑦(𝑛 βˆ’ 1)

Page 32: Fundamentals of DSP -Part 1 - KAIST

A Simple Feedback Lowpass Filter: Frequency Response

● Mathematical approachβ—‹ Use complex sinusoid as input: π‘₯ 𝑛 = 𝑒#$)

β—‹ 𝑦 𝑛 = 𝐺 πœ” 𝑒#($)01 $ ) Γ  𝑦 𝑛 βˆ’ π‘š = 𝑒*#$2𝑦 𝑛 for any π‘šβ—‹ The output is: 𝑦 𝑛 = π‘₯ 𝑛 + π‘Ž % 𝑦(𝑛 βˆ’ 1)

𝑦 𝑛 = π‘₯ 𝑛 + π‘Ž % 𝑒#$𝑦 𝑛

β—‹ Frequency response: 𝐻 πœ” = !!*345#$!

= !!*34678 $ 034#489: $

β—‹ Amplitude response: 𝐻(πœ”) = !(!*34678 $ )"0(3489: $ )"

β—‹ Phase response: ∠𝐻 πœ” = βˆ’atan( 3489: $!*3 4678 $

)

● Note that the this approach is getting complicated

Page 33: Fundamentals of DSP -Part 1 - KAIST

Reson Filter

● Difference equationβ—‹ 𝑦 𝑛 = π‘₯ 𝑛 + 2π‘Ÿ % cos(πœƒ) % 𝑦 𝑛 βˆ’ 1 βˆ’ π‘Ÿ" % 𝑦 𝑛 βˆ’ 2

● Signal flow graph

𝑧!"

π‘₯ 𝑛 𝑦 𝑛+

2π‘Ÿ 0 cosπœƒ+

𝑧!"βˆ’π‘Ÿ#

+

Page 34: Fundamentals of DSP -Part 1 - KAIST

Reson Filter

● Mathematical approachβ—‹ Use complex sinusoid as input: π‘₯ 𝑛 = 𝑒#$)

β—‹ 𝑦 𝑛 = 𝐺 πœ” 𝑒#($)01 $ ) Γ  𝑦 𝑛 βˆ’ π‘š = 𝑒*#2$𝑦 𝑛 for any π‘šβ—‹ The output is: 𝑦 𝑛 = π‘₯ 𝑛 + 2π‘Ÿ % cos(πœƒ) % 𝑦 𝑛 βˆ’ 1 βˆ’ π‘Ÿ" % 𝑦 𝑛 βˆ’ 2

𝑦 𝑛 = π‘₯ 𝑛 + 2π‘Ÿ % cos(πœƒ) % 𝑒#$𝑦 𝑛 βˆ’ π‘Ÿ" % 𝑒#"$𝑦 𝑛 βˆ’ 2β—‹ Frequency response

β–  𝐻 πœ” = !!*";4678(1)45$!0;"45$"!

= !(!*;(678 1 0#489: 1 )5$!)(!*;(678 1 *#489: 1 )5$!)

● Now you see that the this approach is getting even more complicated β—‹ We will introduce more intuitive method to obtain the frequency response

Page 35: Fundamentals of DSP -Part 1 - KAIST

Reson Filter: Frequency Response

● Generate resonance at a particular frequency β—‹ Control the peak height by π‘Ÿ and the peak frequency by πœƒ

For stability: π‘Ÿ < 1

π‘Ÿ < 1 is getting close to 1

πœƒ controls the location of the peak

Page 36: Fundamentals of DSP -Part 1 - KAIST

Infinite Impulse Response (IIR) Filters

● Difference equationβ—‹ 𝑦 𝑛 = 𝑏+ % π‘₯ 𝑛 βˆ’ π‘Ž! % 𝑦 𝑛 βˆ’ 1 βˆ’ π‘Ž" % 𝑦 𝑛 βˆ’ 2 βˆ’β‹―βˆ’ π‘Ž< % 𝑦 𝑛 βˆ’ 𝑁

● Signal flow graphπ‘₯ 𝑛 +

𝑏%𝑧!"

𝑦 𝑛

𝑧!"

𝑧!"

...

-π‘Ž"

-π‘Ž#

-π‘Ž&

𝑦 𝑛 βˆ’ 1

𝑦 𝑛 βˆ’ 2

𝑦 𝑛 βˆ’ 𝑁

Page 37: Fundamentals of DSP -Part 1 - KAIST

General Form of LTI Filters

● Difference equationβ—‹ 𝑦 𝑛 = 𝑏+ % π‘₯ 𝑛 + 𝑏! % π‘₯ 𝑛 βˆ’ 1 + 𝑏" % π‘₯ 𝑛 βˆ’ 2 + …+ 𝑏, % π‘₯ 𝑛 βˆ’ 𝑀

βˆ’π‘Ž! % 𝑦 𝑛 βˆ’ 1 βˆ’ π‘Ž" % 𝑦 𝑛 βˆ’ 2 βˆ’β‹―βˆ’ π‘Ž< % 𝑦 𝑛 βˆ’ 𝑁

● Signal flow graph

𝑧!"

π‘₯ 𝑛 +

𝑧!"

𝑧!"

...

𝑏"

𝑏#

𝑏$

𝑏%

π‘₯ 𝑛 βˆ’ 1

π‘₯ 𝑛 βˆ’ 2

π‘₯ 𝑛 βˆ’π‘€

𝑧!"

𝑦 𝑛

𝑧!"

𝑧!"

...

-π‘Ž"

-π‘Ž#

-π‘Ž&

𝑦 𝑛 βˆ’ 1

𝑦 𝑛 βˆ’ 2

𝑦 𝑛 βˆ’ 𝑁