/* Looks at the line segments that start at point j, that end at * all following points (ending at index rp). The initial point * is on curve s0, the ending point is on curve s1. The curve choice * (s.min vs. s.max) is based on the index in ss[]. The scan * looks for the largest (sign=0) or smallest (sign=1) slope. */ static int search(int rp, int j, int s0, int s1, int sign, struct _seg *answer) { double dt, slope; int n, nextj = 0, cinit = 1; for (n = next_up(j); n != next_up(rp); n = next_up(n)) { #ifdef ENABLE_DEBUG if (debug) DBG("d_ring[%d].s.ss[%d]=%f d_ring[%d].s.ss[%d]=%f", n, s0, d_ring[n].s.ss[s0], j, s1, d_ring[j].s.ss[s1]); #endif dt = d_ring[n].absolute - d_ring[j].absolute; slope = (d_ring[n].s.ss[s0] - d_ring[j].s.ss[s1]) / dt; #ifdef ENABLE_DEBUG if (debug) DBG("slope %d%d%d [%d,%d] = %f", s0, s1, sign, j, n, slope); #endif if (cinit || (slope < answer->slope) ^ sign) { answer->slope = slope; answer->offset = d_ring[n].s.ss[s0] + slope*(d_ring[rp].absolute - d_ring[n].absolute); cinit = 0; nextj = n; } } return nextj; }
int contemplate_data(unsigned int absolute, double skew, double errorbar, int freq) { /* Here is the actual phase lock loop. * Need to keep a ring buffer of points to make a rational * decision how to proceed. if (debug) print a lot. */ static int rp=0, valid=0; int both_sides_now=0; int j, n, c, max_avail, min_avail, dinit; int nextj=0; /* initialization not needed; but gcc can't figure out my logic */ double cum; struct _seg check, save_min, save_max; double last_slope; int delta_freq; double delta_f; int inconsistent=0, max_imax, max_imin=0, min_imax, min_imin=0; int computed_freq=freq; if (debug) printf("xontemplate %u %.1f %.1f %d\n",absolute,skew,errorbar,freq); d_ring[rp].absolute = absolute; d_ring[rp].skew = skew; d_ring[rp].errorbar = errorbar - 800.0; /* quick hack to speed things up */ d_ring[rp].freq = freq; if (valid<RING_SIZE) ++valid; if (valid==RING_SIZE) { /* * Pass 1: correct for wandering freq's */ cum = 0.0; if (debug) printf("\n"); for (j=rp; ; j=n) { d_ring[j].s.s.max = d_ring[j].skew - cum + d_ring[j].errorbar; d_ring[j].s.s.min = d_ring[j].skew - cum - d_ring[j].errorbar; if (debug) printf("hist %d %d %f %f %f\n",j,d_ring[j].absolute-absolute, cum,d_ring[j].s.s.min,d_ring[j].s.s.max); n=next_dn(j); if (n == rp) break; /* Assume the freq change took place immediately after * the data was taken; this is valid for the case where * this program was responsible for the change. */ cum = cum + (d_ring[j].absolute-d_ring[n].absolute) * (double)(d_ring[j].freq-freq)/65536; } /* * Pass 2: find the convex down envelope of s.max, composed of * line segments in s.max vs. absolute space, which are * points in freq vs. dt space. Find points in order of increasing * slope == freq */ dinit=1; last_slope=-100; for (c=1, j=next_up(rp); ; j=nextj) { nextj = search(rp, j, 1, 1, 0, &maxseg[c]); search(rp, j, 0, 1, 1, &check); if (check.slope < maxseg[c].slope && check.slope > last_slope && (dinit || check.slope < save_min.slope)) {dinit=0; save_min=check; } if (debug) printf("maxseg[%d] = %f *x+ %f\n", c, maxseg[c].slope, maxseg[c].offset); last_slope = maxseg[c].slope; c++; if (nextj == rp) break; } if (dinit==1) inconsistent=1; if (debug && dinit==0) printf ("mincross %f *x+ %f\n", save_min.slope, save_min.offset); max_avail=c; /* * Pass 3: find the convex up envelope of s.min, composed of * line segments in s.min vs. absolute space, which are * points in freq vs. dt space. These points are found in * order of decreasing slope. */ dinit=1; last_slope=+100.0; for (c=1, j=next_up(rp); ; j=nextj) { nextj = search(rp, j, 0, 0, 1, &minseg[c]); search(rp, j, 1, 0, 0, &check); if (check.slope > minseg[c].slope && check.slope < last_slope && (dinit || check.slope < save_max.slope)) {dinit=0; save_max=check; } if (debug) printf("minseg[%d] = %f *x+ %f\n", c, minseg[c].slope, minseg[c].offset); last_slope = minseg[c].slope; c++; if (nextj == rp) break; } if (dinit==1) inconsistent=1; if (debug && dinit==0) printf ("maxcross %f *x+ %f\n", save_max.slope, save_max.offset); min_avail=c; /* * Pass 4: splice together the convex polygon that forms * the envelope of slope/offset coordinates that are consistent * with the observed data. The order of calls to polygon_point * doesn't matter for the frequency shift determination, but * the order chosen is nice for visual display. */ if (!inconsistent) { polygon_reset(); polygon_point(&save_min); for (dinit=1, c=1; c<max_avail; c++) { if (dinit && maxseg[c].slope > save_min.slope) { max_imin = c-1; maxseg[max_imin] = save_min; dinit = 0; } if (maxseg[c].slope > save_max.slope) break; if (dinit==0) polygon_point(&maxseg[c]); } if (dinit && debug) printf("found maxseg vs. save_min inconsistency\n"); if (dinit) inconsistent=1; max_imax = c; maxseg[max_imax] = save_max; polygon_point(&save_max); for (dinit=1, c=1; c<min_avail; c++) { if (dinit && minseg[c].slope < save_max.slope) { max_imin = c-1; minseg[min_imin] = save_max; dinit = 0; } if (minseg[c].slope < save_min.slope) break; if (dinit==0) polygon_point(&minseg[c]); } if (dinit && debug) printf("found minseg vs. save_max inconsistency\n"); if (dinit) inconsistent=1; min_imax = c; minseg[min_imax] = save_max; /* not needed for analysis, but shouldn't hurt either */ if (debug) polygon_point(&save_min); } /* !inconsistent */ /* * Pass 5: decide on a new freq */ if (inconsistent) { printf("# inconsistent\n"); } else { delta_f = find_df(&both_sides_now); if (debug) printf("find_df() = %e\n", delta_f); delta_f += find_df_center(&save_min,&save_max, delta_f); delta_freq = delta_f*65536+.5; if (debug) printf("delta_f %f delta_freq %d bsn %d\n", delta_f, delta_freq, both_sides_now); computed_freq -= delta_freq; printf ("# box [( %.3f , %.1f ) ", save_min.slope, save_min.offset); printf ( " ( %.3f , %.1f )] ", save_max.slope, save_max.offset); printf (" delta_f %.3f computed_freq %d\n", delta_f, computed_freq); if (computed_freq < -6000000) computed_freq=-6000000; if (computed_freq > 6000000) computed_freq= 6000000; } } rp = (rp+1)%RING_SIZE; return computed_freq; }
/** * Outputs a comment. The initial opening '//' may be included in the text. * Subsequent openings (if combining comments), should not be included. * The closing (for C/D comments) should not be included. * * TODO: * If reflowing text, the comment should be added one word (or line) at a time. * A newline should only be sent if a blank line is encountered or if the next * line is indented beyond the current line (optional?). * If the last char on a line is a ':' or '.', then the next line won't be * combined. */ static void add_comment_text(const unc_text& text, cmt_reflow& cmt, bool esc_close) { bool was_star = false; bool was_slash = false; bool was_dollar = false; bool in_word = false; int tmp; int len = text.size(); for (int idx = 0; idx < len; idx++) { if (!was_dollar && cmt.kw_subst && (text[idx] == '$') && (len > (idx + 3)) && (text[idx + 1] == '(')) { idx += add_comment_kw(text, idx, cmt); if (idx >= len) { break; } } /* Split the comment */ if (text[idx] == '\n') { in_word = false; add_char('\n'); cmt_output_indent(cmt.brace_col, cmt.base_col, cmt.column); if (cmt.xtra_indent) { add_char(' '); } /* hack to get escaped newlines to align and not dup the leading '//' */ tmp = next_up(text, idx + 1, cmt.cont_text); if (tmp < 0) { add_text(cmt.cont_text); } else { idx += tmp; } } else if (cmt.reflow && (text[idx] == ' ') && (cpd.settings[UO_cmt_width].n > 0) && ((cpd.column > cpd.settings[UO_cmt_width].n) || next_word_exceeds_limit(text, idx))) { in_word = false; add_char('\n'); cmt_output_indent(cmt.brace_col, cmt.base_col, cmt.column); if (cmt.xtra_indent) { add_char(' '); } add_text(cmt.cont_text); } else { /* Escape a C closure in a CPP comment */ if (esc_close && ((was_star && (text[idx] == '/')) || (was_slash && (text[idx] == '*')))) { add_char(' '); } if (!in_word && !unc_isspace(text[idx])) { cmt.word_count++; } in_word = !unc_isspace(text[idx]); add_char(text[idx]); was_star = (text[idx] == '*'); was_slash = (text[idx] == '/'); was_dollar = (text[idx] == '$'); } } }