function [Z, P] = zeroPole(a, b, plt)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function [Z, P] = zeroPole(a, b, plt)
% ex.: [Z, P] = zeroPole( [1 0 0.81], [1 0.5 -1], 1)
% ex.: [Z, P] = zeroPole( [1 -1.2 0.3], [0.1 0.0 0.0], 1)
%
% Task: To experiment with zeros and poles of filters
%
% Inputs:
%	-a: vector of ak weights applied to previous outputs
%	-b: vector of bm weights applied to previous inputs
%	-plt: if different from 0, plot the zeros/poles
%
% Outputs: 
%	
%
% Author: Guillaume Gibert  - guillaume.gibert@ecam.fr, 
%         
%   - Minor adjustments : 
%         Charles Stelandre - charles.stelandre@ecam.fr
%                                              
% Date: 09/04/2025 
%   - Revised on : 
%       01/05/2025
%
% Notes : A small adjustment was made to detect whenever the FIR or the IIR
% is used. Additionally, the poles and zeros labels were adjusted to be
% displayed correctly in the complex z-plane.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% zeros of the filter
Z = roots(b);
% poles of the filter
P = roots(a);
isFIR = (length(a) == 1 && a == 1);
% Print filter type
if isFIR
    disp('Detected FIR filter (no feedback, all poles at origin).');
else
    disp('Detected IIR filter (feedback present).');
end

% Stability check for IIR
if ~isFIR
    if all(abs(P) < 1)
        disp('IIR filter is stable (all poles inside unit circle).');
    else
        warning('IIR filter is NOT stable (some poles are outside the unit circle).');
        disp('Pole magnitudes:');
        disp(abs(P));
    end
end

if (plt)
	figure;
	zplane(Z, P);
	title('Zeros and poles of the transfer function');
	legend('Zeros', 'Poles');
	grid on;
	
	figure;
	impz(b, a);
	title('Impulse response');
	xlabel('Samples (n)');
	grid on;

end
