implementasi fir filter menggunakan matlab

17
Implementasi FIR menggunakan MATLAB 1. FIR FILTER METODE WINDOWING - LPF clear all; N=32; w_c=0.45; b = fir1(N,w_c); bb=fft(b,256); ff=1:length(bb)/2; figure(1); plot(2*ff/length(bb),20*log10(abs(bb(1:length(bb)/2))),'linewidth', 2) xlabel('Normalized Frequency'); ylabel('Magnitude dB'); title('FIR based Windowing'); - HPF clear all; N=32; w_c=0.25; b = fir1(N,w_c,'high'); bb=fft(b,256); ff=1:length(bb)/2; figure(1); plot(2*ff/length(bb),20*log10(abs(bb(1:length(bb)/2))),'linewidth', 2) xlabel('Normalized Frequency'); ylabel('Magnitude dB'); title('FIR based Windowing'); - 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 -90 -80 -70 -60 -50 -40 -30 -20 -10 0 10 X: 0.4327 Y: -3.007 Normalized Frequency Magnitude dB FIR based Windowing Passband Transition Stopband

Upload: mafailmi

Post on 21-Jul-2015

157 views

Category:

Engineering


7 download

TRANSCRIPT

Page 1: Implementasi FIR filter menggunakan matlab

Implementasi FIR menggunakan MATLAB

1. FIR FILTER METODE WINDOWING

- LPF

clear all; N=32; w_c=0.45; b = fir1(N,w_c); bb=fft(b,256); ff=1:length(bb)/2; figure(1); plot(2*ff/length(bb),20*log10(abs(bb(1:length(bb)/2))),'linewidth',

2) xlabel('Normalized Frequency'); ylabel('Magnitude dB'); title('FIR based Windowing');

- HPF

clear all; N=32; w_c=0.25; b = fir1(N,w_c,'high'); bb=fft(b,256);

ff=1:length(bb)/2; figure(1); plot(2*ff/length(bb),20*log10(abs(bb(1:length(bb)/2))),'linewidth',

2) xlabel('Normalized Frequency'); ylabel('Magnitude dB'); title('FIR based Windowing');

-

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-90

-80

-70

-60

-50

-40

-30

-20

-10

0

10X: 0.4327

Y: -3.007

Normalized Frequency

Magnitude d

B

FIR based Windowing

Passband

Transition

Stopband

Page 2: Implementasi FIR filter menggunakan matlab

- BPF

clear all; N=32; w_c=[0.25 0.55]; b = fir1(N,w_c); bb=fft(b,256); ff=1:length(bb)/2; figure(1); plot(2*ff/length(bb),20*log10(abs(bb(1:length(bb)/2))),'linewidth',

2) xlabel('Normalized Frequency'); ylabel('Magnitude dB'); title('FIR based Windowing');

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-80

-70

-60

-50

-40

-30

-20

-10

0

10

X: 0.2829

Y: -3.004

Normalized Frequency

Magnitude d

B

FIR based Windowing

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-90

-80

-70

-60

-50

-40

-30

-20

-10

0

10X: 0.2832

Y: -3

Normalized Frequency

Magnitude d

B

FIR based Windowing

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-90

-80

-70

-60

-50

-40

-30

-20

-10

0

10X: 0.5325

Y: -3.006

Normalized Frequency

Magnitude d

B

FIR based Windowing

Passband

Transition

Stopband

Passband

Transition Transition

Stopband

Stopband

Page 3: Implementasi FIR filter menggunakan matlab

2. FIR FILTER METODE PARK MC CLELLAND

- LPF

clear all; N=16; F=[0 .15 .25 .35 .45 .55 .65 .75 .85 1.0]; A=[1 1 1 1 1 1 0 0 0 0]; b=remez(N,F,A); bb=fft(b,256); figure(1); ff=1:length(bb)/2;

plot(2*ff/length(bb),20*log10(abs(bb(1:length(bb)/2))),'linewidth'

,2) xlabel('Normalized Frequency'); ylabel('Magnitude dB'); title('FIR based Park-Mc Clelland');

- HPF

clear all; N=16; F=[0 .15 .25 .35 .45 .55 .65 .75 .85 1.0]; A=[0 0 1 1 1 1 1 1 1 1]; b=remez(N,F,A); bb=fft(b,256); figure(1); ff=1:length(bb)/2; plot(2*ff/length(bb),20*log10(abs(bb(1:length(bb)/2))),'linewidth'

,2) xlabel('Normalized Frequency'); ylabel('Magnitude dB'); title('FIR based Park-Mc Clelland');

-

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-50

-40

-30

-20

-10

0

10

X: 0.5837

Y: -3.003

Normalized Frequency

Magnitude d

B

FIR based Park-Mc Clelland

Passband

Transition

Stopband

Page 4: Implementasi FIR filter menggunakan matlab

- BPF

clear all; N=16; F=[0 .15 .25 .35 .45 .55 .65 .75 .85 1.0]; A=[1 1 1 1 1 1 0 0 0 0];

b=remez(N,F,A); bb=fft(b,256); figure(1); ff=1:length(bb)/2; plot(2*ff/length(bb),20*log10(abs(bb(1:length(bb)/2))),'linewidth',2) xlabel('Normalized Frequency'); ylabel('Magnitude dB'); title('FIR based Park-Mc Clelland');

-

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-45

-40

-35

-30

-25

-20

-15

-10

-5

0

5

X: 0.2303

Y: -3.003

Normalized Frequency

Magnitude d

B

FIR based Park-Mc Clelland

Passband

Transition

Stopband

Page 5: Implementasi FIR filter menggunakan matlab

PEMFILTERAN SINYAL SEDERHANA

clc; clf;

%pembangkitan sinyal

Fs=2000; t=0:1/Fs:1; f1=200; f2=500;

s1=sin(2*pi*f1*t); s2=0.25*sin(2*pi*f2*t); s_out=s1+s2;

%sinyal masukkan domain frekuensi X=fft(s_out,256); frek=((0:127)/128)*Fs/2; figure(1) plot(frek,abs(X(1:128)));

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-50

-40

-30

-20

-10

0

10

X: 0.5844

Y: -3.001

Normalized Frequency

Magnitude d

B

FIR based Park-Mc Clelland

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-50

-40

-30

-20

-10

0

10

X: 0.2289

Y: -3.008

Normalized Frequency

Magnitude d

B

FIR based Park-Mc Clelland

Transition Transition

Stopband Stopband

Passband

Page 6: Implementasi FIR filter menggunakan matlab

%desain filter N=32; wc=0.45; b=FIR1(N,wc); bb=fft(b,256); ff=1:length(bb)/2; figure(2) subplot(211) plot(2*ff/length(bb),abs(bb(1:length(bb)/2)),'linewidth',2) xlabel('Normalized Frequency'); ylabel('Magnitude Linear Scale'); title('FIR based windowing');

subplot(212) plot(2*ff/length(bb),20*log10(abs(bb(1:length(bb)/2))),'linewidth',2)

xlabel('Normalized requency'); ylabel('Magnitude dB Scale'); title('FIR based windowing');

0 100 200 300 400 500 600 700 800 900 10000

10

20

30

40

50

60

70

80

90

100

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.5

1

1.5

Normalized Frequency

Mag

nitu

de L

inea

r S

cale

FIR based windowing

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-100

-50

0

50

Normalized requency

Mag

nitu

de d

B S

cale

FIR based windowing

Page 7: Implementasi FIR filter menggunakan matlab

%pengujian filter dengan sinyal input %N=32; Fc=300; wc=2*pi*Fc/Fs; alpha=(N+1)/2; n=0:N-1; h=wc/pi*sinc(wc/pi*(n-alpha)); win=hamming(N); hd=h.*transpose(win);

b = filter(hd,1,s_out); bb=fft(b,1024); ff=1:length(bb)/2;

figure(5) plot(2*ff,abs(bb(1:length(bb)/2)),'linewidth',2) title('Sinyal Hasil Pemfilteran')

%penambahan sinyal dengan noise y = s_out + 2*randn(size(t)); figure(3) plot(1000*t(1:50),y(1:50)) title('Signal Corrupted with Zero-Mean Random Noise') xlabel('time (milliseconds)')

0 200 400 600 800 1000 12000

50

100

150

200

250

300

350

400Sinyal Hasil Pemfilteran

0 5 10 15 20 25-6

-4

-2

0

2

4

6Signal Corrupted with Zero-Mean Random Noise

time (milliseconds)

Page 8: Implementasi FIR filter menggunakan matlab

%pengujian filter dengan sinyal input+noise %N=32; Fc=300; wc=2*pi*Fc/Fs; alpha=(N+1)/2; n=0:N-1; h=wc/pi*sinc(wc/pi*(n-alpha)); win=hamming(N); hd=h.*transpose(win);

b = filter(hd,1,y); bb=fft(b,1024); ff=1:length(bb)/2; figure(6) plot(2*ff,abs(bb(1:length(bb)/2)),'linewidth',2) title('Sinyal Hasil Pemfilteran')

