en fr

nyctergatis.com

Sysquake Remote Live

Filter design

The figure below shows the frequency response of an analog filter. You can choose the type of filter and its passband frequencies.

Type of filter: Order: Kind:
Lower cutoff frequency: Hz
Higher cutoff frequency: Hz (used only for bandpass and bandstop filters)
Bandpass ripples: dB (used only for Chebyshev type 1 and elliptic filters)
Bandstop ripples: dB (used only for Chebyshev type 2 and elliptic filters)
Filter response

Source code

Here is the code inserted in the file stored on the server. If you look at the source of this page in your browser, you will see only the HTML code produced by Sysquake Remote. Functions for managing the HTML form and for calculating the filter coefficients come from separate libraries which are provided with Sysquake Remote. The library file filter.lml is the same as the one which comes with Sysquake and other compatible products from Calerga.

<?sqr
% import definitions from libraries
use filter, sqr;

% interactive form
format = ['Type of filter: %{Butterworth,Chebyshev type 1,Chebyshev typ
    'Order: %{1,2,3,4,5}m  ',...
    'Kind: %{Lowpass,Highpass,Bandpass,Bandstop}m\n',...
    'Lower cutoff frequency: %{6}n Hz\n',...
    'Higher cutoff frequency: %{6}n Hz (used only for bandpass and band
    'Bandpass ripples: %{6}n dB (used only for Chebyshev type 1 and ell
    'Bandstop ripples: %{6}n dB (used only for Chebyshev type 2 and ell
    '%{Revert}R%{Update}S'];
names = {'type', 'order', 'kind', 'fl', 'fh', 'rp', 'rs'};
s0.type = 'Butterworth';
s0.kind = 'Lowpass';
s0.order = 3;
s0.fl = 1;
s0.fh = 5;
s0.rp = 3;
s0.rs = 20;
s = processhtmlform(format, names, s0);
displayhtmlform(format, names, s, 'get');

% filter synthesis
switch s.kind
  case 'Lowpass'
    knd = 'lp';
    w = 2 * pi * s.fl;
  case 'Highpass'
    knd = 'hp';
    w = 2 * pi * s.fl;
  case 'Bandpass'
    knd = 'bp';
    w = 2 * pi * [s.fl, s.fh];
  case 'Bandstop'
    knd = 'bs';
    w = 2 * pi * [s.fl, s.fh];
end
switch s.type
  case 'Butterworth'
    [z, p, k] = butter(s.order, w, knd, 's');  % zeros/poles/gain
    r = [];
  case 'Chebyshev type 1'
    [z, p, k] = cheby1(s.order, s.rp, w, knd, 's');
    r = 0.1.^(0.05 * s.rp);  % conv. from dB (.^ = element-wise power)
  case 'Chebyshev type 2'
    [z, p, k] = cheby2(s.order, s.rs, w, knd, 's');
    r = 0.1.^(0.05 * s.rs);
  case 'Elliptic'
    [z, p, k] = ellip(s.order, s.rp, s.rs, w, knd, 's');
    r = 0.1.^(0.05 * [s.rp; s.rs]);
end
num = k * poly(z);
den = poly(p);

% figure
beginfigure('size', [440, 300]);  % default is GIF with transparent bg
scale(2 * pi * [0, 10]);       % range of the horizontal axis (0-10 Hz)
scalefactor([1/(2*pi), 1]);    % display freq in Hz instead of rad/s
line([1, 0], w(:), 'r');       % 1*x + 0*y = w(i) for all i
line([0, 1], r(:), 'b');       % 0*x + 1*y = r(i) for all i
bodemag(num, den);             % freq. response magnitude of num/den
label('f [Hz]');
lgnd = 'Filter response\nCutoff frequency';
if s.type ~== 'Butterworth'    % ripples only if not Butterworth
  lgnd = [lgnd, '\nRipple limit'];
end
title('Filter response');
legend(lgnd, 'krb');
endfigure;
?>