<<

Flanger

Music 206: and Digital Filters II flanger Tamara Smyth, [email protected] • The well known is a feedforward comb filter Department of Music, with a time-varying delay M(n) (see flanging.mov). University of California, San Diego (UCSD) • , used in recording studios since the 1960s, April 18, 2019 creates a rapidly varying high-frequency sound by adding a signal to an image of itself that is delayed by a short, variable amount of time. • Flanging was accomplished in analog studios by summing the outputs of two tape machines playing the same tape.

flange

y(n)

Figure 1: Two tape machines are used to produce the flanging effect.

• By touching (and releasing) the flange on one supply reel, it would s to slow it down (and speed it up).

1 Music 206: Delay and Digital Filters II 2

Flange Comb Filter Parameters Fractional Delay using Linear Interpolation

• The flange simulation is a feedforward comb filter, where the delay M(n) is a function of time, • For a successful flanging effect, M(n) must change smoothly over time: y(n)= x(n)+ gx(n − M(n)). – M(n) should not have jumps in values associated – coefficient g (DEPTH parameter), determines the with rounding to the nearest integer. prominence of the flanging effect. • One of the simplest ways to handle fractional delay is – flange is typically swept from a few milliseconds to by using linear Interpolation: 0 to produce characteristic “flange” sound. – the linear interpolator effectively “draws a line” • The time-varying delay can be handled by modulating between neighbouring samples, and returns the M(n) with a low-frequency oscillator (LFO) sinusoid: appropriate value on that line. M(n)= M [1 + A sin(2πfnT )], 0 Linearly interpolating to obtain value at sample 8.25 1

where 0.5

f , rate or speed of the flanger, in Hz 0 amplitude A , “excursion” (maximum delay swing) −0.5

−1 , 7.5 8 8.25 8.5 9 9.5 M0 average delay length controlling the time (samples)

average notch density. Figure 2: Linear Interpolation.

Music 206: Delay and Digital Filters II 3 Music 206: Delay and Digital Filters II 4 Linear Interpolation (Implementation) Tapped Delay Line

• The fractional part of the delay, δ, effectively • A tap refers to the extraction of the signal at a determines how far to go along the line between certain position within the delay-line. samples. • The tap may be interpolating or non-interpolating, • A fractional delay xˆ(n − (M + δ)), reads from the and also may be scaled. delay line at neighbouring delays M and M +1, and • A tap implements a shorter delay line within a larger takes the weighted sum of the outputs: delay line.

xˆ(n−(M +δ)) = (1−δ)x(n−M)+δx(n−(M +1)), −M1 −(M2−M1) x(n) z z y(n)= x(n − M2)

where M is the integer and δ is the fractional part. b1

• Notice that if δ =0, the fractional delay reduces to b1x(n − M1)

the regular integer delay. Figure 3: A delay line tapped after a delay of M1 samples. • Linear interpolation in a circular delay line (Matlab):

if (outPtr==1) z = (1-delta)*dline(outPtr) + delta*dline(Mmax); else z = (1-delta)*dline(outPtr) + delta*dline(outPtr-1); end

Music 206: Delay and Digital Filters II 5 Music 206: Delay and Digital Filters II 6

Multi-Tap Delay Line Example

• Multi-Tapped delay lines efficiently simulate multiple • A Chorus is produced when several musicians play echoes from the same source signal. simultaneously, but inevitably with small changes in the amplitudes and timings between each individual’s x(n) z−M1 z−(M2−M1) z−(M3−M2) sound. b0 b1 b2 b3

y(n) • The chorus effect is a signal processing unit that

Figure 4: A multi-tapped delay with length M3. changes the sound of a single source to a chorus by implementing the variability occurring when several

• In the above figure, the total delay line length is M3 sources attempt to play in unison. samples, and the internal taps are located at delays of M1 and M2 samples, respectively. • The output signal is a linear combination of the input signal x(n), the delay-line output x(n − M3), and the two tap signals x(n − M1) and x(n − M2). • The difference equation is given by

