% ECE481 Electronic Music Synthesis (S2001-02)
%
% lpfsweep.m: Starting point for MiniProject 7

% Parameters
fs=10e3;	%sampling frequency (Hz)
dur=2;	%duration (s)
N=1000;	%block size (samples)

% Define a time vector
tt=0:1/fs:dur-1/fs;

% Define a wideband signal source
ss=square(2*pi*110*tt);


% Number of blocks that will fit in the signal
numblocks=length(ss)/N;

% Reshape the input signal vector into a matrix (each column is a block)
ssb = reshape(ss,N,numblocks);

% LPF filter corner frequency trajectory
%fc=linspace(0.01,0.99,numblocks);
fc=logspace(-1,-0.01,numblocks)

% Generate the filter coefficients
bcoeff=[];
acoeff=[];
for k=fc
   [b,a]=butter(5,k);
   bcoeff=[bcoeff ; b];
   acoeff=[acoeff ; a];
end

% Apply the filter
y=zeros(size(ssb));
for k=1:length(fc)
   y(:,k)=filter(bcoeff(k,:),acoeff(k,:),ssb(:,k));
end

% Make y into a vector again
y=reshape(y,1,N*numblocks);

% Normalize the output signal
y=y/max(abs(y));

% Play the original
sound(ss,fs)
figure(1)
calspec(ss,[],fs)
disp('Original sound ... (press key to continue)')
pause

% Display the corner frequency trajectory
figure(2)
plot(fc)
disp('Corner frequency trajectory... (press key to continue)')
pause

% Play the filtered sound
sound(y,fs)
figure(3)
calspec(y,[],fs)
disp('Filtered sound.')




