You are on page 1of 39

ELEC 484 Audio Signal Processing

Assignment 2:
Design and implementation of a wah-wah filter

Prepared for:
Peter Driessen
ELEC 484
University of Victoria

Prepared by:
Tim I. Perry
V00213455
May, 2009

Tim Perry
V00213455
2009-05-15

Contents
1.

Preliminary Design (Questions 1-2)............................................................................................... 2

2.

Transfer Function .............................................................................................................................. 4

3.

Frequency Response ......................................................................................................................... 5

4.

Inverse IDFT and Impulse Response ................................................................................................. 9

5.

Difference Equations....................................................................................................................... 11

6.

Impulse Response using Difference Equations ............................................................................... 12

7.

Impulse Response by Analysis ........................................................................................................ 14

8.

Comparison of Impulse Response Results .......................................................................................... 17

9.

Discrete Time Fourier Transform ........................................................................................................ 17

10.

Filter Simulation and Testing .......................................................................................................... 19

11.

Convolution x[n]*h[n] ..................................................................................................................... 25

Phase 3 Implementing Wha-wha Filter.................................................................................................... 27


APPENDIX A ................................................................................................................................................. 29
APPENDIX B ................................................................................................................................................. 38

Tim Perry
V00213455
2009-05-15

ELEC 484 ASSIGNMENT 2


Design and Implementation of a Wah-Wha Filter
A wah-way effect is created by a bandpass filter with variable center frequency and small bandwidth. The center
frequency is controlled by a low frequency oscillator (LFO) with frequency around 1-2 Hz, or can be controlled by
an external control input device such as a mouse or slider. The center frequency varies from near 0 to a frequency
less than half the sampling rate. The output of the bandpass filter is added to the direct (input) signal.
In this assignment you will study a 2-pole 2-zero bandpass filter in detail, and then use it to add a wah-wah effect to
an audio file.

1. Preliminary Design (Questions 1-2)


Realistic specifications for the bandpass filter to be used to create a wah-wah effect are:





sampling rate f_s = 44,100 Hz


center frequency f_1 = 44,100/64 = 689 Hz.
3dB bandwidth B = 100 Hz
implementation uses 2 poles and 2 zeros

For simplification of algebra, the following alternative specifications will be used:







sampling rate f_s = 8000 Hz


center frequency f_1 = 2000 Hz
3dB bandwidth B = 100 Hz
implementation uses 2 poles and 2 zeros

The transfer function for a BPF with two poles and two zeros can be expressed in the form:
 =

    

 


(1)

where  and 
are the zeros of the filter, and and
are the zeros of the filter.
Zeros
First, the zeros are placed on the unit circle at +1 and -1. This corresponds to a frequency range
from 0 to the Nyquist frequency, /2 = 4000 Hz.
Poles and 3dB Bandwidth
The angle of the poles in the complex plane controls the center frequency of the BPF.
Since the specified passband center frequency = 2000 Hz is located at the halfway point in the
Nyquist band, the poles will be located at an angle of:
2 /  = /2 (ideal poles will be on the imaginary axis with no real component)
2

Tim Perry
V00213455
2009-05-15
The distance of the poles from the unit circle |1-a| controls the bandwidth of the BPF.
Using the specification for the 3dB bandwidth, the magnitude |a| of the poles (radial distance of
the poles from the origin) can be calculated using:
 =

2|1 |


2

(2)

where  is the normalized bandwidth B in radians (also known as the time bandwidth
product).
First,  is calculated:

 =

2 2100

=
=


8000
40

Next, |a| is calculated using (2), yielding:


|| = 0.961494
Thus, the locations of the poles and zeros are (Figure 1):
 =
