Documente Academic
Documente Profesional
Documente Cultură
k=0
a
k
y[n k] =
M
r=0
b
r
x[n r] (1)
The MATLAB implementation of the lter can be found in appendix A.3 on page 16. The function
takes the arguments:
A coes Vector containing the polynomial a
k
coecients, which correspond to the system poles.
B coes Vector containing the polynomial b
r
coecients, which correspond to the system zeros.
output state The systems previous output states
input state The systems current and previous inputs
The function myfilter returns the current output sample y[n].
6
2.3 Assignment 3
To implement the function myfilter in MATLAB a for loop is used, to simulate the real-time
behavior of the input signal.
The lters impulse response can be seen in gure 6. The input impulse is delayed to location
n = 5, and its Bode plot in gure 4.
The power spectral density of the input signal and the ltered output signal is displayed in gure 7.
The top plot shows the unltered input signal. The bottom plot shows the output signal ltered
using the myfilter function. The disturbance has clearly been removed from the signal. Unfor-
tunately the lter also removes some of the signal content near the disturbance in the frequency
domain.
The MATLAB code to the lter implementation can be found in appendix A.4 on page 17.
0 0.5 1 1.5 2
x 10
4
400
300
200
100
0
Frequency (Hz)
P
h
a
s
e
(
d
e
g
r
e
e
s
)
0 0.5 1 1.5 2
x 10
4
400
300
200
100
0
100
Frequency (Hz)
M
a
g
n
i
t
u
d
e
(
d
B
)
Assignment 3 Bode plot
Figure 4: Bode plot of the myfilter
7
1 0.5 0 0.5 1
1
0.8
0.6
0.4
0.2
0
0.2
0.4
0.6
0.8
1
2
Real Part
I
m
a
g
i
n
a
r
y
P
a
r
t
Assignment 3 Pole/Zero plot
Figure 5: Pole/Zero plot of the system
1 2 3 4 5 6 7 8 9 10 11
1.5
1
0.5
0
0.5
1
Sample n
A
m
p
l
i
t
u
d
e
Assignment 3 Impulse Response
Figure 6: Impulse Response of the myfilter implementation. Input impulse is delayed 5 samples
8
0 5 10 15 20
150
100
50
0
Frequency (kHz)
P
o
w
e
r
/
f
r
e
q
u
e
n
c
y
(
d
B
/
H
z
)
Assignment 3 Periodogram Power Spectral Density Estimate, unfiltered
0 5 10 15 20
160
140
120
100
80
60
40
20
Frequency (kHz)
P
o
w
e
r
/
f
r
e
q
u
e
n
c
y
(
d
B
/
H
z
)
Assignment 3 Periodogram Power Spectral Density Estimate, filtered
Figure 7: Top: Unltered input signal. Bottom: Filtered output signal
9
2.4 Assignment 4
The design criteria for the IIR lowpass lter is:
Cuto Frequency
p
= /10
Stop Band Frequency
s
= /5
Minimum Pass Band Magnitude PB
min
= 6 dB
Maximum Pass Band Magnitude PB
max
= 3 dB
Maximum Stop Band Magnitude SB
max
= 50 dB
The nal IIR lter design (see code in appendix A.5 on page 19) results in:
Filter Order 8
Cuto Frequency f
3 dB
= 2355.5 Hz
Stop Band Frequency f
50 dB
= 4410 Hz
The Bode plot in gure 8 shows the magnitude and phase responce of the system. In the lower
frequencies the responce is fairly smooth where the signal goes trough the lter without much
attenuation. In the higher frequencies where the attenuation is high, their exact responce is
usually of less importance since the signal will be extremely attenuated.
The pole/zero plot of the lter is shown in gure 9.
The impulse response of the lter is shown in gure 10 resambles a dampened sinusoid.
0 0.5 1 1.5 2
x 10
4
3000
2500
2000
1500
1000
500
0
Frequency (Hz)
P
h
a
s
e
(
d
e
g
r
e
e
s
)
0 0.5 1 1.5 2
x 10
4
500
400
300
200
100
0
100
Frequency (Hz)
M
a
g
n
i
t
u
d
e
(
d
B
)
Assignment 4 Bode plot
Figure 8: Bode plot of the IIR lowpass lter
10
1 0.5 0 0.5 1
1
0.8
0.6
0.4
0.2
0
0.2
0.4
0.6
0.8
1
Real Part
I
m
a
g
i
n
a
r
y
P
a
r
t
Assignment 4 Pole/Zero plot
Figure 9: Pole/Zero plot of IIR lowpass lter system function
0 20 40 60 80 100 120
0.04
0.02
0
0.02
0.04
0.06
0.08
0.1
0.12
Sample n
A
m
p
l
i
t
u
d
e
Assignment 4 Impulse Response
Figure 10: The impulse response of the IIR lowpass lter
11
2.5 Assignment 5
The design criteria for the FIR lowpass lter is:
Cuto Frequency
p
= /10
Stop Band Frequency
s
= /5
Minimum Pass Band Magnitude PB
min
= 6 dB
Maximum Pass Band Magnitude PB
max
= 3 dB
Maximum Stop Band Magnitude SB
max
= 50 dB
The nal FIR lter design (see code in appendix A.6 on page 20) results in:
Filter Order 34
Cuto Frequency f
3 dB
= 2204.5 Hz
Stop Band Frequency f
50 dB
= 4379.5 Hz
The Bode plot in gure 11 shows the magnitude and phase responce of the system. The magnitude
of the system is smooth for the lower frequencies. The signal will be distorted by the comblter
eect seen in the higher frequencies, but because the average high attenuation it wont be as
noticeable on the output signal. The phase is linear in the pass band, so the eect on the output
signal is not so problematic. On the higher frequencies the phase suers from the comblter eect.
Due to the higher attenuation this should not aect the overall response much.
The pole/zero plot of the lter is shown in gure 12.
The impulse response of the lter is shown in gure 13.
0 0.5 1 1.5 2
x 10
4
700
600
500
400
300
200
100
0
Frequency (Hz)
P
h
a
s
e
(
d
e
g
r
e
e
s
)
0 0.5 1 1.5 2
x 10
4
150
100
50
0
Frequency (Hz)
M
a
g
n
i
t
u
d
e
(
d
B
)
Assignment 5 Bode plot
Figure 11: Bode plot of the FIR lowpass lter
12
1 0.5 0 0.5 1 1.5
1
0.8
0.6
0.4
0.2
0
0.2
0.4
0.6
0.8
1
34
Real Part
I
m
a
g
i
n
a
r
y
P
a
r
t
Assignment 5 Pole/Zero plot
Figure 12: Pole/Zero plot of FIR lter system
0 5 10 15 20 25 30 35 40
0.02
0
0.02
0.04
0.06
0.08
0.1
0.12
Sample n
A
m
p
l
i
t
u
d
e
Assignment 5 Impulse Response
Figure 13: The impulse response of the FIR lter
13
A MATLAB Code
A.1 Lab 1 - Main Program
1 % Andre Lundkvist and Rikard Qvarnstrm
2 %
3 % Lab 1
4 % Filter Design and Evaluation in MATLAB
5
6 function lab1(assignment )
7 cl c ;
8 cl ose al l ;
9
10 i f nargin < 1
11 assignment = 3;
12 end
13
14 % Note to self:
15 % use profile to check performance
16 prof = false ;
17 i f prof == true
18 profile on;
19 ti c ;
20 end
21
22 [x,fs] = wavread(boyNY .wav );
23
24 % Chose which assignment to run:
25 switch assignment
26
27 case 1
28 % Code for Assignment 1 <here>
29
30 case 2
31 % See the myfilter() function
32
33 case 3
34 % Code for Assignment 3 <here>
35
36 case 4
37 % Code for Assignment 4 <here>
38
39 case 5
40 % Code for Assignment 5 <here>
41
42 otherwise
43 % If input does not match assignment:
44 f pri ntf (Wrong input \n);
45
46 end
47
48 i f prof == true
49 profile off ;
50 profile viewer ;
51 trun = toc;
52 f pri ntf (Total time elapsed = %g\n,trun);
53 end
54 f pri ntf (\n);
55 end
14
A.2 Lab 1 - Assignment 1
1 % Andre Lundkvist and Rikard Qvarnstrm
2 %
3 % Lab 1, Assignment 1
4 % Notch filter design
5
6 % Analysis
7 h = spectrum .periodogram ;
8 hpsd = psd(h,x,Fs,fs);
9 fi gure ; % fig 1
10 plot(hpsd)
11 t i t l e (Assignment 1 - Periodogram Power Spectral Density Estimate );
12 print -depsc -tiff -r300 Lab_1_Ass_1_PSD
13
14 fx = real ( f f t (x,fs));
15 freq = find(fx == max(fx)); % find the most dominating frequencys
16 freq (2) = fs -freq (2);
17 meanfreq = mean(freq); % take the mean of the 2 detected peaks
18 %figure;
19 %plot(fx);
20 %axis([0 fs/2 min(fx) max(fx)]);
21
22 % Design
23 angle = 2* pi *meanfreq /fs; % the angle for the zeroes placement
24 B = [1 -2* cos(angle) cos(angle)^2+ si n(angle)^2]; % polynomial coefficients
25 A = [1 0 0]; % two poles in the center of the unit circle
26 [hplot , f pl ot ]= freqz(B,A,fs,fs);
27
28 fi gure ; % fig 2
29 zplane (B,A); % the pole and zeroes plot
30 t i t l e (Assignment 1 - Pole/Zero plot);
31 print -depsc -tiff -r300 Lab_1_Ass_1_PZ
32
33 fi gure ; % fig 3
34 freqzplot (hplot ,fpl ot ,Hz); % bode plot (frequency and phase)
35 t i t l e (Assignment 1 - Bode plot );
36 print -depsc -tiff -r300 Lab_1_Ass_1_Bode
37
38 % Filter the input
39 xf = f i l t e r (B, A, x);
40
41 % Play the music files
42 %sound(x, fs); % original file
43 %sound(xf, fs); % After filtering
15
A.3 Lab 1 - Assignment 2
1 % Andre Lundkvist and Rikard Qvarnstrm
2 %
3 % Lab 1, Assignment 2
4 % Filter Design and Evaluation in MATLAB
5
6 % Filter Routine Implementation
7 function output = myfilter (A_coeffs , B_coeffs , output_state , input_state )
8
9 % To be able to divide by a0 and multiply with the other coefficients later
10 a0 = A_coeffs (1);
11 A_coeffs = A_coeffs (2:end);
12
13 % To calculate the lengts only once
14 L_A = length(A_coeffs );
15 L_B = length(B_coeffs );
16 L_OS = length(output_state);
17 L_IS = length(input_state );
18
19 % Fault handling, in case that the size of the input vectors is different
20 % buffer the smaller one with zeros so they become the same length
21
22 i f ( L_IS < L_B)
23 input_state = [ zeros ([1, L_B -L_IS]) input_state ];
24 el s ei f (L_IS > L_B )
25 %B_coeffs = [zeros([1, L_ISL_B]) B_coeffs];
26 input_state = input_state (end-L_B +1:end);
27 end
28 i f ( L_OS < L_A)
29 output_state = [ zeros ([1, L_A -L_OS ]) output_state];
30 el s ei f (L_OS > L_A )
31 %A_coeffs = [zeros([1, L_OSL_A]) A_coeffs];
32 output_state = output_state(end-L_A +1:end);
33 end
34
35 xsum = sum(B_coeffs .* input_state );
36 ysum = sum(A_coeffs .* output_state);
37 output = ( xsum -ysum)/a0;
38
39 end
16
A.4 Lab 1 - Assignment 3
1 % Andre Lundkvist and Rikard Qvarnstrm
2 %
3 % Lab 1, Assignment 3
4 % Filter Design and Evaluation in MATLAB
5
6 fx = real ( f f t (x,fs));
7 freq = find(fx == max(fx)); % find the most dominating frequencys
8 freq (2) = fs -freq (2);
9 meanfreq = mean(freq); % take the mean of the 2 detected peaks
10
11 scale = 1; % Unused when set to 1
12 angle = 2* pi *meanfreq /fs; % the angle for the zeroes placement
13 B = [1 -2* cos(angle)*scale (( cos(angle)*scale)^2+( si n(angle)*scale)^2) ]; %
polynomial coefficients
14 A = [1 0 0]; % two poles in the center of the unit circle
15
16 zplane (B,A); % the pole and zeroes plot
17 t i t l e (Assignment 3 - Pole/Zero plot);
18 print -depsc -tiff -r300 Lab_1_Ass_3_PZ
19
20 fi gure;
21 [hplot , f pl ot ]= freqz(B,A,fs,fs);
22 freqzplot (hplot ,fpl ot ,Hz); % bode plot (frequency and phase)
23 t i t l e (Assignment 3 - Bode plot );
24 print -depsc -tiff -r300 Lab_1_Ass_3_Bode
25
26 xlength = length(x);
27 xf = zeros (1, xlength +1) ; % memory allocation
28
29 % Blength for time optimization
30 Alength = length(A);
31 Blength = length(B);
32 f pri ntf (0 %%\ n);
33 for n = Blength +1: xlength
34 xf(n) = myfilter (A, B, xf(n-Alength +1:n-1) , x(n-Blength +1: n) );
35 % ~time left
36 i f mod(n ,10000) == 0
37 f pri ntf (%i %%\n,round((n/xlength )*100));
38 end
39 end
40 f pri ntf (\n 100 %% Done\n\n);
41
42 wavwrite(xf,fs,process3 .wav );
43
44 h = spectrum .periodogram ;
45 hpsd = psd(h,x,Fs,fs);
46 hpsd2 = psd(h,xf ,Fs,fs);
47 fi gure;
48 subplot (2,1,1);plot(hpsd);
49 t i t l e (Assignment 3 - Periodogram Power Spectral Density Estimate , unfiltered );
50 subplot (2,1,2);plot(hpsd2);
51 t i t l e (Assignment 3 - Periodogram Power Spectral Density Estimate , filtered );
52 print -depsc -tiff -r300 Lab_1_Ass_3_PSD
53
54
55 % Send only an unit impulse to get
56 % impulse response of filter.
57 x = zeros (1 ,10) ;
58 x(5) = 1;
59
60 xlength = length(x);
61 xf = zeros (1, xlength +1) ; % memory allocation
62
63 % Blength for time optimization
64 Alength = length(A);
17
65 Blength = length(B);
66 f pri ntf (0 %%\ n);
67 for n = Blength +1: xlength
68 xf(n) = myfilter (A, B, xf(n-Alength +1:n-1) , x(n-Blength +1: n) );
69 % ~time left
70 i f mod(n ,10000) == 0
71 f pri ntf (%i %%\n,round((n/xlength )*100));
72 end
73 end
74 f pri ntf (\n 100 %% Done\n\n);
75
76 fi gure;
77 stem(xf);
78 xlabel (Sample n);
79 ylabel (Amplitude );
80 t i t l e (Assignment 3 - Impulse Response );
81 print -depsc -tiff -r300 Lab_1_Ass_3_Impulse_Response
82
83 % Play the music files
84 %sound(x, fs); % original file
85 %sound(xf, fs); % After filtering
18
A.5 Lab 1 - Assignment 4
1 % Andre Lundkvist and Rikard Qvarnstrm
2 %
3 % Lab 1, Assignment 4
4 % Filter Design and Evaluation in MATLAB
5
6 % Cutoff frequency wp = pi/10,
7 % Stop band frequency ws = pi/5,
8 % Minimum pass band magnitude 6dB,
9 % Maximum pass band magnitude 3dB,
10 % Maximum stop band magnitude 50dB.
11 wp = pi /10;
12 ws = pi /5;
13 minpb = 6;
14 maxpb = 3;
15 maxsb = 50;
16
17 % normalized
18 wpn = wp/pi ;
19 wsn = ws/pi ;
20
21 [N,Wn] = buttord (wpn ,wsn ,minpb ,maxsb );
22 [B,A] = butter (N,Wn ,low );
23
24 [hplot , f pl ot ]= freqz(B,A,fs,fs);
25 hp = 20* log10(abs(hplot)); % Magnitude in dB
26
27 % Find the 6dB and 50dB limits
28 f6dB = 1; f50dB = 1; % fail safe
29 for i = 1:length(hp)
30 i f hp(i) > -6
31 f6dB = i;
32 el s ei f hp(i) < -50
33 f50dB = i;
34 break;
35 end;
36 end;
37 f pri ntf (Order = %g\n, N);
38 f pri ntf ( -3dB frequency = %g Hz\n, f pl ot(f6dB));
39 f pri ntf (-50 dB frequency = %g Hz\n, f pl ot(f50dB));
40
41 fi gure;
42 zplane (B,A); % the pole and zeroes plot
43 t i t l e (Assignment 4 - Pole/Zero plot);
44 print -depsc -tiff -r300 Lab_1_Ass_4_PZ
45
46 fi gure;
47 freqzplot (hplot ,fpl ot ,Hz); % bode plot (frequency and phase)
48 t i t l e (Assignment 4 - Bode plot );
49 print -depsc -tiff -r300 Lab_1_Ass_4_Bode
50
51
52 % Send only an unit impulse to get
53 % impulse response of filter.
54 x = zeros (120 ,1) ;
55 x(1) = 1;
56
57 xf = f i l t e r (B,A,x);
58
59 fi gure;
60 stem(xf);
61 xlabel (Sample n);
62 ylabel (Amplitude );
63 t i t l e (Assignment 4 - Impulse Response );
64 print -depsc -tiff -r300 Lab_1_Ass_4_Impulse_Response
19
A.6 Lab 1 - Assignment 5
1 % Andre Lundkvist and Rikard Qvarnstrm
2 %
3 % Lab 1, Assignment 5
4 % Filter Design and Evaluation in MATLAB
5
6 % Cutoff frequency wp = pi/10,
7 % Stop band frequency ws = pi/5,
8 % Minimum pass band magnitude 6dB,
9 % Maximum pass band magnitude 3dB,
10 % Maximum stop band magnitude 50dB.
11 wp = pi /10;
12 ws = pi /5;
13
14 % Educated guess of N
15 N = 34;
16 % normalized
17 wpn = wp/pi ;
18 wsn = ws/pi ;
19
20 B = fir1(N,wpn ,low );
21 % Create the pole vector A and place all poles in origo
22 A = zeros (1,length(B));
23 A(1) = 1;
24 [hplot , f pl ot ]= freqz(B,A,fs,fs);
25 hp = 20* log10(abs(hplot)); % Magnitude in dB
26
27 % Find the 6dB and 50dB limits
28 f6dB = 1; f50dB = 1; % fail safe
29 for i = 1:length(hp)
30 i f hp(i) > -6
31 f6dB = i;
32 el s ei f hp(i) < -50
33 f50dB = i;
34 break;
35 end;
36 end;
37 f pri ntf (Order = %g\n, N);
38 f pri ntf ( -6dB frequency = %g Hz\n, f pl ot(f6dB));
39 f pri ntf (-50 dB frequency = %g Hz\n, f pl ot(f50dB));
40
41 fi gure;
42 zplane (B,A); % the pole and zeroes plot
43 t i t l e (Assignment 5 - Pole/Zero plot);
44 print -depsc -tiff -r300 Lab_1_Ass_5_PZ
45
46 fi gure;
47 freqzplot (hplot ,fpl ot ,Hz); % bode plot (frequency and phase)
48 %plot(fplot./(fs/2),hp);
49 t i t l e (Assignment 5 - Bode plot );
50 print -depsc -tiff -r300 Lab_1_Ass_5_Bode
51 %axis([20 20e3 200 10]);
52
53 % Send only an unit impulse to get
54 % impulse response of filter.
55 x = zeros (40 ,1) ;
56 x(1) = 1;
57
58 xf = f i l t e r (B,A,x);
59
60 fi gure;
61 stem(xf);
62 xlabel (Sample n);
63 ylabel (Amplitude );
64 t i t l e (Assignment 5 - Impulse Response );
65 print -depsc -tiff -r300 Lab_1_Ass_5_Impulse_Response
20