void print_statistics(double running_sum, int quantity, double smallest_number, double largest_number) { printf("\n\nYour average is %.2lf.\n", calculate_average(running_sum, quantity)); printf("The smallest number you entered was %.2lf.\n", smallest_number); printf("The largest number you entered was %.2lf.\n", largest_number); }
void inputAbstraction(Input *input){ if (!isPresent() && inputMode == DIGITAL){ input->deltaX = PENTA_MED; input->deltaY = PENTA_MED; input->menu = false; return; } if (inputMode == ANALOG){ int speed = analogRead(speedPin); int direction = analogRead(dirPin); int reference = analogRead(refPin); float y = (float) (speed - reference) /(float) reference; float x = (float) (direction - reference) / (float) reference; int newSpeed = (int) 2500*y + 500; int newDirection = (int) 2500*x + 500; if (newSpeed < PENTA_MIN) newSpeed = PENTA_MIN; if (newSpeed > PENTA_MAX) newSpeed = PENTA_MAX; if (newDirection < PENTA_MIN) newDirection = PENTA_MIN; if (newDirection > PENTA_MAX) newDirection = PENTA_MAX; calculate_average(newSpeed, newDirection); input->deltaY = speed_average; input->deltaX = direction_average; } else { int front = digitalRead(p_frontPin); int back = digitalRead(p_backPin); int left = digitalRead(p_leftPin); int right = digitalRead(p_rightPin); if (front==LOW && back==LOW) input->deltaY = PENTA_MED; else{ if (front==LOW) input->deltaY = PENTA_MAX; else if (back==LOW) input->deltaY = PENTA_MIN; else input->deltaY = PENTA_MED; } if (left==LOW && right==LOW) input->deltaX = PENTA_MED; else{ if (left==LOW) input->deltaX = PENTA_MIN; else if (right==LOW) input->deltaX = PENTA_MAX; else input->deltaX = PENTA_MED; } } input->menu = bool_read_pin(p_buttonPin); return; }
float calculate_standard_deviation(int* values, int number_of_values) { float average = calculate_average(values, number_of_values); float sum_of_squared_differences = 0.0; for (int i = 0; i < number_of_values; ++i) { sum_of_squared_differences += pow(values[i] - average, 2); } float standard_deviation = sqrt(sum_of_squared_differences / number_of_values); return standard_deviation; }
void main(void){ int sum,average,level,level_last,count; char interval =0; int max_timeout ; float ratio; int r,g,b; int lights; int peak[5]; int peak_last[5]; int avg[5]; int max[5]; int filt_out[5]; int color[5]; int i,j; int bytes_read; int temp_val; bytes_read = BUFFER_SIZE; setvbuf(stdout, NULL, _IONBF, 0); printf("Hello World!\n"); SERIAL = fopen("/dev/ttyO1","w"); setvbuf(SERIAL, NULL, _IONBF, 0); kiss_fftr_state = kiss_fftr_alloc(BUFFER_SIZE,0,0,0); sendColor(4,255,0,0,0); sendColor(9,0,255,0,0); color[0] = max[0] = avg[0] = 0; color[1] = max[1] =avg[1] = 0; color[2] = max[2] =avg[2] = 0; color[3] = max[3] =avg[3] = 0; color[4] = max[4] =avg[4] = 0; interval =0; max_timeout = 50; while(bytes_read==BUFFER_SIZE){ // bytes_read = fread(&audio_data,sizeof(short),512,stdin); bytes_read = fread(&audio_data,sizeof(short),BUFFER_SIZE,stdin); for(i = 0; i< BUFFER_SIZE;i++){ rin[i] = (audio_data[i]*hamming_window512[i])/16384; } // for(i = 0;i<bytes_read; i++){ // //sum +=audio_data[i]; // filt_out[0] = calculate_FIR(&filter0,audio_data[i]); // filt_out[1] = calculate_FIR(&filter1,audio_data[i]); // filt_out[2] = calculate_FIR(&filter2,audio_data[i]); // filt_out[3] = calculate_FIR(&filter3,audio_data[i]); // filt_out[4] = calculate_FIR(&filter4,audio_data[i]); // for(j=0;j<5;j++){ // filt_out[j] = abs(filt_out[j]); // peak[j] = (filt_out[j] > peak[j])? filt_out[j]: peak[j]; // } // // } if(interval == 0){ kiss_fftr( kiss_fftr_state, rin, sout ); for(lights = 0;lights<NUM_LIGHTS; lights++){ sum = 0; count = 0; for(i = band_min[lights];i<=band_max[lights];i++){ sum += sqrt(sout[i].r*sout[i].r + sout[i].i*sout[i].i); count ++; } peak[lights] = sum/ count ; avg[lights] = calculate_average(&(averages[lights]),peak[lights]); } for(j=0;j<NUM_LIGHTS;j++){ if(peak[j]>max[j]){ max[j] = peak[j]; } ratio =(avg[j])? ((float)peak[j])/avg[j]:0; level =(int)(threshold[j]*ratio); if(level > 12) level = 12; ratio =(avg[j])? ((float)peak_last[j])/avg[j]:0; level_last =(int)(threshold[j]*ratio); if(level_last > 12) level_last = 12; if(level <1 && level_last >1){ color[j] ++; color[j] %=12; } peak_last[j] = peak[j]; level = (max[j])? (12*peak[j])/max[j]:0; printf("%d:%8d ",level,avg[j]); r = color_array[12-level][color_order[color[j]]][0]; g = color_array[12-level][color_order[color[j]]][1]; b = color_array[12-level][color_order[color[j]]][2]; // if(level>1){ sendColor(address_table[j],r,g,b,0); // }else{ // sendColor(address_table[j],0,0,0,0); // } //sendColor(9,0,255,0,0); if(max_timeout == 0){ printf("Avg[%d]=%d ",j,avg[j]); max[j]= (max[j]/4 > avg[j])? max[j]-(max[j]-avg[j])/2: max[j]; } } printf ("\n"); if(max_timeout == 0){ max_timeout = 300; } max_timeout --; } interval ++; interval %=2; //printf("%8d:%8d:%8d:%8d:%8d\n", peak[0],peak[1],peak[2],peak[3],peak[4]); //printf("%d,%d,%d,%d,%d\n", peak[0],peak[1],peak[2],peak[3],peak[4]); } free(kiss_fftr_state); }