Help with vb.net (refactoring from c++) simple bit of code
Help with vb.net (refactoring from c++) simple bit of code
Author
Discussion

TonyRPH

Original Poster:

13,560 posts

200 months

Monday 29th January 2018
quotequote all
I have a c++ program that parses a .csv file and reads a single column of numbers.

These numbers are derived from a sine wave (waveform) and hence go low... high... low.. etc.

The 'zero' point is not '0' but 127 - so the sequence swings positive and negative, and crossing through 127 is treated as a zero crossing.

The c++ looks like this:


for(unsigned int i=0;i<samples;++i){
getline(file,row,',');//skip the "Data0" text
previous_data=data;
file >> data;
cout << "Data" << " " << data << endl;
cout << "Previous Data" << " " << previous_data << endl;
// ground is 127
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;
}
}
}


The vb.net code I have so far looks like this: (the AndAlso loop is never processed - however I can see that data IS less than 127 (and can be greater than too)

So why does the "AndAlso" not work at all?

The output of the variable 'data' is the same in both programs.

Further back in my code I have:


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 '** there seems to be a problem with this line or block - in the C++ program this iterates 81 times - here it's 4095
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


The data looks like this snippet: (the first column is ignored)


Data0,212
Data1,194
Data2,170
Data3,144
Data4,117
Data5,91
Data6,67
Data7,48
Data8,33
Data9,24
Data10,23
Data11,27
Data12,39
Data13,55
Data14,77
Data15,100
Data16,128
Data17,154
Data18,179


Edited by TonyRPH on Monday 29th January 14:42


Edited by TonyRPH on Monday 29th January 14:43


Edited by TonyRPH on Monday 29th January 14:45


Edited by TonyRPH on Monday 29th January 16:24

maffski

1,905 posts

191 months

Monday 29th January 2018
quotequote all
Your code isn't the same.

In the C++ version the 'zero_crossing += 1' part is the else clause to 'if start = -1' - so it's used to mark the first index that the cross occurs and then count how many other times it crosses from below 127. In the VB version it's simply counting how many of the data elements have not passed from below 127.

Also, in the C++ (and your post) it's implied 'ground' is always 127, but in the VB version you seem to be taking the first data element and using that as 'ground' - so for the example data '212' - although it may be the 2nd element of data?


If it was me I'd nuke that code from orbit, use a StreamReader with a loop (using Peek) around ReadLine and Integer.Parse

TonyRPH

Original Poster:

13,560 posts

200 months

Monday 29th January 2018
quotequote all
Ground is definitely 127 - I have verified that by outputting the variable at various points in the loop.

So far - when comparing outputs at various points in the loop with the c++ code - it always seems to break at the last else statement.


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)))
previous_data = data
data = (Str(DataBuffer1(i + 3)))
ListBox4.Items.Add(i)
ListBox3.Items.Add(data & "," & previous_data)
If data < ground AndAlso previous_data >= ground Then
' this outputs the same values as c++
If start = -1 Then
' this also outputs the same values as c++
start = i
End If
Else
' it's here that it all seems to go wrong.
zero_crossing += 1
dataEnd = i
End If

Next


I don't understand why it fails on that last else statement, when everything else seems to produce the correct output?



I'm afraid that coding is not my strong point (and I'm sure it shows) I thought StreamReader was for reading from files and as for you other suggestions, you might as well be swearing* at me lol!!!

  • not necessarily however I wouldn't know how to implement Peek or integer.parse although I know how to use ReadLine (I think...)

maffski

1,905 posts

191 months

Monday 29th January 2018
quotequote all
TonyRPH said:

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 you had it in the wrong place
zero_crossing += 1
dataEnd = i
End If
End If
Next
Your zero_crossing is in the wrong place - it should be the else clause to 'if start = -1' - I've moved it in the above code snippet


TonyRPH

Original Poster:

13,560 posts

200 months

Monday 29th January 2018
quotequote all
Thanks!

You were spot on there.

I've been looking at this for most of the day....

It doesn't help that Visual Studio autocompleted if / end if statements as they sometimes do end up in the wrong places evidently..