close all; clear;
M = 50;

% Type I FIR
fprintf(1, 'Type I FIR...Lowpass filter\n');
b = firls(M, [0 .2 .3 1], [1 1 0 0]);
[H w] = freqz(b,1,1024); 
figure; subplot(3,1,1); stem(0:length(b)-1, b); xlabel('n'); ylabel('amplitude'); grid on;
subplot(3,1,2); plot(w/pi, 20*log10(abs(H))); xlabel('\omega/pi'); ylabel('Magnitude (dB)'); grid on; zoom on;
subplot(3,1,3); plot(w/pi, unwrap(atan2(imag(H), real(H)))); xlabel('\omega/pi'); ylabel('Phase (radians)'); grid on;
subplot(3,1,1); title (sprintf('Type I FIR - Lowpass filter, M = %d', length(b)-1));
scrsz = get(figure(1),'Position'); set(figure(1), 'Position', [scrsz(1) scrsz(2)/2.5 scrsz(3) scrsz(4)*2]);
fprintf(1, 'Press any key to continue...\n');
pause;

fprintf(1, 'Type I FIR...Highpass filter\n');
b = firls(M, [0 .2 .3 1], [0 0 1 1]);
[H w] = freqz(b,1,1024); 
figure; subplot(3,1,1); stem(0:length(b)-1, b); xlabel('n'); ylabel('amplitude'); grid on;
subplot(3,1,2); plot(w/pi, 20*log10(abs(H))); xlabel('\omega/pi'); ylabel('Magnitude (dB)'); grid on;  zoom on;
subplot(3,1,3); plot(w/pi, unwrap(atan2(imag(H), real(H)))); xlabel('\omega/pi'); ylabel('Phase (radians)'); grid on;
subplot(3,1,1); title (sprintf('Type I FIR - Highpass filter, M = %d', length(b)-1));
scrsz = get(figure(2),'Position'); set(figure(2), 'Position', [scrsz(1) scrsz(2)/2.5 scrsz(3) scrsz(4)*2]);
fprintf(1, 'Press any key to continue...\n');
pause;

% Type II FIR
fprintf(1, 'Type II FIR...Lowpass filter\n');
b = firls(M+1, [0 .2 .3 1], [1 1 0 0]);
[H w] = freqz(b,1,1024); 
figure; subplot(3,1,1); stem(0:length(b)-1, b); xlabel('n'); ylabel('amplitude'); grid on;
subplot(3,1,2); plot(w/pi, 20*log10(abs(H))); xlabel('\omega/pi'); ylabel('Magnitude (dB)'); grid on; zoom on;
subplot(3,1,3); plot(w/pi, unwrap(atan2(imag(H), real(H)))); xlabel('\omega/pi'); ylabel('Phase (radians)'); grid on;
subplot(3,1,1); title (sprintf('Type II FIR - Lowpass filter, M = %d', length(b)-1));
scrsz = get(figure(3),'Position'); set(figure(3), 'Position', [scrsz(1) scrsz(2)/2.5 scrsz(3) scrsz(4)*2]);
fprintf(1, 'Press any key to continue...\n');
pause;

% Type II FIR
% firls will automatically increase the length of the FIR filter
fprintf(1, 'Type II FIR...Highpass filter\n');
b = firls(M+1, [0 .2 .3 1], [0 0 1 1]);
[H w] = freqz(b,1,1024); 
figure; subplot(3,1,1); stem(0:length(b)-1, b); xlabel('n'); ylabel('amplitude'); grid on;
subplot(3,1,2); plot(w/pi, 20*log10(abs(H))); xlabel('\omega/pi'); ylabel('Magnitude (dB)'); grid on; zoom on;
subplot(3,1,3); plot(w/pi, unwrap(atan2(imag(H), real(H)))); xlabel('\omega/pi'); ylabel('Phase (radians)'); grid on;
subplot(3,1,1); title (sprintf('Type II FIR - Highpass filter, M = %d', length(b)-1));
scrsz = get(figure(4),'Position'); set(figure(4), 'Position', [scrsz(1) scrsz(2)/2.5 scrsz(3) scrsz(4)*2]);
fprintf(1, 'Press any key to continue...\n');
pause;

