#include "MusimatChapter9.h"Go to the source code of this file.
Functions | |
| MusimatChapter9Section (C091406) | |
| 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) |
| Static Void | para1 () |
| Real | sum (RealList L) |
| Static Void | para2 () |
| RealList | normalize (RealList L, Real s) |
| Static Void | para3 () |
| 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 | |||
| ) |
| MusimatChapter9Section | ( | C091406 | ) |
Definition at line 2 of file C091406.cpp.
References para1(), para2(), and para3().
00002 { 00003 Print("*** 9.14.6 Cumulative Distribution Function ***"); 00004 /***************************************************************************** 00005 00006 9.14.6 Cumulative Distribution Function 00007 00008 Let’s rotate each of the weights in figure 9.21 and then concatenate them. Their sum is 78, so we divide 00009 the length of each weight by 78 so that the weights sum to a length of 1.0 (figure 9.21). We have effec- 00010 tively divided up the x-axis in the unit interval into 12 areas that are proportional to the weights in the 00011 original distribution. Now we pick a random number in the unit interval with the Random() function, 00012 see which interval the number would fall in, and then determine the chosen pitch. The probability that 00013 a particular interval will be chosen is proportional to the extent of its footprint on the x-axis. 00014 00015 How can we represent this formally so that a computer can do this? First, the definition 00016 of list f shown below defines the weights for each pitch, lowest to highest, left to right. 00017 Note that the type of list f is RealList. 00018 *****************************************************************************/ 00019 para1(); // Step into this function to continue. 00020 para2(); // Step into this function to continue. 00021 para3(); // Step into this function to continue. 00022 }
| RealList normalize | ( | RealList | L, | |
| Real | s | |||
| ) |
Definition at line 60 of file C091406.cpp.
00060 { 00061 For (Integer i = 0; i < Length(L); i = i + 1){ 00062 L[i] = L[i]/s; 00063 } 00064 Return(L); 00065 }
| Static Void para1 | ( | ) |
Definition at line 26 of file C091406.cpp.
References Chapter9::f.
00026 { 00027 Print("*** Cumulative Distribution Function ***"); 00028 Print("f=", f); 00029 /***************************************************************************** 00030 00031 Next, we normalize the weights so that they sum to 1.0 (see appendix A, A.3). 00032 Normalizing is done in two steps: 00033 00034 1. Find the sum of all weights with the sum() function: 00035 *****************************************************************************/ 00036 }
| Static Void para2 | ( | ) |
Definition at line 46 of file C091406.cpp.
References Chapter9::f, guido(), and sum().
00046 { 00047 /***************************************************************************** 00048 Given the definition of RealList f above, 00049 *****************************************************************************/ 00050 00051 Print("Sum of f: ", sum(f)); 00052 00053 /***************************************************************************** 00054 prints 78. 00055 00056 2. Divide each weight by sum(f) so that the sum of the weights equals 1.0: 00057 *****************************************************************************/ 00058 }
| Static Void para3 | ( | ) |
Definition at line 67 of file C091406.cpp.
References Chapter9::f, retrograde(), sum(), and x.
00067 { 00068 /***************************************************************************** 00069 The normalize() function can be replaced with operations performed directly on the list. 00070 This statement: 00071 00072 RealList r = normalize(f, sum(f)); 00073 00074 can be replaced with: 00075 00076 RealList r = f / sum(f); 00077 00078 So we don't really need the normalize() function. 00079 Given the definition of RealList f above, the statements: 00080 *****************************************************************************/ 00081 00082 RealList r = f / sum(f); 00083 Print("Normalized f=", r); 00084 RationalList x = RealListToRationalList(r); // RealListToRationalList is a built-in function 00085 Print(x); 00086 00087 /***************************************************************************** 00088 print 00089 {{2,13}, {11,78}, {5,39}, {3,26}, {4,39}, {7,78}, {1,13}, {5,78}, {2,39}, {1,26}, {1,39}, {1,78}} 00090 which correspond to the reduced form of the following ratios, as we'd expect: 00091 {12/78, 11/78, 10/78, 9/78, 8/78, 7/78, 6/78, 5/78, 4/78, 3/78, 2/78, 1/78}. 00092 00093 After these two steps, r will look like figure 9.20 except that all values are scaled down by 78. (The 00094 built-in RealToRational() function is described in appendix B, B.2.2.) 00095 00096 Next, we create a function such that each step along the x-axis accumulates all the weights to 00097 its left with its own weight (figure 9.22). The first column has a height of 12/78, the second of 00098 12/78 + 11/78, the next of 12/78 + 11/78 + 10/78, and so on. This function is called a cumulative 00099 distribution function. 00100 00101 *****************************************************************************/ 00102 }}
| Real sum | ( | RealList | L | ) |
Definition at line 38 of file C091406.cpp.
00038 { 00039 Real s = 0.0; 00040 For (Integer i = 0; i < Length(L); i = i + 1) { 00041 s = s + L[i]; 00042 } 00043 Return(s); 00044 }
1.4.7