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

📄 resamp.cpp

📁 Intel开发的IPP库的应用实例
💻 CPP
字号:
/*
//
//                  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) 1999-2006 Intel Corporation. All Rights Reserved.
//
//     Intel(R) Integrated Performance Primitives Speech 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.
//
*/

//---------------------------------------------------------------------
//                  data resampling class
//---------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <ipps.h>
#include <ippsr.h>
#include "resamp.h"


Resample::Resample(float rollf, float alpha, bool fast, bool FPcalc) {

   this->rollf=rollf;
   this->alpha=alpha;
   this->FPcalc=FPcalc;
   this->fast=fast;
   ready=false;
   ffix=NULL;
   sfix=NULL;
   ffil=NULL;
   sfil=NULL;
   inBuf=NULL;
   outBuf=NULL;
   finBuf=NULL;
   foutBuf=NULL;

}

Resample::~Resample(void) {

   ready=false;
   if (inBuf)   ippsFree(inBuf);
   if (outBuf)  ippsFree(outBuf);
   if (finBuf)  ippsFree(finBuf);
   if (foutBuf) ippsFree(foutBuf);
   if (ffix)    ippsResamplePolyphaseFixedFree_32f(ffix);
   if (sfix)    ippsResamplePolyphaseFixedFree_16s(sfix);
   if (ffil)    ippsResamplePolyphaseFree_32f(ffil);
   if (sfil)    ippsResamplePolyphaseFree_16s(sfil);

}

void Resample::InitFix(int inRate, int outRate, int length, int bufSize) {

   fixed=true;
   IppHintAlgorithm hint=fast?ippAlgHintFast:ippAlgHintAccurate;
   IppStatus status;
   history=(((length+3)&(~3))>>1)+1;   // for time rounding
   factor=(double)outRate/(double)inRate;
   window=(float)(2*(history-1)*IPP_MIN(1.0,factor));
   this->length=(history-1)<<1;
   this->inRate=inRate;
   this->outRate=outRate;
   this->bufSize=bufSize;

   if (FPcalc) {
      status=ippsResamplePolyphaseFixedInitAlloc_32f(&ffix,inRate,outRate,length,rollf,alpha,hint);
      finBuf=ippsMalloc_32f(bufSize+history+2);
      foutBuf=ippsMalloc_32f((int)((bufSize-history)*factor+2));
      ready=(status==ippStsNoErr)&&(finBuf!=NULL)&&(foutBuf!=NULL);
   } else {
      status=ippsResamplePolyphaseFixedInitAlloc_16s(&sfix,inRate,outRate,length,rollf,alpha,hint);
      ready=(status==ippStsNoErr);
   }
   inBuf=ippsMalloc_16s(bufSize+history+2);
   outBuf=ippsMalloc_16s((int)((bufSize-history)*factor+2));

   ready=ready&&(inBuf!=NULL)&&(outBuf!=NULL);

}

void Resample::InitFix(int inRate, int outRate, float window, int bufSize) {

   fixed=true;
   IppHintAlgorithm hint=fast?ippAlgHintFast:ippAlgHintAccurate;
   IppStatus status;
   factor=(double)outRate/(double)inRate;
   history=(int)(window*0.5*IPP_MAX(1.0,1.0/factor))+1;   // for time rounding
   length=(history-1)<<1;
   this->inRate=inRate;
   this->outRate=outRate;
   this->window=window;
   this->bufSize=bufSize;

   if (FPcalc) {
      status=ippsResamplePolyphaseFixedInitAlloc_32f(&ffix,inRate,outRate,length,rollf,alpha,hint);
      finBuf=ippsMalloc_32f(bufSize+history+2);
      foutBuf=ippsMalloc_32f((int)((bufSize-history)*factor+2));
      ready=(status==ippStsNoErr)&&(finBuf!=NULL)&&(foutBuf!=NULL);
   } else {
      status=ippsResamplePolyphaseFixedInitAlloc_16s(&sfix,inRate,outRate,length,rollf,alpha,hint);
      ready=(status==ippStsNoErr);
   }
   inBuf=ippsMalloc_16s(bufSize+history+2);
   outBuf=ippsMalloc_16s((int)((bufSize-history)*factor+2));

   ready=ready&&(inBuf!=NULL)&&(outBuf!=NULL);

}

