这个函数不知道怎么调用,谁能帮帮我这个小菜鸟呢

满衣兄 2009-05-26 03:00:30
我这里有个FFT的类,可是我不会使用.郁闷.
我是这么调用的:

//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;
}


大家帮忙看看这么调用对不对啊?fft_ifft.cpp在网上找的.如果我不进行fft变换声音是正常的,变换之后就全是杂音了.

我应该怎么调用这个类呢?郁闷.


类定义如下:
fft_ifft.h

#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);
};


fft_ifft.cpp

#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;
}
}


...全文
320 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzh9955 2009-05-27
  • 打赏
  • 举报
回复
一步步来!
满衣兄 2009-05-27
  • 打赏
  • 举报
回复
有谁知道怎么使用的吗?郁闷
goodname 2009-05-27
  • 打赏
  • 举报
回复
瞎猜不行,要么搞定算法,要么请做过这个东西的人。


哎,到处都这么困难。
goodname 2009-05-27
  • 打赏
  • 举报
回复
要是严格按照上面的来还应该
for (int i=0;i<512;i++)
{
yreal[i] = (float)tmp[i];
}

满衣兄 2009-05-27
  • 打赏
  • 举报
回复
[Quote=引用 32 楼 goodname 的回复:]
30楼有例子,使用28,29楼的文件。
[/Quote]
还是不行啊

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];//这样全是杂音
}
Qlaiaqu 2009-05-27
  • 打赏
  • 举报
回复
引来大牛无数阿
goodname 2009-05-27
  • 打赏
  • 举报
回复
30楼有例子,使用28,29楼的文件。
满衣兄 2009-05-27
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 goodname 的回复:]
fft_ifft.h

C/C++ code
#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…
[/Quote]
怎么使用啊,fft_conv各个参数是什么意思
goodname 2009-05-27
  • 打赏
  • 举报
回复
example.cpp


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;
goodname 2009-05-27
  • 打赏
  • 举报
回复
fft_ifft.h

#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);
}

goodname 2009-05-27
  • 打赏
  • 举报
回复
fft_ifft.h

#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);
};
我看你有戏 2009-05-27
  • 打赏
  • 举报
回复
http://www.coolsou.net
抄写
http://www.qvodsou.com
ex_dijkstra 2009-05-27
  • 打赏
  • 举报
回复
既然要置顶,赶快留名占位接分
满衣兄 2009-05-27
  • 打赏
  • 举报
回复
好帖子!申请斑竹置顶!
Sou2012 2009-05-27
  • 打赏
  • 举报
回复
MARK!!
acrobatyuer 2009-05-27
  • 打赏
  • 举报
回复
不错。。。收藏了。。。
bandboy886 2009-05-26
  • 打赏
  • 举报
回复
这也叫菜鸟,我无地自容了·
wwoo_1105 2009-05-26
  • 打赏
  • 举报
回复
帮顶
满衣兄 2009-05-26
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 goodname 的回复:]
是不是调用一个函数就可以了呢。
[/Quote]
不是,我猜应该是这样:先对语音数据进行FFT得到xreal值和ximag值,xreal是需要处理的数据,ximag是中间值.然后对xreal中的值进行处理,然后利用ximag恢复xreal的值.
也不知道猜的对不对,郁闷,关键这个原理不明白,找了一圈也没找到.
我修改成这样:
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;

声音平滑了很多,听起来也舒服了,可是音质变的很差,听对方说话就翁翁的,我晕啊.
goodname 2009-05-26
  • 打赏
  • 举报
回复
http://www.chinavib.com/forum/viewthread.php?tid=69860
加载更多回复(16)

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