Example 1: First-Order Filters Consider the following filter: y[n] − ay[n ...

24 downloads 42 Views 415KB Size Report
ECE 223. DT Filters. Ver. 1.03. 3. Overview of Discrete-Time Filters. • First-order filters. • Ideal filters. • Practical filters. • Frequency-selective filter specifications.
Overview of Discrete-Time Filters

Discrete-Time Filters Overview

• First-order filters

N 

• Ideal filters

ak y[n − k]

=

k=0

• Practical filters

Y (ejω )

• Frequency-selective filter specifications • Ripple versus filter order tradeoff

=

M 

bk x[n − k]

k=0 M −jwk k=0 bk e X(ejω ) N −jwk a e k k=0

• Discrete-time filters are divided into two categories – Finite impulse response (FIR): h[n] = 0 for some a and b such that −∞ < a < n < b < +∞ – Infinite impulse response (IIR): not FIR

• Application example

• Filters that can be described with difference-equations – FIR: N = 0 – IIR: N > 0 • A simple FIR filter is the moving average filter • A simple IIR filter is the first-order lowpass filter Portland State University

ECE 223

DT Filters

Ver. 1.03

1

Portland State University

Example 1: First-Order Filters

ECE 223

DT Filters

Ver. 1.03

2

Ver. 1.03

4

Example 1: Workspace

Consider the following filter: y[n] − ay[n − 1] = (1 − a)x[n] 1. Solve for the filter’s transfer function 2. Find the cutoff frequency as a function of a

Portland State University

ECE 223

DT Filters

Ver. 1.03

3

Portland State University

ECE 223

DT Filters

Example 1: Ωc versus a

Example 1: H(ejω ) for various a

3.5 1 jω

|H(e )|

3

2

0.5

0

−3

1.5 ∠ H(ejω) (o)

c

ω (rad/sample)

2.5

1 0.5 0

−1

0

1

2

3

−1 0 1 Frequency (rads/sample)

2

3

50 0 −50

0

0.1

0.2

Portland State University

0.3

0.4

ECE 223

0.5 a

0.6

0.7

0.8

DT Filters

0.9

Ver. 1.03

1

−3

5

ECE 223

DT Filters

Ver. 1.03

6

Ver. 1.03

8

Example 1: MATLAB Code %function [] = FirstOrderApplied(); close all;

Intel Closing Daily Price Over 1 Year

36

−2

Portland State University

Example 1: Filtered Signals d nd x n

34

= = = =

load(’Intel.txt’); % Closing daily price length(d); d(nd:-1:1); % Reorder so first element is oldest data (0:nd-1)’; % Discrete time index

%============================================================================== % Plot the relationship between cutoff frequency and a %============================================================================== a = 0.00001:0.01:1; wc = acos((1-4*a+a.^2)./(-2*a));

32 x[n], y0.1[n], y0.6[n]

a=0.1 a=0.2 a=0.3 a=0.4 a=0.5 a=0.6 a=0.7 −2 a=0.8 a=0.9

30

figure FigureSet(1,’LTX’); h = plot(a,wc,’LineWidth’,1.0); ylabel(’\omega_c (rad/sample)’); xlabel(’a’); grid on; box off; AxisSet(8); print -depsc FOCutoff;

28 26 24 Raw signal Cutoff:0.56 Cutoff:0.11

22 20 0

50

100

150

%============================================================================== % Plot the relationship between cutoff frequency and a %============================================================================== a = 0.1:0.1:0.9; w = -pi:0.01:pi; H = zeros(length(a),length(w));

200

for cnt = 1:length(a) [h,w] = freqz(1-a(cnt),[1 -a(cnt)],w); H(cnt,:) = h;

Time (day)

Portland State University

ECE 223

DT Filters

Ver. 1.03

7

Portland State University

ECE 223

DT Filters

end;

for cnt = 2:nd, y2(cnt) = a*y2(cnt-1) + (1-a)*x(cnt); end;

figure FigureSet(1,’LTX’); subplot(2,1,1); h = plot(w,abs(H)); xlim([-pi pi]); ylim([0 1.05]); legend(’a=0.1’,’a=0.2’,’a=0.3’,’a=0.4’,’a=0.5’,’a=0.6’,’a=0.7’,’a=0.8’,’a=0.9’,2); ylabel(’|H(e^{j\omega})|’); grid on; box off; subplot(2,1,2); h = plot(w,rem(angle(H)*180/pi,180)); xlim([-pi pi]); ylim([-70 70]); ylabel(’\angle H(e^{j\omega}) (^o)’); xlabel(’Frequency (rads/sample)’); grid on; box off; AxisSet(8); print -depsc FOTransferFunctions;

figure FigureSet(1,’LTX’); h = plot(n,x,’b’,n,y1,’r’,n,y2,’g’); set(h,’LineWidth’,0.6); ylabel(’x[n], y_{0.1}[n], y_{0.6}[n]’); xlabel(’Time (day)’); title(’Intel Closing Daily Price Over 1 Year’); xlim([0 nd-1]); ylim([19 36]); box off; grid on; AxisSet(8); legend(’Raw signal’,sprintf(’Cutoff:%4.2f’,w1),sprintf(’Cutoff:%4.2f’,w2),4); print -depsc FOSignalFiltered;

%============================================================================== % Filter & Plot %============================================================================== a = 0.58; % cutoff frequency approximately 0.1 w1 = acos((1-4*a+a.^2)./(-2*a)); y1 = zeros(nd,1); for cnt = 2:nd, y1(cnt) = a*y1(cnt-1) + (1-a)*x(cnt); end; a = 0.90; % cutoff frequency approximately 0.1 w2 = acos((1-4*a+a.^2)./(-2*a)); y2 = zeros(nd,1);

Portland State University

ECE 223

DT Filters

Ver. 1.03

9

Ideal Filters Lowpass

Highpass

1 Ωc

Ωc

Bandpass

Ω

Ωc

Ω

Bandstop

1

1 Ωc1

Ωc2

Ω

Ωc1

Ωc2

DT Filters

Ver. 1.03

Ω

• MATLAB can be used to design standard frequency selective filters that meet user-specified requirements

• If we know the specifications, we can ask MATLAB to generate the filter for us

• These filters include: lowpass, highpass, bandpass, and bandstop

• There are four popular types of standard filters – Butterworth – Chebyshev Type I – Chebyshev Type II – Elliptic

• Unlike continuous-time filters, these must have cutoff frequencies that range between 0 and π

Portland State University

ECE 223

DT Filters

10

• Lowpass and highpass filters usually have the following requirements – Passband range – Stopband range – Maximum ripple in the passband – Minimum attenuation in the stopband

1

Ω

ECE 223

Practical Filters • Practical filters are usually designed to meet a set of specifications

Notch

1

Portland State University

Ver. 1.03

11

Portland State University

ECE 223

DT Filters

Ver. 1.03

12

Ripple Tradeoff Filter Butterworth Chebyshev Type I Chebyshev Type II Elliptic

Order Largest Moderate Moderate Lowest

Passband Smooth Ripple Smooth Ripple

Example 2: Lowpass Filter Specifications Design a lowpass filter that meets the following specifications:

Stopband Smooth Smooth Ripple Ripple

• The passband ripple is no more than 0.4455 dB (0.95 ≤ |H(ejω )| ≤ 1) • The stopband attenuation is at least 26.02 dB (|H(ejω )| ≤ 0.05) • The passband ranges from 0–0.2π rad/sample

• The four popular filter types differ in how they satisfy the specifications

• The stopband ranges from 0.3π–π rad/sample Plot the magnitude of the resulting transfer function on a linear-linear plot, the impulse response, and the step response. Try the Butterworth, Chebyshev I, Chebyshev II, and elliptic filters.

• In the passband and stopband, each filter is either smooth or contains ripple • Elliptic filters are also called equiripple filters and Cauer filters

Portland State University

ECE 223

DT Filters

Ver. 1.03

13

Portland State University

Example 3: Butterworth Lowpass

ECE 223

DT Filters

Ver. 1.03

14

Example 3: Butterworth Lowpass

Butterworth Lowpass Filter Transfer Function Order: 10

Butterworth Lowpass Filter Impulse Response Order:10

0.25

1 0.2 0.8

0.6

h[n]

|H(ejω)|

0.15 0.1 0.05 0.4 0 0.2 −0.05 0

0

0.5

Portland State University

1

1.5 2 Frequency (rad/sample)

ECE 223

DT Filters

2.5

−0.1

3

Ver. 1.03

15

0

10

Portland State University

20

30

40

ECE 223

50 60 Time (n)

DT Filters

70

80

90

Ver. 1.03

100

16

Example 3: Butterworth Lowpass

Example 3: Chebyshev-I Lowpass Chebyshev−I Lowpass Filter Transfer Function Order: 5

Butterworth Lowpass Filter Step Response Order:10

1.4

1 1.2 0.8

0.8

|H(ejω)|

h[n]

1

0.6

0.6

0.4 0.4 0.2 0.2 0

0

10

20

Portland State University

30

40

50 60 Time (n)

ECE 223

70

DT Filters

80

90

0

100

Ver. 1.03

17

0

0.5

Portland State University

Example 3: Chebyshev-I Lowpass

0.15

1

0.1

0.8

h[n]

h[n]

1.2

0.05

0.6

0

0.4

−0.05

0.2

0

10

Portland State University

20

30

40

ECE 223

50 60 Time (n)

DT Filters

70

ECE 223

2.5

DT Filters

3

Ver. 1.03

18

80

Chebyshev−I Lowpass Filter Step Response Order:5

1.4

0.2

−0.1

1.5 2 Frequency (rad/sample)

Example 3: Chebyshev-I Lowpass

Chebyshev−I Lowpass Filter Impulse Response Order:5

0.25

1

90

Ver. 1.03

0

100

19

0

10

Portland State University

20

30

40

ECE 223

50 60 Time (n)

70

DT Filters

80

90

Ver. 1.03

100

20

Example 3: Chebyshev-II Lowpass

Example 3: Chebyshev-II Lowpass

Chebyshev−II Lowpass Filter Transfer Function Order: 5

Chebyshev−II Lowpass Filter Impulse Response Order:5

0.25

1 0.2 0.8

0.6

h[n]

|H(ejω)|

0.15 0.1 0.05 0.4 0 0.2 −0.05 0

0

0.5

Portland State University

1

1.5 2 Frequency (rad/sample)

ECE 223

2.5

DT Filters

−0.1

3

Ver. 1.03

21

0

10

20

Portland State University

40

ECE 223

Example 3: Chebyshev-II Lowpass

50 60 Time (n)

70

80

DT Filters

90

100

Ver. 1.03

22

Example 3: Elliptic Lowpass Elliptic Lowpass Filter Transfer Function Order: 4

Chebyshev−II Lowpass Filter Step Response Order:5

1.4

30

1 1.2 0.8

0.8

|H(ejω)|

h[n]

1

0.6

0.6

0.4 0.4 0.2 0.2 0

0

10

Portland State University

20

30

40

ECE 223

50 60 Time (n)

70

DT Filters

80

90

Ver. 1.03

0

100

23

0

0.5

Portland State University

1

1.5 2 Frequency (rad/sample)

ECE 223

DT Filters

2.5

3

Ver. 1.03

24

Example 3: Elliptic Lowpass Elliptic Lowpass Filter Impulse Response Order:4

0.25 0.2

1.2

0.15

1

0.1

0.8

0.05

0.6

0

0.4

−0.05

0.2

−0.1

0

10

Portland State University

20

30

40

ECE 223

50 60 Time (n)

70

DT Filters

80

90

Ver. 1.03

0

100

25

Example 3: MATLAB Code

= = = =

0.20; 0.30; -20*log10(0.95); -20*log10(0.05);

% % % %

Passband ends Stopband begins Maximum deviation from 1 in the passband (dB) Minimum attenuation in the stopband (dB)

for cnt = 1:4, if cnt==1, [od,wn] = buttord(Wp,Ws,Rp,Rs); [B,A] = butter(od,wn); stFilter = ’Butterworth’; elseif cnt==2, [od,wn] = ellipord(Wp,Ws,Rp,Rs); [B,A] = ellip(od,Rp,Rs,wn); stFilter = ’Elliptic’; elseif cnt==3, [od,wn] = cheb1ord(Wp,Ws,Rp,Rs); [B,A] = cheby1(od,Rp,wn); stFilter = ’Chebyshev-I’; elseif cnt==4, [od,wn] = cheb2ord(Wp,Ws,Rp,Rs); [B,A] = cheby2(od,Rs,wn); stFilter = ’Chebyshev-II’; else break; end; sys = tf(B,A,-1); wp = Wp*pi; ws = Ws*pi;

Portland State University

0

10

Portland State University

20

30

40

ECE 223

50 60 Time (n)

70

80

DT Filters

90

100

Ver. 1.03

26

Ver. 1.03

28

%============================================================================== % Plot Magnitude on Linear Scale %============================================================================== ymax = 1.05; pbmax = 1; pbmin = 10^(-Rp/20); sbmax = 10^(-Rs/20); wmax = pi; w = 0:0.001:wmax; [H,w] = freqz(B,A,w); mag = abs(H); phs = angle(H); figure; FigureSet(1,’LTX’); h = patch([0 wp wp 0],[0 0 pbmin pbmin],0.5*[1 1 1]); set(h,’LineWidth’,0.0001); hold on; h = patch([0 ws ws wmax wmax 0],[pbmax pbmax sbmax sbmax ymax ymax],0.5*[1 1 1]); set(h,’LineWidth’,0.0001); h = plot(w,mag,’r’); set(h,’LineWidth’,1.0); hold off; ylim([0 ymax]); xlim([0 wmax]); grid on; ylabel(’|H(e^{j\omega})|’); title(sprintf(’%s Lowpass Filter Transfer Function Order: %d’,stFilter,od)); xlabel(’Frequency (rad/sample)’); box off; AxisSet(8); st = sprintf(’print -depsc Lowpass%s’,stFilter); eval(st);

%function [] = Lowpass(); clear all; close all; Wp Ws Rp Rs

Elliptic Lowpass Filter Step Response Order:4

1.4

h[n]

h[n]

Example 3: Elliptic Lowpass

%============================================================================== % Impulse Response %============================================================================== figure;

ECE 223

DT Filters

Ver. 1.03

27

Portland State University

ECE 223

DT Filters

FigureSet(1,’LTX’); n = 0:100; [x,t] = impulse(sys,n); h = stem(t,x,’b’); set(h(1),’MarkerFaceColor’,’b’); set(h(1),’MarkerSize’,2); ylabel(’h[n]’); xlabel(’Time (n)’); title(sprintf(’%s Lowpass Filter Impulse Response Order:%d’,stFilter,od)); box off; hold on; h = plot(xlim,[0 0],’k:’); hold off; AxisSet(8); st = sprintf(’print -depsc Lowpass%sImpulse’,stFilter); eval(st);

end;

%============================================================================== % Step Response %============================================================================== figure; FigureSet(1,’LTX’); n = 0:100; [x,t] = step(sys,n); h = stem(t,x,’b’); set(h(1),’MarkerFaceColor’,’b’); set(h(1),’MarkerSize’,2); ylabel(’h[n]’); xlabel(’Time (n)’); title(sprintf(’%s Lowpass Filter Step Response Order:%d’,stFilter,od)); box off; hold on; h = plot(xlim,[1 1],’k:’); hold off; AxisSet(8); st = sprintf(’print -depsc Lowpass%sStep’,stFilter); eval(st);

Portland State University

ECE 223

DT Filters

Ver. 1.03

29

Practical Filter Tradeoffs

Portland State University

ECE 223

DT Filters

Ver. 1.03

30

Application Example 1: Microelectrode Recording Filter

• Butterworth - Highest order H(ejω ) + No passband or stopband ripple

An engineer wishes to detect action potentials in a microelectrode recording with a simple threshold detector. The signal contains significant baseline drift. Action potentials typically last about 1 ms.

• Chebyshev Type I + No stopband ripple

• What type of filter should the engineer use?

• Chebyshev Type II + No passband ripple

• What should the cutoff frequency(ies) be?

• What should the filter specifications be?

• Elliptic + Lowest order H(ejω ) - Passband and stopband ripple

Portland State University

ECE 223

DT Filters

Ver. 1.03

31

Portland State University

ECE 223

DT Filters

Ver. 1.03

32

Application Example 1: Frequency-Selective Filters

Application Example 1: Frequency-Selective Filters 50

Microelectrode Recording FT Magnitude

0.8 0.6

40 30 20 10

0.4

0 0

0.2

500

1000

1500

2000

500 FT Magnitude

0 −0.2 −0.4

400 300 200 100

−0.6

0 0

1

1.2

1.4

1.6

Portland State University

1.8

2 2.2 Time (sec)

ECE 223

2.4

2.6

DT Filters

50

100

150

200

250

300

2.8

Ver. 1.03

33

Portland State University

Application Example 1: Frequency-Selective Filters

ECE 223

DT Filters

Ver. 1.03

34

Application Example 1: Frequency-Selective Filters 50 FT Magnitude

Microelectrode Recording 0.5 0

40 30 20 10

−0.5

0

0.2

0.4

0.6

0.8

1 1.2 Time (sec)

1.4

1.6

1.8

0

500

1000

1500

2000

0

500

1000

1500

2000

2 50 FT Magnitude Filtered

0

0.5 0 −0.5

40 30 20 10 0

0

0.2

0.4

Portland State University

0.6

0.8

1 1.2 Time (sec)

ECE 223

1.4

DT Filters

1.6

1.8

2

Ver. 1.03

35

Portland State University

ECE 223

DT Filters

Ver. 1.03

36

Application Example 1: MATLAB Code

subplot(2,1,1); h = plot(f,abs(X(k)),’r’); set(h,’LineWidth’,0.6); xlim([min(f) max(f)]); ylim([0 50]); set(gca,’XTick’,[0:500:max(f)]); box off; ylabel(’FT Magnitude’); subplot(2,1,2); h = plot(f,abs(X(k)),’r’); set(h,’LineWidth’,0.6); xlim([0 300]); ylim([0 500]); %set(gca,’XTick’,[0:500:max(f)]); box off; ylabel(’FT Magnitude’); AxisSet(6); print -depsc MERSpectralDensity;

%function [] = MER(); close all; [x,fs,nbits] = wavread(’Henderson2.wav’); x = decimate(x,2); fs = fs/2; k = round(fs*1):round(fs*3); % Look at only 5 s x = x(k); nx = length(x); figure; FigureSet(1,’LTX’); t = (k-1)/fs; h = plot(t,x,’b’); set(h,’LineWidth’,0.6); xrng = max(x)-min(x); xlim([min(t) max(t)]); ylim([min(x)-0.01*xrng max(x)+0.01*xrng]); AxisLines; xlabel(’Time (sec)’); ylabel(’’); title(’Microelectrode Recording’); box off; AxisSet(8); print -depsc MERSignal; X nX k f

= = = =

Wp = 210/(fs/2); % Passband ends Ws = 190/(fs/2); % Stopband begins Rp = -20*log10(0.95); % Maximum deviation from 1 in the passband (dB) Rs = -20*log10(0.05); % Minimum attenuation in the stopband (dB) [od,wn] = ellipord(Wp,Ws,Rp,Rs); [B,A] = ellip(od,Rp,Rs,wn,’high’); stFilter = ’Elliptic’; y = filtfilt(B,A,x);

fft(x,2^12); length(X); 1:floor((length(X)+1)/2); (k-1)*(fs)./(nX+1);

figure; FigureSet(1,’LTX’); k = 1:length(x); t = (k-1)/fs; subplot(2,1,1); t = (k-1)/fs; h = plot(t,x,’b’);

figure; FigureSet(1,’LTX’);

Portland State University

ECE 223

DT Filters

Ver. 1.03

37

set(h,’LineWidth’,0.6); xrng = max(x)-min(x); xlim([min(t) max(t)]); ylim([min(x)-0.01*xrng max(x)+0.01*xrng]); AxisLines; xlabel(’Time (sec)’); ylabel(’’); title(’Microelectrode Recording’); box off; subplot(2,1,2); h = plot(t,y,’g’); set(h,’LineWidth’,0.6); xlim([min(t) max(t)]); ylim([min(x)-0.01*xrng max(x)+0.01*xrng]); AxisLines; xlabel(’Time (sec)’); ylabel(’’); box off; AxisSet(8); print -depsc MERSignalFiltered; Y nY k f

= = = =

S

Portland State University

ECE 223

DT Filters

Ver. 1.03

38

DT Filters

Ver. 1.03

40

h = plot(f,abs(Y(k)),’g’); set(h,’LineWidth’,0.6); xlim([min(f) max(f)]); ylim([0 50]); set(gca,’XTick’,[0:500:max(f)]); box off; ylabel(’FT Magnitude Filtered’); AxisSet(6); print -depsc MERSpectralDensityFiltered;

fft(y,2^12); length(Y); 1:floor((length(Y)+1)/2); (k-1)*(fs)./(nY+1);

figure; FigureSet(1,’LTX’); subplot(2,1,1); h = plot(f,abs(X(k)),’r’); set(h,’LineWidth’,0.6); xlim([min(f) max(f)]); ylim([0 50]); set(gca,’XTick’,[0:500:max(f)]); box off; ylabel(’FT Magnitude’); subplot(2,1,2);

Portland State University

ECE 223

DT Filters

Ver. 1.03

39

Portland State University

ECE 223

Suggest Documents