void Resample::Init(double factor, int length, int nStep, int bufSize) {

   fixed=false;
   IppHintAlgorithm hint=fast?ippAlgHintFast:ippAlgHintAccurate;
   IppStatus status;
   history=(((length+3)&(~3))>>1)+1;   // for time rounding
   window=(float)(2*(history-1)*IPP_MIN(1.0,factor));
   this->length=(history-1)<<1;
   this->factor=factor;
   this->nStep=nStep;
   this->bufSize=bufSize;

   if (FPcalc) {
      status=ippsResamplePolyphaseInitAlloc_32f(&ffil,window,nStep,rollf,alpha,hint);
      finBuf=ippsMalloc_32f(bufSize+history+2);
      foutBuf=ippsMalloc_32f((int)((bufSize-history)*factor+2));
      ready=(status==ippStsNoErr)&&(finBuf!=NULL)&&(foutBuf!=NULL);
   } else {
      status=ippsResamplePolyphaseInitAlloc_16s(&sfil,window,nStep,rollf,alpha,hint);
      ready=(status==ippStsNoErr);
   }
   inBuf=ippsMalloc_16s(bufSize+history+2);
   outBuf=ippsMalloc_16s((int)((bufSize-history)*factor+2));

   ready=ready&&(inBuf!=NULL)&&(outBuf!=NULL);

}

void Resample::Init(double factor, float window, int nStep, int bufSize) {

   fixed=false;
   IppHintAlgorithm hint=fast?ippAlgHintFast:ippAlgHintAccurate;
   IppStatus status;
   history=(int)(window*0.5*IPP_MAX(1.0,1.0/factor))+1;   // for time rounding
   length=(history-1)<<1;
   this->factor=factor;
   this->window=window;
   this->nStep=nStep;
   this->bufSize=bufSize;

   if (FPcalc) {
      status=ippsResamplePolyphaseInitAlloc_32f(&ffil,window,nStep,rollf,alpha,hint);
      finBuf=ippsMalloc_32f(bufSize+history+2);
      foutBuf=ippsMalloc_32f((int)((bufSize-history)*factor+2));
      ready=(status==ippStsNoErr)&&(finBuf!=NULL)&&(foutBuf!=NULL);
   } else {
      status=ippsResamplePolyphaseInitAlloc_16s(&sfil,window,nStep,rollf,alpha,hint);
      ready=(status==ippStsNoErr);
   }
   inBuf=ippsMalloc_16s(bufSize+history+2);
   outBuf=ippsMalloc_16s((int)((bufSize-history)*factor+2));

   ready=ready&&(inBuf!=NULL)&&(outBuf!=NULL);

}

bool Resample::Check(void) {

   if (ready) {
      printf("\n");
      printf("Resampling filter is initialized\n");
      printf("Ideal lowpass filter window %8.2f\n",window);
      printf("Roll-off frequency          %8.2f\n",rollf);
      printf("Kaiser window parameter     %8.2f\n",alpha);
      if (fixed)
      printf("Fixed filter                %8d to %d\n",inRate,outRate);
      else
      printf("Universal filter            %8d steps per 1\n",nStep);
      printf("%s calculations\n",FPcalc?"Float-point":"Integer");
      printf("Resampling factor           %8.4f\n",factor);
      printf("Filter length               %8d\n",length);
      printf("History required            %8d\n",history);
      printf("Buffer size                 %8d\n",bufSize);
   } else {
      printf("Resampling filter is not initialized\n");
   }

   return ready;

}

