Skip to content

Instantly share code, notes, and snippets.

@Arunprakash-A
Last active August 27, 2018 15:10
Show Gist options
  • Save Arunprakash-A/2b11c018ed9584f8320b091814d59769 to your computer and use it in GitHub Desktop.
Save Arunprakash-A/2b11c018ed9584f8320b091814d59769 to your computer and use it in GitHub Desktop.
% ************* Study the Effect of Windowing on FIlter Response************** %
% ************ Author : Arun Prakash A *************************************** %
clc;
clear all;
close all;
step = 0.001;
%Frequency Domain Spec
W=-pi:step:pi; % range of digital frequncy
Wc = -pi/2 : step : pi/2; % cutoff freq
%% Construct an Ideal Filter %%
% Assuming zero phase filter, H(ejw) = 1
H =[zeros(1,length(-pi:step:-pi/2)),ones(1,length(Wc)),zeros(1,length(pi/2:step:pi))];
subplot(221)
plot(W,H) % As frequnecy is continuous variable
axis([-2*pi 2*pi 0 1.2])
%Make graph more elegant
set(gca,'XTick',-pi:pi/2:pi)
set(gca,'XTickLabel',{'-\pi','\pi/2','0','\pi/2','\pi'})
% Label axis
xlabel('Frequency rad/s')
ylabel('H(e^j^w)')
title('IDEAL RESPONSE ')
%ifft
subplot(212)
h=ifft(complex(H)); % explore if H is not made complex
h=ifftshift(h);
len=length(h);
n=(-len/2:1:len/2-1);
subplot(222);
stem(n,h);% h(n) is discrete
xlabel('Sample(n)')
ylabel('h(n)')
title('Original h(n)')
%%Construct a Desired Filter %%
N=71; % Change the Value of N [Make sure N is ODD]
hamm=hamming(N); % CHange Window
rect = ones(1,N);% Rectangular Window
alpha = (N-1)/2;
symmetry = find(h==max(h)); % index of symmetry
wi=h(symmetry-alpha:symmetry+alpha).*hamm'; % take N-1/2 sameples on both
% sides of the symmetry
subplot(223);
stem(0:N-1,wi);% h(n) is discrete
% axis([-4000 4000 -0.5 0.5])
xlabel('sample(n)')
ylabel('h(n)')
title('Truncated h(n)')
a=-pi:(pi/(0.5*(N-1))):pi;
f=fft(wi);
subplot(224);
plot(a,abs(f));
set(gca,'XTick',-pi:pi/2:pi)
set(gca,'XTickLabel',{'-\pi','\pi/2','0','\pi/2','\pi'})
xlabel('Frequency rad/s')
ylabel('H(e^j^w)')
title('OBATAINED RESPONSE')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment