# Two-dimensional a-rate array

**URL:** <https://forum.csound.com/t/two-dimensional-a-rate-array/268>\
**Category:** General\
**Created:** [April 21, 2021, 7:12pm UTC](https://forum.csound.com/t/two-dimensional-a-rate-array/268 "2021-04-21T19:12:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JasonHallen](https://forum.csound.com/user_avatar/forum.csound.com/jasonhallen/32/32_2.png) [@JasonHallen](https://forum.csound.com/u/JasonHallen)\
**Post date:** [April 21, 2021, 7:12pm UTC](https://forum.csound.com/t/two-dimensional-a-rate-array/268/1 "2021-04-21T19:12:31Z")

</div>

Hi there,

In one Csound project I was able to create a phasor by looping through the indices of an a-rate variable like this:

```csound
kphs init 0
kndx = 0
while (kndx < ksmps) do
    aphs[kndx] = kphs
    kphs += 1
    kndx += 1 
od

```

In my current Csound project, I’d like to create an array of 10 phasors using a similar method, but Csound won’t let me do it. Here’s example code:

```csound
; instr 0
gatrack_phase[] init 10

;instr 1
kphs init 0
kndx = 0
while (kndx < ksmps) do
    gatrack_phase[0][kndx] = kphs
    kphs += 1
    kndx += 1
od

```

Csound gives this error: `PERF ERROR in instr 1 (opcode ##array_set.x) line 0: Array dimension 2 does not match for dimensions 1`.

Does Csound not let you use a-rate arrays like in the second example? I know I can use the `phasor` opcode but I was hoping to do it with a `ksmps` loop for the flexibility it offers.

Thanks,  
Jason

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [April 21, 2021, 7:37pm UTC](https://forum.csound.com/t/two-dimensional-a-rate-array/268/2 "2021-04-21T19:37:05Z")

</div>

While I think you can do something like `aSig[0]` to access the first element of an audio vector, what you are trying to do won’t work. Csound see `gatrack_phase[0][kndx]` and thinks you are trying to access values from a 2d array. There may well be a way to do this, but I’m not sure what’s the best one.

On the other hand, if you really want to do this with a `ksmps` loop, then put it in a UDO and set `ksmps` to 1 with `setksmps`. Btw, using an opcode is always much faster than coding it by hand in Csound. 3 times faster, or thereabouts.

---

<div class="post-metadata">

**Author:** ![JasonHallen](https://forum.csound.com/user_avatar/forum.csound.com/jasonhallen/32/32_2.png) [@JasonHallen](https://forum.csound.com/u/JasonHallen)\
**Post date:** [April 21, 2021, 7:41pm UTC](https://forum.csound.com/t/two-dimensional-a-rate-array/268/3 "2021-04-21T19:41:50Z")

</div>

Thanks Rory. I think I’ve figured out a workaround. Looks like this and seems to be working:

```csound
kphs init 0
kindex = 0
while (kindex < ksmps) do
    atemp_phase[kindex] = kphs
    ; gatrack_phase[0][kindex] = kphs
    kphs += 1
    kindex += 1
od
gatrack_phase[0] = atemp_phase

```

Ah, thanks for the tip about the performance gains of opcodes. I was wondering about that. Just to be clear, you’re saying that if I were to use an opcode like `phasor` it’ll be about 3x faster than hand coding a `ksmps` loop?

EDIT: Does the 3x performance gain also apply to UDOs or only to native Csound opcodes due to them being written in C/C++?

Thanks!  
Jason

---

<div class="post-metadata">

**Author:** ![rory](https://forum.csound.com/user_avatar/forum.csound.com/rory/32/7_2.png) [@rory](https://forum.csound.com/u/rory)\
**Post date:** [April 22, 2021, 9:19am UTC](https://forum.csound.com/t/two-dimensional-a-rate-array/268/4 "2021-04-22T09:19:24Z")

</div>

> [@JasonHallen](#):
>
> EDIT: Does the 3x performance gain also apply to UDOs or only to native Csound opcodes due to them being written in C/C++?

It only applies to opcodes, for reasons you already figured out. The 3x faster thing is an approximation of course. I’m pretty sure I could write an opcode so bad that it would be much slower than a UDO 🤣

I believe UDOs might also suffer from a slight performance hit too. Someone with more knowledge of the inner workings of Csound can comment better on this. One thing is clear, they can really help to modularise one’s code. And that’s worth a lot in my book.

---

<div class="post-metadata">

**Author:** ![JasonHallen](https://forum.csound.com/user_avatar/forum.csound.com/jasonhallen/32/32_2.png) [@JasonHallen](https://forum.csound.com/u/JasonHallen)\
**Post date:** [April 22, 2021, 12:49pm UTC](https://forum.csound.com/t/two-dimensional-a-rate-array/268/5 "2021-04-22T12:49:49Z")

</div>

Perfect, thanks for the answer!
