I am facing a problem in unwrapping a phase.
I have a data set presented by $a_i$, where $i$ varies from $1$ to $N$. I need it as follows:

- If $\mathrm{abs}(a_{i+1} - a_i) > 3$, add $6 k \,$ to $a_{i+1}$, otherwise return $a_{i+1}$. If $a_{i+1} > a_i$ for the first time, $k = -1$, otherwise $k = 1$.
  
- For every time $\mathrm{abs}(a_{i+1} - a_i) > 3$ and $a_{i+1} > a_i$, $k \to k + 1$ otherwise $k \to k - 1$.  

  For example, the first time $\mathrm{abs}(a_{i+1} - a_i) > 3$, add $6$ if $a_{i+1} > a_i$.  For the second time $\mathrm{abs}(a_{i+1} - a_i) > 3$, add $12$ if $a_{i+1} > a_i$.  And for third time, if $a_{i+1} < a_i$, add $6$ not $18$.

So, can anyone please help me to write the code in *Mathematica* for the above logic. I am getting stuck. And thank you in adv. for the help.