Upload
others
View
7
Download
0
Embed Size (px)
Citation preview
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Paris Smaragdis [email protected]
paris.cs.illinois.edu
CS 498PS – Audio Computing Lab
Filtering
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Overview
• Filters! • What are they? • How do we make them? • How do we apply them? • When do we use them?
• Common uses of filters • Source/filter models • Subtractive synthesis • Room simulation
2
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Convolution
• Operation between two time series
• Formal definition:
• Commutative:
3
z= y∗ xz(t)= y(0)⋅x(t)+ y(1)⋅x(t−1)+!z(t)= y(i)⋅x(t− i)
i∑
z= y∗ x= x∗ y
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Intuitive explanation
• For each sample of y shift x to that sample’s position, scale it by y’s value, and add to output • Note that this results in a longer output than input
• Output length is length( x) + length( y) – 1
4
x 1 2 3
y 1 0 0 2 0 3 0 0
y ✻ x 1 2 3 2 4 9 6 9 0 0
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Some examples
5
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Convolution ⟶ FIR Filters
• When convolving you apply an FIR filter
• Finite Impulse Response • They have a “finite” effect in time
• i.e. won’t echo infinitely
• “Passive” filters • They remove energy, but cannot add any
• As a result they are very stable
6
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Filter characteristics
• Passband & stopband • Passband: Frequency range where there is no (big) suppression • Stopband: Frequency range where there is suppression
• Transition band • Frequency range in-between passband and stopband
• Cutoff frequency • Point of 3dB down from passband in the transition band
• Ripples • Deviations from desired levels
7
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Filter frequency response
8
Passband Stopband
Transition band
Passband ripples
Stopband ripplesCutoff
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Filter types by response
• Four main types depending on their passband • Can serve as building blocks of other filters
• Arbitrary filter responses • Customized to serve a specific task
• E.g. denoising, dehumming, dehissing, etc.
9
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Lowpass filters
• Pass only the low frequencies • Main parameter: Cutoff frequency • Used to remove high frequency elements (hiss, noise, etc)
10
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Example
11
📻 📻
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Highpass filters
• Lets only high frequencies pass • Specified by cutoff frequency again • Useful for removing low frequency things (rumbling, hum, etc).
12
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Example
13
📻
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Bandpass filters
• Pass a contiguous range of frequencies • Two parameters: center frequency and bandwidth
• Or the two bounding frequencies
14
Center frequency
Bandwidth
Lower frequency
Upper frequency
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Example
15
📻
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Bandreject (or bandstop) filters
• Suppress a contiguous set of frequencies • The opposite of a bandpass filter
• Very useful for removing things (e.g. 60Hz hum)
16
Center frequency
Bandwidth
Lower frequency
Upper frequency
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Example
17
📻
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Shelving filters
• Typically lowpass or highness • Equally suppress all frequencies in stopband
18
Shelving – flat stopband
Non-shelving – non-flat stopband
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Arbitrary responses
• More complex cases • Often used to resolve specific issues (more later)
19
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
So what do we convolve with?
• FIR filters are time series too • Usually too short to listen to
• Filter design • How do we get these filtering time series?
20
📻 📻 📻 📻 📻 📻Lowpass 0.5 Lowpass 0.2 Lowpass 0.7 Highpass 0.5 Highpass 0.2 Highpass 0.7
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Two views of a filter
21
Tim
e dom
ain
Freq
uenc
y do
mai
n
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Frequency response
• Taking the DFT of the filter gives us its response:
• Why is this useful? • If we have a desired response we can get the filter coefficients!
• By using the inverse DFT
22
X= DFT x( )⇒ X[k]= x[n]e− j2πnk/Nn=0
N−1
∑
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Designing a lowpass filter
• The ideal response is a step function
• The inverse DFT of the step function is the sinc function • You can find that analytically
23
X[k]= 1, if k<ω0, otherwise
⎧⎨⎪⎪
⎩⎪⎪
x[n]= IDFT X( )⇒ x[n]=ω sin πnω( )πnω
= ω sinc nω( )
ω
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
How does that look?
• If we generate the sinc it looks like a lowpass filter!
24
Hey, that’s not a step function!
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Some tradeoffs
• The longer the filter the better the response
25
Hey, what’s with the ripples?
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
One more thing to do …
• Remember why this is bad?
• We need to taper the sinc to smooth its spectrum • Same thing that we did with the spectrogram • Tradeoff: Dampens the ripples, but widens the filter
26
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Windowing the filter
• Results in a more palatable response
27
Slower transition, but smaller ripples
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Designing a highpass filter
• Subtract a lowpass from the input • Highpassed = input – lowpass( input)
28
x[n]=1−ω sinc nω( ), if n= 0−ω sinc nω( ), if n≠ 0
⎧
⎨⎪⎪⎪
⎩⎪⎪⎪
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Designing a bandpass filter
• To get a bandpass the coefficients are:
29
x[n]= 2cos πnωc( )ωb sinc ωbn( )
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Designing a bandreject filter
• Remove bandpassed signal from input • Just like with the highpass filter design
30
x[n]=1−2cos πnωc( )ωb sinc ωbn( ), if n= 0−2cos πnωc( )ωb sinc ωbn( ), if n≠ 0
⎧
⎨⎪⎪⎪
⎩⎪⎪⎪
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
• Take desired response, IDFT it, window it
General filter design: The window method
31
a)
b) c)
d)
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
And there’s much more
• Frequency sampling method • Define frequency points and their response
• Optimization methods • E.g. Parks-McClellan, linear programming, etc. • Specify constraints and get optimal filter via optimizer
• Remember there will always be tradeoffs
• Look into scipy.signal, lots of filter things to see
32
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
IIR filters
• Infinite Impulse Response • They don’t get applied with plain convolution • Use feedback to produce infinitely long effects
• Can be powerful but also numerically unstable
• Implemented using:
33
y[n]= 1ao
bix[n− i]+ ai y[n−1]i=0
N
∑i=0
M
∑⎡
⎣⎢⎢
⎤
⎦⎥⎥
FIR coefficients bi convolved with past inputs
Additional coefficients ai applied on past outputs
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Butterworth filters
• My favorite IIR filters for audio • Maximally flat passband • Gradual stopband, steeper with higher orders
34
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Chebyshev Type I
• Flat stopband, ripples in passband • User specifies cutoff and allowable ripples in passband
• Tradeoff: Larger ripples ⟶ steeper transition band
35
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Chebyshev Type II
• Flat passband, ripples in stopband • Similar to Chebyshev Type I, but flip pass/stopband
• Larger ripples allow for steeper transition band
36
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Elliptic filters
• Specify both passband and stopband ripples • Parameters: Cutoff, passband rippling, stopband rippling • More suppressed ripples ⟶ less steep transition band
37
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Allpass filters
• No changes in the magnitude • But can produce a phase delay • Useful for delay effects (more later)
38
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
The Q-factor
• Indicates the amount of resonance in a filter • Ratio of center frequency to bandwidth
• Higher Q means a sharper filter peak • Widely used in rock and dance music
• Wah-wah guitars, filtered synth pads
39
📻
📻Filters with increasing Q
High-Q changing cutoff
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
The source-filter model
• Very popular sound generation model • Used in speech, music, acoustics, …
• Source: • A frequency-rich sound
• Filter: • A filter that shapes the source as needed
40
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Vocal tract model
• Source • Vocal tract waveform
• Filter • Resonances of vocal cavities
• Uses • Speech synthesis • Singing synthesis • Demo!
41Go to https://dood.al/pinktrombone/ for more speech synthesis fun
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Modeling of musical instruments
• Source • Excitation of a string/membrane/airflow/…
• Filter • The resonances of the instrument body
• Piano/violin/guitar body, etc.
• Popular in physical modeling of music
42
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Subtractive synthesis
• Analog synthesizers (and modern emulators) • Source: spectrally rich waveforms (square, sawtooth, noise) • Filter: Sweeping high-Q filters
43
📻
📻
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Room simulations
• Using convolutions for delays • When spaced far apart filter coefficients sound like echoes
• Modeling room reflections • We can model room reverberations • Convolve with delaying noise • Convolve with actual room response
• Examples:
44
📻 📻📻
📻📻
📻📻
InputDecaying noise Masonic lodge Musikvereinsaal
📻📻
Large hall
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Equalizers
• Banks of filters that cover entire frequency range
45
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Graphical EQs
• Each slider is a narrow bandpass filter
46
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Designing a graphical EQ
• Lowpass shelving filter for first band • Highpass shelving filter for last band • Bandpass filters for all others
• Make sure they collectively sum to 1
47
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Parametric EQs
• Limited number of configurable filters • Set frequency peak and width
• Allows for precise filter tuning • Useful for solving resonance problems
• e.g. mic feedback
48
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Designing a parametric EQ
• Easy! • Lowpass filter / bandpass filter(s) / highpass filter • Have adjustable parameters for each
• Cutoffs, Q, filter type, etc.
• By tuning the filters you can approximate arbitrary frequency responses
49
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Why do we need EQs?
• Enthusiasts • Change music characteristics (e.g. boomy bass) • To make up for deficiencies in speakers
• Sound engineers • Top compensate for poor acoustics or feedback • To enhance the quality of recorded sound
• Hearing devices • To compensate for hearing loss in various frequency bands
50
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Fast convolution
• Convolution is very slow! • Lots of multiplications
• It can be sped up significantly using the FFT • Perform convolution in the frequency domain • Complexity drops to 2 N log2 N
• But is this efficient as shown above?
51
z[t]= y[i]⋅x[t−1]i∑
z= x∗ y⇔ DFT z( )= DFT x( )⊙DFT y( )
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Overlap-add fast convolution
• Similar to spectrograms • Step 1: Make frames
• Zero pad! • Do not window
• Step 2: Convolve frames using FFTs • i.e. multiply complex spectra
• Elementwise multiplication • Step 3: Invert back to time
• Use overlap and add!
52
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
What’s happening here?
53
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Cross synthesis
• Fun with crossing spectra of different sounds • Source: A sound with rich spectral content • Filter: The spectrum of a duller sound • Multiply spectra in the frequency domain • Invert back to time domain
• Often referred to as the vocoder • Demo!
54
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Taking shortcuts
• Sometimes it is easier to apply custom filters by directly manipulating spectrograms • E.g. remove unwanted frequencies
• Don’t do that for simple filters • It isn’t technically correct and can produce artifacts • But it can also save you time by making things simpler
55
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Recap
• Overview of filtering for sound • Types of filters • Filter design • Filter parameters
• Common filter uses • Removal of interference • Equalizers • Source/filter models
56
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Reference material
• Intro to digital filters with audio applications • https://ccrma.stanford.edu/~jos/fp/
• If you are brave read all of it! • But we only need this as reference for now
57
UN
IVE
RS
ITY
OF
IL
LIN
OIS
@ U
RB
AN
A-C
HA
MP
AIG
N
Next lab on Thursday
• Filter design • How to design and measure basic filters
• Filter usage • How to use filters to remove unwanted elements • How to produce various sounds using filters
58