% Type III FIR
fprintf(1, 'Type III FIR...Lowpass filter\n');
b = firls(M, [0 .2 .3 1], [1 1 0 0], 'hilbert');
[H w] = freqz(b,1,1024); 
figure; subplot(3,1,1); stem(0:length(b)-1, b); xlabel('n'); ylabel('amplitude'); grid on;
subplot(3,1,2); plot(w/pi, 20*log10(abs(H))); xlabel('\omega/pi'); ylabel('Magnitude (dB)'); grid on; zoom on;
subplot(3,1,3); plot(w/pi, unwrap(atan2(imag(H), real(H)))); xlabel('\omega/pi'); ylabel('Phase (radians)'); grid on;
subplot(3,1,1); title (sprintf('Type III FIR - Lowpass filter, M = %d', length(b)-1));
scrsz = get(figure(5),'Position'); set(figure(5), 'Position', [scrsz(1) scrsz(2)/2.5 scrsz(3) scrsz(4)*2]);
fprintf(1, 'Press any key to continue...\n');
pause;

fprintf(1, 'Type III FIR...Highpass filter\n');
b = firls(M, [0 .2 .3 1], [0 0 1 1], 'hilbert');
[H w] = freqz(b,1,1024); 
figure; subplot(3,1,1); stem(0:length(b)-1, b); xlabel('n'); ylabel('amplitude'); grid on;
subplot(3,1,2); plot(w/pi, 20*log10(abs(H))); xlabel('\omega/pi'); ylabel('Magnitude (dB)'); grid on; zoom on;
subplot(3,1,3); plot(w/pi, unwrap(atan2(imag(H), real(H)))); xlabel('\omega/pi'); ylabel('Phase (radians)'); grid on;
subplot(3,1,1); title (sprintf('Type III FIR - Highpass filter, M = %d', length(b)-1));
scrsz = get(figure(6),'Position'); set(figure(6), 'Position', [scrsz(1) scrsz(2)/2.5 scrsz(3) scrsz(4)*2]);
fprintf(1, 'Press any key to continue...\n');
pause;

% Type IV FIR
fprintf(1, 'Type IV FIR...Lowpass filter\n');
b = firls(M+1, [0 .2 .3 1], [1 1 0 0], 'hilbert');
[H w] = freqz(b,1,1024); 
figure; subplot(3,1,1); stem(0:length(b)-1, b); xlabel('n'); ylabel('amplitude'); grid on;
subplot(3,1,2); plot(w/pi, 20*log10(abs(H))); xlabel('\omega/pi'); ylabel('Magnitude (dB)'); grid on; zoom on;
subplot(3,1,3); plot(w/pi, unwrap(atan2(imag(H), real(H)))); xlabel('\omega/pi'); ylabel('Phase (radians)'); grid on;
subplot(3,1,1); title (sprintf('Type IV FIR - Lowpass filter, M = %d', length(b)-1));
scrsz = get(figure(7),'Position'); set(figure(7), 'Position', [scrsz(1) scrsz(2)/2.5 scrsz(3) scrsz(4)*2]);
fprintf(1, 'Press any key to continue...\n');
pause;

fprintf(1, 'Type IV FIR...Highpass filter\n');
b = firls(M+1, [0 .2 .3 1], [0 0 1 1], 'hilbert');
[H w] = freqz(b,1,1024); 
figure; subplot(3,1,1); stem(0:length(b)-1, b); xlabel('n'); ylabel('amplitude'); grid on;
subplot(3,1,2); plot(w/pi, 20*log10(abs(H))); xlabel('\omega/pi'); ylabel('Magnitude (dB)'); grid on; zoom on;
subplot(3,1,3); plot(w/pi, unwrap(atan2(imag(H), real(H)))); xlabel('\omega/pi'); ylabel('Phase (radians)'); grid on;
subplot(3,1,1); title (sprintf('Type IV FIR - Highpass filter, M = %d', length(b)-1));
scrsz = get(figure(8),'Position'); set(figure(8), 'Position', [scrsz(1) scrsz(2)/2.5 scrsz(3) scrsz(4)*2]);
