void step_diff (struct vector *v, struct input *in, struct output *out,struct param *parms, double h) { struct vector k[4],t[4]; calc_diff(v,in,out,parms,&k[0]); scale(&k[0],h); copy_Vec(&k[0],&t[0]); scale(&t[0],0.5); add_Vec(&t[0],v); calc_diff(&t[0],in,out,parms,&k[1]); scale(&k[1],h); copy_Vec(&k[1],&t[1]); scale(&t[1],0.5); add_Vec(&t[1],v); calc_diff(&t[1],in,out,parms,&k[2]); scale(&k[2],h); copy_Vec(&k[2],&t[2]); add_Vec(&t[2],v); calc_diff(&t[2],in,out,parms,&k[3]); scale(&k[3],h); scale(&k[1],2); scale(&k[2],2); add_Vec(&k[0],&k[1]); add_Vec(&k[0],&k[2]); add_Vec(&k[0],&k[3]); scale(&k[0],1.0/6.0); add_Vec(v,&k[0]); }
int main(int argc, char *argv[]) { int one[3] = {2003, 3, 2}; int other[3] = {2003, 3, 5}; calc_diff(one, other); int d[] = {2016, 7, 1}; printf("%d\n", to_day_of_week(d)); int d2[] = {2016, 2}; to_calendar(d2); }
static int scrolld_subs(WMenu *menu, int d) { int diff=0; WRegion *p=REGION_PARENT_REG(menu); const WRectangle *pg; if(p==NULL) return 0; pg=®ION_GEOM(p); while(menu!=NULL){ diff=maxof(diff, calc_diff(®ION_GEOM(menu), pg, d)); menu=menu->submenu; } return minof(maxof(0, diff), scroll_amount); }
static int scrolld_subs(WMenu *menu, int d) { int diff=0; WRegion *p=REGION_PARENT_REG(menu); const WRectangle *pg; if(p==NULL) return 0; pg=®ION_GEOM(p); while(menu!=NULL){ int new_diff = calc_diff(®ION_GEOM(menu), pg, d); diff=MAXOF(diff, new_diff); menu=menu->submenu; } return MINOF(MAXOF(0, diff), scroll_amount); }
static sample_t diff_resampled(const SRCParams ¶ms, sample_t *buf1, sample_t *buf2, size_t size) { ///////////////////////////////////////////////////////////////////////////// // After resample only q*nyquist of the bandwidth is preserved. Therefore, // to compare output of the resampler with the original signal we must feed // the resampler with the bandlimited signal. Bandlimiting filter has a // transition band and we must guarantee: // passband + transition_band <= q*nyquist. // // It looks reasonable to make the transition band of the bandlimiting filter // to be equal to the transition band of the resampler. In this case we have: // passband + (1-q)*nyquist <= q*nyquist // passband <= (2q - 1)*nyquist // // In normalized form nyquist = 0.5, so we have following parameters of the // bandlimiting filter: passband = q-0.5, transition band = 0.5*(1-q) ParamFIR low_pass(ParamFIR::low_pass, params.q-0.5, 0, 0.5*(1-params.q), params.a + 10, true); const FIRInstance *fir = low_pass.make(params.fs); BOOST_REQUIRE(fir != 0); int trans_len = fir->length * 2; delete fir; Speakers spk(FORMAT_LINEAR, MODE_MONO, params.fs); samples_t s1, s2; s1.zero(); s1[0] = buf1; s2.zero(); s2[0] = buf2; ChunkSource src1(spk, Chunk(s1, size)); ChunkSource src2(spk, Chunk(s2, size)); Convolver conv1(&low_pass); Convolver conv2(&low_pass); SliceFilter slice1(trans_len, size - trans_len); SliceFilter slice2(trans_len, size - trans_len); FilterChain chain1(&conv1, &slice1); FilterChain chain2(&conv2, &slice2); return calc_diff(&src1, &chain1, &src2, &chain2); }
/*========================================================= Name: ramp_temperature Description: This function ramps the temperature according to the ramp rate *=========================================================*/ static float ramp_temperature(float temp_ramp, float temp) { float temp_diff; /* Change in temperature */ temp_diff = calc_diff(temp_ramp, temp); if (temp_diff <= RAMP_RATE) { temp_ramp = temp; } else { if (temp_ramp > temp) { temp_ramp -= RAMP_RATE; } else { temp_ramp += RAMP_RATE; } } return(temp_ramp); }
/* Indent the C-code at memory block <indent>. String <pad> represents * one block of indentation. Only opening curly braces '{' increase the * indentation level, and closing curly braces '}' decrease the indentation level. * Return the pointer to the code after modification. * Calling code is responsible of freeing only the memory block returned by * the function. */ char *indent(char *input, const char *pad) { char *content = malloc(sizeof(char) * (strlen(input) + 1)); memset(content, 0, sizeof(char) * (strlen(input + 1))); strcpy(content, input); char *output = malloc(sizeof(char) * (strlen(input) + 1)); memset(output, 0, sizeof(char) * (strlen(input) + 1)); strcpy(output, input); unsigned int pos = 0; unsigned int len = strlen(output) + 1; int indentation_level = 0; unsigned int pad_len = strlen(pad); char **paragraphs = malloc(sizeof(char*)); unsigned int count = 0; char *curr = content; unsigned int char_count = 0; short newline_count = 0; char *paragraph = malloc(sizeof(char) * (strlen(input) + 1)); memset(paragraph, 0, sizeof(char) * (strlen(input) + 1)); int *trailing_lines = malloc(sizeof(int)); trailing_lines[0] = 0; while (*curr) { if (*curr == '\n') { if (newline_count <= 1) { paragraph[char_count] = *curr; } newline_count++; } else { if (newline_count > 1) { paragraph[char_count] = '\0'; paragraphs[count] = malloc(sizeof(char) * (strlen(input) + 1)); memset(paragraphs[count], 0, sizeof(char) * (strlen(input) + 1)); strcpy(paragraphs[count], paragraph); free(paragraph); paragraph = malloc(sizeof(char) * (strlen(input) + 1)); memset(paragraph, 0, sizeof(char) * (strlen(input) + 1)); trailing_lines[count] = newline_count - 1; count++; paragraphs = realloc(paragraphs, sizeof(char*) * (count + 1)); trailing_lines = realloc(trailing_lines, sizeof(int) * (count + 1)); trailing_lines[count] = 0; char_count = 0; } paragraph[char_count] = *curr; newline_count = 0; } curr++; char_count++; } paragraph[char_count] = '\0'; paragraphs[count] = malloc(sizeof(char) * (strlen(input) + 1)); memset(paragraphs[count], 0, sizeof(char) * (strlen(input) + 1)); strcpy(paragraphs[count], paragraph); count++; for (unsigned int i = 0; i < count; i++) { char *line = strtok(paragraphs[i], "\n"); while (line != NULL) { int diff = calc_diff(line, pad_len); len = len + diff * indentation_level + trailing_lines[i]; output = realloc(output, sizeof(char) * (len + trailing_lines[i] + 1)); output[len + trailing_lines[i]] = '\0'; int indent = get_indent(line); short indented = 0; short read_else = 0; if (line[0] == '}') { if (strstr(line, "else")) { indentation_level = indentation_level - 1; read_else = 1; } else { indentation_level = indentation_level + indent; } indented = 1; } for (int i = 0; i < indentation_level; i++) { for (unsigned int j = 0; j < pad_len; j++) { output[pos] = pad[j]; pos++; } } if (!indented) { indentation_level = indentation_level + indent; } if (read_else) { indentation_level++; } while (isspace(*line)) { line++; } while (*line) { output[pos] = *line; pos++; line++; } output[pos] = '\n'; pos++; line = strtok(NULL, "\n"); } if (i < count - 1) { for (int j = 0; j < trailing_lines[i]; j++) { output[pos] = '\n'; pos++; } } } for (unsigned int i = 0; i < count; i++) { free(paragraphs[i]); } free(paragraph); free(paragraphs); free(trailing_lines); free(input); free(content); output[pos] = '\0'; return output; }
/*========================================================= Name: read_temp Description: This thread reads temperature from the thermistor through the ADC. It converts the ADC counts to temperature and calculates the PWM duty demand for the fan. *=========================================================*/ void * read_temp(void * arg) { unsigned short int thermistor_counts; /* ADC value in counts */ unsigned short int thermistor_counts_prev; /* Previous value of ADC counts */ float temp_adc_v; /* ADC value in volts */ float temp; /* Temperature read by the thermistor */ float temp_ramp = 0.0f; /* Temperature ramped output */ static float prev_temp_ramp; /* Previous value of temperature ramped output */ static float pwm_temp = 0.0f; /* Value of temperature when PWM was updated */ unsigned char index = 0u; static unsigned char first_run = TRUE; /* Flag to indicate first execution */ unsigned char print = FALSE; /* Display temperature */ /* Run indefinitely */ while(1) { /* Transmit ADC read command and receive results */ thermistor_counts = spi_read_adc(0); /* Ignore small change in counts */ if (abs(thermistor_counts_prev - thermistor_counts) < ADC_DIFFERENCE) { thermistor_counts = thermistor_counts_prev; } /* Convert counts to volts */ temp_adc_v = thermistor_counts * ADC_SCALING; /* Convert volts to temperature */ temp = get_temperature(temp_adc_v); /* First run */ if (TRUE == first_run) { first_run = FALSE; /* No need to ramp for the first time */ temp_ramp = temp; print = TRUE; } /* Look for temperature change and ramp slowly */ if (calc_diff(temp_ramp, temp) > 0.75) { temp_ramp = ramp_temperature(temp_ramp, temp); print = TRUE; } /* Display temperature */ if (TRUE == print) { print = FALSE; system("date"); fprintf (stdout, "Temperature is: %4.2f degC\n", temp_ramp); /* Limit fan speed */ if ((temp_ramp/3) < 0) { index = 0; } else if ((temp_ramp/3) > 10) { index = 10; } else { index = (temp_ramp/3); } /* Update PWM demand for temperature change more than 2 degrees */ if (fabsf(temp_ramp - pwm_temp) >= 2.0) { pwm_temp = temp_ramp; pwm_demand = fan_speed_dc[index - 1]; fprintf(stdout, "pwm demand %d%\n", pwm_demand); } } /* Wait before next read */ sleep(1); /* Store previous temperature value */ prev_temp_ramp = temp_ramp; thermistor_counts_prev = thermistor_counts; } }