plot(f1, yDDFTNorm);
axis([0 fs/8 0 1.2]);
xlabel('frequency in Hz')
ylabel('magnitude');
title('Frequency spectrum of down sampled chirp signal' );
%Butterworth anti-aliasing
fc = fs/8;
Rp = .02; RpDB = -20*log10(1-Rp);
Rs = .01; RsDB = -20*log10(Rs);
fpass = fs/8-300;
fstop = fs/8+400;
[Nb fc] = buttord(fpass/(fs/2), fstop/(fs/2), RpDB, RsDB);
[num den] = butter(Nb, fc);
yf = filter(num, den, y);
yfDn = downsample(yf, 4);
figure(1);
subplot(1, 2, 2)
hold on;
[H f] = freqz(num, den, 1024, fs);
plot(f, abs(H));
figure(3);clf;
subplot(1, 2, 1);
t1 = 0:4*T:(N-1)*T;
stem(t1, yfDn, 'MarkerSize', 1);
axis([0 .02 -1.2 1.2]);
xlabel('time in seconds');
ylabel('amplitude');
title('Chirp signal down sampled by 4' );
subplot(1, 2, 2);
yDDFT = fft(yfDn);
f1 = 0:fs/N:fs/4-(fs/4)/N;
yDDFTNorm = abs(yDDFT)/max(abs(yDDFT));
plot(f1, yDDFTNorm);
axis([0 fs/8 0 1.2]);
xlabel('frequency in Hz')
ylabel('magnitude');
title('Frequency spectrum of down sampled chirp signal' );
%Elliptic anti-aliasing
fc = fs/8;
Rp = .02; RpDB = -20*log10(1-Rp);
Rs = .01; RsDB = -20*log10(Rs);
fpass = fs/8-200;
fstop = fs/8+200;
[Ne fp] = ellipord(fpass/(fs/2), fstop/(fs/2), RpDB, RsDB);
[num den] = ellip(Ne, RpDB, RsDB, fp);
yf = filter(num, den, y);
yfDn = downsample(yf, 4);
figure(1);
subplot(1, 2, 2)
hold on;
[H f] = freqz(num, den, 1024, fs);
plot(f, abs(H));
figure(4);clf;
subplot(1, 2, 1);
t1 = 0:4*T:(N-1)*T;
stem(t1, yfDn, 'MarkerSize', 1);
axis([0 .02 -1.2 1.2]);