% Filtering_Basics 
% Author C. Prati
% February 17, 2020
clear
close all
%--------------------------------------------------
% 1
% Let us generate a discrete impulse response hLP of a Low-Pass Filter
% The length of the filter in  N=64 samples
% The cut-off frequency is fc=200Hz and the sampling frequency is 
% fs=2KHz
% The matlab function used to generate h is fir1:
% The general format is hLP = fir1(N,W) where W is the normalized cut-off
% frequency: W must be between 0 < W < 1.0, with 1.0 corresponding to half
% the sampling  frequency.

N=64;
fc=200;
fs=2000;
W=fc/fs*2;

hLP=fir1(N,W);

figure(1)
stem((0:N),hLP,'filled')
title('Low Pass impulse response')

pause

%--------------------------------------------------
% 2
% Let us generate a discrete impulse response hHP of a High-Pass Filter
% The length of the filter in N=64 samples
% The cut-off frequency is fc=200Hz and the sampling frequency is 
% fs=2KzH.
% The matlab function used to generate h is again fir1:
% The general format is hHP = fir1(N,W,'high') where W is the normalized cut-off
% W must be between 0 < W < 1.0, with 1.0 corresponding to half the sampling  frequency.

hHP=fir1(N,W,'high');

figure(1)
clf
stem((0:N),hHP,'filled')
title('high Pass impulse response')

pause




%--------------------------------------------------
% 3
% Let us generate a discrete impulse response hBP of a Band-Pass Filter
% The length of the filter in N=64 samples
% The lower and higher cut-off frequencies are f1=100Hz and f2=200Hz 
% the sampling frequency is fs=2KzH.
% The matlab function used to generate h is again fir1:
% The general format is hBP = fir1(N,[W1 W2],'bandpass') where W1 ans W2
% are the normalized cut-off frequencies. W1 and W2 must be between 0 < W < 1.0, 
% with 1.0 corresponding to half the sampling  frequency.
%--------------------------------------------------

N=64;
f1=100;
f2=200;
fs=2000;
W1=f1/fs*2;
W2=f2/fs*2;

hBP=fir1(N,[W1 W2],'bandpass');

figure(1)
clf
stem((0:N),hBP,'filled')
title('Band Pass impulse response')

pause


%--------------------------------------------------
% 4
% Let us generate a chirp signal x using a sampling frequency fs=2kHz: 
% a chirp is a cosinusoid with frequency that linearly varies with time.
% x = chirp(t,f0,t1,f1) generates samples of a linear swept-frequency
% signal at the time instances defined in array t.  The instantaneous
% frequency at time 0 is f0 Hertz.  The instantaneous frequency f1
% is achieved at time t1.  By default, f0=0, t1=1, and f1=100.
%--------------------------------------------------

T=1/fs;
t=(0:T:2);
f0=10;
f1=300;
t1=2;
x=chirp(t,f0,t1,f1);
figure(1)
clf
plot(t,x)
pause

%--------------------------------------------------
% 5
% Let us plot the signal y obtained by filtering the chirp x with
% a) the LP filter
% b) the HP filter
% c) the BP filter
%----------------------------------------------------

yLP=conv(x,hLP,'same');
yHP=conv(x,hHP,'same');
yBP=conv(x,hBP,'same');
figure(2)
clf
plot(t,yLP)
pause
plot(t,yHP)
pause
plot(t,yBP)

pause

%--------------------------------------------------
% 6
% Let us generate boxcar signal x and let's filter it with
% the low-pass, high-pass and bandpass filters

x=zeros(1,500);
x(200:300)=1;
figure(1)
clf
plot(x)
axis([1 500 -.3 1.2])
pause
yLP=conv(x,hLP,'same');
yHP=conv(x,hHP,'same');
yBP=conv(x,hBP,'same');
figure(2)
clf
plot(x,'r')
hold on
plot(yLP,'b')
axis([1 500 -.3 1.2])
pause
hold off
plot(x,'r')
hold on
plot(yHP,'b')
axis([1 500 -.3 1.2])
pause
hold off
plot(x,'r')
hold on
plot(yBP,'b')
axis([1 500 -.3 1.2])
hold off