%gambaran sinyal dalam domain waktu figure (7) subplot(221) plot(s1) title('Sinyal 1')

subplot(222) plot(s2) title('Sinyal 2')

subplot(223) plot(s_out) title('Sinyal_1 + Sinyal_2')

subplot(224) plot(y) title('Sinyal Noise')

0 200 400 600 800 1000 12000

50

100

150

200

250

300

350

400

450Sinyal Hasil Pemfilteran

Page 9: Implementasi FIR filter menggunakan matlab

%gambaran sinyal dalam domain frekuensi figure (8) plot(s1) freqz(s1,100) title('Sinyal 1')

figure(9) plot(s2) freqz(s2,100) title('Sinyal 2')

0 1000 2000 3000-1

-0.5

0

0.5

1Sinyal 1

0 1000 2000 3000-0.4

-0.2

0

0.2

0.4Sinyal 2

0 1000 2000 3000-2

-1

0

1

2

Sinyal1 + Sinyal

2

0 1000 2000 3000-10

-5

0

5

10Sinyal Noise

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-300

-200

-100

0

100

Normalized Frequency ( rad/sample)

Phas

e (d

egre

es)

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-300

-200

-100

0

100

Normalized Frequency ( rad/sample)

Mag

nitu

de (d

B)

Sinyal 1

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-300

-200

-100

0

100

Normalized Frequency ( rad/sample)

Phas

e (de

grees

)

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-300

-200

-100

0

100

Normalized Frequency ( rad/sample)

Magn

itude

(dB)

Sinyal 2

Page 10: Implementasi FIR filter menggunakan matlab

figure(10) plot(s_out) freqz(s_out,100) title('Sinyal_1 + Sinyal_2')

figure(11) plot(y) freqz(y,100) title('Sinyal Noise')

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-300

-200

-100

0

100

Normalized Frequency ( rad/sample)

Phase (

degre

es)

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-300

-200

-100

0

100

Normalized Frequency ( rad/sample)

Magnitude (

dB

)

Sinyal1 + Sinyal

2

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-2

-1

0

1x 10

5

Normalized Frequency ( rad/sample)

Phase (

degre

es)

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-40

-20

0

20

Normalized Frequency ( rad/sample)

Magnitude (

dB

)

Sinyal Noise

Page 11: Implementasi FIR filter menggunakan matlab

Pemfilteran Sinyal Audio 1

clear all; fs=22050; x=0:0.1:20; y=sin(2*pi*x); N=length(x);

nada=wavread('dspafsx_mono.wav'); yy=length(nada); nois = 0.2*randn(size(nada)); sinyal_nois = nada+nois; wavplay(sinyal_nois,fs) figure(1); plot(sinyal_nois)

%Frame Sampling Rate fs=22050; n=1/fs:1/fs:1; f1=0.02*n; figure(2); subplot(2,1,1) plot(f1); wavplay(sinyal_nois,fs) title('sinyalber Noise domain T')

Y=fft(f1,512);

Pyy = Y.* conj(Y) / 512; ff = 5000*(0:256)/512; subplot(2,1,2) plot(ff,Pyy(1:257)) title('sinyalber Noise domain F')

%Frame nada dan nois f1=0.2*sinyal_nois; figure(3);

subplot(2,1,1) plot(f1); title('sinyal Nada + Noise domain T')

Y=fft(f1,512); Pyy = Y.* conj(Y) / 512; ff = 5000*(0:256)/512; subplot(2,1,2) plot(ff,Pyy(1:257)) title('sinyal Nada + Noise domain F')

%Filter Sinyal nada dan nois wn=0.4; N=64; b=fir1(N,wn); y=filter2(b,sinyal_nois); figure(4); plot(y) title('Output Sinyal Nada + Noise Dalam LPF')

%Frame Filter nada dan nois f1=0.2*y; figure(5); subplot(2,1,1) plot(f1); title('sinyal Filter Nada + Noise domain T')

Y=fft(f1,512); Pyy = Y.* conj(Y) / 512; ff = 5000*(0:256)/512;

Page 12: Implementasi FIR filter menggunakan matlab

subplot(2,1,2) plot(ff,Pyy(1:257)) title('sinyal Filter Nada + Noise domain F')

PEMFILTERAN SINYAL AUDIO II

clear all; N=32;

%LPF w_c1=0.35; b1 = fir1(N,w_c1);

