% Design LP and HP filters for audio speaker crossover network.
% R. Kozick, ELEC 226

w0 = 1000;      % Frequency separating LP and HP filters (rad/sec)
B = 0;          % Cutoffs for LP and HP are offset from w0 by (B/2)
wcL = w0-B/2;   % Cutoff freq. for LP filter (rad/sec)
wcH = w0+B/2;   % Cutoff freq. for HP filter (rad/sec)

wv = logspace(1, 5, 500);   % Vector of freqs. for Bode plot (rad/sec)

% Complex frequency response of LP and HP filters
HL = 1 ./ ( (1 - (wv/wcL).^2) + j*(wv/wcL*sqrt(2)) );       % LP
HH = 1 ./ ( (1 - (wcH./wv).^2) - j*(wcH./wv*sqrt(2)));    % HP

MLdB = 20*log10(abs(HL));   % Magnitude in dB
MHdB = 20*log10(abs(HH));

pL = angle(HL)/pi*180;      % Phase in degrees
pH = angle(HH)/pi*180;

figure(1)   % Magnitude response of LP and HP filters
semilogx(wv, MLdB, 'b-', wv, MHdB, 'r-');
legend('LP', 'HP');
xlabel('Frequency (rad/sec)')
ylabel('dB')
title('Magnitude Responses')
axis([1e1 1e5 -80 10]);
grid

figure(2)   % Phase response of LP and HP filters
semilogx(wv, pL, 'b-', wv, pH, 'r-');
legend('LP', 'HP');
xlabel('Frequency (rad/sec)')
ylabel('Degrees')
title('Phase Responses')
axis([1e1 1e5 -180 180]);
grid

% Total frequency response of system is sum of LP and HP filters
% (the summation occurs by the sound waves in space)
HT = HL + HH;
MTdB = 20*log10(abs(HT));
pT = angle(HT)/pi*180;

figure(3)   % Bode plots of total response
subplot(211)
semilogx(wv, MTdB, 'b-');
xlabel('Frequency (rad/sec)')
ylabel('dB')
title('Total Magnitude Response')
axis([1e1 1e5 (min(MTdB)-3) 10]);
grid
subplot(212)
semilogx(wv, pT, 'b-');
xlabel('Frequency (rad/sec)')
ylabel('Degrees')
title('Total Phase Response')
axis([1e1 1e5 -180 180]);
grid

