Comment

Comment by mlochbaum

Yes, it's a pretty simple example. Something fun along the same lines, plotting pink noise (random walk is Brownian, or brown noise) in two lines of BQN: https://mlochbaum.github.io/BQN/try.html#code=UmFuZCDihpAgLe...

    Rand ← -⟜¬ •rand.Range⟜0
    •Plot {+` 𝕩 ↑ ⥊∘⍉∘≍´⌽ -⟜«∘Rand¨ (1⌈↕∘⌈)⌾(2⋆⁼⊢)2⌈𝕩} 500
A slightly spread out translation of the function to J below. The idea is to take lengths 2 2 4 8 16... up to the required length, and then repeatedly interleave the two smallest ones to get a fractal pattern. The interleaved values are differences of white noise, and a final sum undoes these differences to leave a sum of white noise at several different frequencies.

    pink =: {{
      len   =. (1>.i.@>.)&.:(2&^.) 2>.y
      diffs =. (2-~/\0,[:(--.)?@$&0)&.> len
      +/\ y {. >,@,.&.>/|. diffs
    }}
Adapted from [0], which I was able to simplify some when I ported it from an earlier J version [1]. I think having the whole algorithm there at once was definitely helpful in figuring out what I meant to do and writing it more cleanly.

[0] https://github.com/mlochbaum/BQNoise/blob/master/tracker.bqn...

[1] https://github.com/mlochbaum/JSound/blob/master/makedrums.ij...

parent mlochbaum 4 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton