我正在尝试将此C ++程序移植到vb.net。
我大部分时间都在工作,但是我一直坚持(我希望!)小事。
这是C ++代码,由electronics.stackexchange用户Harry Svensson友情提供 - 完整帖子here
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]){
if(argc!=2){
cout << "Drop a file on me or"<<
"call me from the command line with one argument.\n\n";
//cin.get();//pause, useful for when you drop a file onto the exe.
return 1;
}else{
cout << "You rang?\n\n";
}
string suffix(argv[1]);//convert to string
if(suffix.length()<5){
//bad names are: "t", "xt", "txt", ".txt"
//good names are: "a.txt" or "abc.txt"
cout << "Doesn't look good Sonny," <<
" give me a file on the format of \"*.txt\".\n\n";
//cin.get();//pause, useful for when you drop a file onto the exe.
return 1;
}else{
cout << "The length checks out.\n\n";
}
suffix.erase(0,suffix.length()-3);
if(suffix!="txt"){
cout << "Accepted type is \"txt\", yours is \"" << suffix << "\"\n\n";
//cin.get();//pause, useful for when you drop a file onto the exe.
return 1;
}else{
cout << "Got a file of the correct type. Let's proceed.\n\n";
cout << "===========================================\n\n";
}
ifstream file(argv[1]);//let's finally open the file
string row; //contains row of document, with a ',' as a delimiter
unsigned int samples,
sample_rate,
ground,data,
previous_data,
zero_crossing=0,
end=0,
start=-1;
//First get the number of samples and sample rate.
getline(file,row,'-');//throw away first part
file >> samples; //acquired samples.
cout << "Number of samples = " << samples << endl;
getline(file,row,',');//skip
getline(file,row,',');//skip
file >> sample_rate; //acquired the sample rate
cout << "Sample rate = " << sample_rate << endl;
getline(file,row,',');//skip
getline(file,row,',');//skip
file >> ground;
cout << "Ground level = " << ground << endl << endl;
//Okay, now we just need to read the rest of the document
for(unsigned int i=0;i<samples;++i){
getline(file,row,',');//skip the "Data0" text
previous_data=data;
file >> data;
if(data<ground && previous_data>=ground){
//we've just passed ground and we're going upwards.
if(start==-1){
//is this the first time we've came across this zero crossing?
start=i;
}else{
zero_crossing++;
//this is a potential stop
end=i;
}
}
}
double mean_period = (end-start)/((double)zero_crossing);
double frequency = sample_rate / mean_period;
cout << "First occurance of a negative edge = " << start << endl;
cout << "Last occurance of a negative edge = " << end << endl;
cout << "Length between those two = " << end-start << endl;
cout << "Number of crossings detected = " << zero_crossing << endl;
cout << "Mean length per period = " << mean_period << endl;
cout << "Frequency = " << frequency << endl;
cout << endl << endl;
//cin.get();//pause, useful for when you drop a file onto the exe.
return 0;
}
这是我到目前为止的vb.net代码:
Dim sample_rate, data, previous_data As Integer
Dim zero_crossing As Integer = 0
Dim dataEnd As Integer = 0
Dim start As Integer = -1
Dim ground As Integer = Str(DataBuffer1(2))
Dim j As Integer = 0
For i As UInteger = 0 To 4096 - 1
list.Add(Str(DataBuffer1(i + 3))) ' required to calculate RMS values
ListBox1.Items.Add(Str(i) + Chr(9) + Chr(9) + Str(DataBuffer1(i + 3)))
samples = i
previous_data = data
data = (Str(DataBuffer1(i + 3)))
If data < ground AndAlso previous_data >= ground Then
If start = -1 Then
start = i
End If
Else
zero_crossing += 1
dataEnd = i
End If
Next
Dim mean_period As Double = (dataEnd - start) / (zero_crossing)
Dim frequency As Double = sample_rate / mean_period
MsgBox("Period is" & " " & mean_period & " " & "Frequency is" & " " & frequency)
TextBox12.Text = frequency
我似乎在转换C ++部分时遇到了问题;
zero_crossing++;
end=i;
到vb.net;
zero_crossing += 1
dataEnd = i
似乎vb.net代码没有&#34;拯救&#34;当达到最后的零交叉时。
C ++代码遍历循环81次 - 但是vb.net代码遍历整个4095次。
C ++程序的输出显示样本为4096
Number of samples = 4096
Sample rate = 25000000
Ground level = 127
First occurance of a negative edge = 40
Last occurance of a negative edge = 4090
Length between those two = 4050
Number of crossings detected = 81
Mean length per period = 50
Frequency = 500000
继续循环问题:
for(unsigned int i=0;i<samples;++i){
getline(file,row,',');//skip the "Data0" text
cout << "WIBBLE" << endl;
./parse velleman-500khz.txt > output
cat output|grep WIBBLE |wc -l
4097
所以主外环实际上循环了4097次......
然而此时它循环832次:
if(data<ground && previous_data>=ground){
cout << "WIBBLE" << endl;
cat output|grep WIBBLE |wc -l
83
有人可以帮忙吗?
答案 0 :(得分:0)
zero_crossing位于错误的位置 - 如果start = -1&#39;它应该是&else; - 在下面的片段中更正。
If start = -1 Then
' this also outputs the same values as c++
start = i
Else
' it's here that it all seems to go wrong. - that's because it was in the wrong place
zero_crossing += 1
dataEnd = i
End If
End If
Next