bb=fft(b1,256); ff=1:length(bb)/2; figure(1); plot(2*ff/length(bb),20*log10(abs(bb(1:length(bb)/2))),'linewidth',2) grid on

0 1 2 3 4 5 6

x 104

-1.5

-1

-0.5

0

0.5

1

1.5

0 0.5 1 1.5 2 2.5

x 104

0

0.005

0.01

0.015

0.02sinyalber Noise domain T

0 500 1000 1500 2000 25000

1

2

3x 10

-5 sinyalber Noise domain F

0 1 2 3 4 5 6

x 104

-0.4

-0.2

0

0.2

0.4sinyal Nada + Noise domain T

0 500 1000 1500 2000 25000

2

4

6

8x 10

-3 sinyal Nada + Noise domain F

0 1 2 3 4 5 6

x 104

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6Output Sinyal Nada + Noise Dalam LPF

0 1 2 3 4 5 6

x 104

-0.2

-0.1

0

0.1

0.2sinyal Filter Nada + Noise domain T

0 500 1000 1500 2000 25000

0.5

1

1.5x 10

-3 sinyal Filter Nada + Noise domain F

Page 13: Implementasi FIR filter menggunakan matlab

xlabel('Normalized Frequency'); ylabel('Magnitude dB'); title('FIR based Windowing');

%HPF w_c2=0.55; b2 = fir1(N,w_c2,'high'); bb=fft(b2,256); ff=1:length(bb)/2; figure(2); plot(2*ff/length(bb),20*log10(abs(bb(1:length(bb)/2))),'linewidth',2) grid on

xlabel('Normalized Frequency'); ylabel('Magnitude dB'); title('FIR based Windowing');

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-100

-80

-60

-40

-20

0

20

Normalized Frequency

Magnitude d

B

FIR based Windowing

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-90

-80

-70

-60

-50

-40

-30

-20

-10

0

10

Normalized Frequency

Mag

nitu

de d

B

FIR based Windowing

Page 14: Implementasi FIR filter menggunakan matlab

%BPF w_c3=[0.35,0.55]; b3 = fir1(N,w_c3); bb=fft(b3,256); ff=1:length(bb)/2; figure(3); plot(2*ff/length(bb),20*log10(abs(bb(1:length(bb)/2))),'linewidth',2) grid on xlabel('Normalized Frequency'); ylabel('Magnitude dB'); title('FIR based Windowing');

%sinyal audio [y,Fs] = wavread('Ring08.wav'); Y=resample(y,20000,Fs);

%Filter Sinyal (LPF) lpf=filter2(b1,Y);

figure(4); plot(lpf) title('Output LPF')

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-100

-90

-80

-70

-60

-50

-40

-30

-20

-10

0

Normalized Frequency

Magnitude d

B

FIR based Windowing

0 0.5 1 1.5 2 2.5

x 105

-0.2

-0.15

-0.1

-0.05

0

0.05

0.1

0.15Output LPF

Page 15: Implementasi FIR filter menggunakan matlab

%Filter Sinyal (HPF) hpf=filter2(b2,Y); figure(5); plot(hpf) title('Output HPF')

%Filter Sinyal (BPF) bpf=filter2(b3,Y); figure(6); plot(bpf) title('Output BPF')

0 0.5 1 1.5 2 2.5

x 105

-0.2

-0.15

-0.1

-0.05

0

0.05

0.1

0.15Output HPF

0 0.5 1 1.5 2 2.5

x 105

-0.05

-0.04

-0.03

-0.02

-0.01

0

0.01

0.02

0.03

0.04

0.05Output BPF

Page 16: Implementasi FIR filter menggunakan matlab

%pengambilan satu frame sinyal LPF fs=20000; z=lpf(1:0.02*fs); figure(7) plot(z) title('satu frame sinyal LPF')

%pengambilan satu frame sinyal HPF fs=20000; z=hpf(1:0.02*fs); figure(8) plot(z) title('satu frame sinyal HPF')

0 50 100 150 200 250 300 350 400-2

-1

0

1

2

3

4x 10

-3 satu frame sinyal LPF

0 50 100 150 200 250 300 350 400-2

-1.5

-1

-0.5

0

0.5

1

1.5

2

2.5

3x 10

-3 satu frame sinyal HPF

Page 17: Implementasi FIR filter menggunakan matlab

%pengambilan satu frame sinyal BPF fs=20000; z=bpf(1:0.02*fs); figure(9) plot(z) title('satu frame sinyal BPF')

0 50 100 150 200 250 300 350 400-1

-0.5

0

0.5

1

1.5

2x 10

-3 satu frame sinyal BPF