基于C++的矢量量化器代码
程序代码: 具体代码如下:
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop
#include <math.h> #include "VV.h"
#include "math.hpp"
//--------------------------------------------------------------------------- #pragma package(smart_init)
#pragma resource "*.dfm" TForm1 *Form1;
//--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { }
//--------------------------------------------------------------------------- void __fastcall TForm1::Button6Click(TObject *Sender) {
Application->MessageBox("①成训练矢量,②成初始码书,③生成最优码书,④进行码字转换","说明信息",MB_OK); }
int Range1;
void __fastcall TForm1::Edit5Change(TObject *Sender) {
Range1=atoi(this->Edit5->Text.c_str()); }
//--------------------------------------------------------------------------- float a[1000]; float b[32],c[1000],f[32];
void __fastcall TForm1::Button1Click(TObject *Sender) {
ListBox1->Clear(); ListBox2->Clear(); ListBox3->Clear(); Edit1->Clear(); Edit2->Clear(); Memo1->Clear();
long int x,y;
for(x=0;x<1000;x++) //给a[]赋值 且保证各个元素不相等 {
y=random(Range1); for(int j=0;j<1000;j++) {if(y==a[j]) break; }
a[x]=y; }
for(x=0;x<1000;x++) {
y=random(Range1); for(int j=0;j<1000;j++) {if(y==c[j]) break; }
c[x]=y; }
for(x=0;x<1000;x++) {
AnsiString an; an=a[x];
an+=" "; an+=","; an+=" "; an+=c[x];
ListBox1->Items->Add(an); } }
//--------------------------------------------------------------------------- double Mean1;
void __fastcall TForm1::Edit3Change(TObject *Sender) {
Mean1=atoi(this->Edit3->Text.c_str()); }
//--------------------------------------------------------------------------- double StdDev1;
void __fastcall TForm1::Edit4Change(TObject *Sender) {
StdDev1=atoi(this->Edit4->Text.c_str()); }
//--------------------------------------------------------------------------- void __fastcall TForm1::Button5Click(TObject *Sender)
{
ListBox1->Clear(); ListBox2->Clear(); ListBox3->Clear(); Edit1->Clear(); Edit2->Clear(); Memo1->Clear(); double y; int x;
for(x=0;x<1000;x++) //给a[]赋值 且保证各个元素不相等 {
y=RandG(Mean1,StdDev1); for(int j=0;j<1000;j++) {if(y==a[j]) break; }
a[x]=y; }
for(x=0;x<1000;x++) {
y=RandG(Mean1,StdDev1); for(int j=0;j<1000;j++) {if(y==c[j]) break; }
c[x]=y; }
for(x=0;x<1000;x++) {
AnsiString an; an=a[x];
an+=" "; an+=","; an+=" "; an+=c[x];
ListBox1->Items->Add(an); } }
//--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) {
ListBox2->Clear(); for(int q=0;q<32;q++) {
b[q]=0;
f[q]=0; //
初始化数组b }
for(int q=0;q<32;q++) {
b[q]=a[q+50]; f[q]=c[q+50]; AnsiString an; an=b[q];
an+=" "; an+=","; an+=" "; an+=f[q];
ListBox2->Items->Add(an); } }
//--------------------------------------------------------------------------- long int num[32],numc[32],num1[32],numc1[32] ; int y[8]; double
centR[32],sum[32],sumc[32],centRc[32],dminc[1000],dmin[1000],centR1[32],sum1[32],sumc1[32],centRc1[32];
void __fastcall TForm1::Button3Click(TObject *Sender) {
ListBox3->Clear();
long int Indexa,Indexb,d,R,q,e,Indexc,Rc; long double D, Dn,sumd,sumdc,dc,Dc;
R=0;d=0;e=1;sumd=0;D=0;Dn=0;Indexc=0;Rc=0;dc=0;sumdc=0;Dc=0; for(q=0;q++;q<32) //初始化数组 {
num[q]=0; sum[q]=0; centR[q]=0; sumc[q]=0; numc[q]=0; centR[q]=0; centRc[q]=0; }
bool tag = true; //设置布尔型tag用于控制循环
while(tag) {
for(Indexa=0;Indexa<1000;Indexa++) //双层循环让a中所有元素遍历b中的元素 {
bool isFirst = true;
for(Indexb=0;Indexb<32;Indexb++) {
d=fabs((a[Indexa]-b[Indexb])*
(a[Indexa]-b[Indexb])+(c[Indexa]-f[Indexb])*(c[Indexa]-f[Indexb])); if(isFirst) {
dmin[Indexa] = d; //给d赋初值 isFirst = false; }
if(d<=dmin[Indexa]) //最邻近原则 {
dmin[Indexa]=d;
R=Indexb; //将训练矢量划分到最近的码书内 if(d==0) break; } }
sum[R]+=a[Indexa]; // 计算每个胞腔内元素的总和 sum1[R]+=c[Indexa];
num[R]++; //计算每个胞腔内元素的个数
sumd+=dmin[Indexa]; //计算a中1000个元素的最小失真的和 }
D=sumd/1000; //计算平均失真 for(R=0;R<32;R++) //求质心数组centR {
centR[R]=sum[R]/num[R]; centR1[R]=sum1[R]/num[R]; }
for(Indexa=0;Indexa<1000;Indexa++) //双层循环让a遍历执质心中的元素 {
bool isFirst=true;
for(Indexc=0;Indexc<32;Indexc++) {
double dc=(a[Indexa]-centR[Indexc])* (a[Indexa]-centR[Indexc])+(c[Indexa]-centR1[Indexc])*(c[Indexa]-centR1[Indexc]); //计算新的失真 if(isFirst) {
dminc[Indexa]=dc;
isFirst=false; }
if(dc<=dminc[Indexa]) //最邻近原则 {
dminc[Indexa]=dc;
Rc=Indexc; //将训练矢量划分到最近的码书内 } }
sumc[Rc]+=a[Rc]; sumc1[Rc]+=c[Rc];
sumdc+=dminc[Indexa]; numc[Rc]++; }
Dc=sumdc/1000; //计算新的平均失真
Dn=fabs((D-Dc)/Dc); //计算上一次平均失真与这一次平均失真的下降程度
if(Dn<=e)//与给的的阈值e相比 看是否符合条件 否取如果是 就把Tag 赋值 false 不是 就把值输出 把变量的值放进去 下一次循环做准备 {
tag=false; // tag = false 已经得到需要的结果 =true 说明 结果还不是需要的 ListBox3->Clear(); AnsiString an; int rr;
for(int r=0;r<32;r++) //输出最优码书 { an=r;
an+="号码字"; an+=": " ;
an+=centR[r]; an+=" "; an+=","; an+=" "; an+=centR1[r];
ListBox3->Items->Add(an); } }
else //如果不符合则以得到的质心为新的码书 {
for(Rc=0;Rc<32;Rc++) {
b[Rc]=centR[Rc];
f[Rc]=centR1[Rc]; //令质心为新码书 }
tag=true; } } }
//--------------------------------------------------------------------------- //--------------------------------------------------------------------------- int N;
void __fastcall TForm1::Edit1Change(TObject *Sender) {
N =atoi(this->Edit1->Text.c_str()); //从Edit1中读取所输入的数 }
//--------------------------------------------------------------------------- int N1;
void __fastcall TForm1::Edit2Change(TObject *Sender) {
N1=atoi(this->Edit2->Text.c_str()); }
//--------------------------------------------------------------------------- void __fastcall TForm1::Button4Click(TObject *Sender) {
int R=0;
double dm=0,dmm=0; bool isFirst=true;
for(int r=0;r<32;r++) {
dmm=(N-centR[r])*(N-centR[r])+(N1-centR1[r])*(N1-centR1[r]); //计算失真 if(isFirst) //初始化最小失真dm {
dm=dmm; isFirst=false; }
else if(dmm<dm) {
dm=dmm; //得到最小失真
R=r; //得到所输入数在第几个胞腔内 } }
Memo1->Clear(); AnsiString an;
an=R;
an+="号码书"; an+=": "; an+=centR[R];
an+=" "; an+=","; an+=" "; an+=centR1[R];
an+="索引号为 "; for(int j=0;j<8;j++) {
y[j]=R%2 ; R=R/2; }
for(int j=7;j>=0;j--) {
an+=y[j]; }
Memo1->Lines->Add(an); //在Memo1中输出所对应的码字 }
//--------------------------------------------------------------------------- void __fastcall TForm1::Button7Click(TObject *Sender) { ListBox1->Clear(); ListBox2->Clear(); ListBox3->Clear(); Edit1->Clear(); Edit2->Clear(); Memo1->Clear(); Edit5->Text=0; Edit3->Text=0; Edit4->Text=0; Edit2->Text=0; Edit1->Text=0;
for(int q=0;q++;q<32) //初始化数组 {
num[q]=0; sum[q]=0; centR[q]=0; sumc[q]=0; numc[q]=0; centR[q]=0; centRc[q]=0; centR1[q]=0;
} }
//---------------------------------------------------------------------------