我想把以前C编的程序,转换成VB的,可我转了以后老是提示我 overflow! 我实在搞不懂了!
请各位大虾给看看,这是为什么?
VC程序
#include <stdio.h>
#include <stdlib.h>
#include <math.h>#define SRATE 32000l
#define NTODO 3
#define M_PI 3.14159265358979323846
float fcoefs[NTODO];
float fstate1[NTODO];
float fstate2[NTODO];
short findex = 0;
float Freqs[NTODO] = {100.00,125.00,280.00};
FILE *outfile;
#define INIT_TWO_COS(arg) fcoefs[findex++] = (float)2.0 * (float)cos(((float)2.0)*((float)M_PI)*((float)arg)/(float)SRATE)
main()
{ short intsum;
unsigned long samplenum;
for(findex = 0; findex < NTODO; findex++)
fcoefs[findex] = (float)2.0 * (float)cos(((float)2.0)*((float)M_PI)*Freqs[findex]/(float)SRATE);
for (findex = 0; findex < NTODO; findex++) {
fstate2[findex] = (float)1.0;
fstate1[findex] = ((float)0.5) * fcoefs[findex];
}
outfile = fopen("ee.bin","wb");
for (findex = 0; findex < NTODO; findex++)
{ int j, rval;
float sum, tempf;
rval = rand();
for (j = 0; j < rval; j++) {
sum += tempf = fcoefs[findex] * fstate1[findex] - fstate2[findex];
fstate2[findex] = fstate1[findex];
fstate1[findex] = tempf;
}
}
for (samplenum = 0; samplenum < 3200l; samplenum++) {
register float tempf;
register float sum;
sum = (float)0.0;
for (findex = 0; findex < NTODO; findex++) {
tempf = fcoefs[findex] * fstate1[findex] - fstate2[findex];
sum += ((float)512.0)*tempf;
fstate2[findex] = fstate1[findex];
fstate1[findex] = tempf;
}
sum = sum * (float)(100.0/((float)NTODO));
intsum = (short)sum;
fwrite(&intsum,2,1,outfile);
printf("%d,\n",intsum);
}
fclose(outfile);
}
VB程序:
Dim Freqs(0 To 6) As Double
Dim NTODO As Integer
Dim Findex As Integer
Dim intsum As Integer
Dim Samplenum As Long
Dim Pi As Double
Dim Srate As Integer
Dim fcoefs(0 To 3) As Double
Dim Fstate1(0 To 3) As Double
Dim Fstate2(0 To 3) As Double
Dim Normal(0 To 6) As Double
Dim j As Integer, rval As Long
Dim Sum As Single, tempf As Double
Dim FileNameS As String
Pi = 3.14159265358979
Srate = 32000
NTODO = 3
Freqs(0) = 100
Freqs(1) = 125
Freqs(2) = 280
Freqs(3) = 0
Randomize
For Findex = 0 To NTODO
fcoefs(Findex) = 2 * Cos(2 * Pi * Freqs(Findex) / Srate)
Next Findex
For Findex = 0 To NTODO
Fstate2(Findex) = 1#
Fstate1(Findex) = 0.5 * fcoefs(Findex)
Next Findex
For Findex = 0 To NTODO
rval = Int(50 * Rnd)
For j = 0 To rval
tempf = fcoefs(Findex) * Fstate1(Findex) - Fstate2(Findex)
Sum = Sum + tempf
Fstate2(Findex) = Fstate1(Findex)
Fstate1(Findex) = tempf
Next j
Next Findex
Open ("C:\ee.bin") For Binary As #2
For Samplenum = 0 To 3199
Sum = 0
For Findex = 0 To NTODO
tempf = fcoefs(Findex) * Fstate1(Findex) - Fstate2(Findex)
Sum = Sum + (512# * tempf)
Fstate2(Findex) = Fstate1(Findex)
Fstate1(Findex) = tempf
Next Findex
Sum = Int(Sum * (100 / NTODO))
intsum = Sum
Put #2, , intsum
Next Samplenum
Close #2
[此贴子已经被作者于2006-6-19 18:26:08编辑过]