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

Policy-Driven Design & the Intel IPP Library


January 04:

Listing 3: Signal<> header file.


#include "IPPPolicy.h"  // include the policy class that wraps Intel's IPP library
template <typename T>
class Signal {
public:
    typedef IntelIppPolicy<T> IPP;
    Signal(int N=0) : m_nSamples(N), m_pSamples(NULL) 
    { 
        if (N)
            m_pSamples = IPP::malloc(N); 
    }
    ~Signal() 
    { 
        if (m_pSamples)
            IPP::free(m_pSamples); 
    }
    operator T *() {
        return m_pSamples;
    }
    int getNumSamples() {
        return m_nSamples;
    }
    // adjust the length of the array
    void resize(int N)
    {
        if (m_pSamples)
            IPP::free(m_pSamples);
        m_nSamples = N;
        m_pSamples = IPP::malloc(N);
    }
    // Returns minimum value pIndx is where this point is located.
    T min(int *pIndx) 
    {
        T minVal;
        IppStatus sts = IPP::minIndx(m_pSamples, m_nSamples, &minVal, pIndx);
       if (ippStsOk != sts)
            throw std::runtime_error((char*)ippGetStatusString(sts));
        return minVal;
    }
    // Returns minimum value pIndx is where this point is located.
    T max(int *pIndx) 
    {
        T maxVal;
        IppStatus sts = IPP::maxIndx(m_pSamples, m_nSamples, &maxVal, pIndx);
        if (ippStsOk != sts)
            throw std::runtime_error((char*)ippGetStatusString(sts));
        return maxVal;
    }
    // Computes and returns the magnitude of the FFT of the signal. The FFT 
    // of a real-valued signal is a symmetric complex signal: hence the 
    // length of the return signal will be half (plus 1) of the input signal.
    // There isn't enough error checking performed in this method, for 
    // example one should really verify that the length of the input vector
    // is a power-of-2 length.
    // Finally, IPP library does provide a higher-level API function that
    // computes power spectrum of a signal--the equivalent to this method.
    void fftMagnitude(Signal<T> *pMagFFT)
    {
        IPP::cmplx_type *pFFT = NULL;
        // order of the FFT defined to be log2(length of input)
        int orderFFT = (int)(std::log((double)m_nSamples) / std::log(2.0));
        // this is somewhat inefficient, in a real implementation we'd likely
        // initialize this just once and then cache it away.
        IPP::fft_spec_type *pFFTSpec = NULL;
        IppStatus sts = IPP::allocFFTSpec(&pFFTSpec, orderFFT, IPP_FFT_DIV_INV_BY_N, ippAlgHintNone);
        if (ippStsOk != sts)
            throw std::runtime_error((char*)ippGetStatusString(sts));
        // length of the return signal
        int lenFFT = (1<<(orderFFT-1)) + 1;
        if (NULL == (pFFT = IPP::cmplxMalloc(lenFFT)))
            throw std::bad_alloc("Failed to malloc complex array!");
        // ready to compute the FFT now
        if (ippStsOk != (sts = IPP::fwdFFT(m_pSamples, (IPP::elem_type *)pFFT, pFFTSpec)))
            throw std::runtime_error((char*)ippGetStatusString(sts));

        // magnitude of the FFT is the complex modulus
        pMagFFT->resize(lenFFT);
        if (ippStsOk != (sts = IPP::cmplxModulus(pFFT, pMagFFT->m_pSamples, lenFFT)))
            throw std::runtime_error((char*)ippGetStatusString(sts));
        // clean up
        IPP::free(pFFT);
        IPP::freeFFTSpec(pFFTSpec);
    }
private:
    // the underlying array
    T *m_pSamples;
    // how many data points in the m_pSamples array
    int m_nSamples;
};


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.