% Demonstration of first-, second-, and third-order low-pass filters.
% Music is filtered for each case, and the differences can be heard.
% This program must be run on the Suns to access the music file.
% Rich Kozick, Fall 1997

x = auread('/home/pleiades/ACM/snd/spacemusic.au');
t = (1:length(x))/8192;
w = logspace(1, 5, 500);

[b1,a1] = butter(1, 2*pi*400, 's');
figure(1)
bode(b1,a1,w)
subplot(211)
title('FIRST ORDER FILTER')
axis([1e1 1e5 -80 0])
y1 = lsim(b1, a1, x, t);

[b2,a2] = butter(2, 2*pi*400, 's');
figure(2)
bode(b2,a2,w)
subplot(211)
title('SECOND ORDER FILTER')
axis([1e1 1e5 -80 0])
y2 = lsim(b2, a2, x, t);

[b3,a3] = butter(3, 2*pi*400, 's');
figure(3)
bode(b3,a3,w)
subplot(211)
title('THIRD ORDER FILTER')
axis([1e1 1e5 -80 0])
y3 = lsim(b3, a3, x, t);

% Write results to .wav files, which can be played on a PC
% DOES NOT WORK -- PRODUCES FILES WITH ALL ZEROS

% wavwrite(x, 8192, 'filx')
% wavwrite(y1, 8192, 'fily1.wav')
% wavwrite(y2, 8192, 'fily2.wav')
% wavwrite(y3, 8192, 'fily3.wav')

% Write results to .au files

% auwrite(x, 'filx.au');
% auwrite(y1, 'fily1.au');
% auwrite(y2, 'fily2.au');
% auwrite(y3, 'fily3.au');

% To play the results in MATLAB:

% sound(x)
% sound(y1)
% sound(y2)
% sound(y2)
