⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vadper.c

📁 Intel开发的IPP库的应用实例
💻 C
字号:
/*
//
//                  INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in accordance with the terms of that agreement.
//        Copyright(c) 2002-2006 Intel Corporation. All Rights Reserved.
//
//  Intel(R) Integrated Performance Primitives Audio Processing
//  Sample for Windows*
//
//  By downloading and installing this sample, you hereby agree that the
//  accompanying Materials are being provided to you under the terms and
//  conditions of the End User License Agreement for the Intel(R) Integrated
//  Performance Primitives product previously accepted by you. Please refer
//  to the file ippEULA.rtf located in the root directory of your Intel(R) IPP
//  product installation for more information.
//
//  Description:
//  This source file contains the implementation of periodicity-related computations for
//  Voice Activity Detector based upon the Intel Integrated Performance Primitives.
//
*/

/* headers */
#include <ipps.h>
#include <ippsr.h>
#include "VADcommon.h"
#include "VADfilt.h"

/* function prototypes */
void PER_BandPassAndDownSample(const Ipp32f* pSrc, int srcLen, PERStateStruct32f* pPerState);
void PER_ComputePeriodicity(const Ipp32f* pInFrame, int len, PERStateStruct32f* pPerState);
void PER_SmoothPeriodicity(PERStateStruct32f* pPerState, int frameNum);

/********************************************************************************
// Name:             PER_GetStateSizeBytes
// Description:      Calculate and return the size in bytes required by the
//                   periodicity internal state structure based on the input parameters
// Input Arguments:
//                   cWinSamps      - size of an input data frame in samples
// Output Arguments:
//                   pNumStateBytes - pointer to output variable that contains the
//                                    calculated state size
// Returns:          None
// Notes:
********************************************************************************/

void PER_GetStateSizeBytes(int cWinSamps, int* pNumStateBytes)
{
    int cTmpBytes;
    int cTmpDownSampledWinSamps;

    /* initialize with the size of the static components of the PERStateStruct */
    *pNumStateBytes = sizeof(PERStateStruct32f);

    /* add the size of pState->pBPFrame - band-pass filtered values */
    cTmpBytes = cWinSamps *sizeof(Ipp32f);
    *pNumStateBytes += cTmpBytes;

    /* add any alignment bytes for word boundary alignment */
    cTmpBytes = cTmpBytes % sizeof(Ipp32f); /* align of word boundary */
    *pNumStateBytes += cTmpBytes;

    /* add the size of pState->pDSFrame - downsampled frame */
    cTmpDownSampledWinSamps = (cWinSamps + PER_DOWNSAMPLE_FACTOR - 1 - PER_DOWNSAMPLE_PHASE) / PER_DOWNSAMPLE_FACTOR;
    cTmpBytes = cTmpDownSampledWinSamps * sizeof(Ipp32f);
    *pNumStateBytes += cTmpBytes;

    /* add any alignment bytes for word boundary alignment */
    cTmpBytes = cTmpBytes % sizeof(Ipp32f); /* align of word boundary */
    *pNumStateBytes += cTmpBytes;

    //cTmpBytes = TAPSLEN * sizeof(Ipp32s);
    cTmpBytes = (NUMBIQUAD*2) * sizeof(Ipp32f);
    *pNumStateBytes += cTmpBytes;

    ippsIIRGetStateSize_BiQuad_32f(NUMBIQUAD,&cTmpBytes);
    *pNumStateBytes += cTmpBytes;
}

/********************************************************************************
// Name:            PER_Init
// Description:     Initialize the Periodicity state structure with initial values. Also,
//                  assign the externally allocated memory to the internal variables
//                  of the PERStateStruct.
//
// Input Arguments:
//                  frameShiftMsec - frame shift for overlapping frames in msec
//                  cWinSamps      - size of an input data frame in samples
//                  sampFreqHz     - sampling frequency of the input data in Hz
// Input/Output Arguments:
//                  pPerState      - pointer to an PERState structure to be initialized
// Returns:         None
********************************************************************************/

