# Math Puzzle for the MI nerds

Here’s the story…

I was writing data to a 12-bit DAC I have never used before that was acting funny - the waveform was all distorted. Thinking that it was a SPI data format problem, I first changed the code to write a sawtooth wave (all 12-bit words from 0 to 4095) in ~680ms (6kHz update rate, with a super slow SPI clock speed to make sure it wasn’t a clock integrity problem). Here’s what I saw on the scope:

Huh?

I finally identified the problem (a clock polarity problem) and fixed it, but I’m fascinated by this shape. I have tried various bit shifting / mangling equations that would simulate what would happen with the misaligned SPI data, but I cannot reproduce it - I believe the shift register of the DAC behavior is weird and stateful when you try to clock it with the wrong polarity.

Observing the fractal structure of the thing, I found a recursive function that produces a similar-looking output, but I’m not convinced with it - it still doesn’t tell me what’s going on. Anybody has a better idea as to what’s going on here and how to simulate this without recursion and just a bunch of bit-mangling operations?

This looks like the ultimate destructive waveshaping function, since it produces mess/distortion at all scales for any kind of input.

Ah, I think I have found it:

int(bin(x)[2:].replace(‘10’, ‘00’), 2)

I can now resume all productive activities

Thats exact what i thought. Honestly. If you believe me ill sell you some used cars or insurances or something even more obscure, maybe hot dogs…

That’ll be the first interview question if one day I have to hire someone to help me

Fascinating shape, looks like a fractal or an automata - I would definitely thought some recursion is going on.
How the hell did you figure out what function it was though, did you reverse engineer the bug?

This thing obviously has a fractal structure that seems to be described by the recipe:

Take a graph A.
Split it into two segments A1 and A2.
Append A1 to A to yield A’
Append A2 translated up to A’ to yield A" (A" = A1 . A2 . A1 . (A2 shifted up)).
Repeat the process on A"

I got the recursive function just by trying that.

It looked similar but I wasn’t convinced about it - the whole process had many dependencies between bits. I started to think about something more string-like and “serial”, and thought that there was a particular bit-pattern the shift register was choking on.

and… how does it sound?

harsh