y(n)= b0x(n)+b1x(n−M1)+b2x(n−M2)+b3x(n−M3) • Convolution is equivalent to tapping a delay line every sample and multiplying the output of each tap by the value of the impulse response for that time.

Music 206: Delay and Digital Filters II 7 Music 206: Delay and Digital Filters II 8 Chorus Implementation A Simple Recursive (IIR) Filter

• A chorus effect may be efficiently implemented using • Using FIR filters to reproduce a desired frequency a multi-tap fractional delay line: response often requires a very high-order filter, i.e., a greater number of coefficients and more computation. – taps are not fixed and usually range from 10 to 50 ms. • It is often possible to reduce the number of – their instantaneous delay may be determined using feedforward coefficients by introducing feedback a random noise generator or, as in the flanger, a coefficients. Low Frequency Oscillator (LFO). • A simple first-order recursive low-pass filter is given by y(n)= x(n)+ .9y(n − 1)

g1 z−M1(n) y(n) = x(n)+x(n−1) 1

0.8 g2 M2(n) z y(n) 0.6

x(n) 0.4

g3 0.2 amplitude (linear) −M3(n) z 0 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5

y(n) = x(n)+.9y(n−1) g4 1 z−M4(n) 0.8

0.6

Figure 5: A bank for variable delay lines realize the chorus effect. 0.4

0.2 amplitude (linear)

0 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 • The chorus is similar to the flanger, only there are frequency (normalized) multiple delayed copies of the input, and the delay times are typically longer (where a flanger is about Figure 6: The spectral magnitude of the first-order FIR and IIR (recursive) lowpass filters. 1-10 ms, a chorus is about 10-50 ms).

Music 206: Delay and Digital Filters II 9 Music 206: Delay and Digital Filters II 10

The General Difference Equation for The Simple Feedback Comb Filter LTI filters

• What happens when we multiply the output of a delay • The general difference equation for LTI filters includes line by a gain factor g then feed it back to the input? feedback terms, and is given by x(n) y(n) g y(n)= b0x(n) + b1x(n − 1) + ··· + bM x(n − M) z−M − a1y(n − 1) −···− aN y(n − N) Figure 7: The signal flow diagram of a comb filter. • This can be implemented in Matlab using the filter function: • The difference equation for this filter is B = ...; % feedforward coefficients y(n)= x(n)+ gy(n − M), A = ...; % feedback coefficients y = filter(B, A, x); • If the input to the filter is an impulse • Matlab specifies coefficients according to the filter x(n)= {1, 0, 0,...} transfer function and NOT the difference equation: the output (impulse response) will be ... – all feedback coefficients (except the first) have a 1 sign opposite to that in the difference equation;

– this is explained by moving the y terms in the Amplitude g g2 difference equation to the left of the equal sign (a g3 step in arriving at the filter transfer function): 0 τ Time (s)

y(n)+a1y(n−1)+··· = b0x(n)+b1x(n−1)+··· Figure 8: Impulse response for filter y(n)= x(n)+ gy(n − M), where τ = M/fs.

Music 206: Delay and Digital Filters II 11 Music 206: Delay and Digital Filters II 12 Effect of Feedback Delay Effect of the Feedback coefficient g

• Since the pulses are equally spaced in time at an • Coefficient g is the depth parameter, where values interval equal to the loop time τ = M/fs seconds, it closer to 1 yield more extreme maxima and minima. is periodic and will sound at the frequency f0 =1/τ. Comp filter feedback coefficient g = 0.1 1

0.8

0.6

0.4

Simple feedback comp filter where feedback coefficient g = 0.8 Amplitude 0.2 1 0 0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05 Time (s) 0.8 0

−5 0.6 −10

−15

0.4 −20 Magnitude (dB)

Amplitude −25 0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05 0.2 Frequency (H) Comp filter feedback coefficient g = 0.5 1

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

