function y = lowpass (X, Wc)

% This function is an audio filter. Specifically, a low pass filter. It
% will apply a low pass filter to the input signal.
%
% A low pass filter is an accurate description by its own name. It will
% not attenuate low frequencies (the frequencies below the chosen
% frequency) and will attenuate the frequencies above.
%
% Syntax to run the low pass filter function:
% y = lowpass (X, Wc)
%
% Function input arguments:
% X: Input sound.
% Wc: The cut-off frequency. This is a normailised frequency which requires
% it to be inputted as such. The formula used to find the normalised
% frequency is simple: cut off frequency / sample rate. For example, if you
% wanted the cut off to be at 1 kHz (1000 Hz) and the sample rate was
% 44100: 1000 / 44100 = 0.022675736961451247165532879818594. The further
% you round this number down the less precisely it will be at 1000 Hz,
% however after a few decimal points it becomes relatively accurate and
% will improve processing time.
% 
% Based on the 'alowpass' filter function by M. Holters.

c = (tan(pi*Wc/2)-1) / (tan(pi*Wc/2)+1);
xh = 0;
for n = 1:length(X)
xh_new = X(n) - c*xh;
ap_y = c * xh_new + xh;
xh = xh_new;
y(n) = 0.5 * (X(n) + ap_y); % The defining difference between a low pass
% and a high pass filter '+' for a low pass filter and '-' for high pass
% filter.
end;