% routine to show that we can have a filter with
% the same modulus, but with quite different phase
% which give unwanted effects when applied to a signal.
% figure 1 shows the modulus of the 2 filters and the 2 different phases
% figure 2 shows the original signal and the 2 different filtered versions

clear, figure(1),clf
set(1,'Position',get(0,'DefaultFigurePosition'));

% test linear phase: see hlfilter.m

 b0 = -3;
 b2 = -1;
 highpass = 1;

% make the high/low pass filter

[a,b] = hlfilter(b0,b2,highpass)
N = 1000;
n = 100;
omega = (-N:N)*pi/N;

% plot modulus

subplot(2,1,1);
A = DTFTfast(a,N); AA = abs(A); plot(omega,AA,'g'); hold on
B = DTFTfast(b,N); AB = abs(B); plot(omega,AB,'r'); hold off
title('Filters: |X(\omega)|=|FFT(x)|'), xlabel('\omega')

% plot phase

subplot(2,1,2);
FA = unwrap(angle(A)); FB = unwrap(angle(B));
plot(omega,FA,'g',omega,FB,'r'),
title('Phase[X(\omega)]'), xlabel('\omega')

% generate a signal

t = (1:n);
om1 = 2.1; x1 = cos(om1*t);
om2 = pi; x2 = 2*cos(om2*t);
x = x1+x2;

% filter it

y1a = filter(a,1,x1);
y2a = filter(a,1,x2);
y1b = filter(b,1,x1);
y2b = filter(b,1,x2);
ya = filter(a,1,x);
yb = filter(b,1,x);

%i1 = fix(N+1+om1*N/pi);
%i2 = fix(N+1+om2*N/pi);
%yya = y1a/AA(i1) + y2a/AA(i2);
%yyb = y1b/AB(i1) + y2b/AB(i2);

figure(2),
subplot(2,1,1), plot(x,'b'), title('signal')
subplot(2,1,2), plot(t,ya,'g',t,yb,'r'),
title('filtered signals')
figure(1)