void PER_Init(PERStateStruct32f* pPerState, int frameShiftMsec, int cWinSamps, int sampFreqHz)
{
    int   halfFrameShiftMsec;
    int   tmpBytes;
    char* pMemory;
    int   i;

    /* half the frame shift used to round of the computations of variables */
    halfFrameShiftMsec = frameShiftMsec/2;

    /* start memory pointer just after the memory for the PERStateStruct */
    pMemory = (char*)(&pPerState->pMemoryBlock + 1);

    /* Initialize periodicity values of state */
    pPerState->cInitPerEstFrames = (PER_INIT_PER_ESTIMATE_MSEC + halfFrameShiftMsec) / frameShiftMsec;
    pPerState->minPeriodSamps = sampFreqHz / (PER_DOWNSAMPLE_FACTOR * PER_MAX_PITCH_FREQ_HZ);
    pPerState->maxPeriodSamps = sampFreqHz / (PER_DOWNSAMPLE_FACTOR * PER_MIN_PITCH_FREQ_HZ);

    pPerState->smoothPeriodicity = 0;

    /* assign memory to the buffer that holds the bandpass filtered values */
    pPerState->pBPFrame = (Ipp32f *) pMemory;
    tmpBytes = cWinSamps * sizeof(Ipp32f);

    /* align on word boundary */
    tmpBytes += (tmpBytes % sizeof(Ipp32f));
    pMemory += tmpBytes;

    /* assign memory to the buffer that holds the downsampled values */
    pPerState->cDSFrameSamps = (cWinSamps + PER_DOWNSAMPLE_FACTOR - 1 - PER_DOWNSAMPLE_PHASE) / PER_DOWNSAMPLE_FACTOR; 
    pPerState->pDSFrame = (Ipp32f *) pMemory;
    tmpBytes = pPerState->cDSFrameSamps * sizeof(Ipp32f);

    /* align on word boundary */
    tmpBytes += (tmpBytes % sizeof(Ipp32f));
    pMemory += tmpBytes;

    /* assign memory to the delayLine for the IIR filter */
    pPerState->pDelayLine = (Ipp32f *) pMemory;
    tmpBytes = (NUMBIQUAD*2) * sizeof(Ipp32f);
    pMemory += tmpBytes;

    pPerState->pBuffer=(Ipp8u*) pMemory;

    for (i=0; i<(NUMBIQUAD*2); i++)
    {
        pPerState->pDelayLine[i] = 0.0f;
    }
}

/********************************************************************************
// Name:            PER_UpdatePerState
// Description:     Update the periodicity state based on the current input frame.
//
// Input Arguments:
//                  pFrame    - input frame
//                  frameNum  - current frame number used in initializing the noise floor
//                  len       - number of samples in the input frame
// Input/Output Arguments:
//                  pPerState - pointer to an periodicity state structure
//
// Returns:         None
********************************************************************************/
void PER_UpdatePerState(const Ipp32f* pFrame, int frameNum, int len, PERStateStruct32f* pPerState)
{
    /* bandpass filter the input data */
    PER_BandPassAndDownSample(pFrame, len, pPerState);

    /* compute periodicity on the bandpass filtered data */
    PER_ComputePeriodicity(pPerState->pDSFrame, pPerState->cDSFrameSamps, pPerState);

    /* smooth the periodicity using history */
    PER_SmoothPeriodicity(pPerState, frameNum);

}

/********************************************************************************
// Name:            PER_BandPassAndDownSample
// Description:     Band-pass filter (70-1000Hz) the input data using Intel(R) IPP data
//
// Input Arguments:
//                  pSrc      - input data
//                  srcLen    - number of samples in the input and output buffers
// Output Arguments:
//                  pPerState - output band pass filtered data
// Returns:         None
********************************************************************************/

void PER_BandPassAndDownSample(const Ipp32f* pSrc, int srcLen, PERStateStruct32f* pPerState)
{
    IppsIIRState_32f* pState;
    int    phase,DstLen;

    ippsIIRInit_BiQuad_32f(&pState,pTaps,NUMBIQUAD,pPerState->pDelayLine, pPerState->pBuffer);
    ippsIIR_32f(pSrc, pPerState->pBPFrame, srcLen, pState);
    ippsIIRFree_32f(pState);

    /* downsample */
    phase = PER_DOWNSAMPLE_PHASE;
//    ippsDownSample_16s(pPerState->pBPFrame, srcLen, &phase, pPerState->pDSFrame, PER_DOWNSAMPLE_FACTOR);
    ippsSampleDown_32f(pPerState->pBPFrame,srcLen,pPerState->pDSFrame, &DstLen,PER_DOWNSAMPLE_FACTOR,&phase);
}


/********************************************************************************
// Name:            PER_ComputePeriodicity
// Description:     Compute periodicity using Intel(R) IPP function
//
// Input Arguments:
//                  pInFrame  - input data
//                  len       - number of samples in the input buffer
// Input/Output Arguments:
//                  pPerState - pointer to an periodicity state structure
//
// Returns:         None
********************************************************************************/

void PER_ComputePeriodicity(const Ipp32f* pInFrame, int len, PERStateStruct32f* pPerState)
{
   ippsPeriodicityLSPE_32f(pInFrame,len,&(pPerState->periodicity),&(pPerState->period),pPerState->maxPeriodSamps,pPerState->minPeriodSamps);
}


/********************************************************************************
// Name:            PER_SmoothPeriodicity
// Description:     Smooth the computed periodicity by summing over periodicity history.
//                  The average noise periodicity value is removed before smoothing.
//
// Input Arguments:
//                  frameNum  - current frame number used in initializing the noise floor
// Input/Output Arguments:
//                  pPerState - pointer to an periodicity state structure
//
// Returns:         None
********************************************************************************/

void PER_SmoothPeriodicity(PERStateStruct32f* pPerState, int frameNum)
{
    if (frameNum <= pPerState->cInitPerEstFrames)
    {
        /* Initialize periodicity */
        pPerState->smoothPeriodicity = pPerState->smoothPeriodicity * (frameNum-1) + pPerState->periodicity;
        pPerState->smoothPeriodicity /= frameNum;
    }
    else
    {
        WEIGHTED_AVG(pPerState->periodicity, pPerState->smoothPeriodicity, PER_ADAPT_ALPHA, pPerState->smoothPeriodicity)
    }
}

/* EOF */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -