// // Programmer: Craig Stuart Sapp // Creation Date: Tue Jan 11 14:21:06 PST 2005 // Last Modified: Tue Jan 11 14:21:09 PST 2005 // Filename: ...sig/examples/all/tinfo.cpp // Web Address: http://sig.sapp.org/examples/museinfo/humdrum/tinfo.cpp // Syntax: C++; museinfo // // Description: Basic information theory measurements on **kern // #include "humdrum.h" #include "CircularBuffer.h" #include // function declarations void checkOptions (Options& opts, int argc, char* argv[]); void example (void); void usage (const char* command); int storesymbol (Array& table, const char* token); int buildtable (HumdrumFile& infile, Array& symboltable, Array& frequency); void printTable (Array& symboltable, Array& frequency, int totalcount); double entropy (Array& frequency, int totalcount); int findsymbol (Array& table, const char* token); void printAnalysis (HumdrumFile& infile, Array& symboltable, Array& frequency, int totalcount); int buildnthorder (HumdrumFile& infile, Array >& nthsymboltable, Array& nthfrequency, Array symboltable, int order); int storenthsymbol (Array >& nthsymboltable, CircularBuffer& buffer); void printNthOrder (Array& sequence, Array lookup); void printnthTable (Array& symboltable, Array >& nthsymboltable, Array& nthfrequency, int nthtotalcount); void extractSequence (HumdrumFile& infile, Array& sequence, Array& symboltable); int findNthSymbol (Array >& nthsymboltable, CircularBuffer& buffer); void fillRow (Array& row, Array& sequence, Array& symboltable, int order, HumdrumFile& infile); void printPicture (Array& symboltable, HumdrumFile& infile); void printNthAnalysis (HumdrumFile& infile, Array& symboltable, Array >& nthsymboltable, Array& nthfrequency, int nthtotalcount, int order); // global variables Options options; // database for command-line arguments int debugQ = 0; // used with the --debug option int appendQ = 0; // used with the -a option int maxcount = 50000; // used with the --max option int tableQ = 0; // used with the -t option int order = 1; // used with the -n option int bgval = 255; // used with the -b option int fileQ = 0; // used with the -n option /////////////////////////////////////////////////////////////////////////// int main(int argc, char* argv[]) { HumdrumFile infile; // process the command-line options checkOptions(options, argc, argv); // if no command-line arguments read data file from standard input int numinputs = options.getArgCount(); if (numinputs < 1) { infile.read(cin); } else { infile.read(options.getArg(1)); } int nthtotalcount = 0; Array symboltable; Array frequency; int totalcount = 0; totalcount = buildtable(infile, symboltable, frequency); Array > nthsymboltable; Array nthfrequency; nthtotalcount = buildnthorder(infile, nthsymboltable, nthfrequency, symboltable, order); if (tableQ) { if (order == 1) { printTable(symboltable, frequency, totalcount); } else { printnthTable(symboltable, nthsymboltable, nthfrequency, nthtotalcount); } } else if (fileQ) { // printAnalysis(infile, symboltable, frequency, totalcount); printNthAnalysis(infile, symboltable, nthsymboltable, nthfrequency, nthtotalcount, order); } else { printPicture(symboltable, infile); } return 0; } /////////////////////////////////////////////////////////////////////////// ////////////////////////////// // // printNthAnalysis -- // void printNthAnalysis(HumdrumFile& infile, Array& symboltable, Array >& nthsymboltable, Array& nthfrequency, int nthtotalcount, int order) { CircularBuffer buffer; buffer.setSize(order * 2); buffer.reset(); int i; double freqnorm; int sindex = 0; int nthindex; for (i=0; i= order) { nthindex = findNthSymbol(nthsymboltable, buffer); freqnorm = (double)nthfrequency[nthindex]/nthtotalcount; cout << -log(freqnorm)/log(2.0); buffer.extract(); } else { cout << "."; } } else if (infile[i].getType() == E_humrec_data_comment) { cout << "\t!"; } cout << "\n"; } } ////////////////////////////// // // printAnalysis -- // void printAnalysis(HumdrumFile& infile, Array& symboltable, Array& frequency, int totalcount) { int i; double freqnorm; int sindex = 0; for (i=0; i& frequency, int totalcount) { double sum = 0.0; double value = 0.0; int i; for (i=0; i& symboltable, Array >& nthsymboltable, Array& nthfrequency, int nthtotalcount) { double freqnorm; int i; cout << "!! tokencount = " << nthtotalcount << "\n"; cout << "!! weighted_entropy = " << entropy(nthfrequency, nthtotalcount) << "\n"; cout << "!! unique_states = " << nthfrequency.getSize() << "\n"; cout << "**idx\t**sym\t**freq\t**norm\t**info\n"; for (i=0; i& sequence, Array lookup) { int i; for (i=0; i& symboltable, Array& frequency, int totalcount) { double freqnorm; int i; cout << "!! tokencount = " << totalcount << "\n"; cout << "!! weighted_entropy = " << entropy(frequency, totalcount) << "\n"; cout << "!! unique_states = " << frequency.getSize() << "\n"; cout << "**idx\t**sym\t**freq\t**norm\t**info\n"; for (i=0; i& symboltable, Array& frequency) { symboltable.setSize(maxcount); symboltable.setGrowth(maxcount); symboltable.setSize(0); frequency.setSize(maxcount); frequency.setGrowth(maxcount); frequency.setSize(0); int i=0; int total = 0; int zero=0; int hindex = 0; for (i=0; i >& nthsymboltable, Array& nthfrequency, Array symboltable, int order) { nthsymboltable.setSize(maxcount); nthsymboltable.setGrowth(maxcount); nthsymboltable.setSize(0); nthfrequency.setSize(maxcount); nthfrequency.setGrowth(maxcount); nthfrequency.setSize(0); int i=0; int total = 0; int zero=0; int hindex = 0; CircularBuffer buffer; buffer.setSize(order*2); buffer.reset(); int sindex; for (i=0; i= order) && (strcmp(infile[i][0], ".") != 0)) { hindex = storenthsymbol(nthsymboltable, buffer); if (hindex == nthfrequency.getSize()) { nthfrequency.append(zero); } buffer.read(); nthfrequency[hindex]++; total++; } } return total; } /////////////////////////////// // // storenthsymbol -- // int storenthsymbol(Array >& nthsymboltable, CircularBuffer& buffer) { int i; int j; int found = 0; for (i=0; i& table, const char* token) { int i; for (i=0; i& table, const char* token) { int i; for (i=0; i 255) bgval = 255; } ////////////////////////////// // // example -- example usage of the maxent program // void example(void) { cout << " \n" << endl; } ////////////////////////////// // // usage -- gives the usage statement for the quality program // void usage(const char* command) { cout << " \n" << endl; } ////////////////////////////// // // fillRow -- // void fillRow(Array& row, Array& sequence, Array& symboltable, int order, HumdrumFile& infile) { row.setSize(0); double value; Array > nthsymboltable; Array nthfrequency; int nthtotalcount; nthtotalcount = buildnthorder(infile, nthsymboltable, nthfrequency, symboltable, order); int i; int nthindex; CircularBuffer buffer; buffer.setSize(order*2); buffer.reset(); for (i=0; i= order) { nthindex = findNthSymbol(nthsymboltable, buffer); value = (double)nthfrequency[nthindex] / nthtotalcount; value = -log(value)/log(2.0); row.append(value); buffer.extract(); } }; if (!debugQ) { double mmin = 0; double mmax = 0; mmin = row[0]; mmax = row[0]; for (i=0; i mmax) mmax = row[i]; } if (mmax == mmin) { for (i=0; i& symboltable, HumdrumFile& infile) { Array sequence; extractSequence(infile, sequence, symboltable); Array row; row.setSize(sequence.getSize()); // changes in fillRow cout << "P3\n"; cout << sequence.getSize() << " " << sequence.getSize() << "\n"; cout << "255\n"; int i; int j; int pre; int post; for (i=0; i& sequence, Array& symboltable) { sequence.setSize(infile.getNumLines()); sequence.setSize(0); int i; int sindex = 0; for (i=0; i >& nthsymboltable, CircularBuffer& buffer) { int i; int j; int found = 0; for (i=0; i