function [y, b, a] = allpass(x, g, d) 

% A simple all-pass filter function
% Inputs:
% x - input signal
% g - feedforward gain, should be less than 1 for stability
% d - delay length in samples
% Outputs:
% y - output signal
% b - numerator coefficients of the transfer function
% a - denominator coefficients of the transfer function 

% construct the row vector for the denominator (a) where the first sample 
% value is 1, then a number of 0 samples equal to the delay length - 1, 
% and the last sample equals to the gain factor
a=[1 zeros(1,d-1) g];

% construct the row vector for the numerator (b) where the first sample 
% value is the gain factor, then a number of 0 samples equal to the delay
% length - 1, and the last sample equals to 1
b=[g zeros(1,d-1) 1];

% create an impulse response using the built-in function impz where the 
% numerator and denominator as input variables.
impresp = impz(b, a);

% filter the input signal with the built-in function conv using the 
% impulse response. call the filtered output signal y.
y = conv(x, impresp);