Time (s) 0.4

Amplitude 0.2

0 0 0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05 Time (s)

0

−5 −5

−10 −10 −15 −20 Magnitude (dB) −25 0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05 −15 Frequency (H)

Magnitude (dB) Comp filter feedback coefficient g = 0.9 1

0.8 −20 0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05 0.6 Frequency (H) 0.4 Amplitude 0.2

0 0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05 Time (s) Figure 9: Impulse and magnitude response of a comb filter with feedback g = 0.8. 0 −5

−10

−15

−20 Magnitude (dB) −25 0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05 Frequency (H) • The spacing between the maxima of the “teeth” is

equal to the natural frequency f0. Figure 10: Impulse and Magnitude Response with increasing feedback coefficient.

Music 206: Delay and Digital Filters II 13 Music 206: Delay and Digital Filters II 14

Feedback Comb Filter Decay Rate Obtaining a desired T60

• The response decays exponentially as determined by • The T60 is the time to decay to an inaudible level of the loop time and gain factor g (values near 1 yield -60 dB or by 0.001 on a linear scale. longer decay times). • Given a loop time of M samples (frequency f0) and a desired T60, what should be the value of g? f0 = 220.5 Hz, T60 = .25 (blue), .5 (green), and .75 (red) seconds • If the loop has a delay of M samples, the number 3 of trips through the loop after n samples, or after t seconds is 2.5 n tfs = = tf0, 2 M M

where f0 is the fundamental frequency of the loop. 1.5 • Attenuation at time t is given by

1 Amplitude (offset for comparison) α(t)= gtf0.

0.5 • At time t = T60, the attenuation is 0.001,

0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 T60f0 T60fs/M Time (s) α(T60)= g = g =0.001, and solving for g yields Figure 11: Comb filter impulse responses with a changing the decay rate. g =0.001M/(fsT60).

Music 206: Delay and Digital Filters II 15 Music 206: Delay and Digital Filters II 16 General Comb Filter A very simple string model

• Combining both the feedforward and feedback comb • A very simple string model can be implemented using filter yields the general comb filter, given by the a single delay line and our simple first-order low pass difference equation filter H(z) to model frequency-dependent loss. y(n) y(n − N) y(n)= x(n)+ g1x(n − M1) − g2y(n − M2) z−N

where g1 and g2 are the feedforward and feedback coefficients, respectively. H(z) g 1 Figure 14: A very simple model of a rigidly terminated string. z−M1

x(n) y(n) • Though losses are distributed along the length of the g2 string, in an LTI system they may be lumped to a −M2 z single observation point and approximated with H(z). Figure 12: Signal flow diagram for digital comb filters. • Different quality string sounds can be created by changing this filter. 1.5

1 • This model may be interpreted as a feedback comb 0.5 filter with lowpassed feedback or a simplified digital 0 waveguide model. Amplitude −0.5 plucked −1 • How is this model excited? How is the string ? 0 5 10 15 20 25 30 35 40 Time (s)

Figure 13: Comb Filter Impulse Response.

Music 206: Delay and Digital Filters II 17 Music 206: Delay and Digital Filters II 18

Karplus-Strong Pluck String Controlling Karplus-Strong

• When the delay-line initial conditions consist of white • Controlling Dynamics: noise, the algorithm is known as the Karplus-Strong – Limit the range of random numbers—change the algorithm. Matlab line • White noise is a sequence of uncorrelated random xn = 2*(x-0.5); % uniform between -1 and 1 values. It can be generated in Matlab as follows: – Filter the white noise serving as the initial N=...; %lengthofvector conditions. The cut-off frequency of the filter will control the effective dynamic level (since acoustic y = randn(1, N); % N samples of Gaussian white noise % with zero mean and unit variance instruments are usually brighter at louder dynamic levels). x = rand(1, N); % N samples of white noise, % uniform between 0 and 1 • Sounding frequency (pitch)

