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

% A tapped delay function
% Inputs: 
% x - input signal
% n - number of taps (reflections)
% g - vector of gains of each tap
% d - vector of delay lengths in samples of each tap
% Outputs:
% y - output signal
% b - numerator coefficients of the transfer function
% a - denominator coefficients of the transfer function

% find the maximum delay length of the delay lengths vector using the 
% built-in function max. call the variable max_delay
max_delay = max(d);

% set the denominator (a) to 1
a = 1;

% create a numerator (b) row vector. The first value of the row = 1
% and the rest of the row values = 0. The length of this row should be 
% equal to the length of max_delay + 1
b = [1, zeros(1,max_delay)];

% use a for loop to assign the correct gain values to their corresponding 
% delay length values + 1 for the numerator vector. The gain values should 
% replace the 0 values in this vector. For example to assign the first 
% gain value at time corresponding to the first delay length value the 
% code-line will look like this: b(d(1)+1) = b(d(1)+1) + g(1);
for k = 1:n
    b(d(k)+1) = b(d(k)+1) + g(k);
end

% 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);