%navrh filtru IIR
%PSP '99

clg;
clc;

%parametry filtru
as=40
ap=2
fs=600
fd=1000 		%dolni
fh=2000 		%horni
fq=2580
fv=8000



%prepocet pro matlab frekvence*2/fv!
[n1 Wn1]=buttord([fd*2/fv fh*2/fv],[fs*2/fv fq*2/fv],ap,as)
[n2 Wn2]=cheb1ord([fd*2/fv fh*2/fv],[fs*2/fv fq*2/fv],ap,as)
[n3 Wn3]=ellipord([fd*2/fv fh*2/fv],[fs*2/fv fq*2/fv],ap,as)

%Wn=[fd*2/fv fh*2/fv];
%stupen filtru je urcujici pro typ filtru
[b1 a1]=butter(n1,Wn1);
[b2 a2]=cheby1(n2,ap,Wn2);
[b3 a3]=ellip(n3,ap,as,Wn3);

[H1 w1]=freqz(b1,a1,512);
[H2 w2]=freqz(b2,a2,512);
[H3 w3]=freqz(b3,a3,512);

figure(1);
plot(w1,abs(H1),'b');
hold on;
plot(w2,abs(H2),'g');
plot(w3,abs(H3),'y');
axis tight;
xlabel('Omega')
ylabel('|H(j*Omega)|')
title('Tolerancni schema a jeho aproximace')


grid;
%tolerancni pole
AP=10^-(ap/20);
AS=10^-(as/20);

%2*pi odpovida fv
%pi ~ fv/2

plot([ 0 fs]*2/fv*pi,[AS AS],'r');
plot([fs fs]*2/fv*pi,[AS  1],'r');
plot([fd fd]*2/fv*pi,[ 0 AP],'r');
plot([fd fh]*2/fv*pi,[AP AP],'r');
plot([fh fh]*2/fv*pi,[AP  0],'r');
plot([fq fq]*2/fv*pi,[ 1 AS],'r');
plot([fq fv]*2/fv*pi,[AS AS],'r');
hold off;

figure(2)
subplot(131);
zplane(b1,a1);
subplot(132);
zplane(b2,a2);
subplot(133);
zplane(b3,a3);

%vyroba diracu :)
di=[1 zeros(1,511)]';

hn=filter(b1,a1,di);
fz=real(ifft(fft(H1).*fft(di)));


figure(3);
subplot(311);
stem(di);
subplot(312);
plot(hn);
subplot(313);
plot(fz);


%pokus o aproximaci funkci fir1
%figure(4);
%[n4 Wn4]=kaiserord([fd*2/fv fh*2/fv],[fs*2/fv fq*2/fv],ap,as);
%b4=fir1(n4,Wn4);
%[H4 w4]=freqz(b11,1,512);
%plot(w4,abs(H4),'b');
