+ All Categories
Home > Documents > TPerry-A2_whafilter

TPerry-A2_whafilter

Date post: 14-Feb-2016
Category:
Upload: antoniopallone
View: 212 times
Download: 0 times
Share this document with a friend
Description:
TPerry-A2_whafilter
Popular Tags:
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
Transcript

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

1

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

2

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)

Tim Perry

V00213455

2009-05-15

3

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��� = 2��100���

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 |�./012| is set equal to 1 and

evaluated at � = �/2.

�./012 = ��./01 − 12./01 + 12�/01 − *0.9615��/01 + *0.9615�

|�./0345/462| = 1 = ��|./0345/46 − 12./0345/46 + 12||�/0345/46 − *0.9615��/0345/46 + *0.9615�|

where 2��/� = �/2

Simplifications yields:

�� = 0.5�1 − �� = 1 − �0.9615)/2 = #. #788&'

Tim Perry

V00213455

2009-05-15

4

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:

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

(4)

9��� = #. #788&'. − �;(2� + #. %(::8 �;(�

(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

5

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

6

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

7

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

8

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�� + �2�C

Therefore, at -3dB from unity gain, the magnitude should be:

H E� + FG = 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

9

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

10

Table 1: Comparison of IDFT Results (IR sample values) for first 16 samples

n h[n] (manual IDFT) h[n] (Matlab IFFT)

0 0.0378 0.0378

1 0 0

2 -0.0741 -0.0741

3 0 0

4 0.0712 0.0712

5 0 0

6 -0.0685 -0.0685

7 0 0

8 0.0658 0.0658

9 0 0

10 -0.0633 -0.0633

11 0 0

12 0.0609 0.0609

13 0 0

14 -0.0585 -0.0585

15 0 0

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

Tim Perry

V00213455

2009-05-15

11

4.3. Frequency Resolution

The frequency resolution is equal to the window size N divided by the sampling frequency.

=# = =IJ (7)

�� = 8000��1024 = 8. K (' 9�

5. Difference Equations

5.1. Difference Equation by Analysis

From (4), the transfer function of the BPF is:

���� = 0.037765�� − 1��� + 0.924471� = M���

N���

���� = 0.037765�1 − �;��1 + 0.924471�;� = M���

N���

Eliminating H(z):

Y�z� ∗ �1 + 0.924471�;� = N��� ∗ 0.037765�1 − �;�

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.

Tim Perry

V00213455

2009-05-15

12

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 x[n] h[n] = y[n]

-2 0 0

-1 0 0

0 1 0.037765

1 0 0

2 0 -0.07268

3 0 0

4 0 0.067188

5 0 0

6 0 -0.06211

7 0 0

8 0 0.057422

9 0 0

10 0 -0.05309

11 0 0

12 0 0.049076

13 0 0

14 0 -0.04537

15 0 0

16 0 0.041942

17 0 0

18 0 -0.03877

19 0 0

20 0 0.035846

21 0 0

22 0 -0.03314

23 0 0

24 0 0.030636

25 0 0

Tim Perry

V00213455

2009-05-15

13

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 x[n] h[n] = y[n]

-2 0 0

-1 0 0

0 1 0.03776500

1 0 0

2 0 -0.07267765

3 0 0

4 0 0.06718838

5 0 0

6 0 -0.06211371

7 0 0

8 0 0.05742232

9 0 0

10 0 -0.05308527

11 0 0

12 0 0.04907579

13 0 0

14 0 -0.04536915

15 0 0

16 0 0.04194246

17 0 0

18 0 -0.03877459

19 0 0

20 0 0.03584598

Tim Perry

V00213455

2009-05-15

14

21 0 0

22 0 -0.03313857

23 0 0

24 0 0.03063565

25 0 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:

���� = 0.037855 − 0.072670�; − 0.067189�;Z + �.�[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

Tim Perry

V00213455

2009-05-15

15

7.2. Partial Fractions with First Order Factors

���� = 0.037765�� − 1��� + 1��� − *0.9615��� + *0.9615� = 0.037765�� − 1�

�� + 0.924471�

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

��� − *0.9615��� + *0.9615� = f� + �

� − *0.9615 + g� + *0.9615

f = h�. ����� i

\j�= h 0.037765�� − 1�

�� − *0.9615��� + *0.9615�i\j�

= −0.0377650.924471 = −0.04085

� = h�� − *0.9615�. ����� i

\j0�.k[`= h0.037765�� − 1�

��� + *0.9615� i\j0�.k[`

= −0.072678−1.84896 = 0.039307

g = h�� + *0.9615�. ����� i

\j;0�.k[`= h0.037765�� − 1�

��� − *0.9615� i\j;0�.k[`

= −0.072678−1.84896 = 0.039307

The transfer function takes the form:

���� = f + ��� − *0.9615 + �g

� + *0.9615

9��� = −#. #:#K' + #. #7%7#8l� − "#. %& ' + #. #7%7#8l

� + "#. %& ' (9)

Taking the inverse Z transform:

ℎSXU = −0.04085cSXU + 0.039307m�*0.9615�n + �−*0.9615�no. pSXU

Tim Perry

V00213455

2009-05-15

16

7.2. Partial Fractions with Quadratic Factors

���� = 0.037765�� − 1��� + 0.924471�

����� = 0.037765�� − 1�

��� + 0.924471� = f� + �� − g

�� + 0.924471�

Using the residue method to solve for A.

f = h�. ����� i

\j�= h0.037765�� − 1�

�� + 0.924471� i\j�

= −0.0377650.924471 = −#. #:#K'

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: A + B = 0.037765 → > = #. #8K& '

1st

Order Terms: r = # = stI��/2�

The transfer function takes the form:

���� = f + ���� − g��� + 0.924471�

9��� = −#. #:#K' + � E#. #8K& '� − uvw Ex(GG��( + #. %(::8 �

Taking the inverse Z transform:

ℎSXU = −0.04085cSXU + 0.07862 d�0.924471n� cos E�2 XGe . pSXU

Tim Perry

V00213455

2009-05-15

17

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

Difference Equations

(MATLAB & Excel) WSXU= 0.037765YSXU− 0.037765YSX − 2U− 0.924471WSX − 2U

Long Division

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

Partial Fractions

(1st Order Factors) ℎSXU= −0.04085cSXU+ 0.039307m�*0.9615�n

+ �−*0.9615�no. pSXU

Partial Fractions

(Quadratic Factors) ℎSXU= −0.04085cSXU+ 0.078 d0.924n cos E�

2 XGe . pSXU

n h[n] h[n] = y[n] h[n] h[n] h[n]

0 0.0378 0.03776500 0.037855 0.037764 0.037764

1 0 0 0 0 0

2 -0.0741 -0.07267765 -0.07264 -0.072677 -0.072677

3 0 0 0 0 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

Tim Perry

V00213455

2009-05-15

18

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��1024 = 8. K (' 9�

Tim Perry

V00213455

2009-05-15

19

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 filter’s 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)

Tim Perry

V00213455

2009-05-15

20

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.

Tim Perry

V00213455

2009-05-15

21

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� + FG = 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.

Tim Perry

V00213455

2009-05-15

22

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.

Tim Perry

V00213455

2009-05-15

23

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.

Tim Perry

V00213455

2009-05-15

24

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.

Tim Perry

V00213455

2009-05-15

25

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

Tim Perry

V00213455

2009-05-15

26

Figure 16: Response of cosine wave oscillating at filter’s 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.

Tim Perry

V00213455

2009-05-15

27

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 filter’s ‘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

Tim Perry

V00213455

2009-05-15

28

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

Tim Perry

V00213455

2009-05-15

29

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)')

Tim Perry

V00213455

2009-05-15

30

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;

% 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.

r = 0.961494; % magnitude of poles in complex plane

K = 0.037765;

Tim Perry

V00213455

2009-05-15

31

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;

Tim Perry

V00213455

2009-05-15

32

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]')

Tim Perry

V00213455

2009-05-15

33

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)

plot(nnT*1000, spline(nT,x_f1,nnT),'r-'); %interpolated 'x(t)'

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)')

Tim Perry

V00213455

2009-05-15

34

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])

title('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

% 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

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

Tim Perry

V00213455

2009-05-15

35

% 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)')

Tim Perry

V00213455

2009-05-15

36

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]

Tim Perry

V00213455

2009-05-15

37

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)')

Tim Perry

V00213455

2009-05-15

38

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');