%=====================================================
%= Filter Design Example Using The YuleWalk Function =
%=====================================================

fHz0 = [0 100 150 200 225 250];  %Filter's Frequencies
m0 = [1.0 1.0 0.5 0.5 1.0 1.0];  %Filter's Magnitudes

%========================================================
%= Plot The Filter's Characteristics As Described Above =
%========================================================

figure(1);
plot(fHz0,m0);
title('The IIR BAND STOP Filter Characteristics');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on

%===============================
%= Design of The Actual Filter =
%===============================

Fs=500;  							%Sample Frequency
f0 = fHz0 / (Fs/2);                 %Convert The Filter's Frequencies To NonDimensional Frequencies
[bIIR,aIIR] = yulewalk(6,f0,m0);    %Design The Actual Filter Coefficients  

%===============================================
%= Calculate The Filter's Characteristics With =
%=      The FREQZ() MATALAB Function           =
%===============================================

figure(2);
freqz(bIIR,aIIR,800,Fs);   %Calculate The Filter's Characteristics

%===============================================
%= Plot The Designed Filter's Characteristics  =
%= Calculate The Filter's Characteristics By   =
%= Applying The Frequency Response Equation    =
%===============================================

XAxisInHz = linspace(0,250,50);             %X-axis in Hz
                                            %To convert Frequency in Hz to RAD/S, Fhz = 2*pi*Fhz = W
XAxisInRad = 2*pi*XAxisInHz;                %X-axis in RAD/S
z=exp(sqrt(-1)*XAxisInRad/Fs);              %At The System Function Substitute z With e^jè To Obtain The Frequency Response Equation
IIR=abs(polyval(bIIR,z)./polyval(aIIR,z));  %Calculate The Filter's Freqency Response

figure(3); 
plot(fHz0,m0,XAxisInHz,IIR);
title('The IIR BAND STOP Filter Design (Blue: Idial Filter Design - Green: Actual Filter Design)');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on