11-21-2023 11:57 AM
@eeeeeeeeeeehelp wrote:
but I think it would be easier to break the loop and count the ones from the start, since you get the same result anyway.
Now that I'm writing this, I might be confusing loops with iterations, well I won't know until I try to make the actual code.
Here's a quick draft. All you need is one loop and keep track of the running odd/even state. Modify as needed.
(If you start over from the beginning, you are processing the early parts N times, leading to a O(N²) complexity that will slow you down significantly for large inputs)
11-22-2023 12:33 PM
@altenbach wrote:
@eeeeeeeeeeehelp wrote:
but I think it would be easier to break the loop and count the ones from the start, since you get the same result anyway.
Now that I'm writing this, I might be confusing loops with iterations, well I won't know until I try to make the actual code.
Here's a quick draft. All you need is one loop and keep track of the running odd/even state. Modify as needed.
(If you start over from the beginning, you are processing the early parts N times, leading to a O(N²) complexity that will slow you down significantly for large inputs)
Thank you, this is exactly what I needed! It is much simpler than anything I could come up with.
There was a very minor issue with the code, where the odd case would invert the odd/even case conditions, but it was very easy to fix by simply resetting the comparison input back to 0.
11-22-2023 01:09 PM - edited 11-22-2023 01:19 PM
You are correct. With the "even" replacement we are actually changing history data, so the toggle on the first replaced element is missed on the next iteration. Did you test your solution for all possible scenarios?
If the solution is correct, the main problem is that you added a zero constant that is probably I32, leading to several coercions and carrying four times too many bits for the odd/even state.
Instead, you should just configure the output tunnel to "use default if unwired" as follows. This way you get a zero in the existing U8 representation and the code is cleaner overall.
11-23-2023 09:29 AM
11-23-2023 10:31 AM
@jamiva wrote:
The OP mentioned that he is "trying to do HDB3 encoding". This is a bit more complicated algorithm than just substituting 0001 or 1001 in a sequence of 4 or more zeros. The HDB3 encoding converts data bits (0,1) into a bipolar code format (-1, 0 +1).The conversion from bits to bipolar code must be done first. In short, a "1" bit toggles the polarity of the previous bipolar pulse, and a "0" bit is 0 pulse. This resulting bipolar code is then modified to insert deliberate violations in long sequences of 0's to help in maintaining clock synchronization, and the choice of the inserted pattern is chosen to prevent a dc voltage from being introduced.The choice of 0001 or 1001 is also not correct. It's actually a bipolar pulse train dependent on the polarity of the Preceding Pulse. It should be as follows:Polarity of Number of Pulses since last substitutionPreceding Pulse Odd Even"-" 000- +00+"+" 000+ -00-
I know, but I was able to do that part myself and didn't need help with it. The code provided above helped me a lot already.
11-23-2023 10:48 AM
Yes, I only tried to solve the question as posed. I guess we need to change the datatype to I8 to allow -1, 0, +1 but the bulk of the code can be reused and modified. Apparently the OP was able to do all that. 😄