/* input: [[1,1,1],[1,1,1],[1,1,1]] output: [[1,1,1],[1,1,1],[1,1,1]] */ void test2(){ unsigned int M = 3; unsigned int N = 3; FourierDomain2D* input = (FourierDomain2D*)malloc(sizeof(FourierDomain2D*)*M*N); FFT_Type* output = (FFT_Type*)malloc(sizeof(FFT_Type*)*M*N); assert(input); assert(output); for(int i = 0; i < M * N; i++){ input[i] = i; } FFT2(M, N, input, output); IFFT2(M, N, output, 0, input); for(int i = 0; i < M*N; i++){ assert(isEqual(input[i],i)); // print_complex(input[i]); } fprintf(stderr, "the second test passes\n"); free(input); free(output); }
/* input: [[1,2,3],[4,5,6],[7,8,9,],[10,11,12]] output:[[1,2,3],[4,5,6],[7,8,9,],[10,11,12]] */ void test3(){ unsigned int M = 20; unsigned int N = 20; FourierDomain2D* input = (FourierDomain2D*)malloc(sizeof(FourierDomain2D*)*M*N); FFT_Type* output = (FFT_Type*)malloc(sizeof(FFT_Type*)*M*N); assert(input); assert(output); for(int i = 0; i < M * N; i++){ input[i] = (float)i / 10; } FFT2(M, N, input, output); IFFT2(M, N, output, 0, input); for(int i = 0; i < M*N; i++){ assert(isEqual(input[i],(float)i/10)); // print_complex(input[i]); } fprintf(stderr, "the third test passes\n"); free(input); free(output); }
//--- summerize --- void FFT2vector(const std::vector< Tyfft_ >& rawdata, size_t freq_point_num, FFTwindow window, double rate_){ rate = rate_; size_t size2Power = getPower2Num(freq_point_num); size_t time_point_num = size2Power/2; size_t time_slice_num = (size_t)floor(rawdata.size()/time_point_num); // number of time slice fft_timing.resize(time_slice_num); Power.resize(time_slice_num); double* src_x = new double[size2Power]; double* amp_x = new double[size2Power]; for(size_t t=0; t<rawdata.size(); t+=time_point_num){ // time slice double t_avg=0.0; for(size_t i=0; i<size2Power; i++){ src_x[i] = getVal(rawdata[t+i]); t_avg += getTime(i); } // fft FFT2(src_x, size2Power, window); FFT2Power(amp_x, src_x, size2Power); Power[t]=(Pointer2Stdv(amp_x, size2Power/2); //time fft_timing[t] = t_avg/size2Power; } // frequency frequency.resize(size2Power/2); for(size_t i=0; i<size2Power/2; i++) frequency[i] = rate*i/size2Power; delete[] src_x; delete[] amp_x; }
/* input: [[1,1,1],[1,1,1],[1,1,1]] output: [[1,1,1],[1,1,1],[1,1,1]] */ void test1(){ unsigned int M = 3; unsigned int N = 3; FourierDomain2D* input = (FourierDomain2D*)malloc(sizeof(FourierDomain2D*)*M*N); FFT_Type* output = (FFT_Type*)malloc(sizeof(FFT_Type*)*M*N); assert(input); assert(output); for(int i = 0; i < M; i++){ for(int j = 0; j < N; j++){ input[i*N+j] = 1.0 + 0.0*I; } } FFT2(M, N, input, output); IFFT2(M, N, output, 0, input); for(int i = 0; i < M*N; i++){ assert(isEqual(input[i],1.0+0*I)); // print_complex(input[i]); } fprintf(stderr, "the first test passes\n"); free(input); free(output); }
/** @function ******************************************************************************** <PRE> 函数名: FrameMultiHamming() 功能: 音框化并乘汉明窗,然后快速傅立叶变换:对差分后的语音数据分帧,并排除静默语音以256个 采样点为一个音框单位(帧),以128为音框之间的重迭单位然后乘以汉明窗,最后每帧做FFT2。 用法: 参数: [IN] pDiffVoice: 增强(差分)后的语音数据 [OUT] pVoiceFrame: 指向可用的训练帧 [IN] train_frame_num: 训练帧帧数 返回: TRUE: 分帧成功 FALSE: 分帧失败 调用: 主调函数: voiceToMFCC() 备注: pVoiceFrame指向的训练帧存储空间在此函数中申请 </PRE> *******************************************************************************/ static BOOL FrameMultiHamming(double * pDiffVoice, VOICEFRAME * pVoiceFrame, int train_frame_num) { int i; // 分帧中间参数 int iFrame = 0; // 帧序号 int iPointNum = FRAME_LEN -1; //--------------------------------------------------------------------------- // 边分帧边乘汉明窗,FFT2变换 // for (iFrame = 0 ; iFrame < train_frame_num; ++iFrame) { pVoiceFrame[iFrame].pCmpx = (COMPX *)malloc(sizeof(COMPX)*FRAME_LEN); if (!pVoiceFrame[iFrame].pCmpx) { printf("malloc Frame error"); while (iFrame > 0) { free(pVoiceFrame[--iFrame].pCmpx); } return FALSE; } for (i = 0; i < FRAME_LEN; ++i) { // 乘汉明窗 pVoiceFrame[iFrame].pCmpx[i].real = pDiffVoice[128*iFrame+i] * (0.54-0.46*cos(_2PI*i/iPointNum)); pVoiceFrame[iFrame].pCmpx[i].imag = 0; } FFT2(pVoiceFrame[iFrame].pCmpx, FRAME_LEN); // 基二快速傅立叶变换(时域) } return TRUE; }
/* This test test whether the fft works properly input: [[1,2,3],[4,5,6],[7,8,9]] output: [[ 45.0+0.j , -4.5+2.59807621j, -4.5-2.59807621j], [-13.5+7.79422863j, 0.0+0.j , 0.0+0.j ], [-13.5-7.79422863j, 0.0+0.j , 0.0+0.j ] ]) */ void test2(){ unsigned int M = 3; unsigned int N = 3; FFT_Type* input = (FFT_Type*)malloc(sizeof(FFT_Type*)*M*N); FFT_Type* output = (FFT_Type*)malloc(sizeof(FFT_Type*)*M*N); assert(input); assert(output); for(int i = 0; i < M*N; i++){ input[i] = i + 1; } FFT2(M, N, input, output); assert(isEqual(output[0],45.0 + 0*I)); assert(isEqual(output[1],-4.5+2.5980762*I)); assert(isEqual(output[2],-4.5-2.5980762*I)); assert(isEqual(output[3],-13.5+7.7942286*I)); assert(isEqual(output[4],0+0*I)); assert(isEqual(output[5],0+0*I)); assert(isEqual(output[6],-13.5-7.7942286*I)); assert(isEqual(output[7],0+0*I)); assert(isEqual(output[8],0+0*I)); fprintf(stderr, "the second test passes\n"); free(input); free(output); }
/* This test test whether the fft works properly input: [[1,1,1],[1,1,1],[1,1,1]] output: [[9,0,0],[0,0,0],[0,0,0]] */ void test1(){ unsigned int M = 3; unsigned int N = 3; FFT_Type* input = (FFT_Type*)malloc(sizeof(FFT_Type*)*M*N); FFT_Type* output = (FFT_Type*)malloc(sizeof(FFT_Type*)*M*N); assert(input); assert(output); for(int i = 0; i < M; i++){ for(int j = 0; j < N; j++){ input[i*N+j] = 1.0 + 0.0*I; } } FFT2(M, N, input, output); assert(isEqual(output[0],9.0+0.0*I)); for(int i = 1; i < M*N; i++){ assert(isEqual(output[i], 0.0+0*I)); } fprintf(stderr, "the first test passes\n"); free(input); free(output); }
/* input: [[1,2,3],[4,5,6],[7,8,9]] output: [[5,6],[8,9]] */ void test1(){ unsigned int M = 3; unsigned int N = 3; FourierDomain2D* input = (FourierDomain2D*)malloc(sizeof(FourierDomain2D*)*M*N); FFT_Type* output = (FFT_Type*)malloc(sizeof(FFT_Type*)*M*N); assert(input); assert(output); for(int i = 0; i < M * N; i++){ input[i] = i + 1; output[i] = 0; } FFT2(M, N, input, output); _IFFT2_Shifted(M, N, output, M-1, N-1, 0, input); FFT_Type correct[4] = {5,6,8,9}; for(int i = 0; i < 4; i++){ assert(isEqual(input[i],correct[i])); } fprintf(stderr, "the first test of shifted ifft2 passes\n"); free(input); free(output); }
/* input: [[1,2,3],[4,5,6],[7,8,9,],[10,11,12]] output:[[1,2,3],[4,5,6],[7,8,9,],[10,11,12]] */ void test3(){ unsigned int M = 1; unsigned int N = 2; FFT_Type* input = (FFT_Type*)malloc(sizeof(FFT_Type*)*M*N); FFT_Type* output = (FFT_Type*)malloc(sizeof(FFT_Type*)*M*N); assert(input); assert(output); for(int i = 0; i < M * N; i++){ input[i] = i; } FFT2(M, N, input, output); assert(isEqual(output[0], 1)); assert(isEqual(output[1], -1)); fprintf(stderr, "the third test passes\n"); free(input); free(output); }
int main(int argc, char **argv) { int dim = 0; int m = 0; int n = 0; int i = 0; int j = 0; FILE* datfile = NULL; printf("Dimension(1 or 2):"); while (1 != scanf("%d", &dim) && dim == EOF) { ; } if (1 != dim && 2 != dim) { printf("invalid input! Dimension should be 1 or 2\n"); return -1; } if (NULL == (datfile = fopen(dim == 1 ? "fft.dat" : "fft2.dat", "r"))) { fprintf(stderr, "fopen:%s, Line %d in %s\n", strerror(errno),\ __LINE__, __FILE__); return -1; } if (1 == dim) { fscanf(datfile, "%d", &n); printf("vector number:%d\n", n); COMPLEX *Ak = (COMPLEX*)malloc(sizeof(COMPLEX) * n); COMPLEX *Xn = NULL; for (i = 0; i < n; i++) { fscanf(datfile, "%f,%f", &Ak[i].real, &Ak[i].img); } printf("Input vector:\n"); for (i = 0; i < n; i++) { printf("%.2f%+.2fi\n", Ak[i].real, Ak[i].img); } Xn = FFT(Ak, n); printf("output vector:\n"); for (i = 0; i < n; i++) { printf("%.2f%+.2fi\n", Xn[i].real, Xn[i].img); } free(Ak); free(Xn); } else { fscanf(datfile, "%d %d", &m, &n); printf("row length: %d, column length: %d\n", m, n); COMPLEX** Ak = NULL; if (NULL == (Ak = (COMPLEX**)malloc(m * sizeof(COMPLEX**)))) { fprintf(stderr, "malloc:%s, Line %d in %s\n", strerror(errno),\ __LINE__, __FILE__); exit(-1); } COMPLEX** Xn = NULL; printf("Input matrix:\n"); for (i = 0; i < m; i++) { if (NULL == (Ak[i] = (COMPLEX*)malloc(sizeof(COMPLEX) * n))) { fprintf(stderr, "malloc:%s, Line %d in %s\n", strerror(errno),\ __LINE__, __FILE__); exit(-1); } for (j = 0; j < n; j++) { fscanf(datfile, "%f,%f", &Ak[i][j].real, &Ak[i][j].img); printf("%.2f%+.2fi ", Ak[i][j].real, Ak[i][j].img); } printf("\n"); } Xn = FFT2(Ak, m, n); printf("output matrix:\n"); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { printf("%.2f%+.2fi ", Xn[i][j].real, Xn[i][j].img); } printf("\n"); } free(Ak); free(Xn); } fclose(datfile); return 0; }
BOOL CGaborFilter::GaborTransform(int size) // type will decide the transform result, { if(size != m_nImgWidth*m_nImgHeight) return FALSE; int ScaleIdex; int GaborSize = m_nGaborWinWidth * m_nGaborWinHeight; int ImageSize = m_nImgWidth*m_nImgHeight; int i,j; float *pRealpart = NULL ; float *pConjpart = NULL ; float **pImageReal = NULL ; float **pImageConj = NULL ; float **pGaborReal = NULL ; float **pGaborConj = NULL ; int ExtendedWidth,ExtendedHeight; pGaborReal = New2DPointer<float>(m_nGaborWinHeight,m_nGaborWinWidth); pGaborConj = New2DPointer<float>(m_nGaborWinHeight,m_nGaborWinWidth); ScaleIdex = GetMatchScale(); ////////////////////////////// //ScaleIdex = 4; ////////////////////////////// int dsize = sizeof(float); int BufferSize = m_nGaborWinWidth * dsize; float *pReal = NULL, *pConj = NULL; pReal = m_ppGaborKernelReal[ScaleIdex]; pConj = m_ppGaborKernelConj[ScaleIdex]; for(i = 0; i < m_nGaborWinHeight; i++) { //memcpy(pGaborReal[0],m_ppGaborKernelReal[ScaleIdex],GaborSize*dsize); //memcpy(pGaborConj[0],m_ppGaborKernelConj[ScaleIdex],GaborSize*dsize); memcpy(pGaborReal[i],pReal, BufferSize); memcpy(pGaborConj[i],pConj, BufferSize); pReal += m_nGaborWinWidth; pConj += m_nGaborWinWidth; } pReal = NULL; pConj = NULL; pImageReal = New2DPointer<float>(m_nImgHeight,m_nImgWidth); pImageConj = New2DPointer<float>(m_nImgHeight,m_nImgWidth); float *pImage = NULL; pImage = m_pOriImageData; BufferSize = m_nImgWidth * dsize ; for(i= 0 ; i < m_nImgHeight; i++) { memcpy(pImageReal[i],pImage,BufferSize); memset(pImageConj[i],0,BufferSize); pImage += m_nImgWidth; } pImage = NULL; ExtendedWidth = m_nImgWidth + m_nGaborWinWidth - 1 ; ExtendedHeight = m_nImgHeight + m_nGaborWinHeight - 1 ; long Wid_M = Get2MofSerialSize(ExtendedWidth); long Hei_M = Get2MofSerialSize(ExtendedHeight); int FFTWidth = (int)pow((double)2,(int)Wid_M); int FFTHeight = (int)pow((double)2,(int)Hei_M); float **pGaborRealExtended = New2DPointer<float>(FFTHeight,FFTWidth); float **pGaborConjExtended = New2DPointer<float>(FFTHeight,FFTWidth); MatrixExtend(m_nGaborWinWidth,m_nGaborWinHeight,FFTWidth,FFTHeight,pGaborReal,pGaborRealExtended); MatrixExtend(m_nGaborWinWidth,m_nGaborWinHeight,FFTWidth,FFTHeight,pGaborConj,pGaborConjExtended); Delete2DPointer<float>(pGaborReal,m_nGaborWinHeight); Delete2DPointer<float>(pGaborConj,m_nGaborWinHeight); float **pImageRealExtended = New2DPointer<float>(FFTHeight,FFTWidth); float **pImageConjExtended = New2DPointer<float>(FFTHeight,FFTWidth); MatrixExtend(m_nImgWidth,m_nImgHeight,FFTWidth,FFTHeight,pImageReal,pImageRealExtended); MatrixExtend(m_nImgWidth,m_nImgHeight,FFTWidth,FFTHeight,pImageConj,pImageConjExtended); Delete2DPointer<float>(pImageReal,m_nImgHeight); Delete2DPointer<float>(pImageConj,m_nImgHeight); float **pImageRealTrans = New2DPointer<float>(FFTHeight,FFTWidth); float **pImageConjTrans = New2DPointer<float>(FFTHeight,FFTWidth); FFT2(1,Wid_M,Hei_M,pGaborRealExtended,pGaborConjExtended); FFT2(1,Wid_M,Hei_M,pImageRealExtended,pImageConjExtended); ComplexMatirxProduction(FFTWidth,FFTHeight,pImageRealExtended,pImageConjExtended, pGaborRealExtended,pGaborConjExtended,pImageRealTrans,pImageConjTrans); FFT2(-1,Wid_M,Hei_M,pImageRealTrans,pImageConjTrans); Delete2DPointer<float>(pGaborRealExtended,FFTHeight); Delete2DPointer<float>(pGaborConjExtended,FFTHeight); Delete2DPointer<float>(pImageRealExtended,FFTHeight); Delete2DPointer<float>(pImageConjExtended,FFTHeight); int widthup,widthdown,heightdown,heightup; SetWinodwsSize(m_nGaborWinWidth,m_nGaborWinHeight,&widthup,&widthdown,&heightdown,&heightup); if(m_pReal != NULL) { delete m_pReal; m_pReal = NULL; } if(m_pImaginary != NULL) { delete m_pImaginary; m_pImaginary = NULL; } if(m_pMagnitude != NULL) { delete m_pMagnitude; m_pMagnitude = NULL; } m_pReal = new float[ImageSize]; m_pImaginary = new float[ImageSize]; m_pMagnitude = new float[ImageSize]; int k=0; for(i = abs(heightdown); i < m_nImgHeight+m_nGaborWinHeight-1-heightup ;i ++) { for(j = abs(widthdown) ; j < m_nImgWidth+m_nGaborWinWidth-1-widthup ;j++) { m_pMagnitude[k] = sqrt(pImageRealTrans[i][j]*pImageRealTrans[i][j] + pImageConjTrans[i][j]*pImageConjTrans[i][j]); m_pReal[k] = pImageRealTrans[i][j]; m_pImaginary[k] = pImageConjTrans[i][j]; k++; } } Delete2DPointer<float>(pImageRealTrans,FFTHeight); Delete2DPointer<float>(pImageConjTrans,FFTHeight); return TRUE; }