// ---------------------------------------------------------------------------- vector<float>& ofOpenALSoundPlayer::getAverages(){ if(averages.size() > 0){ getSpectrum(bins.size()); float max = 0; for (int i = 0; i < octaves; i++){ float lowFreq, hiFreq, freqStep; if (i == 0){ lowFreq = 0; } else{ lowFreq = (samplerate / 2) / powf(2, octaves - i); } hiFreq = (samplerate / 2) / powf(2, octaves - i - 1); freqStep = (hiFreq - lowFreq) / avgPerOctave; float f = lowFreq; for (int j = 0; j < avgPerOctave; j++){ int offset = j + i * avgPerOctave; averages[offset] = calculateAverage(f, f + freqStep); max = MAX(max,averages[offset]); f += freqStep; } } } return averages; }
void getStudentInformation(Result *person, int *currentIndex) { int i; float tempGrade, tempAverage = 0; printf("Please enter the student ID:\n"); fgets(person[*currentIndex].id.content, MAXLENGTH, stdin); person[*currentIndex].id.content[strlen(person[*currentIndex].id.content) - 1] = '\0'; printf("Please enter the student's name:\n"); fgets(person[*currentIndex].name.content, MAXLENGTH, stdin); person[*currentIndex].name.content[strlen(person[*currentIndex].name.content) - 1] = '\0'; printf("Please enter the student's class:\n"); fgets(person[*currentIndex].className.content, MAXLENGTH, stdin); person[*currentIndex].className.content[strlen(person[*currentIndex].className.content) - 1] = '\0'; printf("Please enter the student's grade:\n"); for(i = 0; i < 5; i++) { do { printf("Grade %d\n", i + 1); tempGrade = getFloat(); if(tempGrade < 0 || tempGrade > 10) { printf("Please enter another grade!\n"); } } while(tempGrade < 0 || tempGrade > 10); person[*currentIndex].score[i] = tempGrade; } calculateAverage(person, *currentIndex); (*currentIndex)++; }
int main(void){ float mat[R][C] = { {3, 0, 9, 5, 2}, {7, 3, 8, 4, 3}, {1, 6, 5, 8, 1}, {2, 3, 2, 4, 6} }; float output_mat[R][C]; int i, j; for (i = 0; i < R; ++i){ for (j = 0; j < C; ++j){ output_mat[i][j] = calculateAverage(mat, i, j); //printf("%f\n", average); } printf("\n"); } for (i = 0; i < R; ++i){ for (j = 0; j < C; ++j){ printf("%6.2f\t", output_mat[i][j]); } printf("\n"); } return 0; }
void normalizeSampleFiles(int minVal, int maxVal, boolean doNorm, char *files[], int numFiles) /* Top level function to do nomalization. */ { float ave = 0; int i = 0; int numSamples = 0; char buff[10*strlen(files[0])]; struct sample *sList = NULL, *s = NULL; float normVal = 0; FILE *out = NULL; for(i=0; i<numFiles; i++) { float currentAve = 0; sList = sampleLoadAll(files[i]); numSamples = countGoodSamples(sList, minVal, maxVal); currentAve = calculateAverage(sList, minVal, maxVal, numSamples); printf("Average val for %s is %f\n", files[i], currentAve); sampleFreeList(&sList); ave += currentAve; } ave = ave/numFiles; printf("Global Average is: %f\n", ave); if(doNorm) { printf("Normalizing files."); for(i=0; i<numFiles; i++) { float currentAve = 0; fprintf(stdout, "."); fflush(stdout); sList = sampleLoadAll(files[i]); numSamples = countGoodSamples(sList, minVal, maxVal); currentAve = calculateAverage(sList, minVal, maxVal, numSamples); normVal = currentAve/ave; normalizeSamples(sList, normVal); snprintf(buff, sizeof(buff), "%s.norm", files[i]); out = mustOpen(buff, "w"); for(s=sList; s != NULL; s = s->next) sampleTabOut(s,out); carefulClose(&out); sampleFreeList(&sList); } fprintf(stdout, "\tDone.\n"); } }
//Provided main function from the textbook int main (void) { int i; fp = fopen("csis.txt", "w"); for (i =1; i <=4; ++i) { calculateAverage(); } fclose(fp); return 0; }
int main() { srand(time(NULL)); int devices[DEVICE_COUNT][RUN_COUNT]; int colcount = 0; int i=0; int j=0; for(j=0; j < RUN_COUNT; j++) // Initialize for(i=0; i < DEVICE_COUNT; i++) devices[i][j] = 0; for(j = 0; j < RUN_COUNT; j++, colcount=0) for(i = 0; checkFinished(devices, j, i) == false; i++) if(getSendCount(devices,j, i) > 1){ collisionOccured(devices,j, i, colcount); colcount++; } for(i=0; i < DEVICE_COUNT; i++) // Prints the average for each device printf("Average for device %d: %d slot wait over %d runs\n", i+1,calculateAverage(devices,i), RUN_COUNT); sortRunsByFinishTime(devices); // Sort by finish time for(i=0; i < DEVICE_COUNT; i++) // Average time to complete by order of completion printf("Average for place %d: %d slot wait over %d runs\n", i+1,calculateAverage(devices,i), RUN_COUNT); }
void AsyncProfiler::end() { // compute derived types for all steps within all trials for (size_t i = 0; i < trials.size(); i++) { trials[i].computeStepsDerived(); } // compute attributes for all trials for (size_t i = 0; i < trials.size(); i++) { trials[i].computeAttributes(); } calculateAverage(); }
void QualityScores::printQScores(ofstream& qFile){ try { double aveQScore = calculateAverage(); qFile << '>' << seqName << '\t' << aveQScore << endl; for(int i=0;i<seqLength;i++){ qFile << qScores[i] << ' '; } qFile << endl; } catch(exception& e) { m->errorOut(e, "QualityScores", "printQScores"); exit(1); } }
int main() { float input[10]; float smallest; float largest; float average; int inputsize = sizeof(input) / sizeof(int); getInput(input, inputsize); smallest = getSmallestNumber(input, inputsize); largest = getLargestNumber(input, inputsize); average = calculateAverage(input, inputsize); printf("Original values: "); outputOriginalNumbers(input, inputsize); printf("\nAverage is : %0.2f", average); printf("\nSmallest is : %0.2f", smallest); printf("\nLargest is : %0.2f", largest); return(0); }
void display(int *grades, int length, float minPoints) { printf("----------------------------------------------\n"); printf("Statistics (%d Students, %.1f Points needed for mark 6)\n\n", length, minPoints); int i, j=0; for(i = 1; i<7; i++) { int tmp = 0; while(grades[j] == i) { tmp++; j++; } printf("Mark %d: %d\n", i, tmp); } printf("\n\n\n\n"); printf("Best Mark: %d\n", grades[length-1]); printf("Worst Mark: %d\n", grades[0]); printf("Average: %.2f\n", calculateAverage(grades, length)); printf("Mark >=4: %d\n", calculateMarkOverFour(grades,length)); printf("----------------------------------------------\n\n\n\n"); }
int fix(Result *person, int amount, String id, int subjectIndex, float newGrade) { int i; if(newGrade <= 4) { return 1; } for(i = 0; i < amount; i++) { if(strcmp(id.content, person[i].id.content) == 0) { if(person[i].score[subjectIndex] > newGrade) { return 1; } person[i].score[subjectIndex] = newGrade; calculateAverage(person, i); return 0; } } return 1; }
bool QualityScores::cullQualAverage(Sequence& sequence, double qAverage){ try { string rawSequence = sequence.getUnaligned(); bool success = 0; //guilty until proven innocent if(seqName != sequence.getName()) { m->mothurOut("sequence name mismatch btwn fasta: " + sequence.getName() + " and qual file: " + seqName); m->mothurOutEndLine(); } double aveQScore = calculateAverage(); if(aveQScore >= qAverage) { success = 1; } else { success = 0; } return success; } catch(exception& e) { m->errorOut(e, "QualityScores", "cullQualAverage"); exit(1); } }
void PckReceiver::threadedFunction(){ ofLog() << "PckReceiver: thread started"; while(isThreadRunning()){ unsigned char byte; while(serial.available() > 0){ // if there is something in the buffer byte = serial.readByte(); switch(byte){ case 0xFE:{ // end delimiter switch(buffer[0]){ case 0xFD:{ //lowPassFilter(buffer, preMatrix); int total = matrixTotal(&buffer[1]); // check the total value int delta = matrixDelta(&buffer[1], preMatrix); // check the difference float localRowCentroids[NUM_COLUMNS]; float localColumnCentroids[NUM_ROWS]; float localRowCentroid, localColumnCentroid; calculateAllCentroids(&buffer[1], localRowCentroids, localColumnCentroids); localRowCentroid = calculateAverage(localRowCentroids, NUM_COLUMNS); localColumnCentroid = calculateAverage(localColumnCentroids, NUM_ROWS); /****** CRITICAL SESSIONS ********/ lock(); Peacock* peacock = static_cast<Peacock*>(ofGetAppPtr()); // get next frame index int nextFrameIndex = (peacock->getFrameIndex() + 1) % NUM_FRAMES; // get a pointer to the frame x in frameData array PckFrameData* frameDataPtr = peacock->getFrameData(nextFrameIndex); // get a pointer to the matrix in the frameData unsigned char* matrixPtr = frameDataPtr->matrix; // copy the data in buffer to the matrix in the shared resource memcpy(static_cast<void*>(matrixPtr), static_cast<void*>(&buffer[1]), NUM_SENSORS); // set the precalculated total and buffer in the shared resource frameDataPtr->totalValue = total; frameDataPtr->totalDelta = delta; // copy data of centroids memcpy(static_cast<void*>(frameDataPtr->rowCentroids), static_cast<void*>(localRowCentroids), sizeof(float) * NUM_COLUMNS); memcpy(static_cast<void*>(frameDataPtr->columnCentroids), static_cast<void*>(localColumnCentroids), sizeof(float) * NUM_ROWS); // advance frame index by one frameDataPtr->rowCentroid = localRowCentroid; frameDataPtr->columnCentroid = localColumnCentroid; peacock->setFrameIndex(nextFrameIndex); unlock(); /****** CRITICAL SESSIONS ********/ memcpy(static_cast<void*>(preMatrix), static_cast<void*>(&buffer[1]), NUM_SENSORS); index = 0; break; }case 0xFC:{ ofLog() << "Button State:" << static_cast<int>(buffer[1]); break; } break; } case 0xFF:{ // start delimiter index = 0; // ensure that buffer is empty break; } default:{ buffer[index] = byte; index++; } } } } Peacock *peacock = static_cast<Peacock*>(ofGetAppPtr()); PckSynthesizer *synthesizer = peacock->getSynthesizer(); synthesizer->lock(); synthesizer->pushNotification(PCK_ENTER); synthesizer->unlock(); }// thread loop serial.close(); ofLog()<< "PckReceiver: connection to Peacock hardware closed."; }
int phasorView() { float Ts = 1.0/10000; int data_size = 0.2/Ts; // quantidade de medições de tensão uint16_t data_v1[data_size]; //unsigned int since input > 0 uint16_t data_v0[data_size]; // vetor de medições de tensão do segundo circuito float ten_average1[10], ten_average0[10]; float ten_tension1[10], ten_tension0[10]; float ten_f1[10], ten_f0[10]; float ten_phase[10]; int count = 0; pc.printf("START \r\n"); while (1) { float average1 = 0; // tensão média float average0 = 0; float v_eff1 = 0; // valor eficaz quadratico do primeiro circuito float v_eff0 = 0; // valor eficaz quadratico do segundo circuito float offset1 = 1.61; // offset do primeiro circuito float offset0 = 1.61; // offset do segundo circuito float voltage = 0; int n = 0; // iterador do loop de medições tick.attach(&interrupt, Ts); do { if (aux == 1) { aux = 0; // 1st circuit measure data_v1[n] = a1.read_u16(); // 2nd circuit measure data_v0[n] = a0.read_u16(); n += 1; } } while (n != data_size); // 1st circuit average for (int i=0; i < data_size; i++) { voltage = 3.3*((float) data_v1[i]/PFACTOR); average1 += (voltage - offset1)/data_size; v_eff1+= (voltage - offset1)*(voltage - offset1)/data_size; } // 2nd circuit average for (int i=0; i < data_size; i++) { voltage = 3.3*((float) data_v0[i]/PFACTOR); average0 += (voltage - offset0)/data_size; v_eff0 += (voltage - offset0)*(voltage - offset0)/data_size; } // calcular aqui a fase com base nos vetores v_eff1 e v_eff2 float phase1, phase0; float f0 = frequency(data_v0, data_size, Ts, &phase0); float f1 = frequency(data_v1, data_size, Ts, &phase1); phase0 = phase1 = 0; phase0 = phase(data_v0, data_size, Ts, 1/f0); phase1 = phase(data_v1, data_size, Ts, 1/f1); float phase = phase1 - phase0; if (phase < -180) { phase += 360; } if (phase > 180) { phase -= 360; } ten_average1[count] = average1; ten_average0[count] = average0; ten_tension1[count] = tension1(sqrt(v_eff1)); ten_tension0[count] = tension0(sqrt(v_eff0)); ten_f1[count] = f1; ten_f0[count] = f0; ten_phase[count] = phase; count += 1; if (count == 10) { count = 0; average1 = calculateAverage(ten_average1); average0 = calculateAverage(ten_average0); float t1 = calculateAverage(ten_tension1); float t0 = calculateAverage(ten_tension0); f1 = calculateAverage(ten_f1); f0 = calculateAverage(ten_f0); phase = calculateAverage(ten_phase); pc.printf("C1 (polar) : (%f V, %f graus)) \r\n", t1*sqrt(2), phase); pc.printf("C1 (cartesiano): %f(%f, %f) V\r\n", t1*srqt(2), cos(phase), sin(phase)); } } }
void hgExperiment(char *database, char *table, char *expFile, char *posFile, char *dataFile) /* Main function */ { struct lineFile *lf; int *data = NULL; int *scores; FILE *f = NULL; char expTable[32]; char *words[3]; int wordCt; struct bed *bedList, *bed; int expCount; struct hash *expHash, *dataHash; struct hashEl *hel; /* Open experiment file and use it to create experiment table. Use optional fields if present, otherwise defaults */ safef(expTable, ArraySize(expTable), "%sExps", table); expHash = makeExpsTable(database, expTable, expFile, &expCount); /* Read in positions file */ bedList = bedLoadAll(posFile); slSort(&bedList, bedCmp); /* Read data file into a hash of arrays of data values, keyed by name */ dataHash = newHash(0); lf = lineFileOpen(dataFile, TRUE); while ((wordCt = lineFileChopNext(lf, words, ArraySize(words)))) { /* format: <region-name> <experiment-name> <data-value> */ char *name, *exp; int expId; int value; if (wordCt != 3) errAbort("Expecting 3 words in data file, got %d line %d of %s", wordCt, lf->lineIx, lf->fileName); name = words[0]; hel = hashLookup(dataHash, name); if (!hel) { AllocArray(data, expCount); hel = hashAdd(dataHash, name, data); } data = (int *)hel->val; exp = words[1]; expId = hashIntVal(expHash, exp); if (expId < 0 || expId > expCount-1) errAbort("Invalid experiment ID %d for %s, line %d of %s", expId, exp, lf->lineIx, lf->fileName); //value = atoi(words[2]); value = round(atof(words[2])); if (data[expId] != 0) errAbort("Extra experiment data value %d for %s %s, line %d of %s", value, name, exp, lf->lineIx, lf->fileName); data[expId] = value; } lineFileClose(&lf); /* Fill in BED15 fields - add experiment values, and setup block (only 1)*/ for (bed = bedList; bed != NULL; bed = bed->next) { int i; bed->thickStart = bed->chromStart; bed->thickEnd = bed->chromEnd; bed->blockCount = 1; AllocArray(bed->blockSizes, 1); bed->blockSizes[0] = bed->chromEnd - bed->chromStart; AllocArray(bed->chromStarts, 1); bed->chromStarts[0] = 0; bed->expCount = expCount; AllocArray(bed->expIds, expCount); for (i = 0; i < expCount; i++) bed->expIds[i] = i; AllocArray(bed->expScores, expCount); scores = hashMustFindVal(dataHash, bed->name); for (i = 0; i < expCount; i++) bed->expScores[i] = scores[i]; /* set score for bed to the average of the scores in all experiments */ calculateAverage(bed); } /* from affyPslAndAtlsoToBed ? convertIntensitiesToRatios(bedList); */ /* Write BED data file */ f = hgCreateTabFile(tabDir, table); for (bed = bedList; bed != NULL; bed = bed->next) bedTabOutN(bed, 15, f); /* Cleanup */ carefulClose(&f); freeHash(&expHash); freeHash(&dataHash); bedFreeList(&bedList); }