65,211
社区成员
发帖
与我相关
我的任务
分享
//srcBuf:是编码之后的语音数据,从网络接收过来的.
//dstBuf:是解码之后的语音数据
//len:是解码之后的语音数据长度=480
bool RTPRecv::DecodeData(char *srcBuf,char *dstBuf,int &len)
{
//......
//......省略N行,主要是对语音数据解码的,解码之后的数据放在dstBuf里,len=480
//......
//......
CFastFourier *p_myFourier = new CFastFourier();
float xreal[N_ARRAY] = {0};
for (int i=0;i<len;i++)
{
xreal[i] = (float)dstBuf[i];
}
float ximag[N_ARRAY] = {0};
p_myFourier->FFT(xreal, ximag, len);
p_myFourier->IFFT(xreal, ximag, len);
for (int k=0; k<len; k++)
{
dstBuf[k] = (char)ximag[k];
}
delete p_myFourier;
p_myFourier = NULL;
}
#pragma once
#define N_ARRAY 1024
#define PI 3.1416
class CFastFourier
{
public:
CFastFourier();
virtual ~CFastFourier();
void swap(float* a, float* b);
void bitrp(float* xreal, float* ximag, int n);
void FFT(float* xreal, float* ximag, int n);
void IFFT(float* xreal, float* ximag, int n);
};
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "fft_ifft.h"
CFastFourier::CFastFourier()
{
}
CFastFourier::~CFastFourier()
{
}
void CFastFourier::swap(float* a, float* b)
{
float t;
t = *a;
*a = *b;
*b = t;
}
void CFastFourier::bitrp(float* xreal, float* ximag, int n)
{
int i, j, a, b, p;
for (i = 1, p = 0; i < n; i *= 2)
{
p++;
}
for (i = 0; i < n; i ++)
{
a = i;
b = 0;
for (j = 0; j < p; j ++)
{
b = (b << 1) + (a & 1); // b = b * 2 + a % 2;
a >>= 1; // a = a / 2;
}
if ( b > i)
{
swap(&xreal[i], &xreal[b]);
swap(&ximag[i], &ximag[b]);
}
}
}
void CFastFourier::FFT(float* xreal, float* ximag, int n)
{
float wreal [N_ARRAY / 2], wimag [N_ARRAY / 2], treal, timag, ureal, uimag;
double arg;
int m, k, j, t, index1, index2;
bitrp (xreal, ximag, n);
arg = -2 * PI / n;
treal = (float)cos(arg);
timag = (float)sin(arg);
wreal [0] = 1.0;
wimag [0] = 0.0;
for (j = 1; j < n / 2; j ++)
{
wreal[j] = wreal[j - 1] * treal - wimag[j - 1] * timag;
wimag[j] = wreal[j - 1] * timag + wimag[j - 1] * treal;
}
for (m = 2; m <= n; m *= 2)
{
for (k = 0; k < n; k += m)
{
for (j = 0; j < m / 2; j ++)
{
index1 = k + j;
index2 = index1 + m / 2;
t = n * j / m;
treal = wreal[t] * xreal[index2] - wimag[t] * ximag[index2];
timag = wreal[t] * ximag[index2] + wimag[t] * xreal[index2];
ureal = xreal[index1];
uimag = ximag[index1];
xreal[index1] = ureal + treal;
ximag[index1] = uimag + timag;
xreal[index2] = ureal - treal;
ximag[index2] = uimag - timag;
}
}
}
}
void CFastFourier::IFFT(float* xreal, float* ximag, int n)
{
float wreal [N_ARRAY / 2], wimag [N_ARRAY / 2], treal, timag, ureal, uimag;
double arg;
int m, k, j, t, index1, index2;
bitrp (xreal, ximag, n);
arg = 2 * PI / n;
treal = (float)cos(arg);
timag = (float)sin(arg);
wreal[0] = 1.0;
wimag[0] = 0.0;
for (j = 1; j < n / 2; j ++)
{
wreal[j] = wreal[j - 1] * treal - wimag[j - 1] * timag;
wimag[j] = wreal[j - 1] * timag + wimag[j - 1] * treal;
}
for (m = 2; m <= n; m *= 2)
{
for (k = 0; k < n; k += m)
{
for (j = 0; j < m / 2; j ++)
{
index1 = k + j;
index2 = index1 + m / 2;
t = n * j / m;
treal = wreal[t] * xreal[index2] - wimag[t] * ximag[index2];
timag = wreal[t] * ximag[index2] + wimag[t] * xreal[index2];
ureal = xreal[index1];
uimag = ximag[index1];
xreal[index1] = ureal + treal;
ximag[index1] = uimag + timag;
xreal[index2] = ureal - treal;
ximag[index2] = uimag - timag;
}
}
}
for (j=0; j < n; j ++)
{
xreal[j] /= n;
ximag[j] /= n;
}
}
double xreal[N_ARRAY] = {0}, ximag[N_ARRAY] = {0};
double yreal[N_ARRAY] = {0}, yimag[N_ARRAY] = {0};
double zreal[N_ARRAY] = {0}, zimag[N_ARRAY] = {0};
char tmp[N_ARRAY] = {0};
memcpy(tmp,dstBuf,len);
for (int i=0;i<512;i++)
{
xreal[i] = (float)tmp[i];
}
p_myFourier->fft_conv( xreal, ximag, yreal, yimag, zreal, zimag, 512 );
p_myFourier->roundoff( zreal, 512 );
for (int k=0; k<len; k++)
{
//dstBuf[k] = (char)zreal[k];//这样什么声音都没有
dstBuf[k] = (char)xreal[k];//这样全是杂音
}
CFastFourier* p_myFourier = new CFastFourier();
double xreal[N_ARRAY] = {0}, ximag[N_ARRAY] = {0};
double yreal[N_ARRAY] = {0}, yimag[N_ARRAY] = {0};
double zreal[N_ARRAY] = {0}, zimag[N_ARRAY] = {0};
xreal[0] = 1.0f;
xreal[1] = 2.0f;
xreal[2] = 3.0f;
xreal[3] = 4.0f;
yreal[0] = 1.0f;
yreal[1] = 2.0f;
yreal[2] = 3.0f;
yreal[3] = 4.0f;
p_myFourier->fft_conv( xreal, ximag, yreal, yimag, zreal, zimag, 8 );
p_myFourier->roundoff( zreal, 8 );
delete p_myFourier;
p_myFourier = NULL;
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "fft_ifft.h"
CFastFourier::CFastFourier()
{
}
CFastFourier::~CFastFourier()
{
}
void CFastFourier::swap(double* a, double* b)
{
double t;
t = *a;
*a = *b;
*b = t;
}
void CFastFourier::bitrp(double* xreal, double* ximag, int n)
{
int i, j, a, b, p;
for (i = 1, p = 0; i < n; i *= 2)
p++;
for (i = 0; i < n; i ++)
{
a = i;
b = 0;
for (j = 0; j < p; j ++)
{
b = (b << 1) + (a & 1); // b = b * 2 + a % 2;
a >>= 1; // a = a / 2;
}
if ( b > i)
{
swap(&xreal[i], &xreal[b]);
swap(&ximag[i], &ximag[b]);
}
}
}
void CFastFourier::FFT(double* xreal, double* ximag, int n)
{
double wreal [N_ARRAY / 2], wimag [N_ARRAY / 2], treal, timag, ureal, uimag;
double arg;
int m, k, j, t, index1, index2;
bitrp (xreal, ximag, n);
arg = -2 * PI / n;
treal = cos(arg);
timag = sin(arg);
wreal [0] = 1.0;
wimag [0] = 0.0;
for (j = 1; j < n / 2; j ++)
{
wreal[j] = wreal[j - 1] * treal - wimag[j - 1] * timag;
wimag[j] = wreal[j - 1] * timag + wimag[j - 1] * treal;
}
for (m = 2; m <= n; m *= 2)
{
for (k = 0; k < n; k += m)
{
for (j = 0; j < m / 2; j ++)
{
index1 = k + j;
index2 = index1 + m / 2;
t = n * j / m;
treal = wreal[t] * xreal[index2] - wimag[t] * ximag[index2];
timag = wreal[t] * ximag[index2] + wimag[t] * xreal[index2];
ureal = xreal[index1];
uimag = ximag[index1];
xreal[index1] = ureal + treal;
ximag[index1] = uimag + timag;
xreal[index2] = ureal - treal;
ximag[index2] = uimag - timag;
}
}
}
}
void CFastFourier::IFFT(double* xreal, double* ximag, int n)
{
double wreal [N_ARRAY / 2], wimag [N_ARRAY / 2], treal, timag, ureal, uimag;
double arg;
int m, k, j, t, index1, index2;
bitrp (xreal, ximag, n);
arg = 2 * PI / n;
treal = cos(arg);
timag = sin(arg);
wreal[0] = 1.0;
wimag[0] = 0.0;
for (j = 1; j < n / 2; j ++)
{
wreal[j] = wreal[j - 1] * treal - wimag[j - 1] * timag;
wimag[j] = wreal[j - 1] * timag + wimag[j - 1] * treal;
}
for (m = 2; m <= n; m *= 2)
{
for (k = 0; k < n; k += m)
{
for (j = 0; j < m / 2; j ++)
{
index1 = k + j;
index2 = index1 + m / 2;
t = n * j / m;
treal = wreal[t] * xreal[index2] - wimag[t] * ximag[index2];
timag = wreal[t] * ximag[index2] + wimag[t] * xreal[index2];
ureal = xreal[index1];
uimag = ximag[index1];
xreal[index1] = ureal + treal;
ximag[index1] = uimag + timag;
xreal[index2] = ureal - treal;
ximag[index2] = uimag - timag;
}
}
}
for (j=0; j < n; j ++)
{
xreal[j] /= n;
ximag[j] /= n;
}
}
void CFastFourier::roundoff(double* zreal, int n)
{
int i;
for(i=0;i<n;i++)
zreal[i] = (double)((int)(zreal[i] + 0.5));
}
void CFastFourier::mularray(double* xreal, double* ximag, double* yreal, double* yimag, double* zreal, double* zimag, int n)
{
int i;
for(i=0;i<n;i++)
{
zreal[i] = xreal[i] * yreal[i] - ximag[i] * yimag[i];
zimag[i] = xreal[i] * yimag[i] + yreal[i] * ximag[i];
}
}
void CFastFourier::fft_conv(double* xreal, double* ximag, double* yreal, double* yimag, double* zreal, double* zimag, int n)
{
FFT(xreal, ximag, n);
FFT(yreal, yimag, n);
mularray(xreal, ximag, yreal, yimag, zreal, zimag, n);
IFFT(zreal, zimag, n);
}
#define N_ARRAY 1024
#define PI 3.1416
class CFastFourier
{
public:
CFastFourier();
virtual ~CFastFourier();
void swap(double* a, double* b);
void bitrp(double* xreal, double* ximag, int n);
void FFT(double* xreal, double* ximag, int n);
void IFFT(double* xreal, double* ximag, int n);
void roundoff(double* zreal, int n);
void mularray(double* xreal, double* ximag, double* yreal, double* yimag, double* zreal, double* zimag, int n);
void fft_conv(double* xreal, double* ximag, double* yreal, double* yimag, double* zreal, double* zimag, int n);
};

float xreal[N_ARRAY] = {0};
char tmp[N_ARRAY] = {0};
memcpy(tmp,dstBuf,len);
for (int i=0;i<512;i++)
{
xreal[i] = (float)tmp[i];
if (xreal[i]<20||xreal[i]>44100)
{
xreal[i]=0;
}
}
float ximag[N_ARRAY] = {0};
p_myFourier->FFT(xreal, ximag, 512);
p_myFourier->IFFT(xreal, ximag, 512);
for (int k=0; k<len; k++)
{
dstBuf[k] = (char)xreal[k];
}
delete p_myFourier;
p_myFourier = NULL;