( =

! = "#. %& '


!( = "#. %& '

From (1), the resulting transfer function of the BPF is:


 =

  1 + 1
 *0.9615 + *0.9615

Unity Passband Gain


The filter gain at 0 Hz  must be adjusted in order to achieve unity gain at the center frequency.
In order to solve for  , the magnitude of the frequency response |./ 01 2| is set equal to 1 and
evaluated at  = /2.
./ 01 2 =
|./ 0
345 /46 2| = 1 =

Simplifications yields:

 ./ 01 12./ 01 + 12
/ 01 *0.9615/ 01 + *0.9615
 |./ 0
345 /46 12./ 0
345 /46 + 12|
|/ 0
345 /46 *0.9615/ 0
345 /46 + *0.9615|
where 2 / = /2

 = 0.51 
 = 1 0.9615
)/2 = #. #788&'
3

Tim Perry
V00213455
2009-05-15

2. Transfer Function
From the parameters calculated in the previous sections, the normalized transfer function with
unity gain at = 2000 Hz is:
9 =

#. #788&'  + 
 "#. %& ' + "#. %& '

(3)

Alternatively:
#. #788&'.( 2
9 =
( + #. %(::8 
9 =

#. #788&'. ;( 2
 + #. %(::8 ;( 

(4)

(5)

Figure 1: Pole-zero plot of second order Butterworth BPF Filter (Matlab filter design).

Figure 2: Magnitude response of the BPF in dB (Matlab filter design).

Tim Perry
V00213455
2009-05-15

3. Frequency Response
3.1. Frequency Response Plots
The frequency response of the BPF is plotted below in Figure 3. Amplitude is plotted on a linear scale,
power is plotted on logarithmic (dB) scale, and phase is plotted in radians.

Figure 3: Plots of amplitude on a linear scale, power on a dB scale, and phase.

Tim Perry
V00213455
2009-05-15
3.2. Verification of 3dB Bandwidth

Figure 4 verifies that the power response at /2 is 3dB below the power response at the center
frequency, confirming the 3dB bandwidth.

Figure 4: Power response is down 3dB at = + >/( .

3.3. Phase Shift


Figure 5 shows the phase shift in radians at and /2. This corresponds to a phase of 0 at the
center frequency, 45.0345 at /2 and -45.0345 at + /2.

Figure 5: Figure 5: Phase shift at = and = >/( .

Tim Perry
V00213455
2009-05-15
3.4. MATLAB Program for Frequency Response Plots
%==========================================================================
% FRplots.m
Author: Tim Perry
% Elec484: DAFX
V00213455
% Assignment 2, Problem 3
2009-05-15
% Frequency response plots for a 2-pole 2-zero BPF for a wha-wha effect
%==========================================================================
f_s=8000 %sampling frequency
f_1=2000; %center frequency
f=[0:1:4000]; %freq range 0 to 4000 Hz (positive Nyquist band)
B=100; %3 dB bandwidth
w=2*pi*f/f_s;
z=exp(w*j);
%amplitude response plot (linear scale)
H=((0.037765*(z.*z-1))./(z.*z+0.924471));
figure
subplot(3,1,1);
plot(f,abs(H))
grid on;
axis([0 4000 0 1.1]);
title('Amplitude Response H(f)')
ylabel('H(z)')
xlabel('Frequency (Hz)')
%power on logarithmic (dB) scale
H=((0.037765*(z.*z-1))./(z.*z+0.924471));
power_H = 20*log10(abs(H)/max(abs(H)));
subplot(3,1,2);
plot(f,power_H)
grid on;
axis([0 4000 -60 3]);
title('Power Spectrum 20log(|H(f)|)');
ylabel('20log(|H(f)|) (dB)');
xlabel('Frequency (Hz)');
%phase response plot
subplot(3,1,3);
plot(f,angle(H))
grid on;
title('Phase Response of H(f)')
ylabel('Theta (rad)')
xlabel('Frequency (Hz)')
%Zoomed in passband power plot for -3dB measurement
figure
plot(f,power_H)
grid on;
axis([1900 2100 -5 1]);
title('Power Spectrum 20log(|H(f)|)');
ylabel('20log(|H(f)|) (dB)');
xlabel('Frequency (Hz)');
%Zoomed in phase response plot surrounding passband
figure
plot(f,angle(H))
grid on;
axis([1500 2500 -2 2]);
title('Phase Response of H(f)')
ylabel('Theta (rad)')
xlabel('Frequency (Hz)')

Tim Perry
V00213455
2009-05-15
3.5. POI Plots
From the frequency response magnitude and phase plots generated in POI (Figure 6), similar results are
obtained at the 3dB margins.

Figure 6: POI plots of the BPF frequency response (magnitude and phase)

Measurement taken in POI are not as precise as those taken in Matlab. POI indicates:



unity gain and zero phase shift at


a magnitude of 0.71 and phase of -45.12 at + /2

If the results are consistent, than the following should hold true:

3 = 20@AB CH + C
2
Therefore, at -3dB from unity gain, the magnitude should be:
F

H E +
G = 10;H/
 = 0.707946
This demonstrates that the POI measurements at the 3dB margins, while less precise, are consistent with
the Matlab results and the theoretical values.

Tim Perry
V00213455
2009-05-15

4. Inverse IDFT and Impulse Response


In order to obtain the impulse response h[n], the IDFT (Inverse Discrete Fourier Transform) of the
sampled frequency response will be computed.
4.1. Manual Computation
Using the function IDFT_compute written below, the Inverse Discrete Fourier Transform was computed
with a window size of N=1024.
% Computes the Inverse Discrete Time Fourier Transform
% Input Parameters: Frequency Response H, Window Size N
function [h] = IDFT_compute(H, N)
for n = 0:N-1
sum = 0;
for k = 0:N-1
sum = sum + (H(k+1)*exp(i*2*pi/N*k*n));
end
h(n+1) = sum/N;
end
h = real(h);

The script for the execution of this function can be found in the appendix. In order to take the IDFT, the
frequency response of the system was zero-padded and re-defined to include negative frequencies.

The IDFT and DFT routines that were written are based on the following:

(6)

where
and

N = the length of the vector (window size)

4.2. Comparing with MATLAB IFFT Routine


The Matlab Inverse Fast Fourier Transform algorithm was also used to compute the IDFT for N = 1024.
The results were identical to the manual computation. This is expected, as both algorithms use the same
definition for the IDFT.
Impulse response plots for both the manual and IFFT methods are shown in Figure 7, and the results are
tabulated for comparison in Table 1.

Tim Perry
V00213455
2009-05-15
Table 1: Comparison of IDFT Results (IR sample values) for first 16 samples
n
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

h[n] (manual IDFT)


0.0378
0
-0.0741
0
0.0712
0
-0.0685
0
0.0658
0
-0.0633
0
0.0609
0
-0.0585
0

h[n] (Matlab IFFT)


0.0378
0
-0.0741
0
0.0712
0
-0.0685
0
0.0658
0
-0.0633
0
0.0609
0
-0.0585
0

Figure 7: IDFT manual computation versus the IFFT function in Matlab.

10

Tim Perry
V00213455
2009-05-15
4.3. Frequency Resolution
The frequency resolution is equal to the window size N divided by the sampling frequency.

=# =
 =

=I
J

(7)

8000
= 8. K (' 9
1024

5. Difference Equations
5.1. Difference Equation by Analysis
From (4), the transfer function of the BPF is:
 =

 =
Eliminating H(z):

0.037765
1
M
=

 + 0.924471
N

0.0377651  ;

M
=
;

1 + 0.924471 
N

Yz 1 + 0.924471 ;
 = N 0.0377651  ;


Using the inverse Z transform, the difference equation can be obtained:


RSTU = #. #788&'VSTU #. #788&'VST (U #. %(::8 RST (U

(8)

5.2. POI Difference Equation


The difference equation produced by POI is:
y[n] = +1.0000*x[n] - 1.0000*x[n-2] - 0.9245*y[n-2]

This equation lacks the gain coefficient 0.037765 that is found in equation (7), which scales the filter for
unity gain at = 2000 Hz. However, the magnitude response plot of Figure 6 does display unity gain at
the center frequency, indicating that POI has applied a scale factor.

11

Tim Perry
V00213455
2009-05-15

6. Impulse Response using Difference Equations


6.1. IR using Spreadsheet and MATLAB
Difference Equations with Spreadsheet
Implementing the difference equation (7) with an excel spreadsheet, the impulse response h[n]
was numerically calculated for 0 < n <25. The results are tabulated in Table 2.

Table 2: IR Using Difference Equations in Excel


WSXU = 0.037765YSXU 0.037765YSX 2U 0.924471WSX 2U

n
-2
-1
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

x[n]
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

h[n] = y[n]
0
0
0.037765
0
-0.07268
0
0.067188
0
-0.06211
0
0.057422
0
-0.05309
0
0.049076
0
-0.04537
0
0.041942
0
-0.03877
0
0.035846
0
-0.03314
0
0.030636
0

12

Tim Perry
V00213455
2009-05-15
Difference Equation in MATLAB
The difference equations were also implemented in Matlab (See Appendix), and the resulting
impulse response was plotted (Figure 8). The numerical results are tabulated in Table 3, and are
consistent with those from the spreadsheet implementation.

Figure 8: IR obtained from difference equations

Table 3: IR Using Difference Equations in MATLAB


WSXU = 0.037765YSXU 0.037765YSX 2U 0.924471WSX 2U

n
-2
-1
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

x[n]
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

h[n] = y[n]
0
0
0.03776500
0
-0.07267765
0
0.06718838
0
-0.06211371
0
0.05742232
0
-0.05308527
0
0.04907579
0
-0.04536915
0
0.04194246
0
-0.03877459
0
0.03584598

13

Tim Perry
V00213455
2009-05-15
21
22
23
24
25

0
0
0
0
0

0
-0.03313857
0
0.03063565
0

6.2. Comparing h[n] with hmax[n]


For odd values of n, h[n] = 0, and is therefore less than one percent of the maximum value, hmax[n]. All
points meeting this requirement in the range 0 < n <25 are odd values.
At higher sample numbers, as the magnitude of h[n] decreases, even numbered samples begin to have
magnitudes < 0.1 | hmax[n]|. This begins to happen at N = 124, after which all samples have magnitudes
below the threshold of 0.1 | hmax[n]|.

7. Impulse Response by Analysis


7.1. Long Division

The resulting transfer function form is:


.[
Z\ ]^

 = 0.037855 0.072670 ;


0.067189 ;Z + .H__[`\a ; .H__[`
Taking the inverse Z transform, the impulse response is found below. The last term of h[n] has decreasing
magnitude with increasing even n terms (oscillating about the origin, as odd sample magnitudes are 0).
SXU = 0.037855cSXU 0.072640cSX 2U + 0.067198cSX 4U +

; d

.[
Z\ ]^

.H__[`\ a ; .H__[`

e
14

Tim Perry
V00213455
2009-05-15
7.2. Partial Fractions with First Order Factors
0.037765 1 + 1
0.037765
1
 =
=
 *0.9615 + *0.9615

+ 0.924471

0.037765 1 + 1
f

g
=
= +
+

 *0.9615 + *0.9615

 *0.9615  + *0.9615
. 
0.037765
1
0.037765
i
= h
i
=
= 0.04085
 *0.9615 + *0.9615 \j

0.924471
\j

f=h

 *0.9615. 
0.037765
1
0.072678
i
= h
i
=
= 0.039307

 + *0.9615 \j0.k[ `
1.84896
\j0.k[ `

=h

 + *0.9615. 
0.037765
1
0.072678
i
= h
i
=
= 0.039307
 *0.9615 \j;0.k[ `

1.84896
\j;0.k[ `

g=h

The transfer function takes the form:


 = f +


g
+
 *0.9615  + *0.9615

9 = #. #:#K' +

#. #7%7#8l
#. #7%7#8l
+
 "#. %& '  + "#. %& '

(9)

Taking the inverse Z transform:


SXU = 0.04085cSXU + 0.039307m*0.9615n + *0.9615n o. pSXU

15

Tim Perry
V00213455
2009-05-15
7.2. Partial Fractions with Quadratic Factors
 =

0.037765
1

+ 0.924471


0.037765
1
f
 g
=
= +

 + 0.924471

 + 0.924471

Using the residue method to solve for A.

.

0.037765
1
0.037765
h
h
f=
i
=
i
=
= #. #:#K'

+ 0.924471


0.924471
\j
\j

Using system of equations to solve for B and C:


f. 
+ 0.924471 +  g.  = 0.037765
1

f
+ 0.924471f + 
g = 0.037765
0.037765

2nd Order Terms:


1st Order Terms:

A + B = 0.037765 > = #. #8K& '

r = # = stI/2

The transfer function takes the form:


 = f +
9 = #. #:#K' +

 g

+ 0.924471

x
 E#. #8K& ' uvw E ( GG
( + #. %(::8 

Taking the inverse Z transform:



SXU = 0.04085cSXU + 0.07862 d0.924471n  cos E XGe . pSXU
2

16

Tim Perry
V00213455
2009-05-15

8. Comparison of Impulse Response Results


Table 4 compares the impulse response results for the computer simulation in part 6, the analytical
methods in part, and the IDFT calculation of part 4. The comparison is done for sample numbers
0 < n < 3.

Table 4: Comparison of h[n] Results


IDFT, IFFT
Algorithms

n
0
1
2
3

h[n]
0.0378
0
-0.0741
0

Difference Equations
(MATLAB & Excel)

Long Division

Partial Fractions
(1st Order Factors)

WSXU
= 0.037765YSXU
0.037765YSX 2U
0.924471WSX 2U

SXU =
0.037855cSXU
0.07264cSX 2U +
0.0672cSX 4U +...

SXU
= 0.04085cSXU
+ 0.039307m*0.9615n
+ *0.9615n o. pSXU

h[n] = y[n]
0.03776500
0
-0.07267765
0

h[n]
0.037855
0
-0.07264
0

h[n]
0.037764
0
-0.072677
0

Partial Fractions
(Quadratic Factors)

SXU
= 0.04085cSXU


+ 0.078 d0.924n cos E XGe . pSXU
2

h[n]
0.037764
0
-0.072677
0

Table 4 indicates that all impulse response results are consistent with one another.

9. Discrete Time Fourier Transform


9.1. Manual DFT Computation
Using the function DFT_compute written below, the Discrete Time Fourier Transform was computed
with a window size of N=1024. This algorithm is derived from the definition in equation (6). The script
for the execution of this function can be found in the appendix.
% Computes the Discrete Time Fourier Transform
% Input Parameters: discrete time transfer function h, window size N
function [H] = DFT_compute(h, N)
for n = 0:N-1
sum = 0;
for k = 0:N-1
sum = sum + (h(k+1)*exp(-i*2*pi/N*k*n));
end
H(n+1) = sum;
end

17

Tim Perry
V00213455
2009-05-15
The DFT was computed and plotted for the transfer function from step 6; Figure 9 contains plots of the
magnitude and phase response. Figure 9 indicates that the frequency response for the transfer function
derived with the difference equations is consistent with the frequency response obtained during the filter
design stage.

Figure 9: DFT computed manually on h[n]. Magnitude response and phase response are displayed.

9.1. Frequency Resolution


The specification requires a frequency resolution of 10 or less. Using Equation (7), the frequency
resolution is equal to the window size N divided by the sampling frequency. Since the window side is the
same as that used for the IDFT, the frequency resolution is the same as that in the earlier frequency
response plots.

 =

| 8000
=
= 8. K (' 9
}
1024

18

Tim Perry
V00213455
2009-05-15

10. Filter Simulation and Testing


Demonstrate that the filter works correctly by computer simulation
as follows: Evaluate the filter output y(n) with sinusoidal input
x(n) by using the difference equations.

10.1. Sampled Cosine Waves


The filter was tested for samples cosine waves at the following frequencies:






, the center frequency


+ /2
+ 
/2


Filtering was performed using the filter command in Matlab (see Appendix A), which takes as parameters
the filter coefficients from the difference equation, in addition to the input function:
y = filter(b,a,x)
For the filters difference equation:
y[n] + 0.924471y[n-2]=0.037765x[n]- 0.037765x[n-2]
a = [1, 0, 0.924471]

b = [0.037765, 0, -0.037765]

The responses are plotted below in Figure 10 and Figure 11. The input discreet signal x[nTs] is plotted
with red stems, and the response y[nTs] is plotted with blue stems. Interpolation plots for the
corresponding continuous time signals are included to assist with visualizing the response.

Figure 10: Filter input x[nT_s] (red) and response y[nT_s] (blue) to a cosine wave with frequency f_1=2000Hz (center freq)

19

Tim Perry
V00213455
2009-05-15

Figure 11: Filter response at (top to bottom) = + >/(, = + >, = >/(, and = >). At these
frequencies, the maximum amplitude of the response y[nT_s] is reached earlier than at the center frequency,
and the amplitude is less.

20

Tim Perry
V00213455
2009-05-15
Response at Center Frequency
In Figure 10, the passband response at the center frequency of 2000Hz, the filter output output
increases to a magnitude of 1, or unity gain. Additionally, the input and output are in phase,
consistent with the expectations illustrated by Figure 5 (no phase shift at the center frequency)
Response at = >/(
In Figure 11, the maximum magnitude reached for frequencies at /2 is very close to the
expected value of:

H E + G = 10;H/
 = 0.707946
This is consistent with the expected 3dB of attenuation. Additionally, Figure 11 illustrates the
input and output are out of phase. At + /2 there is a phase lead of ~ /4 (phase shift of -45), and

at /2 there is a phase lag of ~ /4 (phase shift of +45). This is expected at the 3dB bandwidth
margins, as illustrated in Figure 5.

Figure 12: Zoomed in BFT response to = + >/( (the 3dB bandwidth margin). The - /4 phase shift in
the output is evident.

21

Tim Perry
V00213455
2009-05-15
10.2. Amplitude and Phase vs. Frequency
Verify from the graph that both the amplitude and phase of the y(n) cosine
wave output relative to the input x(n) are as predicted by H(f) in part 3.
Do this by plotting the amplitude of y(n) versus frequency (5 points on the
curve) and confirm that these points lie on the amplitude response plotted in
question 3. Repeat for the phase of y(n).

The DFT of the response y[nTs] was evaluated for each input discrete cosine waveform. Amplitude versus
frequency and phase versus frequency area plotted in the figures below. These plots confirm the
observations from the filter response plots in the above section (Section 10.1)

Figure 13: Filter Amplitude and Phase response for a cosine wave oscillating the center frequency.

22

Tim Perry
V00213455
2009-05-15

Figure 14: Filter Amplitude and Phase response for a cosine wave oscillating at = + >/( (blue) and = >/( (red).
This does not match expected -3dB margin response as well as Figure 13 matches the center frequency response.

23

Tim Perry
V00213455
2009-05-15

Figure 15: Filter Amplitude and Phase response for a cosine wave oscillating at f_1+ B (blue) and f_1- B (red). Gain is
significantly attenuated and the output signal exhibits a notable phase shift, as expected.

24

Tim Perry
V00213455
2009-05-15

11. Convolution x[n]*h[n]


Repeat item 10 (at f_1 only) by computing the convolution of the input
x(n) with h(n), and compare the results with the difference equation
method. How many terms of h(n) are needed to get reasonable agreement?

The convolution was computed and plotted (Figure 16) in Matlab with the script below. The results are
similar to those obtained with the difference equations. To produce an accurate convolution, a sufficient
number of terms of h[n] must be used such that the entire transient part of the response is convoluted. 120
samples are contained within the first 15ms (where most of the transient occurs). This can be seen as a
minimum number of samples for computing the convolution accurately.

%====================================================================
% Convolution with h[n] (Question 11)
%====================================================================
f_s=8000;
f_1=2000;
T_s=1/f_s
theta=2*pi*f_1*(T_s:T_s:150/f_s);
x_Conv= cos(theta);
n=0:1:150-1;
N=150;
z=exp(j.*2.*pi.*n./N);
figure(13)
hold on
y=conv(h1 , x_Conv);
y_Real = real(y);
plot(x_Conv,'r-'); %x(nT_s]
plot(y_Real);
%y(nT_s]
grid on;
axis([0 plotPeriods/f_1*10000 -1.2 1.2])
title('Filter Response by Convolution cos(2*pi*(f_1)*nT_s) * h[nT_s]')
xlabel('nT')
ylabel('x[nT_s](red); y[nT_s](blue)')
hold off

25

Tim Perry
V00213455
2009-05-15

Figure 16: Response of cosine wave oscillating at filters center frequency plotted by convoluting x[nT_s]*h[nT_s].

Figure 17: FFT of the convolution in Figure 16. While the phase shift is small at the center frequency, the target
phase response of figure 5 was not achieved.

26

Tim Perry
V00213455
2009-05-15

Phase 3 Implementing Wha-wha Filter


The wha-wha filter was implemented by modulating the angle of the poles for the band pass filter that
was designed. By changing the angle of the poles in the complex plane, the center frequency of the BPF is
altered. This results in the well known wha-wha effect, where high-Q bandpass filtering is applied to a
signal, and the passband is swept or modulated in real-time.
A cosine wave is used to achieve a relatively smooth modulation, and the speed of modulation can be
adjusted simply by changing the frequency of oscillation of the cosine wave. This method works by
manipulating the filters a1 coefficient (see Appendix B), which is multiplied by the periodic waveform.
Additionally, Q of the filter can by adjusted by changing the magnitude (length) of the pole vector in the
complex plane.
I was unable to find a Wha-wha effect in Audacity. However, using the spectrum plot feature, Figures 18
and 19 compare the unfiltered broadband noise with the filtered noise (the output of the Matlab Wha-wha
implementation. The magnitude response is greater for frequencies represented by brighter colours.
Clearly, the Q of the filter is narrow (illustrated by the bright bands in Figure 19) and the center frequency
is swept in a periodic fashion. The spectrum is consistent with what the ear hears.

Figure 18: Specrum of Noise in Audacity

Figure 19: Spectrum of Noise Filtered with the Matlab Implemented Wha-wha effect

27

Tim Perry
V00213455
2009-05-15
For the sake of interest, since I could not find a Wha-wha effect in Audacity, I also used a flanger effect
on the broadband noise (Figure 20). The flanger incorporates more complex filtering, with multiple peaks
(but smaller Q values). In addition, the time-frequency behaviour is different for the flanger as it is an
effect that incorporates delay.

Figure 20: Flanger effect on bradband noise in Audacity

28

Tim Perry
V00213455
2009-05-15

APPENDIX A
Phase 1 BPF Design and Testing
MATLAB Code
%==========================================================================
% Elec484-A2_BPF.m
Author: Tim Perry
% Elec484: DAFX
V00213455
% Assignment 2, Problem 3
2008-01-28
%
% Frequency response plots for a 2-pole 2-zero BPF for a wha-wha effect
%==========================================================================
close all;
clear all;
hold on;
f_s=8000 %sampling frequency
f_1=2000; %center frequency
f=[0:1:4000]; %freq range 0 to 4000 Hz (positive Nyquist band)
B=100; %3 dB bandwidth
w=2*pi*f/f_s;
z=exp(w*j);
% Transfer Function
H=((0.037765*(z.*z-1))./(z.*z+0.924471));
%-------------------------------------------------------------------% Frequency Response Plots (Questions 3)
%-------------------------------------------------------------------%amplitude reponse plot (linear scale)
figure(1)
subplot(3,1,1);
plot(f,abs(H))
grid on;
axis([0 4000 0 1.1]);
title('Amplitude Response H(f)')
ylabel('H(z)')
xlabel('Frequency (Hz)')
%power on logarithmic (dB) scale
H=((0.037765*(z.*z-1))./(z.*z+0.924471));
power_H = 20*log10(abs(H)/max(abs(H)));
subplot(3,1,2);
plot(f,power_H)
grid on;
axis([0 4000 -60 3]);
title('Power Spectrum 20log(|H(f)|)');
ylabel('20log(|H(f)|) (dB)');
xlabel('Frequency (Hz)');
%phase response plot
subplot(3,1,3);
plot(f,angle(H))
grid on;
title('Phase Response of H(f)')

29

Tim Perry
V00213455
2009-05-15
ylabel('Theta (rad)')
xlabel('Frequency (Hz)')
%-------------------------------------------------------------------% 3dB Bandwidth checking
%-------------------------------------------------------------------%Zoomed in passband power plot for -3dB measurement
% figure(2)
% plot(f,power_H)
% grid on;
% axis([1900 2100 -5 1]);
% title('Power Spectrum 20log(|H(f)|)');
% ylabel('20log(|H(f)|) (dB)');
% xlabel('Frequency (Hz)');
%Zoomed in phase response plot surrounding passband
% figure(3)
% plot(f,angle(H))
% grid on;
% axis([1500 2500 -2 2]);
% title('Phase Response of H(f)')
% ylabel('Theta (rad)')
% xlabel('Frequency (Hz)')
%magnitude and phase at center frequency f_1
% w=2*pi*f_1/f_s;
% z=exp(w*j);
% H=((0.037765*(z.*z-1))./(z.*z+0.924471));
% mag_f_o=abs(H)
% phase_F_1=angle(H)
%
% %magnitude and phase at passband edge, f_1 + B/2
% w=2*pi*(f_1+B/2)/f_s;
% z=exp(w*j);
% H=((0.037765*(z.*z-1))./(z.*z+0.924471));
% mag=abs(H)
% phase=angle(H)
%====================================================================
% Inverse Discrete Fourier Transform and Impulse Response (Question 4)
%====================================================================
%H=((0.037765*(z.*z-1))./(z.*z+0.924471)); %transfer function
%H=((0.037765*(z-1).*(z+1))./((z-i*0.961494).*(z-i*0.961494)));
%--------------------------------------------------------%Re-define tranfer function for taking the IDFT
%--------------------------------------------------------f_s=8000; %sampling frequency
T_s= 1/f_s; % sampling time
N=1024; %window size
n = [0:N-1]';
k = n;
%
%
%
%
r
K

Re-defining transfer function


Notes: sys = tf(num,den,Ts)
creates a discrete-time transfer function
with sample time Ts (in seconds). Set Ts = -1 or Ts = [] to leave the
sample time unspecified.
= 0.961494; % magnitude of poles in complex plane
= 0.037765;

30

Tim Perry
V00213455
2009-05-15
G = tf(K*[1 0 -1], [1 0 r], T_s)
% Frequency response H_f
% freqz calculates the frequency response of a digital filter
b=K*[1,0,-1];
a=[1,0,r];
H_f = zeros(N,1); % initialize to zero
[H_fill,w]=freqz(b,a,N/2) % do 512 frequencies in the range 0 to pi
H_f(1:N/2) = H_fill; %add non zero response values to H_f
%ensure conjugate symmetry.
H_f(N/2+2:N) = conj(flipud(H_fill(2:N/2)));
% H_fill = zeros(N/2,1);
% [H_f,w]=freqz(b,a,N) % do 1024 frequencies in the range 0 to pi
% H_f(1:N) = H_fill; %add non zero response values to H_f
numSamples = linspace(0,N,N);
figure(5)
% IDFT with manual computation function
manual_idft = IDFT_Compute(H_f,N);
subplot(2,1,1);
stem(numSamples, manual_idft, '.')
grid on;
title('IDFT Manually Computed')
xlabel('n')
ylabel('h[n]')
ymax = max(abs(manual_idft)*1.1);
axis([-5 150 -ymax ymax])

% IDFT with Matlab IFFT function


matlab_idft = ifft(H_f,N);
subplot(2,1,2);
stem(numSamples, matlab_idft,'.')
grid on;
title('IFFT using Matlab Function')
xlabel('n')
ylabel('h[n]')
ymax = max(abs(matlab_idft)*1.1);
axis([-5 150 -ymax ymax])

%-------------------------------------------------------------------------% Numerical Impulse Response (Question 6)


%-------------------------------------------------------------------------N=1024;
% x[n] is delta function
x=zeros(1,N);
x(1)=1;
% Impulse Response
h = zeros(1,N);
h(1) = K*x(1);
h(2) = 0;

31

Tim Perry
V00213455
2009-05-15
for n=3:N
h(n) = K*x(n)-K*x(n-2)-0.924471*h(n-2);
end
for n=1:25
fprintf(1,'%3d\t %12.8f\n', n-1, h(n))
end
hmax = max(abs(h))
for n=1:N
if abs(h(n))<(hmax/100)
fprintf(1, '%3d\t %12.8f\n', n, h(n))
end
end

figure(6)
stem(numSamples, h,'.')
grid on;
title('Impulse Response from Difference Equation')
ylabel('h[n]')
xlabel('n')
axis([-5 150 -ymax ymax])

%====================================================================
% Discrete Fourier Transform and Frequency Response (Question 9)
%====================================================================
N = 1024;
f_s = 8000;
%
%
%
%
%

computes the dft of the ifft that was taken in step 4


H1_f = zeros(N,1); % initialize with zeros
h1 = matlab_idft;
H1_fill = DFT_Compute(h1,N/2);
H1_f(1:N/2) = H_fill; %add non zero response values to H_f

% computes the dft of the h[n] from part 6


h1 = h;
H1_f = DFT_compute(h1,N);
freq = linspace(0,f_s,N);
figure(6)
%
subplot(2,1,1)
plot(freq,abs(H1_f));
grid on;
axis([0 f_s/2 0 1.2])
title('Magnitude Response |H(f)| using DFT of h[n]')
ylabel('|H(f)|')
xlabel('f (Hz)')
subplot(2,1,2)
plot(freq,angle(H1_f));
grid on;
axis([0 f_s/2 -2 2])
title('Phase Response of H(f) using DFT of h[n]')

32

Tim Perry
V00213455
2009-05-15
ylabel('Theta (rad)')
xlabel('Frequency (Hz)')

%====================================================================
% Filter Sumulation and Testing (Question 10)
%====================================================================
f_s = 8000
f_1 = 2000
T_s = 1/f_s;
% Filter coefficients from difference equation
%y[n] + 0.924471y[n-2]=0.037765x[n]- 0.037765x[n-2]
a_coeff = [1, 0, 0.924471] %from y terms
b_coeff = [0.037765, 0, -0.037765]; % from x terms
nT = 0:T_s:N*T_s;
nnT = 0:0.1*T_s:N*T_s;% for sampling the spline over a finer mesh
% cosine waves x[n] to filter:
x_f1 = cos(2*pi*f_1*nT);
x_hiCorner = cos(2*pi*(f_1+B/2)*nT);
x_f1plusB = cos(2*pi*(f_1+B)*nT);
x_loCorner = cos(2*pi*(f_1-B/2)*nT);
x_f1minusB = cos(2*pi*(f_1-B)*nT);
% filter responses y[n]:
y_f1 = filter(b_coeff,a_coeff,x_f1);
y_hiCorner = filter(b_coeff,a_coeff,x_hiCorner);
y_f1plusB = filter(b_coeff,a_coeff,x_f1plusB);
y_loCorner = filter(b_coeff,a_coeff,x_loCorner);
y_f1minusB = filter(b_coeff,a_coeff,x_f1minusB);
plotPeriods = 30;
figure(7)
% Passband Response
hold on
stem(nT*1000, spline(nT,x_f1,nT),'.','r-','MarkerSize',13); % x(nT)
%interpolated 'x(t)'
plot(nnT*1000, spline(nT,x_f1,nnT),'r-');
stem(nT*1000, spline(nT,y_f1,nT),'.','b-','MarkerSize',13); % y(nT)
plot(nnT*1000, spline(nT,y_f1,nnT),'b-');
%interpolated 'x(t)'
axis([0 plotPeriods/f_1*1000 -1.2 1.2])
title('Filter Response to cos(2*pi*f_1*nT_s)
(Passband Response)')
xlabel('Time (ms)')
ylabel('x[nT_s](red); y[nT_s](blue)')
hold off
figure(8)
% Response at f_1+B/2 (-3dB Response plot)
subplot(4,1,1);
hold on
stem(nT*1000, spline(nT,x_hiCorner,nT),'.','r-','MarkerSize',13); % x(nT)
plot(nnT*1000, spline(nT,x_hiCorner,nnT),'r-');
%interpolated 'x(t)'
stem(nT*1000, spline(nT,y_hiCorner,nT),'.','b-','MarkerSize',13); % y(nT)
plot(nnT*1000, spline(nT,y_hiCorner,nnT),'b-');
%interpolated 'x(t)'
axis([0 plotPeriods/f_1*1000 -1.2 1.2])
title('Filter Response to cos(2*pi*(f_1+B/2)*nT_s)
(-3dB Response)')

33

Tim Perry
V00213455
2009-05-15
xlabel('Time (ms)')
ylabel('x[nT_s](red); y[nT_s](blue)')
hold off
% Response at f_1+B
subplot(4,1,2);
hold on
stem(nT*1000, spline(nT,x_f1plusB,nT),'.','r-','MarkerSize',13); % x(nT)
plot(nnT*1000, spline(nT,x_f1plusB,nnT),'r-');
%interpolated 'x(t)'
stem(nT*1000, spline(nT,y_f1plusB,nT),'.','b-','MarkerSize',13); % y(nT)
plot(nnT*1000, spline(nT,y_f1plusB,nnT),'b-');
%interpolated 'x(t)'
axis([0 plotPeriods/f_1*1000 -1.2 1.2])
title('Filter Response to cos(2*pi*(f_1+B)*nT_s)')
xlabel('Time (ms)')
ylabel('x[nT_s](red); y[nT_s](blue)')
hold off
% Response at f_1-B/2 (-3dB Response plot)
subplot(4,1,3);
hold on
stem(nT*1000, spline(nT,x_loCorner,nT),'.','r-','MarkerSize',13); % x(nT)
plot(nnT*1000, spline(nT,x_loCorner,nnT),'r-');
%interpolated 'x(t)'
stem(nT*1000, spline(nT,y_loCorner,nT),'.','b-','MarkerSize',13); % y(nT)
plot(nnT*1000, spline(nT,y_loCorner,nnT),'b-');
%interpolated 'x(t)'
axis([0 plotPeriods/f_1*1000 -1.2 1.2])
(-3dB Response)')
title('Filter Response to cos(2*pi*(f_1-B/2)*nT_s)
xlabel('Time (ms)')
ylabel('x[nT_s](red); y[nT_s](blue)')
hold off
% Response at f_1-B
subplot(4,1,4);
hold on
stem(nT*1000, spline(nT,x_f1minusB,nT),'.','r-','MarkerSize',13); % x(nT)
plot(nnT*1000, spline(nT,x_f1minusB,nnT),'r-');
%interpolated 'x(t)'
stem(nT*1000, spline(nT,y_f1minusB,nT),'.','b-','MarkerSize',13); % y(nT)
plot(nnT*1000, spline(nT,y_f1minusB,nnT),'b-');
%interpolated 'x(t)'
axis([0 plotPeriods/f_1*1000 -1.2 1.2])
title('Filter Response to cos(2*pi*(f_1-B)*nT_s)')
xlabel('Time (ms)')
ylabel('x[nT_s](red); y[nT_s](blue)')
hold off
% Checking Phase Response at f_1+B/2
(-3dB Response plot zoomed in)
figure(9)
hold on
stem(nT*1000, spline(nT,x_hiCorner,nT),'.','r-','MarkerSize',13); % x(nT)
plot(nnT*1000, spline(nT,x_hiCorner,nnT),'r-');
%interpolated 'x(t)'
stem(nT*1000, spline(nT,y_hiCorner,nT),'.','b-','MarkerSize',13); % y(nT)
plot(nnT*1000, spline(nT,y_hiCorner,nnT),'b-');
%interpolated 'x(t)'
axis([15/f_1*1000 20/f_1*1000 -1.2 1.2])
title('Zoomed in Filter Response to cos(2*pi*(f_1+B/2)*nT_s)
(-3dB Response)')
xlabel('Time (ms)')
ylabel('x[nT_s](red); y[nT_s](blue)')
hold off
% ------------------------------------------% Amplitude vesus Frequency and Phase
% -------------------------------------------

34

Tim Perry
V00213455
2009-05-15
% computes the dft of the responses y[n]
Y_f1 = DFT_compute(y_f1,N);
Y_hiCorner = DFT_compute(y_hiCorner,N);
Y_f1plusB = DFT_compute(y_f1plusB,N);
Y_loCorner = DFT_compute(y_loCorner,N);
Y_f1minusB = DFT_compute(y_f1minusB,N);
freq = linspace(0,f_s,N);
figure(10)
% FFT of response to center freq
subplot(2,1,1)
plot(freq,abs(Y_f1)/500);
grid on;
axis([0 f_s/2 0 1.2])
title('Magnitude Response |Y(f)| for input x(nT_s)=cos(2*pi*f_1*nT_s)')
ylabel('|Y(f)|')
xlabel('f (Hz)')
subplot(2,1,2)
plot(freq,angle(Y_f1));
grid on;
axis([0 f_s/2 -2 2])
title('Phase Response of Y(f) for input x(nT_s)=cos(2*pi*f_1*nT_s)')
ylabel('Theta (rad)')
xlabel('Frequency (Hz)')

figure(11)
% FFT of response to f_1+B/2
subplot(4,1,1)
plot(freq,abs(Y_hiCorner)/500,'b-');
grid on;
axis([0 f_s/2 0 1.2])
title('Magnitude Response |Y(f)| for inputs x(nT_s)=cos(2*pi*(f_1+B/2)*nT_s)')
ylabel('|Y(f)|')
xlabel('f (Hz)')
subplot(4,1,2)
plot(freq,angle(Y_hiCorner));
grid on;
axis([0 f_s/2 -2 2])
title('Phase Response of Y(f) for inputs x(nT_s)=cos(2*pi*(f_1+B/2)*nT_s)')
ylabel('Theta (rad)')
xlabel('Frequency (Hz)')
% FFT of response to f_1+B/2
subplot(4,1,3)
plot(freq,abs(Y_loCorner)/500,'r-');
grid on;
axis([0 f_s/2 0 1.2])
title('Magnitude Response |Y(f)| for inputs x(nT_s)=cos(2*pi*(f_1-B/2)*nT_s)')
ylabel('|Y(f)|')
xlabel('f (Hz)')
subplot(4,1,4)
plot(freq,angle(Y_loCorner),'r-');
grid on;
axis([0 f_s/2 -2 2])
title('Phase Response of Y(f) for inputs x(nT_s)=cos(2*pi*(f_1-B/2)*nT_s)')

35

Tim Perry
V00213455
2009-05-15
ylabel('Theta (rad)')
xlabel('Frequency (Hz)')

figure(12)
% FFT of response to f_1+B
subplot(4,1,1)
plot(freq,abs(Y_f1plusB)/500,'b-');
grid on;
axis([0 f_s/2 0 1.2])
title('Magnitude Response |Y(f)| for inputs x(nT_s)=cos(2*pi*(f_1+B)*nT_s)')
ylabel('|Y(f)|')
xlabel('f (Hz)')
subplot(4,1,2)
plot(freq,angle(Y_f1plusB),'b-');
grid on;
axis([0 f_s/2 -2 2])
title('Phase Response of Y(f) for inputs x(nT_s)=cos(2*pi*(f_1+B)*nT_s)')
ylabel('Theta (rad)')
xlabel('Frequency (Hz)')
% FFT of response to f_1-B
subplot(4,1,3)
plot(freq,abs(Y_f1minusB)/500,'r-');
grid on;
axis([0 f_s/2 0 1.2])
title('Magnitude Response |Y(f)| for inputs x(nT_s)=cos(2*pi*(f_1-B)*nT_s)')
ylabel('|Y(f)|')
xlabel('f (Hz)')
subplot(4,1,4)
plot(freq,angle(Y_f1minusB),'r-');
grid on;
axis([0 f_s/2 -2 2])
title('Phase Response of Y(f) for inputs x(nT_s)=cos(2*pi*(f_1-B)*nT_s)')
ylabel('Theta (rad)')
xlabel('Frequency (Hz)')

%====================================================================
% Convolution with h[n] (Question 11)
%====================================================================
f_s=8000;
f_1=2000;
T_s=1/f_s
theta=2*pi*f_1*(T_s:T_s:150/f_s);
x_Conv= cos(theta);
n=0:1:150-1;
N=150;
z=exp(j.*2.*pi.*n./N);
figure(13)
hold on
y=conv(h1 , x_Conv);
y_Real = real(y);
plot(x_Conv,'r-'); %x(nT_s]

36

Tim Perry
V00213455
2009-05-15
plot(y_Real);
%y(nT_s]
grid on;
axis([0 plotPeriods/f_1*10000 -1.2 1.2])
title('Filter Response by Convolution cos(2*pi*(f_1)*nT_s) * h[nT_s]')
xlabel('nT')
ylabel('x[nT_s](red); y[nT_s](blue)')
hold off

% ------------------------------------------% Amplitude vesus Frequency and Phase of convoluted response


% ------------------------------------------% computes the dft of the responses y[n]
Y_f1Conv = DFT_compute(y_Real,N);

freq = linspace(0,f_s,N);
figure(14)
% FFT of response to center freq
subplot(2,1,1)
plot(freq,abs(Y_f1Conv)/44);
grid on;
axis([0 f_s/2 0 1.2])
title('Magnitude Response |Y(f)| for Convolution cos(2*pi*(f_1)*nT_s) * h[nT_s]')
ylabel('|Y(f)|')
xlabel('f (Hz)')
subplot(2,1,2)
plot(freq,angle(Y_f1Conv));
grid on;
axis([0 f_s/2 -2 2])
title('Phase Response of Y(f) for Convolution cos(2*pi*(f_1)*nT_s) * h[nT_s]')
ylabel('Theta (rad)')
xlabel('Frequency (Hz)')

37

Tim Perry
V00213455
2009-05-15

APPENDIX B
Phase 3 Implementing Wha-wha Filter
MATLAB Code
%==========================================================================
% a2_wha.m
Author: Tim Perry
% Elec484: DAFX
V00213455
% Assignment 2, Phase 3
2009-05-18
%
% Uses the second order BPF designed in Phase 1 for a wha-wha effect.
% Operation: a cosine wave modulates the center frequency of the BPF.
%==========================================================================
clear all;
close all;

% Read Input Sound file


[audio,f_s,bps]=wavread('white_noise.wav');
% Get length of file in samples
numSamples = length(audio)
T = numSamples/f_s
N = 50 %block size for windowed FFT
%---------------------------------------------------------------% Filter coefficients
%---------------------------------------------------------------% 'b' coefficients are constant
b = [0.037765, 0, -0.037765]
% 'a 'parameters are modulated by pole movements in the complex plane.
% Changes in the pole locations alter the passband of the BPF filter.
p = 0.9615;
a0 = 1;
a2 = p^2;
whaSpeed = 0.2;
%---------------------------------------------------------------% Apply wha-wha filter to audio file
%---------------------------------------------------------------for n = (0:N:numSamples-1)
f = (f_s/2)*(1+cos(2*pi*whaSpeed*n/f_s));
phi = 2*pi*f/f_s;
a1 = -2*p*cos(phi);
a = [a0 a1 a2]
y(n+1:n+N) = filter(b, a, audio(n+1:n+N));
end
y = (1/(2.*max(y))).*y;
wavwrite(y,f_s,bps,'whawha_output.wav');

38

You might also like