clear all, close all, clc, format short e

% Specification

Fp = 3400;
Fs = 5000;
Ap = 1;
As = 20;

Fplot = 6000;

% Step 1: Approximation
% Select Chebyshev Type I analog approximation
% Determine order
[n, Wn] = cheb1ord(Fp, Fs, Ap, As, 's');
% Compute poles and zeros
[z,p,k] = cheb1ap(n, Ap);
% Compute Q-factors and magnitudes of poles
% Determine transfer function
Fn = Fp;
num = 10^(-Ap/20); den = 1;
for i = 1:length(p)
    if imag(p(i)) >0
        Q(i) = (-abs(p(i))./(2*real(p(i))));
        w(i) = round(abs(p(i))*(2*pi*Fn));
        D(i,:) = [1 w(i)/Q(i) w(i)^2];
        N(i,1) = w(i)^2;
        num = conv(num,N(i,:));
        den = conv(den,D(i,:));
    end
end

f = 0:Fplot/200:Fplot;
H = freqs(num,den,2*pi*f);
M = -20*log10(abs(H));

plot(f,M)
plot([0 Fp Fp],[Ap Ap As],'r--',[0 Fs Fs Fplot],[-Ap -Ap As As],'r--')
hold on
plot(f,M,'LineWidth',3)
hold off
%ytick1 = [0 Ap As]; set(gca,'YTick',ytick1)
xtick1 = [0 Fp Fs]; set(gca,'XTick',xtick1,'XTickLabel',{'0';'3.1';'4.2'},'FontSize',14)
xlabel('Frequency (10^6 Hz)','FontSize',14)
ylabel('Attenuation (dB)','FontSize',14)
