Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Constructing FIR Digital Filters with valarray


November 1999/Constructing FIR Digital Filters with valarray/Figure 5

Figure 5: Partial listing of file Signal.h, the Signal class implementation

#include "FIR.h"

enum Fade_in_modes { linear, cosine_shaped };

template <class T>
class Signal
{
public:
            // **************  Constructor  ******************
    Signal (int requested_size = 256, int fade_in_len = 10, 
            Fade_in_modes mode = linear);

            // *********  Samples insertion methods  *********
    Signal<T> & operator<< (const T & value)
    {
        recent_sample_pos = (recent_sample_pos + 1) & mask;
        x[recent_sample_pos] = value;
        return *this;
    }

    void fade_in (const T & value);  // definition omited here

    template <class Iterator>
    void insert_block (Iterator block_start, size_t block_size)
    {
        if (block_size > capacity() || block_size < 1)
            throw exception("Bad block_size in Signal::insert_block()");

        if (recent_sample_pos + block_size < x.size())
        {
            copy (block_start, block_start + block_size, 
                  &x[recent_sample_pos + 1]);
            recent_sample_pos += block_size;
        }
        else
        {
            copy (block_start, 
                  block_start + x.size() - recent_sample_pos - 1, 
                  &x[recent_sample_pos + 1]);
            copy (block_start + x.size() - recent_sample_pos - 1, 
                  block_start + block_size, 
                  &x[0]);
            recent_sample_pos += block_size - x.size();
        }
    }

            // ************  Samples read methods  ************
    const T & operator[] (int subscript) const
    {
        if (subscript <= 0)
            return const_cast<Signal<T> *>(this) -> 
                   x[(recent_sample_pos + subscript) & mask];
        else
            throw out_of_range("Subscript must be negative");
    }

    const T & most_recent_sample () const
    {
        return const_cast <Signal<T> *>(this)->x[recent_sample_pos];
    }

    const T & front () const  { return most_recent_sample(); }

        // ************  Iterators  ***************
    class iterator
    {
        friend class const_iterator;
    public:
        iterator (T * sig = NULL, int it = 0, int msk = 0)
            : signal(sig), iter(it), mask(msk) {}

        T & operator* () const  { return signal[iter]; }
            // iter was already "ANDed" with mask in 
            // all the iterator arithmetic operators

        const iterator & operator++ ()
        {
            iter = (iter + 1) & mask;
            return *this;
        }

        const iterator operator+ (int offset) const
        {
            return iterator (signal, (iter+offset) & mask, mask);
        }
            // The rest of the operators are not shown here

    private:
        T * signal;
        int iter;
        int mask;
    };

        // **  class const_iterator is not shown here, to save 
        // **  magazine space;  its definition is very similar 
        // **  to that of iterator.

    iterator begin ()
    {
        return iterator(&x[0], recent_sample_pos, mask);
    }

    const_iterator begin () const
    {
        return const_iterator (const_cast<Signal<T> *>(this)->begin());
    }

        // ************ Filtering Functions ******************
    template <class FilterCoeff>
    void filtered_output (const basic_FIR<FilterCoeff> & filter, 
                          T & result) const
    {
        if (recent_sample_pos >= filter.length() - 1)
            filter.output (&const_cast<Signal<T> *>(this) -> 
                           x[recent_sample_pos], 
                           result);
        else
            filter.output (begin(), result);
    }

    template <class FilterCoeff>
    T filtered_output (const basic_FIR<FilterCoeff> & filter) const
    {
        T result;
        filtered_output (filter, result);
        return result;
    }

    template <class FilterCoeff, class OutputIterator>
    void filtered_block (const basic_FIR<FilterCoeff> & filter, 
                         int num_samples, OutputIterator y) const
    {
        for (int i = -num_samples + 1; i <= 0; i++)
            if (((recent_sample_pos + i) & mask) >= filter.length()-1)
                filter.output (&const_cast<Signal<T> *>(this) -> 
                               x [(recent_sample_pos + i) & mask], 
                               *y++);
            else
                filter.output (begin() + i, *y++);
    }

            // **************  Misc. / Utility  ***************
    int capacity () const  { return size; }

private:
    valarray<T> x;
    int mask, size, recent_sample_pos;

    Fade_in_modes fade_in_mode;
    int fade_in_counter, fade_in_length;
};


// *************************************
//      Member functions definitions
// *************************************

template <class T>
Signal<T>::Signal (int requested_size, int fade_len, 
                   Fade_in_modes mode)
    : size(requested_size), fade_in_length(fade_len), 
      fade_in_mode(mode), recent_sample_pos(0), 
      fade_in_counter(0)
{
    mask = -1;
    while (mask & (size-1))
        mask <<= 1;

    mask = ~mask;
    x.resize(mask + 1);
    x = T();      // Fill the buffer (a valarray) with zeroes
}


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.