//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <math>
#include "SpectrumThread.h"
#pragma package(smart_init)
const double PI=3.14159265;
//---------------------------------------------------------------------------
__fastcall SpectrumThread::SpectrumThread(bool CreateSuspended, TPaintBox *in, int ic) :
TThread(CreateSuspended)
{
count=ic;
view=in;
data=new short[ic * 2];
WaveR=new double[ic];
WaveI=new double[ic];
busy=false;
}
//---------------------------------------------------------------------------
__fastcall SpectrumThread::~SpectrumThread(void)
{
delete[] data;
delete[] WaveR;
delete[] WaveI;
}
//---------------------------------------------------------------------------
void SpectrumThread::Process(short *idata)
{
if(!busy)
{
memcpy(data, idata, 2 * count * sizeof(short));
Resume();
}
}
//---------------------------------------------------------------------------
void SpectrumThread::SetMousePoint(int x, int y)
{
if(!busy)
{
point.x=x;
point.y=y;
}
}
//---------------------------------------------------------------------------
void __fastcall SpectrumThread::Execute(void)
{
//---- Place thread code here ----
while(!Terminated)
{
FillBuffer();
}
}
//---------------------------------------------------------------------------
void __fastcall SpectrumThread::FillBuffer(void)
{
long zerolevel=view->ClientHeight - 30;
int i, dx, freq;
double ts=static_cast<double>(view->ClientWidth) / static_cast<double>(count) * 2;
busy=true;
view->Canvas->Lock();
view->Canvas->Brush->Color=clBlack;
view->Canvas->Pen->Color=clWhite;
view->Canvas->Rectangle(0, 0, view->ClientWidth, view->ClientHeight);
view->Canvas->Pen->Color=clYellow;
view->Canvas->MoveTo(0, zerolevel);
view->Canvas->LineTo(view->ClientWidth, zerolevel);
dx=view->ClientWidth / 22;
for(long i=dx; i < view->ClientWidth - dx; i+=dx)
{
view->Canvas->TextOutA(i - 12, zerolevel + 2, IntToStr(i / dx) + "kHz");
}
freq=point.x * 22050 / view->ClientWidth;
view->Canvas->TextOutA(view->ClientWidth - 50, 10, IntToStr(freq) + "Hz");
memset(WaveI, 0, count * sizeof(double));
for(i=0; i < count; i++)
{
WaveR[i]=data[i * 2];
}
FFT(WaveR, WaveI, count, 1);
view->Canvas->Pen->Color=clAqua;
view->Canvas->MoveTo(0, zerolevel - sqrt(WaveR[0] * WaveR[0] + WaveI[0] * WaveI[0]) / count * 2);
for(i=1; i < count / 2; i++)
{
view->Canvas->LineTo(i * ts, zerolevel - sqrt(WaveR[i] * WaveR[i] + WaveI[i] * WaveI[i]) / count * 2);
}
memset(WaveI, 0, count * sizeof(double));
for(i=0; i < count; i++)
{
WaveR[i]=data[i * 2 + 1];
}
FFT(WaveR, WaveI, count, 1);
view->Canvas->Pen->Color=clRed;
view->Canvas->MoveTo(0, zerolevel - sqrt(WaveR[0] * WaveR[0] + WaveI[0] * WaveI[0]) / count * 2);
for(i=1; i < count / 2; i++)
{
view->Canvas->LineTo(i * ts, zerolevel - sqrt(WaveR[i] * WaveR[i] + WaveI[i] * WaveI[i]) / count * 2);
}
view->Canvas->Unlock();
busy=false;
Suspend();
}
//---------------------------------------------------------------------------
void __fastcall SpectrumThread::fft1(double *br, double *bi, int il, const double *st, const double *ct)
{
//the size of br,bi should be equal to il.
//and size of st and ct no less than il.
int is=il >> 1;
int mpx=0;
int ic=1;
int ia;
while(is)
{
is>>=1;
mpx++;
}
is=il;
for(ia=1; ia <= mpx; ia++)
{
int ib;
int ka=0;
is>>=1;
for(ib=1; ib <= ic; ib++)
{
int in=1;
int k;
for(k=1; k <= is; k++)
{
int j1=ka + k;
int j2=j1 + is;
double xr=br[j1];
double xi=bi[j1];
double yr=br[j2];
double yi=bi[j2];
br[j1]=xr + yr;
bi[j1]=xi + yi;
xr-=yr;
xi-=yi;
br[j2]=xr * ct[in] - xi * st[in];
bi[j2]=xr * st[in] + xi * ct[in];
in+=ic;
}
ka+=is << 1;
}
ic<<=1;
}
}
/* */
void __fastcall SpectrumThread::binrv(double *bc, int il, const int *lb)
{
int is=il - 1;
int i;
for(i=2; i <= is; i++)
{
int ig=lb[i];
if(ig <= i) continue;
double xx=bc[i];
bc[i]=bc[ig];
bc[ig]=xx;
}
}
/* */
void __fastcall SpectrumThread::brtab(int *lbr, int il)
{
int is=il >> 1;
int mpx=0;
while(is)
{
is>>=1;
mpx++;
}
int ln;
for(ln=1; ln <= il; ln++)
{
int j1=ln - 1;
int ibord=0;
for(int k=1; k <= mpx; k++)
{
int j2=j1 >> 1;
ibord=ibord * 2 + (j1 - 2 * j2);
j1=j2;
}
lbr[ln]=ibord + 1;
}
}
/* */
void __fastcall SpectrumThread::cstab(double *st, double *ct, int il, int ity)
{
double yy;
yy= -PI * 2.0 / il;
if(ity < 0) yy= -yy;
int l;
double ang=0.0;
for(l=1; l <= il; l++)
{
st[l]=sin(ang);
ct[l]=cos(ang);
ang+=yy;
}
}
/* */
void __fastcall SpectrumThread::FFT(double *br, double *bi, int n, int ity)
//ity>0 forward ,ity<0 backward
//n=pow(2,m);
//Here br and bi start from 0.
{
double *ct, *st;
int *il;
if(n <= 0) return;
double invsqrtn=1.0 / sqrt(n);
ct= (double *)malloc(sizeof(double) * n) - 1;
st= (double *)malloc(sizeof(double) * n) - 1;
cstab(st, ct, n, ity);
il= (int *)malloc(sizeof(int) * n) - 1;
brtab(il, n);
fft1(br - 1, bi - 1, n, st, ct);
binrv(br - 1, n, il);
binrv(bi - 1, n, il);
for(int i=0; i < n; i++)
{
br[i]*=invsqrtn;
bi[i]*=invsqrtn;
}
free(il + 1);
free(ct + 1);
free(st + 1);
}
没有合适的资源?快使用搜索试试~ 我知道了~
语音信号数字处理研究
共13个文件
cpp:4个
h:3个
exe:1个
3星 · 超过75%的资源 需积分: 3 46 下载量 165 浏览量
2008-12-11
22:17:50
上传
评论 1
收藏 254KB ZIP 举报
温馨提示
《语音信号数字采集研究》的主要目的是实现语音的采集、量化、控制、频谱显示功能。整个程序是通过windows平台下,使用VC++6.0的库函数实现声卡的信号采集从而生成wave文件;之后,对信号进行FFT,并在windows窗口下完成频谱显示。
资源推荐
资源详情
资源评论
收起资源包目录
cb_Spectrum.zip (13个子文件)
FFT_WIN.exe 469KB
f_main.dfm 2KB
SpectrumThread.h 1KB
TimeThread.cpp 4KB
SpectrumThread.cpp 6KB
f_main.h 2KB
f_main.cpp 5KB
FFT_WIN.cpp 979B
sound.ico 766B
FFT_WIN.dsk 6KB
FFT_WIN.bpr 4KB
TimeThread.h 3KB
FFT_WIN.res 876B
共 13 条
- 1
资源评论
- beyondsyq2012-09-10有可执行文件和部分代码,没有完整的源程序。
cjsvjcxy
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功