% FilterChirp - filter a chirp signal with different filters
% 
% demonstrates filters frequency response via spectrogram and sound

%% Parameters
fs =11025;
fc =700;
fb =1500;

f1 = 0;
f2 =fs/2;
T = 10;

%% construct a chirp
tt = 0:1/fs:T;
psi = 2*pi*(f2-f1)/(2*T)*tt.^2 + 2*pi*f1*tt;
xx = cos(psi);

soundsc(xx,fs)

pause

%% First filter - 31-point averager
hh1 = ones(1,31)/31;
yy1 = conv(xx,hh1);

figure(1)
spectrogram(yy1, 256, 128, 256, fs, 'yaxis');
colorbar();
soundsc(yy1, fs);

figure(6)
freqz(hh1, 1, 256, fs);

pause

%% Second filter - a very good low-pass filter
hh2 = firpm(100, [0 fc, 1.5*fc, fs/2]*2/fs, [1 1 0 0]);
yy2 = conv(xx,hh2);

figure(2)
spectrogram(yy2, 256, 128, 256, fs, 'yaxis');
colorbar();
soundsc(yy2, fs);

figure(6)
freqz(hh2, 1, 256, fs);

pause

%% Third filter - Bandpass equivalent of 31-point averager
hh3 = cos(2*pi*fb/fs*(-15:15))/31;
yy3 = conv(xx,hh3);

figure(3)
spectrogram(yy3, 256, 128, 256, fs, 'yaxis');
colorbar();
soundsc(yy3, fs);

figure(6)
freqz(hh3, 1, 256, fs);

pause

%% Fourth filter - a very good band-pass filter
hh4 = firpm(100, [0 (fb-1.5*fc) (fb-0.9*fc) (fb+0.9*fc) (fb+1.5*fc) fs/2]*2/fs, [0 0 1 1 0 0]);
yy4 = conv(xx,hh4);

figure(4)
spectrogram(yy4, 256, 128, 256, fs, 'yaxis');
colorbar();
soundsc(yy4, fs);

figure(6)
freqz(hh4, 1, 256, fs);

pause

%% Fifth filter - a very good band-stop filter
hh5 = firpm(100, [0 (fb-1.5*fc) (fb-0.9*fc) (fb+0.9*fc) (fb+1.5*fc) fs/2]*2/fs, [1 1 0 0 1 1]);
yy5 = conv(xx,hh5);

figure(5)
spectrogram(yy5, 256, 128, 256, fs, 'yaxis');
colorbar();
soundsc(yy5, fs);

figure(6)
freqz(hh5, 1, 256, fs);

