Professional Documents
Culture Documents
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.
2.
3.
4.
5.
Difference Equations....................................................................................................................... 11
6.
7.
8.
9.
10.
11.
Tim Perry
V00213455
2009-05-15
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
1 + 1
*0.9615 + *0.9615
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).
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.
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.
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:
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
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
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
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 ;
(8)
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
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.
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
; 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
g
+
*0.9615 + *0.9615
9 = #. #:#K' +
#. #7%7#8l
#. #7%7#8l
+
"#. %& ' + "#. %& '
(9)
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
f
+ 0.924471f +
g = 0.037765
0.037765
r = # = stI/2
g
+ 0.924471
x
E#. #8K& ' uvw E ( GG
( + #. %(::8
16
Tim Perry
V00213455
2009-05-15
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.
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.
=
| 8000
=
= 8. K (' 9
}
1024
18
Tim Perry
V00213455
2009-05-15
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
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
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.
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
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])
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;
%
%
%
%
%
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
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;
38