void Resample::Do(char* inFile, char* outFile) {

   FILE *in,*out;
   int inCount=0,outCount=0;
   int inLen,outLen,lastread;
   float scale=0.98f;
   double time;
   clock_t time_start;
   clock_t time_finish;
   double rt_time;

   in = fopen(inFile,"rb");
   if (!in) {
      fprintf(stderr,"Could not open input file \"%s\"\n",inFile);
      exit(1);
   }

   out = fopen(outFile,"wb");
   if (!out) {
      fprintf(stderr,"Could not open input file \"%s\"\n",outFile);
      exit(1);
   }
   printf("\nResampling input file %s to output file %s ...\n",inFile,outFile);

   time_start = clock();
   if (fixed) {
      if (FPcalc) {
         time=(double)history;
         lastread=history;
         ippsZero_32f(finBuf,history);
         while ((inLen=fread(inBuf+lastread,sizeof(short),bufSize-lastread,in))>0) {
            ippsConvert_16s32f(inBuf+lastread,finBuf+lastread,inLen);
            lastread+=inLen;
            ippsResamplePolyphaseFixed_32f(ffix,finBuf,lastread-history-(int)time,foutBuf,scale,&time,&outLen);
            ippsConvert_32f16s_Sfs(foutBuf,outBuf,outLen,ippRndNear,0);
            inCount+=inLen;
            outCount+=outLen;
            fwrite(outBuf,outLen,sizeof(short),out);
            ippsMove_32f(finBuf+(int)time-history,finBuf,lastread+history-(int)time);
            lastread-=(int)time-history;
            time-=(int)time-history;
         }
         ippsZero_32f(finBuf+lastread,history);
         ippsResamplePolyphaseFixed_32f(ffix,finBuf,lastread-(int)time,foutBuf,scale,&time,&outLen);
         ippsConvert_32f16s_Sfs(foutBuf,outBuf,outLen,ippRndNear,0);
         outCount+=outLen;
         fwrite(outBuf,outLen,sizeof(short),out);
      } else {
         time=(double)history;
         lastread=history;
         ippsZero_16s(inBuf,history);
         while ((inLen=fread(inBuf+lastread,sizeof(short),bufSize-lastread,in))>0) {
            lastread+=inLen;
            ippsResamplePolyphaseFixed_16s(sfix,inBuf,lastread-history-(int)time,outBuf,scale,&time,&outLen);
            inCount+=inLen;
            outCount+=outLen;
            fwrite(outBuf,outLen,sizeof(short),out);
            ippsMove_16s(inBuf+(int)time-history,inBuf,lastread+history-(int)time);
            lastread-=(int)time-history;
            time-=(int)time-history;
         }
         ippsZero_16s(inBuf+lastread,history);
         ippsResamplePolyphaseFixed_16s(sfix,inBuf,lastread-(int)time,outBuf,scale,&time,&outLen);
         outCount+=outLen;
         fwrite(outBuf,outLen,sizeof(short),out);
      }
   } else {
      if (FPcalc) {
         time=(double)history;
         lastread=history;
         ippsZero_32f(finBuf,history);
         while ((inLen=fread(inBuf+lastread,sizeof(short),bufSize-lastread,in))>0) {
            ippsConvert_16s32f(inBuf+lastread,finBuf+lastread,inLen);
            lastread+=inLen;
            ippsResamplePolyphase_32f(ffil,finBuf,lastread-history-(int)time,foutBuf,factor,scale,&time,&outLen);
            ippsConvert_32f16s_Sfs(foutBuf,outBuf,outLen,ippRndNear,0);
            inCount+=inLen;
            outCount+=outLen;
            fwrite(outBuf,outLen,sizeof(short),out);
            ippsMove_32f(finBuf+(int)time-history,finBuf,lastread+history-(int)time);
            lastread-=(int)time-history;
            time-=(int)time-history;
         }
         ippsZero_32f(finBuf+lastread,history);
         ippsResamplePolyphase_32f(ffil,finBuf,lastread-(int)time,foutBuf,factor,scale,&time,&outLen);
         ippsConvert_32f16s_Sfs(foutBuf,outBuf,outLen,ippRndNear,0);
         outCount+=outLen;
         fwrite(outBuf,outLen,sizeof(short),out);
      } else {
         time=(double)history;
         lastread=history;
         ippsZero_16s(inBuf,history);
         while ((inLen=fread(inBuf+lastread,sizeof(short),bufSize-lastread,in))>0) {
            lastread+=inLen;
            ippsResamplePolyphase_16s(sfil,inBuf,lastread-history-(int)time,outBuf,factor,scale,&time,&outLen);
            inCount+=inLen;
            outCount+=outLen;
            fwrite(outBuf,outLen,sizeof(short),out);
            ippsMove_16s(inBuf+(int)time-history,inBuf,lastread+history-(int)time);
            lastread-=(int)time-history;
            time-=(int)time-history;
         }
         ippsZero_16s(inBuf+lastread,history);
         ippsResamplePolyphase_16s(sfil,inBuf,lastread-(int)time,outBuf,factor,scale,&time,&outLen);
         outCount+=outLen;
         fwrite(outBuf,outLen,sizeof(short),out);
      }
   }
   time_finish = clock();

   printf("%d input samples converted to %d output samples\n",inCount,outCount);
   rt_time = (double)(time_finish-time_start)/1000.0;
   printf ("Execution time %10.4f seconds\n",rt_time);

}

⌨️ 快捷键说明

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