Você está na página 1de 10

SITI IMANIA LUHRI

03411640000011
ADD C

TUGAS FILTER

Filter High Pass

High Pass

Script Penjelasan
clc, clear all, close all

%% Signal (Time Domain)


F = [ 300 400 1000 2000 ]; %
frequencies
A = [1 1 3 1]; Menentukan parameter sinyal untuk
Fs = 6000; persamaan gelombang awal
fc=350; %Frequency cutoff
N = 0.05*Fs;
t = 0:1/Fs:(N-1)/Fs;
y=0;
for j=1:length(F)
y=y+A(j)*cos(2*pi*F(j)*t);
end
figure(1),subplot(3,2,1),plot(t,y)
title('Sinyal asli'), xlabel('time
(s)'),ylabel('y(t)'),
ax2=axis(gca);
set(gcf,'Units','Normalized','OuterPosit
ion',[0 0.04 0.5 .96])
% axis([min(t) max(t) min(y) max(y)])

%% Filter parameters:
L = N+1; % filter length
n_h = (-(L-1)/2:(L-1)/2);

% hideal = (2*fc/Fs)*sinc(2*fc/Fs*n_h);
% Ideal Lowpass Filter

hideal = (-2*fc/Fs)*sinc(2*fc/Fs*n_h); %
Ideal Highpass Filter Persamaan ideal untuk high pass
% hideal(ceil(L/2)) = (pi- filter
2*pi*fc/Fs)/pi; % Untuk Highpass. Kalo
tidak di off kan saja

% h = kaiser(L,10)' .* hideal; % h is
our filter
h = hideal; %This non-windowed filter
will results Gibbs Phenomenon
n_h2=0:L-1;
figure(1),subplot(3,2,5:6),plot(n_h2,h),
xlabel('Time (samples)'),
Plotting hasil high pass filter tanpa
ylabel('Amplitude') window
title(['Impulse Response of '
num2str(fc) 'Hz Lowpass filter (without
Window) L = ' num2str(L)])

%% Spectrum of Our Signal and The Filter


N2=L+N-1;
Y = fft(y,N2);
H = fft(h,N2);
A_Y=2*abs(Y(1:floor(N2/2)))/N;
A_H=2*abs(H(1:floor(N2/2)))/N;
f = linspace(0,Fs/2,N2/2); Membuat spectrum amplitude dari
fft gelombang asli (y) dan filter ideal
figure(2),suptitle('Amplitude Spectrum') (h)
set(gcf,'Units','Normalized','OuterPosit
ion',[0.5 0.04 0.5 0.96])
subplot(2,2,1),plot(f,A_Y,'r'),title('Or
iginal')
ax=axis(gca);
subplot(2,2,3),plot(f,A_H),
title('Filter Response')
xlabel('f (Hz)'),ylabel('Amplitude')
% Y_dB =
10*log10((abs(Y(1:floor(N2/2)))));
% H_dB =
10*log10((abs(H(1:floor(N2/2)))));
% figure(10),plot(f,H_dB)
% xlabel('Frequency (Hz)'),
ylabel('Amplitude (dB)')
% suptitle([num2str(fc) 'Hz Low-pass
filter (Hann Window) : L = '
num2str(L)])
%% Applying Filter (Way 1 : time domain;
Convolution)
hy=conv(h,y); Filter untuk time domain (konvolusi)
HY=fft(hy,N2);
A_HY=2*abs(HY(1:floor(N2/2)))/N;
figure(2), subplot(2,2,4)
plot(f,A_HY), title('Filtered
(Convolution)'), axis(ax)

y2_conv=ifft(HY,N2);
t2_conv = 0:1/Fs:(N2-1)*(1/Fs);
figure(1),subplot(3,2,2), plot(t,y),
%axis(ax2)
hold on,plot(t2_conv,y2_conv)
legend('Origin','Filtered (conv)')

%% Applying Filter (Way 2 : Frequency


domain)
YH = Y.*H;
A_YH = 2*abs(YH(1:floor(N2/2)))/N;
figure(2),subplot(2,2,2),plot(f,A_YH,'k' Filter untuk frekuensi domain (Kali)
)
title('Filtered (kali)')
axis(ax)

y2_kali=ifft(YH,N2);
t2_kali = 0:1/Fs:(N2-1)*(1/Fs);
figure(1),subplot(3,2,4),plot(t,y),
hold on,
plot(t2_kali,y2_kali),%axis(ax2)
legend('Origin','Filtered (kali)')

%% Applying Filter (Freq domain; Length


N)
Y2=fft(y,N);
H2=fft(h,N);
YH2=Y2.*H2; Filter untuk frekuensi sepanjang N
y2=ifft(YH2); (Kali)
figure(1),subplot(3,2,3),plot(t,y),%axis
(ax2)
hold on,plot(t,y2)
legend('Origin','Filtered (kali, length
N)')

%% Built-in MATLAB Filter (without zero Membuat filter tanpa zero padding
padding)
%% fir1
Wn = (2/Fs)*fc;
b = fir1(N,Wn,'low',kaiser(N+1,3));
y_fir1 = filter(b,1,y);
%% fdesign.lowpass
fp = 2*fc/Fs; Membuat filter desain
d=fdesign.lowpass('Fp,Fst,Ap,Ast',fp-
0.01,fp+0.01,2,60);
designmethods(d);
Hd=design(d,'equiripple');
y_fdesignlow =filter(Hd,y);
figure(100), subplot(2,1,1)
set(gcf,'Units','Normalized','OuterPosit
ion',[0 0.04 0.5 0.96])
plot(t,y_fir1),hold on,
plot(t,y_fdesignlow)
title('Without Zero Padding'),
legend('fir1','fdesign.lowpass')
%% Removing delay Menghilangkan delay
% grpdelay(firf,N,Fs)
delay = mean(grpdelay(Hd));
t_fdesignlow2=t(1:end-floor(delay));
y_fdesignlow2=y_fdesignlow(floor(delay)+
1:end);
figure(101),subplot(2,1,1)
set(gcf,'Units','Normalized','OuterPosit
ion',[0.5 0.04 0.5 0.96])
plot(t_fdesignlow2,y_fdesignlow2,t,y_fde
signlow)
title('fdesign.lowpass : Without Zero
Padding'), legend('Moved delay','Still
delayed')

%% Built-in MATLAB Filter (with zero Membuat filter dengan zero padding
padding)
nol=zeros(size(y));
ypad=[nol y nol];
%% fir1
Wn = (2/Fs)*fc;
b = fir1(N,Wn,'low',kaiser(N+1,3));
y_fir1 = filter(b,1,ypad);

%% fdesign.lowpass Membuat filter desain


fp = 2*fc/Fs;
d=fdesign.lowpass('Fp,Fst,Ap,Ast',fp-
0.01,fp+0.01,2,60);
designmethods(d);
Hd=design(d,'equiripple');
y_fdesignlow =filter(Hd,ypad);
figure(100), subplot(2,1,2)
plot(y_fir1),hold on, plot(y_fdesignlow)
title('With Zero
Padding'),legend('fir1','fdesign.lowpass
')

%% Removing delay Menghilangkan delay


delay = mean(grpdelay(Hd));
t_fdesignlow2=t(1:end-floor(delay));
y_fdesignlow2=y_fdesignlow(floor(delay)+
1+length(nol):end-
length(nol)+floor(delay));
figure(101),subplot(2,1,2)
plot(y_fdesignlow),hold on,
plot(y_fdesignlow2)
title('fdesign.lowpass : With Zero
Padding'),legend('Moved delay','Still
delayed')

%% fvtool Menunjukkan magnitude dari sinyal


fvtool(A_H,1,'fs',Fs)
fvtool(b,1,'fs',Fs)
fvtool(Hd,1,'fs',Fs)

Low Pass
Script Penjelasan
%% Filter parameters:
L = N+1; % filter length
n_h = (-(L-1)/2:(L-1)/2);

hideal = (2*fc/Fs)*sinc(2*fc/Fs*n_h); %
Ideal Lowpass Filter

% hideal = (-2*fc/Fs)*sinc(2*fc/Fs*n_h);
% Ideal Highpass Filter
% hideal(ceil(L/2)) = (pi-
2*pi*fc/Fs)/pi; % Untuk Highpass. Kalo Persamaan ideal untuk high pass
tidak di off kan saja
filter
% h = kaiser(L,10)' .* hideal; % h is
our filter
h = hideal; %This non-windowed filter
will results Gibbs Phenomenon
n_h2=0:L-1;
figure(1),subplot(3,2,5:6),plot(n_h2,h),
xlabel('Time (samples)'),
ylabel('Amplitude')
title(['Impulse Response of ' Plotting hasil low pass filter tanpa
num2str(fc) 'Hz Lowpass filter (without window
Window) L = ' num2str(L)])

Hasil Run Script


High Pass
Gambar 1.

Gambar 2.
Gambar 3.

Gambar 4.

Gambar 5.
Pada gambar-gambar diatas dapat diketahui bahwa sinyal yang difilter adalah yang frekuensinya
tinggi.

Low Pass

Gambar 6.

Gambar 7.
Gambar 8.

Gambar 9.
Gambar 10.

Pada gambar-gambar diatas dapat diketahui bahwa sinyal yang difilter adalah yang frekuensinya
rendah.

Você também pode gostar