Boost logo

Boost :

From: nbecker_at_[hidden]
Date: 2001-11-01 14:09:49


The discussion about fft brings up the subject of signal processing
components. I have been heavily developing components for quite a
while. I don't know if they have any place in boost. What is the
general feeling?

BTW, here is a simple example:

#ifndef _IIR_H
#define _IIR_H

// $Id: IIR.H,v 1.1 1999/11/24 14:23:26 nbecker Exp $

#include <algorithm>
#include <vector>
#include <complex>
#include <cmath>

using std::vector;
using std::complex;

template<class coeftype, class inputtype = coeftype>
class IIR {
  vector<coeftype> num;
  vector<coeftype> den;
  int order;
  vector<inputtype> z;
public:
  // h(z) = (a0 + a1 z^-1 + ...)/(1 + b1 z^-1 + ...)
  template<class NumInputIterator, class DenInputIterator>
  IIR (NumInputIterator numstart, NumInputIterator numend, DenInputIterator denstart, DenInputIterator denend) :
    num (numstart, numend),
    den (denstart, denend),
    order (std::max (num.size(), den.size())),
    z (order)
    {}

  // initialize internal state
  template<class NumInputIterator, class DenInputIterator, class CoefIterator>
  IIR (NumInputIterator numstart, NumInputIterator numend, DenInputIterator denstart, DenInputIterator denend, CoefIterator zstart, CoefIterator zend) :
    num (numstart, numend),
    den (denstart, denend),
    order (std::max (num.size(), den.size())),
    z (zstart, zend)
    {}

  // Noop default
  IIR () {}

  template <class outputtype>
  outputtype Compute (inputtype x) {
    inputtype w = x;
    for (int i = 1; i < den.size(); i++)
      w += -den[i] * z[i];

    outputtype y = num[0] * w;
    for (int i = 1; i < num.size(); i++)
      y += num[i] * z[i];

    for (int i = order - 1; i > 1; i--)
      z[i] = z[i-1];

    z[1] = w;

    return y;
  }

  void Reset (inputtype x = 0) {
    for (int i = 0; i < z.size(); i++)
      z[i] = x;
  }

  template<typename flt>
  complex<flt> H (complex<flt> z) const {
    complex<flt> zi = pow (z, -1);
    complex<flt> top (0., 0);
    for (int i = 0; i < num.size(); i++)
      top += num[i] * pow (zi, i);

    complex<flt> bot (1.,0);
    for (int i = 1; i < den.size(); i++)
      bot += den[i] * pow (zi, i);

    return top/bot;
  }

  template<typename flt>
  flt MagSqr (complex<flt> z) const {
    return norm (H (z));
  }

};

#endif


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk