% demo of allpass filtering creating a reverberation effect
% you must have "overview.wav" in the same directory
% as this file.  Any other "wav" file can be substituted,
% but it works best with a voice recording.
% 
% R Kakarala
% UC Berkeley Extension
% 

[X,Fs,Nbits]=wavread('overview.wav');

% create all pass filter with 10 ms delay in FIR portion.
R = round( 10/1000 * Fs );  % sample number at 10 ms
alpha=0.75; % reflection coefficient.
b=[alpha,zeros(1,R-1),1];  % numerator coefficients
a=fliplr(b);                           % denominator coefficients are reverse of numerator

Y=filter(b,a,X);                    % filter with all pass

% rescale to [-0.9,0.9] to avoid clipping
m=min(Y);
M=max(Y);
Y=1.8*(Y-m)/(M-m) - 0.9;

% save filtered wav file
wavwrite(Y,Fs,'reverboverview.wav');

%plot impulse reponse of filter
delta=zeros(1,4096);  % take first 4096 points;
delta(1)=1;
h=filter(b,a,delta);  
stem(1000*(0:4095)/Fs,h);  xlabel('time (ms)'); title('impulse response');

%compute freq response and group delay
iirfreqresp(b,a);
iirgroupdelay(b,a);