Tperry_Elec484-A3

37
ELEC 484 – Audio Signal Processing Assignment 3: Filter Implementations Wah-wah Filter, Comb Filters, Allpass Filter Prepared for: Peter Driessen ELEC 484 University of Victoria Prepared by: Tim I. Perry V00213455 May, 2009

description

Tperry_Elec484-A3

Transcript of Tperry_Elec484-A3

Page 1: Tperry_Elec484-A3

ELEC 484 – Audio Signal Processing Assignment 3:

Filter Implementations

Wah-wah Filter, Comb Filters, Allpass Filter

Prepared for:

Peter Driessen

ELEC 484

University of Victoria

Prepared by:

Tim I. Perry

V00213455

May, 2009

Page 2: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

1

CONTENTS

PART 1: Wha-Wha Filter Derived from 2nd Order Allpass .......................................................................... 2

PART 2: Convolution of two Audio Files .................................................................................................... 5

PART 3: FIR and IIR Comb Filter Derivations ............................................................................................ 7

PART 4: FIR and IIR Comb Filter Implementation ................................................................................... 13

PART 5: Vibrato Effect .............................................................................................................................. 18

PART 6: Second Order Allpass Filter Design ............................................................................................ 19

PART 7: Second Order Allpass Filter Group Delay ................................................................................... 22

PART 8: Sound Collage Using Filtered White Noise ................................................................................. 24

REFERENCES ........................................................................................................................................... 27

APPENDIX A ............................................................................................................................................. 27

Page 3: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

2

ELEC 484 ASSIGNMENT 3

Implementations of: Wah-wah Filter, Comb Filters, Allpass Filter

PART 1: Wha-Wha Filter Derived from 2nd Order Allpass

The wah-wah filter that was designed in Assignment 2 is implemented here using the second order

bandpass filter on page 43 of the DAFX text. This structure is based on the allpass filter (page 41 of

DAFX). The center frequency is altered often enough to make a smooth sound without noticeable

transients.

For this implementation, the following filter specifications were used:

� sampling rate f_s = 44,100 Hz

� center frequency f_1 = 44,100/32 = 689 Hz

� 3dB bandwidth B = 100 Hz

� implementation uses 2 poles and 2 zeros

Figure 1 is the block diagram for a tuneable second order allpass filter, denoted A(z). From this structure,

the tuning parameters c and d are used to implement a second order BPF.

Figure 1: Second-order allpass filter block diagram [1]

Page 4: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

3

For the allpass filter of Figure 1:

���� = −� + �1 − ����� + ���1 + �1 − ����� − ���� (1)

For a second-order bandpass/bandreject filter:

���� = 12 �1 ∓ ����� �−��� �� − ��� ���

(2)

� = tan ����� ! − 1tan �2���� ! + 1 (3)

= −cos �2��%� ! (4)

Figure 2: Second-order bandpass and bandreject implemented with allpass filter [1]

Therefore, the second order bandpass transfer function can be expressed as:

&�'� = () *( − −+ + ,�( − +�'�( + '�)

( + ,�( − +�'�( − +'�) - (5)

� cut-off frequency (phase = -180˚, group delay is maximum) is controlled

by the coefficient d

� bandwidth is controlled by the coefficient c

Expanding this, we can find the a and b parametric filter coefficients. The ��� terms in the numerator

cancel out, and the resulting form is analogous to the BPF seen in assignment 2:

���� = 12 * �� + 1� − �� + 1����

1 + �1 − ����� − ����-

For constructing the filter in Matlab, the coefficient arrays are implemented as:

b = [(c+1) 0 (c+1)]; a = 2*[(1 d*(1-c) -c];

Page 5: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

4

MATLAB Code for Part 1

%========================================================================== % a3part1_Wha.m Author: Tim Perry % Elec484: DAFX V00213455 % Assignment 3, Part 4 2009-05-21 % % Repeat the wah-wah filter from Assignment 2, but using the second order % bandpass filter on page 43 of the DAFX text (based on the allpass % filter on page 41). Adjust the center frequency of the bandpass often % enough to make a smooth sound without transients. %========================================================================== clear all; close all;

%-------------------------------------------------------------- % Filter specifications and constants %-------------------------------------------------------------- f_s = 44100; f_1 = 689; % Default center frequency (sweep f_c with respect to f_1) B = 100; % 3dB bandwidth is 100 Hz whaSpeed = 0.45; % Multiplier for filter sweep frequency sweepRange = 0.5; % Multiplier for range of filter sweep

%-------------------------------------------------------------- % Read Input Sound file, get sampling freq and bit rate %-------------------------------------------------------------- [audio,f_s,bps]=wavread('white_noise.wav'); % Get length of file in samples numSamples = length(audio);

dN = 50; % increment samples for each loop iteration %---------------------------------------------------------------- % Apply wha-wha filter to audio file %---------------------------------------------------------------- for n = (0:dN:numSamples-1) f_c = f_1*(1 + sweepRange*cos(2*pi*whaSpeed*n/f_s)); % change center

frequency

% allpass filter form (Pg 41-43, DAFX) coefficients d = -cos(2*pi*f_c/f_s); % apply new f_c to d param c = (tan(pi*B/f_s)-1)/(tan(2*pi*B/f_s)+1); % controls the bandwidth

% standard parametric filter coefficients a = 2*[1 d*(1-c) -c]; % update denominator terms b = [(c+1) 0 (c+1)]; % update numerator terms (if bandwidth altered)

y(n+1:n+dN) = filter(b, a, audio(n+1:n+dN)); % filter audio end

y = (1/(2.*max(y))).*y; % lower amplitude to prevent wavwrite clipping wavwrite(y,f_s,bps,'a3pt1-wha.wav');

Page 6: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

5

Figure 3 contains the output spectrums of the above Matlab script, with two different choices for

parameters. The first is a typical sweeping about 689 Hz. The second version uses a higher value of 10 for

the sweep range multiplier variable, and the odd high frequency effects are the result of negative values

for f_c being calculated and then reflected back into the positive frequency spectrum.

Figure 3: Output spectrum for two versions of the Matlab implemented wha filter. Typical paremeters (top), "lazer_wha" parameters with sweep range multiplier set to 10 (bottom). In both cases, the center frequency is clearly time-varying.

PART 2: Convolution of two Audio Files

Tom’s Diner and the flute2.wav were convolved with a high tempo drum sample from an old project of

mine. The result is a rhythmic (and distorted) versions of the original sound files; however, the timbre of

the original drums is not present. It’s apparent that the convolution process mapped the spectral content of

the input file onto the transients of the drum track, such the amplitude of the entire file is increased during

a drum hit. There are certainly comb filtering effects, which in practical applications are an important

element of convolution reverb (subtle, diffuse comb filtering is perceived as ambience).

Waveform images of the convolution results are presented in the figures below. To avoid clipping when

executing the Matlab wavwrite command, the output sample amplitudes were scaled down.

Page 7: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

6

Figure 4: Original Tom's Diner (top), "TransSiberian_Drums.wav" (center), and mono-convoluted and scaled output file (bottom).

Figure 5: Original “flute2.wav” (top), "TransSiberian_Drums.wav" (center), and mono-convoluted and scaled output file (bottom).

Page 8: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

7

PART 3: FIR and IIR Comb Filter Derivations For the FIR comb filter with delay M=4, starting with the difference

equation, derive the transfer function, find the poles and zeros and sketch

them in the z plane, plot the frequency response and the impulse response.

Repeat for the IIR comb filter with delay M=4.

FIR Comb Filter

The FIR comb filter simulates a single delay. The difference equation is given by:

.�/� = 0�/� + 10�/ − 2�

where 2 = 3/56 (6)

Deriving the transfer function for the general case:

7�8� = 9�8� + :9�8 − ;�

<��� = =��� + :=�����>

&�'� = ?�'�@�'� = ( + 1'�2 (7)

For M = 4:

&�'� = ( + 1'�A

���� = �B + :�B

Poles:

There is a 4th order pole at z = 0.

Zeros:

0 = �B + :

� = D−:E

For g = 1, the 4 zeros are at:

z = ± √�� ± j √�

Figure 6 displays the pole-zero plot for the FIR comb filter, as displayed in WinPOI.

Figure 7 displays the frequency response and Figure 8 displays the impulse response with

g chosen to be 0.9. Figure 9 displays the block diagram as described by Equation (7).

Page 9: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

8

Figure 6: FIR comb filter pole-zero plot, magnitude response and phase response

Figure 7: Frequency Response of FIR Comb Filter

Page 10: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

9

Figure 8: Impulse Response of FIR Comb Filter (g chosen to be 0.9)

Figure 9: FIR Comb Filter block diagram

IIR Comb Filter

The IIR comb filter produces an infinite series of responses y(n) to an input signal x(n). The delay

line provides attenuation by g (Figure 10). Additional scaling by c can be applied at the input

signal in order to compensate for the system’s high amplification [1].

Figure 10: IIR Comb Filter block diagram

Page 11: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

10

The difference equation for the IIR comb filter is given by:

.�/� = +0�/� + 1.�/ − 2�

where 2 = 3/56 (8)

Deriving the transfer function for the general case:

7�8� − :7�8 − ;� = �9�8�

<��� − :��><��� = �=���

<����1 − :��>� = �=���

&�'� = ?�'�@�'� = +

( − 1'�2 (9)

For delay M = 4:

���� = <���=��� = �

1 − :��B

���� = ��B�B − :

Poles:

� = D:E

This results in 4 poles in the complex plane with magnitude g at angles of:

0, �2 , �, 3�

2

For stability,

|:| ≤ 1

Zeros:

There is a 4th order zero at z = 0.

The pole-zero plot for the IIR filter is shown in Figure 11. In practice, the poles will lie inside the

unit circle as opposed to right on it. This is to ensure that there is an attenuated signal in the

feedback loop. |:| ≤ 1 is a necessary condition for stability. The pole-zero plot, frequency

response, and impulse response plots are shown below. For the FR and IR plots, the feedback

loop gain g was set to 0.8.

Page 12: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

11

Figure 11: IIR comb filter pole-zero plot, magnitude response and phase response.

Figure 12: Frequency Response of FIR Comb Filter with delay line gain g set to 0.8

Page 13: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

12

Figure 13: Impulse Response of IIR Comb Filter with delay line gain g set to 0.8

Universal Comb Filter

Figure 14: Parameter Table and Block Diagram for a Universal Comb Filter [1]. Both the FIR and IIR comb filters may be implemented with this general structure.

Page 14: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

13

PART 4: FIR and IIR Comb Filter Implementation Implement and test both IIR and FIR comb filters with delays in the

range from 0 to 100 msec. The sound effect will be different depending

on the delay. Using the Tom's Diner.wav and the flute.wav sound sample,

describe the sound effect and the corresponding range of delays, and explain

the result.

See Table 3.4 on page 71 for some ideas.

FIR Comb Filter

The perceived effects resulting from passing the audio files through the for the FIR comb filter with

various delays are summarized in Table 1.

Table 1: FIR Comb Filter Effects with g= 0.9

Delay Time (ms)

Effects on Tom’s Diner Effects on Flute 2

0 No change. An increase in amplitude is expected due to the doubling of signals, however, the output was reduced to prevent clipping.

Same effects as those o Tom’s Diner.

10 “Metallic” vocal timbre is present, resulting from out of phase identical signals occurring closely together in time. There is a loss in vocal clarity.

Subtle thickening of sound.

30 “Metallic” timbre is still present, but a doubling/thickening effect is also starting to become pronounced as a result of the wider separation between voices.

Loss in clarity and obscured “vibrato” (more of a tremolo) due to out of phase amplitude variations.

50 Clear echo on transients (particularly the hard consonants) can now be heard, in addition to the previous effects.

When the vibrato speeds up toward the end, the flute seems to separate into two distinct voices. Overall: thicker sound, loss of clarity.

100 There is now a clear delayed voice, and the wider separation has reduced the “Metallic” effect. The resulting effect is a single echo, although higher in amplitude than a naturally occurring Initial Time Delay Gap (g=-0.9 seems too large for a reflection occurring at 100ms)

Altering of dynamics (see Figure 19), distortion and partial elimination of vibrato (see discussion below), loss of clarity, thickening of sound, and apparent addition of a second voice. Multiple oscillations in amplitude from the distorted vibrator are apparent, and oscillating at different frequencies.

For lower delay times, the effects on the vocal timbre were more noticeable than the flute. However, the

effects of comb filtering were particularly pronounced for the flute sample that was filtered through the

FIR comb filter with 100ms delay. The interference caused by the out of phase copy of the original signal

causes the flute “vibrato” (flute vibrato is actually closer to a tremolo) to be altered. Since the flute’s

vibrato is essentially a tremolo, the amplitude variations are distorted by the comb filtering, and the

overall dynamics are altered. Additionally, a thickening of texture, loss of clarity, and the typical

disorienting effects of extreme comb filtering are present. This effect would be more significant if the

delay gain parameter g was set to 1 (for the FIR comb filtered audio files, a value of g= 0.9 was used).

Page 15: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

14

Figure 15: Response of FIR comb filter applied to Tom's Diner with 0ms delay.

Figure 16: Response of FIR comb filter applied to Tom's Diner with 10ms delay.

Figure 17: Response of FIR comb filter applied to Tom's Diner with 100ms delay.

Page 16: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

15

Figure 18: FIR comb filter response to flute2.wav as input; delay time = 10ms.

Figure 19: FIR comb filter response to flute2.wav as input; delay time = 100ms.

Page 17: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

16

FIR Comb Filter

The perceived effects resulting from passing the audio files through the for the IIR comb filter with

various delays are summarized in Table 1. To achieve an effect without over modulating the input sound,

the delay loop gain g was set to 0.5. However, the IIR comb filter was also tested with higher feedback

loop gains.

Table 2: FIR Comb Filter Effects with default g= 0.5

Delay Time (ms)

Effects on Tom’s Diner Effects on Flute 2

0 No change. An increase in amplitude is expected due to the doubling of signals, however, the output was reduced to prevent clipping.

Same effects as those o Tom’s Diner.

10 “Metallic” vocal timbre is present, as in the FIR case.

Thickening of sound.

30 “Metallic” timbre is enhanced, and there is a significant loss in vocal clarity.

Thickening of sound and subtle smoothing of transients.

50 The effects described above are enhanced. The series of delays that can be perceived have a hollow sound similar to flutter echo. Echo is very pronounced on the transients, and comb filtering has distorted the source sound significantly.

As time progresses, multiple flute voices can be perceived.

100 The first delay creates a noticeable echo voice, and the initial sound resembles a very un-diffuse reverb. Toward the end, successive delays begin to stack up.

With g = 0.5, there is a significant thickening of the sound, resembling multiple flutes playing in unison. In addition, there is an impression of ambiance (expected, as subtle comb filtering is an important element of reverb). There is also a loss of clarity due to the delays, which are out of phase from the original signal and therefore cause distortion to the waveform.

As g approaches 1 for the IIR comb filter, successive delays decay so slowly that they stack up as time

progresses (Figure 22), making for an unnatural but interesting sounding effect. If g is greater than 1

(corresponding to poles outside of the unit circle), the system is unstable (Figure 23) and will quickly

blow up toward infinity.

Page 18: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

17

Figure 20: IIR comb filter response to flute2.wav as input; delay time = 0ms, g = 0.5.

Figure 21: IIR comb filter response to flute2.wav as input; delay time = 100ms, g = 0.50.

Page 19: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

18

Figure 22: IIR comb filter response to flute2.wav as input; delay time = 100ms, g = 0.9.

Figure 23: IIR comb filter response to flute2.wav as input; delay time = 100ms, g = 1.1 (left) and g = 1.3 (right). These filters are unstable.

PART 5: Vibrato Effect Add a vibrato effect to a steady pitched sound (single instrument melody)

The vibrato effect was implemented in the time domain, using the algorithm presented on page 69 of

DAFX [1]. A typical modulation frequency of 4Hz was chosen (around average for human vocals, which

other instruments tend to mimic). I originally tried to apply this algorithm to a cello sample; however,

there were some implementation problems that were taking a very long time to work around. In the end,

the vibrato was applied to a sinusoid oscillating at 110Hz (a low A).

The sine wave makes it easy to view the effects of vibrato, which is a relatively slow and narrow

oscillation in pitch that deviates about the fundamental frequency of a tone. Without vibrato, we would

expect to see a constant spectrum for a sin wave.

Page 20: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

19

Figure 24: Vibrato effect applied to sine wav

PART 6: Second Order Allpass Filter Design Design a second order allpass filter. Show the pole and zero locations

and use POI to verify the amplitude and phase response are as expected.

The second-order allpass filter was used to create the wha BP filter of Part 1. From Equations (1), (3) and

(4), the transfer function for the allpass filter is:

���� = −� + �1 − ����� + ���1 + �1 − ����� − ����

� = tan ����� ! − 1tan �2���� ! + 1

= −cos �2��%� !

Page 21: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

20

Figure 25: Second-Order Allpass Filter block diagram [1]

To construct a second-order allpass filter with with real coefficients, two allpass pole-zero couples must

be multiplied together, where the poles are complex conjugates. The steady state magnitude must be

constant over the entire frequency range, therefore:

|��NOP�Q|� = Q������−��|RSTUV = QW���W�−��X���X�−��Y

RSTUV= Z� �Z is the transfer function gain�

Therefore the allpass filter must satisfy:

W��� = ZX�−��

such that the zeros are the images of the poles.

Choosing the poles to be the complex conjugates:

b� = 0.9NOeB , b� = 0.9N�OeB

The corresponding image zeros will lie diagonally opposite the poles in the z-plane. This means that the

zeros will be located outside the unit circle at an equal distance to the unit circle as the poles, with the

same angle as the poles:

�� = 1.1NOeB , �� = 1.1N�OeB

The z-plane pole zero-plot shown below. The resulting magnitude and phase response are as desired:

� Unity gain at all frequencies for the magnitude response

� Phase drops from 0 to -360˚, about the cutoff frequency ωωωω+

(the phase response passes -180˚ at ωωωω+)

Page 22: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

21

Figure 26: Pole-zero plot for allpass filter in winPOI.

Figure 27: Magnitude Response (top) and Phase Response (bottom) for all pass filter. The apparent phase discontinuity is simply due to the axis of the plot (the phase range is from –π to π instead of from -2π to 0).

Page 23: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

22

PART 7: Second Order Allpass Filter Group Delay Plot the group delay of the allpass filter designed in part 6.

Verify that the group delay is the "time delay of the amplitude envelope of a

sinusoid at a particular frequency" by testing with two suitable AM signals

with carrier frequencies near the minimum and maximum group delay.

The magnitude response, phase response, and group delay of the allpass filter from part 6 are plotted in

Matlab below (Figure 28). Additionally, the two input AM waveforms are plotted, and the time delay of

the amplitude envelope can be used to determine an identical group delay (Figure 31).

From the plots below, the maximum group delay was determined to be 19 samples, occurring at 5500Hz.

This is at the cutoff frequency, where the phase response passes -180˚.

Figure 28: Second-order allpass filter frequency response and group delay

Page 24: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

23

Figure 29: Envelope comparison between sinusoids having maximum (top) and minimum (bottom) delay. Modulating frequency = 30Hz

Figure 30: Envelope comparison between sinusoids having maximum (top) and minimum (bottom) delay. Modulating frequency = 30Hz

Page 25: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

24

Figure 31: Zoomed in on envelope comparison plot. The difference between the sample times with a particular amplitude on the envelope can be counted to reveal the delay. Half of the samples are not shown as they occupy the top half of the envelope, and thus the result should be multiplied by 2.

PART 8: Sound Collage Using Filtered White Noise A sound collage was created using only white noise as input. As a starting point, the filters that were

already designed in this assignment were used. This includes the filter titled “lazer_wha”, which was

documented in Part 1. A delay effect was applies to one copy of the “lazer_wha” file, and convolution

reverb was applied to all the filtered tracks, which are panned throughout the stereo field. Multiple

delayed copies of “lazer wha” where used, and as they are panned through the stereo field, comb filtering

effects occur.

There were two filters used that have not been documented yet. The first is the whawha filter in Audacity.

This filter gives a very low pitch wha, and almost has a sound resembling the “low chest” sound formant

in human speech. The following parameters were used for this filter:

� LFO Freq = 1.5 Hz

� Depth = 70%

� Resonance = 2.5

� Wha Frequency Offset = 30%

Page 26: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

25

The other new filter that was used is a 6 band parametric EQ (Figure 33). The center frequencies were

carefully chosen to add or remove the impression of proximity and listener envelopment. These gain of

these 6 center frequencies was automated to change through time by defining them as automation

parameters in Pro Tools. The gain was altered throughout the track.

Figure 32: Initial filtered noise files used for sound college.

Page 27: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

26

Figure 33: 6 Band parametric EQ with automated gain at center frequencies - applied to white noise to cause proximity related frequencies to swell and wane as time progresses.

Page 28: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

27

REFERENCES

[1] Udo Zölzer, DAFX. John Wiley & Sons, 2002.

APPENDIX A

PART 2 MATLAB Code

Convolution of Two Audio Files %========================================================================== % a5part7_Convolve.m Tim Perry % Elec484: DAFX V00213455 % Assignment 3, Part 7 2009-05-21 % % Convolve about 5 seconds of the Tom's Diner .wav sound file with % a rhythmic pattern (made up from the given samples or your own sample), % and describe the result. % Repeat with the flute.wav sound file. %==========================================================================

clear all; close all;

% input audio files wavfile1 = 'flute2.wav'; %wavfile1 = 'x1.wav'; wavfile2 = 'TransSiberian_Drums';

[x1, fs1, NBITS1] = wavread(wavfile1); [x2, fs2, NBITS2] = wavread(wavfile2);

% Default to left channel for mono treatment of stereo input files x1t = x1(:,1); x2 = x2(:,1);

blockTime = 20e-3; %20 ms blockLength = floor(blockTime*fs1); blockNos = min( ceil( [length(x1) length(x2)]/blockLength ) );

y = zeros((blockLength*(blockNos+1)),1);

% Define window function t = transpose(linspace(0,1,blockLength*2+1)); window = (1-cos(2*pi*t))/2;

% Perform convolution for i = 0:blockNos-1

offset = 1 + i * blockLength;

Page 29: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

28

CurrentBL = min([blockLength length(x1)-offset length(x2)-offset]); BlockConvolved = conv( x1(offset:offset+CurrentBL),

x2(offset:offset+CurrentBL)); BlockConvolved = window(1:length(BlockConvolved)).*BlockConvolved; y(offset:offset+length(BlockConvolved)-1) =

y(offset:offset+length(BlockConvolved)-1) + BlockConvolved;

end y = (1/(2.*max(y))).*y; % lower amplitude to prevent wavwrite clipping

% name output file file_out=strcat(wavfile1(1:length(wavfile1)-4),'Convolve'); % write output audio file wavwrite(y, fs1, NBITS1, file_out);

PART 3 MATLAB Code FIR Comb Filter Plots %========================================================================== % a3pt3_FIRcomb.m Author: Tim Perry % Elec484: DAFX V00213455 % Assignment 3, Part 3 2009-05-21 % % FIR comb filter (delay M=4) plots %========================================================================== clear all; close all;

% Filter Specs/Constants M = 4; % delay g = 0.9; % delay gain N = 1024; % FFT window size

% Filter coefficients b = zeros(1,M+1); b(1) = 1; b(M+1) = g; a = [1];

%------------------------------------------- % Frequency Response %------------------------------------------- H_w = freqz(b,a,N); w = linspace(0,pi,N);

figure(1)

% Magnitude response plot subplot(2,1,1) plot(w, abs(H_w)); grid on;

Page 30: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

29

axis([0 pi 0 2]) title('Magnitude Response of FIR Comb Filter') xlabel('Normalized Frequency (radians/s)') ylabel('|H(e^jw)|')

% Phase response plot subplot(2,1,2) plot(w, angle(H_w)); grid on; axis([0 pi -pi pi]) title('Phase Response of FIR Comb Filter') xlabel('Normalized Frequency (radians/s)') ylabel('Arg(H(e^jw)) (radians)')

%------------------------------------------- % Impulse Response %------------------------------------------- H_whole = freqz(b,a,N,'whole'); h = ifft(H_whole,N); nSamples = linspace(0,N,N);

figure(2)

% Impulse response plot stem(nSamples, h,'.','b-','MarkerSize',15); grid on; axis([-5 35 -0.2 1.2]) title('Impulse Response of FIR Comb Filter') xlabel('n') ylabel('h[n]')

IIR Comb Filter Plots %========================================================================== % a3pt3_FIRcomb.m Author: Tim Perry % Elec484: DAFX V00213455 % Assignment 3, Part 3 2009-05-21 % % IIR comb filter (delay M=4) plots %========================================================================== clear all; close all;

% Filter Specs/Constants M = 4; % delay g = 0.8; % delay gain c = 1; % input signal gain N = 1024; % FFT window size

% Filter coefficients b = [c]; a = zeros(1,M+1); a(1) = 1 a(M+1) = -g;

Page 31: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

30

%------------------------------------------- % Frequency Response %------------------------------------------- H_w = freqz(b,a,N); w = linspace(0,pi,N);

figure(1)

% Magnitude response plot subplot(2,1,1) plot(w, abs(H_w)); grid on; axis([0 pi 0 max(H_w)+1]) title('Magnitude Response of IIR Comb Filter') xlabel('Normalized Frequency (radians/s)') ylabel('|H(e^jw)|')

% Phase response plot subplot(2,1,2) plot(w, angle(H_w)); grid on; axis([0 pi -pi pi]) title('Phase Response of IIR Comb Filter') xlabel('Normalized Frequency (radians/s)') ylabel('Arg(H(e^jw)) (radians)')

%------------------------------------------- % Impulse Response %------------------------------------------- H_whole = freqz(b,a,N,'whole'); h = ifft(H_whole,N); nSamples = linspace(0,N,N);

figure(2)

% Impulse response plot stem(nSamples, h,'.','b-','MarkerSize',15); grid on; axis([-5 60 -1 1.2]) title('Impulse Response of IIR Comb Filter') xlabel('n') ylabel('h[n]')

Page 32: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

31

PART 4 MATLAB Code

FIR Comb Filter Implementation %========================================================================== % a3pt4_FIRcomb.m Author: Tim Perry % Elec484: DAFX V00213455 % Assignment 3, Part 4 2009-05-23 % % Implements FIR comb filter with adjustable delay parameters. % Input Audio: Tom's Diner.wav and the flute.wav sound sample % See Table 3.4 on page 71 of DAFX for ideas. %========================================================================== clear all; close all;

% Filter Specs/Constants delayTime = 10; % delay time in ms g = 0.9; % delay gain N = 1024; % FFT window size

% Input Audio wavfile = 'flute2.wav'; % wavfile = 'diner.wav'; [audio_in, f_s, nbits] = wavread(wavfile);

numSamples = length(audio_in);

% Define delay M (samples) M = (delayTime/1000)*f_s;

% Filter coefficients b = zeros(1,M+1); b(1) = 1; b(M+1) = g; a = [1];

%------------------------------------------- % Apply filter to audio %------------------------------------------- y = filter(b,a,audio_in); y = (1/(2.*max(y))).*y; % lower amplitude to prevent wavwrite clipping

% name output file delaylabel = int2str(delayTime); file_out=strcat(wavfile(1:length(wavfile)-4),'FIRcomb-',delaylabel,'ms') % write output audio file wavwrite(y, f_s, nbits, file_out);

%------------------------------------------- % Plot IR of output file %------------------------------------------- samples = linspace(0,numSamples,numSamples);

Page 33: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

32

figure(1)

% Impulse response plot stem(samples, y,'.','b-','MarkerSize',15); grid on; axis([-5 numSamples -1 1]) title('FIR Comb Filter Output') xlabel('n') ylabel('h[n]')

IIR Comb Filter Implementation %========================================================================== % a3pt4_FIRcomb.m Author: Tim Perry % Elec484: DAFX V00213455 % Assignment 3, Part 4 2009-05-23 % % Implements IIR comb filter with adjustable delay parameters. % Input Audio: Tom's Diner.wav and the flute.wav sound sample % See Table 3.4 on page 71 of DAFX for some ideas. %========================================================================== clear all; close all;

% Filter Specs/Constants delayTime = 100; % delay time in ms g = 0.5; % delay gain c = 1; % input signal gain N = 1024; % FFT window size

% Input Audio % wavfile = 'flute2.wav'; wavfile = 'diner.wav'; [audio_in, f_s, nbits] = wavread(wavfile);

numSamples = length(audio_in);

% Define delay M (samples) M = (delayTime/1000)*f_s;

% Filter coefficients b = [c]; a = zeros(1,M+1); a(1) = 1; a(M+1) = -g;

%------------------------------------------- % Apply filter to audio %------------------------------------------- y = filter(b,a,audio_in); y = (1/(2.*max(y))).*y; % lower amplitude to prevent wavwrite clipping

% name output file delaylabel = int2str(delayTime); file_out=strcat(wavfile(1:length(wavfile)-4),'IIRcomb-',delaylabel,'ms');

Page 34: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

33

% write output audio file wavwrite(y, f_s, nbits, file_out);

%------------------------------------------- % Plot IR of output file %------------------------------------------- samples = linspace(0,numSamples,numSamples);

figure(1)

% Impulse response plot stem(samples, y,'.','b-','MarkerSize',11); grid on; axis([-5 numSamples -1 1]) title('IIR Comb Filter Output') xlabel('n') ylabel('h[n]')

wavplay(y, f_s);

PART 5 MATLAB Code

Vibrato.m [1]

function y=Vibrato(x,SAMPLERATE,Modfreq,Width) ya_alt=0; Delay=Width; % basic delay of input sample in sec DELAY=round(Delay*SAMPLERATE); % basic delay in # samples WIDTH=round(Width*SAMPLERATE); % modulation width in # samples if WIDTH>DELAY error(’delay greater than basic delay !!!’); return; end;

MODFREQ=Modfreq/SAMPLERATE; % modulation frequency in # samples LEN=length(x); % # of samples in WAV-file L=2+DELAY+WIDTH*2; % length of the entire delay Delayline=zeros(L,1); % memory allocation for delay y=zeros(size(x)); % memory allocation for output vector

for n=1:(LEN-1) M=MODFREQ; MOD=sin(M*2*pi*n); ZEIGER=1+DELAY+WIDTH*MOD; i=floor(ZEIGER); frac=ZEIGER-i; Delayline=[x(n);Delayline(1:L-1)]; %---Linear Interpolation----------------------------- y(n,1)=Delayline(i+1)*frac+Delayline(i)*(1-frac); %---Allpass Interpolation------------------------------ %y(n,1)=(Delayline(i+1)+(1-frac)*Delayline(i)-(1-frac)*ya_alt); %ya_alt=ya(n,1); End;

Page 35: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

34

Script for Running Vibrato [1] %========================================================================== % a5part7_Allpass.m Tim Perry % Elec484: DAFX V00213455 % Assignment 3, Part 5 2009-05-23 % % Add a vibrato effect to a steady pitched sound (single instrument melody) % Algorithm provided by U. Zolzer in DAFX %========================================================================== clear all close all

%define constants f_s=44100; f = 110; vibwidth = 1.5e-3; modfreq = 4; duration = 5; nbits = 16:

% input audio file %wavfile = 'cello.wav'; % read input audio %[x, f_s, NBITS] = wavread(wavfile); %numSamples = length(wavfile);

n=[1:duration*f_s]; x=sin(2*pi*n*f/f_s);

y = vibrato(x', f_s, modfreq, vibwidth);

wavwrite(y, f_s, nbits, 'a3pt5_vibrato');

Page 36: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

35

PART 7 MATLAB Code Allpass Filter Testing

%==========================================================================

% a5part7_Allpass.m Author: Tim Perry

% Elec484: DAFX V00213455

% Assignment 3, Part 7 2009-05-23

%

% Plot the group delay of the allpass filter designed in part 6.

% Verify that the group delay is the "time delay of the amplitude envelope

% of a sinusoid at a particular frequency" by testing with two suitable AM

% signals with carrier frequencies near the minimum and maximum group delay.

%==========================================================================

clear all;

close all;

hold on;

%--------------------------------------------------------------

% Filter specifications and constants

%--------------------------------------------------------------

f_s = 44100;

f=[0:1:f_s/2]; %freq range 0 to 4000 Hz (positive Nyquist band)

N = 1024; % FFT window size

w=2*pi*f/f_s;

z=exp(w*j);

%---------------------------------------

% Filter coefficients from POI

%---------------------------------------

K = 1.2344

a = [1.2343 -1.5712 1]

b = K*[0.8100 -1.2728 1]

%----------------------------------------------------------------

% Frequency Response

%----------------------------------------------------------------

f = linspace(0,f_s/2,N);

H_f = freqz(b,a,N);

magH_f = abs(H_f); %magnitude response

argH_f = angle(H_f); %phase response

gd = grpdelay(b,a,f,f_s); %group delay

figure(1)

% Magnitude response plot

subplot(3,1,1)

plot(f, magH_f );

grid on;

axis([0 f_s/2 0 1.2])

title('Magnitude Response of Allpass Filter')

xlabel('Frequency (Hz)')

ylabel('|H(e^jw)|')

% Phase response plot

subplot(3,1,2)

plot(f, argH_f);

grid on;

Page 37: Tperry_Elec484-A3

Tim Perry

V00213455

2009-05-21

36

axis([0 f_s/2 -pi pi])

title('Phase Response of Allpass Filter')

xlabel('Frequency (Hz)')

ylabel('Arg(H(e^jw)) (radians)')

% Group delay plot

subplot(3,1,3)

plot(f, gd);

grid on;

axis([0 f_s/2 0 max(gd)*1.1])

title('Group Delay of Allpass Filter')

xlabel('Frequency (Hz)')

ylabel('Group Delay (samples)')

%----------------------------------------------------------------

% Test Sinusiods

%----------------------------------------------------------------

f1 = 5500; % cutoff frequency (where max group delay occurs)

f2 = 15000; % freq where group delay approches zero

n = linspace(0,N,N);

% AM signals

fm = 10; %message frequency

Am = 0.1; %message amplitude

x1 = (1 + Am*cos(2*pi*fm*n)).*sin(2*pi*f1*n);

x2 = (1 + Am*cos(2*pi*fm*n)).*sin(2*pi*f2*n);

%x1 = 0 + cos(2*pi*f1*n);

%x2 = 0 + cos(2*pi*f2*n);

% filter AM signals with allpass filter

y1 = filter(b,a,x1);

y2 = filter(b,a,x2);

figure(2)

% plot filtered AM signals to compare delay

subplot(2,1,1)

stem(n, y1,'.','b-','MarkerSize',13);

grid on;

axis([0 N min(x1)*1.1 max(x1)*1.1])

title('Allpass filter response to x1 = 1 + cos(2*pi*f1*n)')

xlabel('n')

ylabel('y_1[n]')

subplot(2,1,2)

stem(n, y2,'.','r-','MarkerSize',13);

grid on;

axis([0 N min(y1)*1.1 max(y1)*1.1])

title('Allpass filter response to x2 = 1 + cos(2*pi*f2*n)')

xlabel('n')

ylabel('y_1[n]')