You are on page 1of 4

KODENYA :

clear;

%A program to demonstrate a simple noise filtering with an ideal filter


f1 = 100;
f2 = 500;
f3 = 300;

%cut off frequency


f_c_1 = 80; %Low cut off frequency
f_c_2 = 320; %High cut off frequency

fs = 4000; %Sampling frequency


Ts = 1./fs; %Sampling period

N_h = 102; %Filter tap

N = 2048; %Fourier transform point


del_f = fs./N;

t = 0:Ts:(N-1)*Ts;

f = 0:del_f:(N-1)*del_f;

%Create an input signal


x = 2*sin(2.*pi.*f1.*t)-2*sin(2.*pi.*f2.*t)+sin(2.*pi.*f3.*t);

%Add noise
noise = randn(size(t));

%noise signal
x_n = x+noise;

%Frequency domain
X_N = DFT(x_n,N);

%Standard Band Pass Filter;


H = zeros(size(t));
H(f>=(min(f_c_1,f_c_2)-del_f)) = 1;
H(f>(max(f_c_1,f_c_2)+del_f)) = 0;
H(N/2+2:end) = H(N/2:-1:2);
h = iDFT(H,N)./N;

%Time-Shift the filter impulse response


h_shift = zeros(1,N);
h_shift(N/2+1:end) = h(1:N/2);
h_shift(1:N/2) = h(N/2+1:end);

%Hanning Windowing function


if round(N_h/2)==N_h/2
%if the number of filter tp is even
n = -round((N_h)/2):round((N_h)/2)-1;
w = 0.5*0.5*cos(2*pi*n./(N_h-1));
else
%if the number of filter tp is odd
n = -round((N_h-1)/2):round((N_h-1)/2);
w = 0.5*0.5*cos(2*pi*n./(N_h-1));
end

%Zero padded the windowing function


w_zp = zeros(1,N);
w_zp(N/2+1) = w(n==0);
n_positive = sum(n>0);
n_negative = sum(n<0);
w_zp(N/2+2:N/2+1+n_positive)= w(n>0);
w_zp(N/2-(n_negative-1):N/2)=w(n<0);

%Apply windowing to the filter


h_final = h_shift.*w_zp;
H_final = DFT(h_final,N);

%Apply signal filtering using convolution


%Since we know that h_final contains many zeros, we only take some part of
%the result
%This gives a similar effect to the circular convolution
y_long = conv(x_n,h_final);
y = y_long(round(N/2)+1:round(N/2)+(N));
Y = DFT(y,N);

%Plotting
figure, plot(t,x_n);grid;
xlabel('Time (s)');ylabel('Noisy Input Signal in Time Domain');
figure, plot(f,abs(X_N));grid;
xlabel('Frequency (Hz)');ylabel('Frequency Domain Noisy Input Signal');
figure, plot(t,y);grid;
xlabel('Time (s)');ylabel('Filtered signal');
figure, plot(f,abs(Y));grid;
xlabel('Frequency (Hz)');ylabel('Frequency domain filtered signal');

function [ xn_synth ] = iDFT( X,N)


%UNTITLED6 Summary of this function goes here
% Detailed explanation goes here

Xr = real(X);
Xi = imag(X);

xn_synth = zeros(1,N);

for n = 1: N
for k = 1:N
xn_synth(n) = xn_synth(n)+Xr(k).*cos(2.*pi.*(k-1).*(n-
1)/N)+Xi(k).*sin(2.*pi.*(k-1).*(n-1)/N);
end
end
end

function [ X ] = DFT( x_n,N )


%UNTITLED7 Summary of this function goes here
% Detailed explanation goes here

%Fourier transform
Xr = zeros(1,N); %Generate a vector of Xr
Xi = zeros(1,N); %Generate a vector of Xi)
for k = 1:N
for n = 1:N
Xr(k) = Xr(k)+x_n(n).*cos(2*pi*(k-1)*(n-1)/N);
Xi(k) = Xi(k)+x_n(n).*sin(2*pi*(k-1)*(n-1)/N);
end
Xr(k) = Xr(k)/N;
Xi(k) = Xi(k)/N;
end

X = Xr + 1i*Xi;

end

HASIL RUNNING KODENYA

You might also like