% Conversion of the analog to th digital circuit
%
% -----------------------------------------------------------------------------------
%
% (C) 2017, Krzysztof Sozanski, Digital Signal Processing in Power Electronics Control 
% Circuits, second edition, Springer-Verlag, London, 2017. 
% 
% -----------------------------------------------------------------------------------
%
clear all; close all; l_width = 1.5; f_size = 10;
fs = 1.0;                  % sampling frequency
[z,p,k] = ellipap(8,7,50); % elliptic analog low-pass filter prototype omega=1
[num,den] = zp2tf(z,p,k);  % convert to transfer function form
w = logspace(-1,1,2^14);   % generate logarithmically spaced vectors
figure('Name','Analog prototype','NumberTitle','off');
h = freqs(num,den,w); fa=w/(2*pi);
figure('Name','Frequency response of analog prototype','NumberTitle','off');
subplot(2,1,1), plot(fa,20*log10(abs(h)),'r','linewidth',l_width),
set(gca,'FontSize',f_size), grid on, axis([0 0.5 -100 5])
title('(a)'), ylabel('Magnitude [dB]');
subplot(2,1,2), plot(fa,180/pi*angle(h),'b','linewidth',l_width), 
set(gca,'FontSize',f_size),grid on, set(gca,'Xlim',[0 0.5]);
xlabel 'Frequency (Hz)', ylabel('Phase [deg]'); title('(b)'),
print('analog_prototype.pdf','-dpdf');
[numd,dend] = bilinear(num,den,fs);   % analog to digital conversion
N=2^14; imp=[1 zeros(1,N-1)];
resp=filter(numd,dend,imp);
spect=fft(resp); f=(0:N-1)/(fs*N);
figure('Name','Frequency response of digital filter','NumberTitle','off');
subplot(2,1,1), plot(f,20*log10(abs(spect)),'r','linewidth',l_width),
set(gca,'FontSize',f_size), grid on, axis([0 fs/2 -100 5]);
title('(c)'), ylabel('Magnitude [dB]');
subplot(2,1,2), plot(f,180/pi*angle(spect),'b','linewidth',l_width), 
set(gca,'FontSize',f_size),grid on, title('(d)'), set(gca,'Xlim',[0 fs/2]);
xlabel 'Frequency (f/f_{s})', ylabel('Phase [deg]');
print('digital_filter.pdf','-dpdf');