- Main Page
- Namespaces
- Files

- File List
- File Members

# C:/Musimathics_local/Musimat/MusimatChapter9/C091406b.cpp File Reference

`#include "MusimatChapter9.h"`

Go to the source code of this file.

## Functions
| MusimatChapter9Section (C091406b)
Integer | getIndex (RealList L, Real R)
Static Void | para1 ()

## Function Documentation

Integer getIndex | ( | RealList | L,
|  | Real | R |
| ) |  |  |

Definition at line 15 of file C091406b.cpp.

    00015                                     {
    00016         Integer i;
    00017         For (i = Length(L) - 1; i >= 0; i = i - 1){
    00018                 If (R > L[i]){
    00019                         Return(i + 1);
    00020                 }
    00021         }
    00022         Return( 0 );
    00023 }

MusimatChapter9Section | ( | C091406b |  | ) |

Definition at line 2 of file C091406b.cpp.

References para1().

    00002                                  {
    00003 Print("*** Traversing the Cumulative Distribution Function ***");
    00004 /*****************************************************************************
    00005
    00006 Traversing the Cumulative Distribution Function
    00007
    00008 To automate this, we start at the top end of the cumulative distribution function and work
    00009 down. As we go, we compare the value of R to the current step size. We’ve gone one step too far
    00010 when the value of R exceeds the step size, so we return the previous step as the answer, and stop.
    00011 *****************************************************************************/
    00012         para1(); // Step into this function to continue.)
    00013 }

Static Void para1 | ( |  | ) |

Definition at line 25 of file C091406b.cpp.

References accumulate(), Chapter9::f, getIndex(), n(), normalize(), Random(), and sum().

    00025                     {
    00026 Print("*** Invoking getIndex() ***");
    00027 /*****************************************************************************
    00028 We can invoke getIndex() as follows:
    00029 *****************************************************************************/
    00030
    00031 Real R = Random();
    00032 Integer p = getIndex(f, R); // where f was defined previously
    00033 Print(p);
    00034
    00035 /*****************************************************************************
    00036 If R is 0.1, then p prints 0. Now let’s bring all the pieces together. Here is a program that creates
    00037 a melody of 25 pitches favoring pitches that are at the low end of the chromatic scale:
    00038 *****************************************************************************/
    00039 Print("*** Melody of 25 low pitches ***");
    00040 RealList f(12.0, 11.0, 10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0);
    00041 StringList n("C", "Cs", "D", "Ds", "E", "F", "Fs", "G", "Gs", "A", "As", "B", "c");
    00042 f = normalize(f, sum(f));       // replace f with its normalized form
    00043 f = accumulate(f);      // calculate cumulative distribution function
    00044 StringList s;   // a place to put the result
    00045
    00046 For (Integer i = 0; i < 25; i = i + 1){
    00047         Integer p = getIndex(f, Random());
    00048         s[i] = n[p];
    00049 }
    00050
    00051 Print("Melody: ", s);                                                   // print the melody
    00052
    00053 /*****************************************************************************
    00054 Running this program will generate something like figure 9.23, depending upon the values pro-
    00055 duced by Random(). As we see, lower pitches are favored in approximately the proportions we
    00056 specified. The longer the sample melody, the more likely the pitch choices would conform on aver-
    00057 age to the distribution function.
    00058 *****************************************************************************/
    00059 }}