"farey" — Fills a table with the Farey Sequence F_{n} of the integer n.
A Farey Sequence F_{n} of order n is a list of fractions in their lowest terms between 0 and 1 and in ascending order. Their denominators do not exceed n. This means a fraction a/b belongs to F_{n} if 0 ≤ a ≤ b ≤ n. The numerator and denominator of each fraction are always coprime. 0 and 1 are included in F_{n} as the fractions 0/1 and 1/1. For example F_{5} = {0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1} Some properties of the Farey Sequence:
The length of any Farey Sequence F_{n} is determined by |F_{n}| = 1 + SUM over n (phi(m)) where phi(m) is Euler's totient function, which gives the number of integers ≤ m that are coprime to m.
Some values for the length of F_{n} given n:
n | F_{n} |
---|---|
1 | 2 |
2 | 3 |
3 | 5 |
4 | 7 |
5 | 11 |
6 | 13 |
7 | 19 |
8 | 23 |
9 | 29 |
10 | 33 |
11 | 43 |
12 | 47 |
13 | 59 |
14 | 65 |
15 | 73 |
16 | 81 |
17 | 97 |
18 | 103 |
19 | 121 |
20 | 129 |
size -- number of points in the table. Must be a power of 2 or power-of-2 plus 1 (see f statement).
fareynum -- the integer n for generating Farey Sequence F_{n}
mode -- integer to trigger a specific output to be written into the table:
0 -- outputs floating point numbers representing the elements of F_{n}.
1 -- outputs delta values of successive elements of F_{n}, useful for generating note durations for example.
2 -- outputs only the denominators of the integer ratios, useful for indexing other tables or instruments for example.
3 -- same as mode 2 but with normalised output.
4 -- same as mode 0 but with 1 added to each number, useful for generating tables for tuning opcodes, for example cps2pch.
f1 0 -23 "farey" 8 0
Generates generates Farey Sequence F_{8}. The table contains all
23 elements of F_{8} as floating point numbers.
f1 0 -18 "farey" 7 1
This generates Farey Sequence F_{7}. The table contains 18 delta values of F_{7},
i.e. the difference between r_{i+1} - r_{i}, where r is the ith element of F_{n}.
f1 0 -43 "farey" 11 2
This generates Farey Sequence F_{11}. The table contains the denominators of all
43 fractions in F_{11}.
f1 0 -43 "farey" 11 3
This generates Farey Sequence F_{11}. The table contains the denominators of all
43 fractions in F_{11}, each of those divided by 11, i.e. normalised.
f1 0 -18 "farey" 7 4
This generates Farey Sequence F_{7}. The table contains all fractions of F_{7},
same as mode 0, but this time '1' is added to each table element.
Example 948. A simple example of the GENfarey routine.
See the sections Real-time Audio and Command Line Flags for more information on using command line flags.
<CsoundSynthesizer> <CsOptions> </CsOptions> <CsInstruments> sr=44100 ksmps=10 nchnls=1 instr 4 kndx init 0 ; read out elements of F_8 one by one and print to file if (kndx < 23) then kelem tab kndx, 1 fprintks "farey8table.txt", "%2.6f\\n", kelem kndx = kndx+1 endif endin </CsInstruments> <CsScore> ; initialise integer for Farey Sequence F_8 f1 0 -23 "farey" 8 0 ; if mode=0 then the table stores all elements of the Farey Sequence ; as fractions in the range [0,1] i4 0 1 e </CsScore> </CsoundSynthesizer>