GENfarey
Prev | Score Statements and GEN Routines | Next

## GENfarey

"farey" — Fills a table with the Farey Sequence Fn of the integer n.

## Description

A Farey Sequence Fn 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 Fn if 0 ≤ a ≤ b ≤ n. The numerator and denominator of each fraction are always coprime. 0 and 1 are included in Fn as the fractions 0/1 and 1/1. For example F5 = {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:

- If a/b and c/d are two successive terms of Fn, then bc - ad = 1.
- If a/b, c/d, e/f are three successive terms of Fn, then: c/d = (a+e) / (b+f). In this case c/d is called the mediant fraction between a/b and e/f.
- If n > 1, then no two successive terms ofFn have the same denominator.

The length of any Farey Sequence Fn is determined by |Fn| = 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 Fn given n:

n | Fn
--- | ---
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

## Syntax

    f # time size "farey" fareynum mode

## Initialization

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 Fn

mode -- integer to trigger a specific output to be written into the table:

- 0 -- outputs floating point numbers representing the elements of Fn.

- 1 -- outputs delta values of successive elements of Fn, 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.

## Examples

    f1 0	-23 "farey" 8 0

Generates generates Farey Sequence F8. The table contains all 23 elements of F8 as floating point numbers.

    f1 0 -18 "farey" 7 1

This generates Farey Sequence F7. The table contains 18 delta values of F7, i.e. the difference between ri+1 - ri, where r is the ith element of Fn.

    f1 0	-43 "farey" 11 2

This generates Farey Sequence F11. The table contains the denominators of all 43 fractions in F11.

    f1 0	-43 "farey" 11 3

This generates Farey Sequence F11. The table contains the denominators of all 43 fractions in F11, each of those divided by 11, i.e. normalised.

    f1 0	-18 "farey" 7 4

This generates Farey Sequence F7. The table contains all fractions of F7, same as mode 0, but this time '1' is added to each table element.

Example 1011. 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>

## Credits

Author: Georg Boenn
University of Glamorgan
2010

New in Csound version 5.13

Prev | Up | Next
GENsone | Home | The Utility Programs