xn = 2*(x-0.5); % uniform between -1 and 1 – Change the delay line length, where

• Filling the delay line with white noise is akin to f0 = fs/(N +1/2). plucking the string with a random initial – The 1/2 term in the denominator is due to the displacement—a very energetic excitation. low-pass filter’s phase delay of 1/2 sample. • What are the control parameters of this model? – Notice that the delay-line length is of an integer size. This limits the resolution of possible sounding frequencies.

Music 206: Delay and Digital Filters II 19 Music 206: Delay and Digital Filters II 20 Limits of integer-length delay lines Frequency-dependent decay rate

• At low frequencies (large N), this is less of a problem, • Another (control) problem with KS is that, because of but becomes increasingly problematic at higher the low-pass filter in the feedback loop, the decay rate frequencies when delay-line lengths are small and a is dependent on frequency. single sample delay can make a bigger difference.

Plucked string at 441 Hz 1 Integer Delay Frequency Quantization 8000 0.8 7000 0.6

6000 0.4 Amplitude

5000 0.2

4000 0 0 0.5 1 1.5 2 2.5 3 3000 Time (s) Quantized Frequency (Hz) Plucked string at 882 Hz 2000 1

1000 0.8

0 0.6 0 1000 2000 3000 4000 5000 6000 7000 8000 Desired Frequency (Hz) 0.4 Amplitude

Figure 15: As the desired frequency gets higher, it is quantized to fewer possible values; 0.2 there is a single frequency value for all desired frequencies between 7000 and 8000 Hz. 0 0 0.5 1 1.5 2 2.5 3 Time (s)

Figure 16: Decay rate is faster at higher frequencies. • Example: at fs = 44100, – to obtain a frequency of 882 Hz, a delay of fs/882 = 50 samples is required; • This behaviour is generally desired, as it is a – the next highest possible frequency with an integer characteristic of acoustic systems, but it is too number of samples is fs/49 = 900 Hz. extreme in the KS.

Music 206: Delay and Digital Filters II 21 Music 206: Delay and Digital Filters II 22

Selected Smith and Jaffe Extensions Tuning

• In a paper by Smith and Jaffe (Computer Music • For large N (low pitches) the difference between N Journal, Summer 1983) extensions to the and N +1 is slight, but becomes increasingly Karplus-Strong are developed in a musical context: noticeable for small N (high pitches). – Tuning (fractional delay) using allpass filters as an • Recall, the fundamental frequency (which is inversely alternative to linear interpolation proportional to the period) is given by

– Decay rate shortening and stretching 1 fs f1 , = – Dynamics (N +1/2)Ts N +1/2 – Plucking position which may be expressed more generally in terms of – Rests at the ends of notes (i.e. turning off the phase delay of our feedback filter:

algorithm without hearing a click) fs f1 = . – Glissandi and Slurs N + Pa(f1) – Sympathetic String Simulation • We need to introduce a filter into the feedback loop • Find paper here that can contribute a small delay without alterning the loop gain. • What kind of filters can introduce a frequency-dependent delay without having an effect on gain?

Music 206: Delay and Digital Filters II 23 Music 206: Delay and Digital Filters II 24 First-Order Allpass Filter Phase delay of First-Order Allpass Filter

• The first-order allpass filter has difference equation • The low-frequency phase delay may be approximated y(n)= Cx(n)+ x(n − 1) − Cy(n − 1), by 1 − C where |C| < 1 for stability. P (f) ≈ . c 1+ C • The phase delay, unlike the 2-point averager, is dependent on frequency. • The filter coefficient C may be solved as a function of the desired phase delay Pc(f). First order allpass with c = .9 0 1 − P (f) C ≈ c . −0.5 1+ P (f) −1 c −1.5 • Notice if P =0 then C =1: −2 c

phase (radians) −2.5 – this produces a pole-zero cancellation on the unit −3 0 5 10 15 20 circle which can cause an unstable filter due to

Low−frequency phase response (up to omega = 1) round-off errors! 0

−0.01 – thus, the one-sample delay control is shifted to

−0.02

−0.03 ǫ ≤ Pc ≤ (1 + ǫ).

−0.04

phase (radians) −0.05

−0.06 0 1 2 3 4 5 6 7 frequency (kHz)

Music 206: Delay and Digital Filters II 25 Music 206: Delay and Digital Filters II 26

Setting the Allpass Phase Delay to a Attenuation of “Harmonics” Desired Frequency

• On each pass through the delay-line loop, a partial at • A fundamental frequency f1 has a corresponding frequency f is subject to an attenuation equal to the period of loop amplitude response |H(ωT )|. P = f /f samples. 1 s 1 • The frequency response H(ωT ) of the simple lowpass • The model should then have a phase delay of filter may be found by testing with a complex sinusoid x(n)= eωnT : N + Pa(f1)+ Pc(f1)= P1 samples. y(n) = x(n)+ x(n − 1) where P (f )=1/2 for the two-point averager. a 1 = ejωnT + ejω(n−1)T • The delay line length N becomes = ejωnT + ejωnT e−jωT −jωT ωnT N , Floor(P1 − Pa(f1) − ǫ), = (1+ e )e −jωT where ǫ is a number much less than 1, that was used = (1+ e )x(n), jωT −jωT to shift Pc(f1)’s one-sampe delay range above 0 to 1. where H(e )=(1+ e ). • The fractional phase delay (in samples) for the allpass • The gain of the filter is given by interpolator becomes G(ω) = |H(ejωT )| , −jωT Pc(f1) P1 − N − Pa(f1). = |(1 + e )| = |(ejωT/2 + e−jωT/2)e−jωT/2| = |2cos(ωT/2)e−jωT/2| = 2cos(ωT/2)

Music 206: Delay and Digital Filters II 27 Music 206: Delay and Digital Filters II 28 Loop Attenuation at frequency f1 Solving for corresponding time constant

• The gain of the low-pass filter at frequency f is • The time constant τ is the time to decay by 1/e. Ga(f) = cos(πfTs). • To solve for τf , the time constant for frequency f, • After M passes through the delay-line loop, a partial −t/τf at frequency f is subject to attenuation αf (t) = e t M ln αf (t) = − (take log of both sides) cos(πfTs) . τf t • Since the round-trip time in the loop is N +1/2 τ = − f ln α (t) samples, the number of trips through the loop after n f t samples (n = tfs) is given by = − seconds tf1 ln (cos(πfTs)) n tfs 1 M = = = tf1. = − seconds N +1/2 N +1/2 f1 ln(cos(πfTs)) (N +1/2) Ts • The attenuation factor at time t = nTs is given by = − seconds. ln(cos(πfTs)) tf1 αf (t) , cos(πfTs) . • That is, a partial or harmonic of frequency f, having an initial amplitude of A at time 0, will have amplitude Aαf (t) at time t seconds.

Music 206: Delay and Digital Filters II 29 Music 206: Delay and Digital Filters II 30

Attenuation and Decay with General Relating to the T60 Loss Filter

• For audio/music, it is more useful to define the time • The filter accounting for frequency-dependent loss constant as the time it takes to decay -60dB, or 0.001 may be other than a two-point averager. times the initial value.

• A general presentation of the attenuation factor for • The attenuation factor at time t = T60(f) is given by the kth harmonics is given by αf (T60(f))=0.001. tfs N+P (f ) αk(t)= Ga(fk) a k , • Conversion from τ to T60 is done by and the decay for each harmonic becomes 0.001 = e−T60/τ N + Pa(fk) T60 τk = − , ln(0.001) = − fs ln Ga(2πfkTs) τ T60 = − ln(0.001)τ where Ga(fk) and Pa(fk) are the gain and phase ≈ 6.91τ delays, respectively, of the filter used.

Music 206: Delay and Digital Filters II 31 Music 206: Delay and Digital Filters II 32 Decay of non-harmonics Decay-time Shortening

• The previous analysis describes the attenuation due to • To shorten the decay time, a loss factor of ρ can be “propagation” around the loop. introduced in the feedback loop, yielding • Sinusoids that do not “fit” into the loop, are quicky y(n − N)+ y(n − (N + 1)) y(n)= x(n)+ ρ destroyed by self interference. 2

12 • The amplitude envelope of a sinusoid at frequency f,

f 10 0 is now proportional to

8 2f tf1 tf1 0 αf (t,ρ)= |ρ cos(πfTs)| = |ρ| αf (t).

6 3f 0 and the decay-time constant for the fundamental

4 Amplitude frequency becomes 4f 0 2 1 τ (ρ)= − . 0 5f 1 0 f1 ln |ρ cos(πf1Ts)|

−2 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 Time (s) • Note that ρ cannot be used to lengthen the decay

Figure 17: Destructive interference occurs at odd harmonics of the fundamental frequency. time, since the amplitude at 0 Hz would increase exponentially. •|ρ|≤ 1 if the string is to be stable. • Though the loop is initialized with random numbers, after a very short time the primary frequencies • ρ is used to shorten the low-pitch notes. remaining in the loop are those with an integer number of periods in N + 1/2 samples.

Music 206: Delay and Digital Filters II 33 Music 206: Delay and Digital Filters II 34

Setting ρ for a desired T60 Decay Stretching

• For a desired T60, determine the corresponding time • To stretch the decay, and reduce the lowpass effect at constant τ high frequencies, the simple lowpass can be replaced t τ ≈ 60 . with a two-point weigthed average 6.91 • Use this value in solving for ρ, y(n)=(1 − S)x(n)+ Sx(n − 1), 1 where S, the stretching factor, is between 0 and 1. τ = − f1 ln |ρ cos(πf1T )| • For stability, S can’t be greater than 1. 1 ln |ρ cos(πf1T )| = − • When S =1/2, the filter reduces the the previous f1τ 1 two-point averager. −f τ |ρ cos(πf1T )| = e 1 • When S = 0 or 1, the frequency-dependent term e−1/(f1τ) |ρ| = (delay) disappears, and the gain response is unity for | cos(πf T )| 1 all f. • At intermediate values, 0 < S < 1, the note duration is finite, with a minimum for S =1/2. • The resulting decay time is then a function of loss factor ρ and stretch factor S.

Music 206: Delay and Digital Filters II 35 Music 206: Delay and Digital Filters II 36 Effect of Decay Stretching on Tuning Time constant as a function of S

• Changing S changes the effective loop length as a • Recall the gain of the simple 2-point averager is function of frequency since it changes the phase delay G(ω)= |1+ e−jωT | of the overall loop. • The gain of the weighted 2-point averager is – we must therefore compute Pa(f1) when using the allpass filter fractional delay to tune to the desired G(S; ω) = |(1 − S)+ Se−jωT | frequency. = |(1 − S)+ S[cos(ωT )+ j sin(ωT ))]| • As shown in the paper by Smith and Jaffe, for low = [(1 − S)+ S(cos(ωT )]2 + S2 sin2(ωT )) frequencies relative to the sampling rate, we may use q = (1 − S)2 +2S(1 − S)cos(ωT )+ S2. the approximation p

Pa(f,S) ≈ S, 0 ≤ S ≤ 1. • The time constant is 1 N + S • See this in Matlab: τ = − = − f0 ln(G(S; ω)) fs ln(G(S; ω)) S = .6; [H, omega] = freqz([1-S S], 1);

% start at index 2 to avoid division by 0 mean(angle(H(2:end))./omega(2:end)); • When S =1/2, we have the basic string algorithm.

Music 206: Delay and Digital Filters II 37 Music 206: Delay and Digital Filters II 38