static uint64_t closest_supported_channel_layout(AVCodec *codec, uint64_t target) { // if we can, return exact match. otherwise, return the layout with the // minimum number of channels >= target if (!codec->channel_layouts) return target; int target_count = av_get_channel_layout_nb_channels(target); const uint64_t *p = codec->channel_layouts; uint64_t best = *p; int best_count = av_get_channel_layout_nb_channels(best); while (*p) { if (*p == target) return target; int count = av_get_channel_layout_nb_channels(*p); if ((best_count < target_count && count > best_count) || (count >= target_count && abs_diff(target_count, count) < abs_diff(target_count, best_count))) { best_count = count; best = *p; } p += 1; } return best; }
void diff_c2 ( unsigned char *src, unsigned char *src_2, unsigned char *dst, int m, // columnas int n, // filas int src_row_size, int src_2_row_size, int dst_row_size ) { unsigned char (*src_matrix)[src_row_size] = (unsigned char (*)[src_row_size]) src; unsigned char (*src_2_matrix)[src_2_row_size] = (unsigned char (*)[src_2_row_size]) src_2; unsigned char (*dst_matrix)[dst_row_size] = (unsigned char (*)[dst_row_size]) dst; for (int i = 0; i < n; i++) { for (int j = 0; j < 4 * m; j = j + 4) { unsigned char b_diff = abs_diff(src_matrix[i][j], src_2_matrix[i][j]); unsigned char g_diff = abs_diff(src_matrix[i][j+1], src_2_matrix[i][j+1]); unsigned char r_diff = abs_diff(src_matrix[i][j+2], src_2_matrix[i][j+2]); unsigned char diff = max_3(b_diff, g_diff, r_diff); dst_matrix[i][j] = diff; dst_matrix[i][j+1] = diff; dst_matrix[i][j+2] = diff; dst_matrix[i][j+3] = 255; } } }
static enum GrooveSampleFormat closest_supported_sample_fmt(AVCodec *codec, enum GrooveSampleFormat target) { // if we can, return exact match. otherwise, return the format with the // next highest sample byte count if (!codec->sample_fmts) return target; int target_size = av_get_bytes_per_sample((enum AVSampleFormat)target); const enum GrooveSampleFormat *p = (enum GrooveSampleFormat*) codec->sample_fmts; enum GrooveSampleFormat best = *p; int best_size = av_get_bytes_per_sample((enum AVSampleFormat)best); while (*p != GROOVE_SAMPLE_FMT_NONE) { if (*p == target) return target; int size = av_get_bytes_per_sample((enum AVSampleFormat)*p); if ((best_size < target_size && size > best_size) || (size >= target_size && abs_diff(target_size, size) < abs_diff(target_size, best_size))) { best_size = size; best = *p; } p += 1; } // prefer interleaved format enum GrooveSampleFormat packed_best = (enum GrooveSampleFormat)av_get_packed_sample_fmt((enum AVSampleFormat)best); return codec_supports_fmt(codec, packed_best) ? packed_best : best; }
static int closest_supported_sample_rate(AVCodec *codec, int target) { // if we can, return exact match. otherwise, return the minimum sample // rate >= target if (!codec->supported_samplerates) return target; const int *p = codec->supported_samplerates; int best = *p; while (*p) { if (*p == target) return target; if ((best < target && *p > best) || (*p >= target && abs_diff(target, *p) < abs_diff(target, best))) { best = *p; } p += 1; } return best; }
unsigned MaxComponentDifference(const SkBitmap& a, const SkBitmap& b) { if (a.info() != b.info()) { SkFAIL("Can't compare bitmaps of different shapes."); } unsigned max = 0; const SkAutoLockPixels lockA(a), lockB(b); if (a.info().colorType() == kRGB_565_SkColorType) { // 565 is special/annoying because its 3 components straddle 2 bytes. const uint16_t* aPixels = (const uint16_t*)a.getPixels(); const uint16_t* bPixels = (const uint16_t*)b.getPixels(); for (size_t i = 0; i < a.getSize() / 2; i++) { unsigned ar, ag, ab, br, bg, bb; unpack_565(aPixels[i], &ar, &ag, &ab); unpack_565(bPixels[i], &br, &bg, &bb); max = SkTMax(max, abs_diff(ar, br)); max = SkTMax(max, abs_diff(ag, bg)); max = SkTMax(max, abs_diff(ab, bb)); } } else { // Everything else we produce is byte aligned, so max component diff == max byte diff. const uint8_t* aBytes = (const uint8_t*)a.getPixels(); const uint8_t* bBytes = (const uint8_t*)b.getPixels(); for (size_t i = 0; i < a.getSize(); i++) { max = SkTMax(max, abs_diff(aBytes[i], bBytes[i])); } } return max; }
static void hsw_wrpll_update_rnp(uint64_t freq2k, unsigned budget, unsigned r2, unsigned n2, unsigned p, struct hsw_wrpll_rnp *best) { uint64_t a, b, c, d, diff, diff_best; /* No best (r,n,p) yet */ if (best->p == 0) { best->p = p; best->n2 = n2; best->r2 = r2; return; } /* * Output clock is (LC_FREQ_2K / 2000) * N / (P * R), which compares to * freq2k. * * delta = 1e6 * * abs(freq2k - (LC_FREQ_2K * n2/(p * r2))) / * freq2k; * * and we would like delta <= budget. * * If the discrepancy is above the PPM-based budget, always prefer to * improve upon the previous solution. However, if you're within the * budget, try to maximize Ref * VCO, that is N / (P * R^2). */ a = freq2k * budget * p * r2; b = freq2k * budget * best->p * best->r2; diff = abs_diff(freq2k * p * r2, LC_FREQ_2K * n2); diff_best = abs_diff(freq2k * best->p * best->r2, LC_FREQ_2K * best->n2); c = 1000000 * diff; d = 1000000 * diff_best; if (a < c && b < d) { /* If both are above the budget, pick the closer */ if (best->p * best->r2 * diff < p * r2 * diff_best) { best->p = p; best->n2 = n2; best->r2 = r2; } } else if (a >= c && b < d) { /* If A is below the threshold but B is above it? Update. */ best->p = p; best->n2 = n2; best->r2 = r2; } else if (a >= c && b >= d) { /* Both are below the limit, so pick the higher n2/(r2*r2) */ if (n2 * best->r2 * best->r2 > best->n2 * r2 * r2) { best->p = p; best->n2 = n2; best->r2 = r2; } } /* Otherwise a < c && b >= d, do nothing */ }
// Compress a block by using the bounding box of the pixels without taking into // account the extremal values. The generated palette will contain extremal values // and fewer points along the line segment to interpolate. static uint64_t compress_latc_block_bb_ignore_extremal(const uint8_t pixels[]) { uint8_t minVal = 255; uint8_t maxVal = 0; for (int i = 0; i < kLATCPixelsPerBlock; ++i) { if (is_extremal(pixels[i])) { continue; } minVal = SkTMin(pixels[i], minVal); maxVal = SkTMax(pixels[i], maxVal); } SkASSERT(!is_extremal(minVal)); SkASSERT(!is_extremal(maxVal)); uint8_t palette[kLATCPaletteSize]; generate_latc_palette(palette, minVal, maxVal); uint64_t indices = 0; for (int i = kLATCPixelsPerBlock - 1; i >= 0; --i) { // Find the best palette index uint8_t idx = 0; if (is_extremal(pixels[i])) { if (0xFF == pixels[i]) { idx = 7; } else if (0 == pixels[i]) { idx = 6; } else { SkFAIL("Pixel is extremal but not really?!"); } } else { uint8_t bestError = abs_diff(pixels[i], palette[0]); for (int j = 1; j < kLATCPaletteSize - 2; ++j) { uint8_t error = abs_diff(pixels[i], palette[j]); if (error < bestError) { bestError = error; idx = j; } } } indices <<= 3; indices |= idx; } return SkEndian_SwapLE64( static_cast<uint64_t>(minVal) | (static_cast<uint64_t>(maxVal) << 8) | (indices << 16)); }
// Return the squared minimum error cost of approximating 'pixel' using the // provided palette. Return this in the middle 16 bits of the integer. Return // the best index in the palette for this pixel in the bottom 8 bits. static uint32_t compute_error(uint8_t pixel, uint8_t palette[8]) { int minIndex = 0; uint8_t error = abs_diff(palette[0], pixel); for (int i = 1; i < 8; ++i) { uint8_t diff = abs_diff(palette[i], pixel); if (diff < error) { minIndex = i; error = diff; } } uint16_t errSq = static_cast<uint16_t>(error) * static_cast<uint16_t>(error); SkASSERT(minIndex >= 0 && minIndex < 8); return (static_cast<uint32_t>(errSq) << 8) | static_cast<uint32_t>(minIndex); }
void debug() { if(InPlaceFFT) { myfft.fft_forward(in.data()); myfft.fft_backward(in.data()); } else { myfft.fft_forward(in.data(),out.data()); myfft.fft_backward(out.data(),in.data()); } int fft_dim=myfft(FFT_LENGTH); int howmany=myfft(FFT_NUMBER_OF_TRANSFORMS); real_type norm=1.0/static_cast<real_type>(fft_dim); real_type eps=10*numeric_limits<real_type>::epsilon(); in *= norm; for(int i=0; i<howmany; ++i) { cout << "State index = " << i << endl; for(int k=0; k<fft_dim; ++k) { //cout << target(i,k) << " " << sample[k] << endl; if(abs_diff(in(i,k),sample[k])>eps) cerr << "WRONG " << in(i,k) << " " << sample[k] << " " << endl; } } }
// Compress a block by using the bounding box of the pixels. It is assumed that // there are no extremal pixels in this block otherwise we would have used // compressBlockBBIgnoreExtremal. static uint64_t compress_latc_block_bb(const uint8_t pixels[]) { uint8_t minVal = 255; uint8_t maxVal = 0; for (int i = 0; i < kLATCPixelsPerBlock; ++i) { minVal = SkTMin(pixels[i], minVal); maxVal = SkTMax(pixels[i], maxVal); } SkASSERT(!is_extremal(minVal)); SkASSERT(!is_extremal(maxVal)); uint8_t palette[kLATCPaletteSize]; generate_latc_palette(palette, maxVal, minVal); uint64_t indices = 0; for (int i = kLATCPixelsPerBlock - 1; i >= 0; --i) { // Find the best palette index uint8_t bestError = abs_diff(pixels[i], palette[0]); uint8_t idx = 0; for (int j = 1; j < kLATCPaletteSize; ++j) { uint8_t error = abs_diff(pixels[i], palette[j]); if (error < bestError) { bestError = error; idx = j; } } indices <<= 3; indices |= idx; } return SkEndian_SwapLE64( static_cast<uint64_t>(maxVal) | (static_cast<uint64_t>(minVal) << 8) | (indices << 16)); }
/* * Returns true if we're sure to have found the definitive divider (ie * deviation == 0). */ static bool skl_wrpll_try_divider(struct skl_wrpll_context *ctx, uint64_t central_freq, uint64_t dco_freq, unsigned int divider) { uint64_t deviation; bool found = false; deviation = div64_u64(10000 * abs_diff(dco_freq, central_freq), central_freq); /* positive deviation */ if (dco_freq >= central_freq) { if (deviation < SKL_MAX_PDEVIATION && deviation < ctx->min_deviation) { ctx->min_deviation = deviation; ctx->central_freq = central_freq; ctx->dco_freq = dco_freq; ctx->p = divider; #if 0 found = true; #endif } /* we can't improve a 0 deviation */ if (deviation == 0) return true; /* negative deviation */ } else if (deviation < SKL_MAX_NDEVIATION && deviation < ctx->min_deviation) { ctx->min_deviation = deviation; ctx->central_freq = central_freq; ctx->dco_freq = dco_freq; ctx->p = divider; #if 0 found = true; #endif } if (found) { printf("Divider %d\n", divider); printf("Deviation %"PRIu64"\n", deviation); printf("dco_freq: %"PRIu64", dco_central_freq %"PRIu64"\n", dco_freq, central_freq); } return false; }
int main (){ int a,b,*p1,*p2; float f,*result; result=NULL; p1=&a; p2=&b; scanf("%d%d",&a,&b); printf("%d\n",abs_diff(p1,p2)); printf("%d\n",*bezwzgledna(p1,p2)); printf("%d %d\n",*p1,*p2); printf("\n\n%d\n\n",avg(a,b,result)); return 0; }
/** * \brief The interrupt monitor. * * For each registered interrupt, this function compares the counter with the * expected value. There is an error if the difference is greater than the limit * or if the interrupt is disabled and the counter is different than zero. * If \ref CLASSB_CONDITION2_INTERRUPT is true, the monitor will stop * immediately, i.e., the remaining interrupts are not checked. * * \note This should be called back from the RTC interrupt. See \ref rtc_driver. * * \callergraph */ void classb_intmon_callback(void) { for (uint8_t i = 0; i < N_INTERRUPTS; i++ ) { switch (monitored_interrupts[i].state) { case ON: /* Check if the counter is within the allowed range */ if ((uint16_t)abs_diff(monitored_interrupts[i].count, monitored_interrupts[i].expected) > monitored_interrupts[i].limit) { CLASSB_ERROR_HANDLER_INTERRUPT(); break; } /* Reset counter */ monitored_interrupts[i].count = 0; break; case OFF: /* The counter is only increased if the state is ON. */ if (monitored_interrupts[i].count) { CLASSB_ERROR_HANDLER_INTERRUPT(); } break; case M_ENABLE: /* Change state */ monitored_interrupts[i].state = ON; break; case M_DISABLE: /* Change state and reset the counter */ monitored_interrupts[i].state = OFF; monitored_interrupts[i].count = 0; break; default: CLASSB_ERROR_HANDLER_INTERRUPT(); } /* If CLASSB_CONDITION2_INTERRUPT is true, there is no need to * check the other interrupts. */ if (CLASSB_CONDITION2_INTERRUPT) { break; } } }
/** * \fn void beep_low_ms(uint16_t ms) * \brief Generate 2Khz sine signal for x miliseconds. * \param ms Number of miliseconds. * */ void beep_low_ms(uint16_t ms) { uint32_t count = ((uint32_t)ms*1000)/50; uint16_t counter = 50; uint32_t i; timer_reset(); for(i=0; i<count; i++) { DAC_SetChannel2Data(DAC_Align_12b_R, sine_samples[sine_samples_index]); while(abs_diff(counter,timer_get_value())<50) {} counter+=50; sine_samples_index++; if(sine_samples_index==SINE_SAMPLES_COUNT) sine_samples_index = 0; } }
// return true if the two signals are the same // within numerical tolerances // bool SIGNAL::roughly_equal(SIGNAL& s) { double second = 1.0/86400.0; // 1 second as a fraction of a day if (type != s.type) return false; // tolerances common to all signals if (abs_diff(ra, s.ra) > .00066) return false; // .01 deg if (abs_diff(decl, s.decl) > .01) return false; // .01 deg if (abs_diff(time, s.time) > second) return false; // 1 sec if (abs_diff(freq, s.freq) > .01) return false; // .01 Hz if (abs_diff(chirp_rate, s.chirp_rate) > .01) return false; // .01 Hz/s if (fft_len != s.fft_len) return false; // equal switch (type) { case SIGNAL_TYPE_SPIKE: case SIGNAL_TYPE_BEST_SPIKE: if (rel_diff(power, s.power) > .01) return false; // 1% return true; case SIGNAL_TYPE_GAUSSIAN: case SIGNAL_TYPE_BEST_GAUSSIAN: if (rel_diff(peak_power, s.peak_power) > .01) return false; // 1% if (rel_diff(mean_power, s.mean_power) > .01) return false; // 1% if (rel_diff(sigma, s.sigma) > .01) return false; // 1% if (rel_diff(chisqr, s.chisqr) > .01) return false; // 1% //if (rel_diff(max_power, s.max_power) > .01) return false; // ?? jeffc return true; case SIGNAL_TYPE_PULSE: case SIGNAL_TYPE_BEST_PULSE: if (rel_diff(power, s.power) > .01) return false; // 1% if (rel_diff(mean_power, s.mean_power) > .01) return false; // 1% if (abs_diff(period, s.period) > .01) return false; // .01 sec if (rel_diff(snr, s.snr) > .01) return false; // 1% if (rel_diff(thresh, s.thresh) > .01) return false; // 1% return true; case SIGNAL_TYPE_TRIPLET: case SIGNAL_TYPE_BEST_TRIPLET: if (rel_diff(power, s.power) > .01) return false; // 1% if (rel_diff(mean_power, s.mean_power) > .01) return false; // 1% if (rel_diff(period, s.period) > .01) return false; // 1% return true; case SIGNAL_TYPE_AUTOCORR: case SIGNAL_TYPE_BEST_AUTOCORR: if (rel_diff(power, s.power) > .01) return false; // 1% if (rel_diff(delay, s.delay) > .01) return false; // 1% return true; } return false; }
void feed_way(const osmium::Way& way) { try { const char* interpolation = way.tags().get_value_by_key("addr:interpolation"); if (interpolation) { std::unique_ptr<OGRLineString> ogr_linestring = m_factory.create_linestring(way); OGRFeature* feature = OGRFeature::CreateFeature(m_layer->GetLayerDefn()); const osmium::object_id_type first_node_id = m_geometry_helper.get_first_node_id(way); const osmium::object_id_type last_node_id = m_geometry_helper.get_last_node_id(way); feature->SetGeometry(ogr_linestring.get()); feature->SetField("way_id", static_cast<double>(way.id())); //TODO: node.id() is of type int64_t. is this ok? feature->SetField("typename", interpolation); feature->SetField("firstid", static_cast<double>(first_node_id)); //TODO: node.id() is of type int64_t. is cast do double ok? feature->SetField("lastid", static_cast<double>(last_node_id)); //TODO: node.id() is of type int64_t. is cast do double ok? feature->SetField("lastchange", way.timestamp().to_iso().c_str()); AltTagList first_taglist = m_addr_interpolation_node_map->get(first_node_id); AltTagList last_taglist = m_addr_interpolation_node_map->get(last_node_id); // the raw expression given in the first addr:housenumber= entry std::string first_raw = first_taglist.get_value_by_key("addr:housenumber"); // the raw expression given in the last addr:housenumber= entry std::string last_raw = last_taglist.get_value_by_key("addr:housenumber"); unsigned int first; unsigned int last; std::string first_numeric; // the numeric part of the first housenumber std::string last_numeric; // the numeric part of the last housenumber bool is_alphabetic_ip_correct = false; if (first_raw != "") { feature->SetField("firstno", first_raw.c_str()); first = atoi(first_raw.c_str()); } else { first = 0; } if (last_raw != "") { feature->SetField("lastno", last_raw.c_str()); last = atoi(last_raw.c_str()); } else { last = 0; } if (!strcmp(interpolation, "alphabetic") && // second last characters are numbers? !isalpha(first_raw[first_raw.length()-2]) && !isalpha(last_raw[last_raw.length()-2])){ // last characters are letters? if (isalpha(first_raw[first_raw.length()-1]) && isalpha(last_raw[last_raw.length()-1])) { first_numeric = std::string(first_raw.begin(), first_raw.end() - 1); last_numeric = std::string(last_raw.begin(), last_raw.end() - 1); if (first_numeric == last_numeric) { first = first_raw[first_raw.length() - 1]; last = last_raw[last_raw.length() - 1]; is_alphabetic_ip_correct = true; } else { is_alphabetic_ip_correct = false; feature->SetField("error", "numeric parts of housenumbers not identical"); } } else { is_alphabetic_ip_correct = false; feature->SetField("error", "no alphabetic part in addr:housenumber"); } } if (!( !strcmp(interpolation, "all") || !strcmp(interpolation, "even") || !strcmp(interpolation, "odd") || !strcmp(interpolation, "alphabetic"))) { feature->SetField("error", "unknown interpolation type"); } else if ( strcmp(interpolation, "alphabetic") && // don't overwrite more precise error messages set before (first == 0 || last == 0 || first_raw.length() != floor(log10(first))+1 || // make sure 123%& is not recognized as 123 last_raw.length() != floor(log10(last) )+1 // )) { feature->SetField("error", "endpoint has wrong format"); } else if (abs_diff(first,last) > 1000) { feature->SetField("error", "range too large"); } else if (((!strcmp(interpolation,"even") || !strcmp(interpolation,"odd")) && abs_diff(first,last)==2) || (!strcmp(interpolation,"all") && abs_diff(first,last)==1) ) { feature->SetField("error", "needless interpolation"); } else if (!strcmp(interpolation,"even") && ( first%2==1 || last%2==1 )) { feature->SetField("error", "interpolation even but number odd"); } else if (!strcmp(interpolation,"odd") && ( first%2==0 || last%2==0 )) { feature->SetField("error", "interpolation odd but number even"); } else if ( (first_taglist.get_value_by_key("addr:street") != last_taglist.get_value_by_key("addr:street")) || (first_taglist.get_value_by_key("addr:postcode") != last_taglist.get_value_by_key("addr:postcode")) || (first_taglist.get_value_by_key("addr:city") != last_taglist.get_value_by_key("addr:city")) || (first_taglist.get_value_by_key("addr:country") != last_taglist.get_value_by_key("addr:country")) || (first_taglist.get_value_by_key("addr:full") != last_taglist.get_value_by_key("addr:full")) || (first_taglist.get_value_by_key("addr:place") != last_taglist.get_value_by_key("addr:place")) ) { feature->SetField("error", "different tags on endpoints"); } else if (way.is_closed()) { feature->SetField("error", "interpolation is a closed way"); } else if ( // no interpolation error (!strcmp(interpolation, "all")) || (!strcmp(interpolation, "odd")) || (!strcmp(interpolation, "even")) || (is_alphabetic_ip_correct == true)) { double length = ogr_linestring.get()->get_Length(); int increment; if (strcmp(interpolation, "all") && strcmp(interpolation, "alphabetic")) { increment = 2; // even , odd } else { increment = 1; //all , alphabetic } double fraction; unsigned int lower, upper; if (first < last) { fraction = 1/static_cast<double>(last-first); lower = first; upper = last; } else { fraction = 1/static_cast<double>(first-last); increment *= -1; lower = last; upper = first; } for (unsigned int nr=first+increment; nr<upper && nr>lower; nr+=increment) { std::unique_ptr<OGRPoint> point (new OGRPoint); if (increment > 0) { ogr_linestring.get()->Value((nr-lower)*fraction*length, point.get()); } else { ogr_linestring.get()->Value((1-((nr-lower)*fraction))*length, point.get()); } std::string road_id(""); std::string nrstr; if(strcmp(interpolation, "alphabetic")) { nrstr = std::to_string(nr); } else { // is alphabetic // std::string strend = printf("%d", nr); nrstr = first_numeric + static_cast<char>(nr); } m_clpp.process_interpolated_node( // osmi_addresses_connection_line *(point.get()), road_id, first_taglist.get_value_by_key("addr:street") ); m_nwa_writer.process_interpolated_node( //osmi_addresses_nodes_with_addresses *(point.get()), nrstr, //nr, //std::to_string(nr), first_taglist.get_value_by_key("addr:street"), first_taglist.get_value_by_key("addr:postcode"), first_taglist.get_value_by_key("addr:city"), first_taglist.get_value_by_key("addr:country"), first_taglist.get_value_by_key("addr:full"), first_taglist.get_value_by_key("addr:place"), road_id ); } } create_feature(feature); } } catch (osmium::geometry_error& e) { catch_geometry_error(e, way); } }
static uint64_t lat_info_get_lat_tsc(struct lat_info *lat_info) { uint64_t lat = abs_diff(lat_info->rx_time, lat_info->tx_time); return lat << LATENCY_ACCURACY; }
static bool skl_ddi_calculate_wrpll1(int clock /* in Hz */, struct skl_wrpll_params *wrpll_params) { uint64_t afe_clock = clock * 5; /* AFE Clock is 5x Pixel clock */ uint64_t dco_central_freq[3] = {8400000000ULL, 9000000000ULL, 9600000000ULL}; uint32_t min_dco_pdeviation = 100; /* DCO freq must be within +1%/-6% */ uint32_t min_dco_ndeviation = 600; /* of the DCO central freq */ uint32_t min_dco_index = 3; uint32_t P0[4] = {1, 2, 3, 7}; uint32_t P2[4] = {1, 2, 3, 5}; bool found = false; uint32_t candidate_p = 0; uint32_t candidate_p0[3] = {0}, candidate_p1[3] = {0}; uint32_t candidate_p2[3] = {0}; uint32_t dco_central_freq_deviation[3]; uint32_t i, P1, k, dco_count; bool retry_with_odd = false; /* Determine P0, P1 or P2 */ for (dco_count = 0; dco_count < 3; dco_count++) { found = false; candidate_p = div64_u64(dco_central_freq[dco_count], afe_clock); if (retry_with_odd == false) candidate_p = (candidate_p % 2 == 0 ? candidate_p : candidate_p + 1); for (P1 = 1; P1 < candidate_p; P1++) { for (i = 0; i < 4; i++) { if (!(P0[i] != 1 || P1 == 1)) continue; for (k = 0; k < 4; k++) { if (P1 != 1 && P2[k] != 2) continue; if (candidate_p == P0[i] * P1 * P2[k]) { /* Found possible P0, P1, P2 */ found = true; candidate_p0[dco_count] = P0[i]; candidate_p1[dco_count] = P1; candidate_p2[dco_count] = P2[k]; goto found; } } } } found: if (found) { uint64_t dco_freq = candidate_p * afe_clock; #if 0 printf("Trying with (%d,%d,%d)\n", candidate_p0[dco_count], candidate_p1[dco_count], candidate_p2[dco_count]); #endif dco_central_freq_deviation[dco_count] = div64_u64(10000 * abs_diff(dco_freq, dco_central_freq[dco_count]), dco_central_freq[dco_count]); #if 0 printf("Deviation %d\n", dco_central_freq_deviation[dco_count]); printf("dco_freq: %"PRIu64", " "dco_central_freq %"PRIu64"\n", dco_freq, dco_central_freq[dco_count]); #endif /* positive deviation */ if (dco_freq > dco_central_freq[dco_count]) { if (dco_central_freq_deviation[dco_count] < min_dco_pdeviation) { min_dco_pdeviation = dco_central_freq_deviation[dco_count]; min_dco_index = dco_count; } /* negative deviation */ } else if (dco_central_freq_deviation[dco_count] < min_dco_ndeviation) { min_dco_ndeviation = dco_central_freq_deviation[dco_count]; min_dco_index = dco_count; } } if (min_dco_index > 2 && dco_count == 2) { /* oh well, we tried... */ if (retry_with_odd) break; retry_with_odd = true; dco_count = 0; } } if (min_dco_index > 2) { WARN(1, "No valid values found for the given pixel clock\n"); return false; } else { uint64_t dco_freq; wrpll_params->central_freq = dco_central_freq[min_dco_index]; switch (dco_central_freq[min_dco_index]) { case 9600000000ULL: wrpll_params->central_freq = 0; break; case 9000000000ULL: wrpll_params->central_freq = 1; break; case 8400000000ULL: wrpll_params->central_freq = 3; } switch (candidate_p0[min_dco_index]) { case 1: wrpll_params->pdiv = 0; break; case 2: wrpll_params->pdiv = 1; break; case 3: wrpll_params->pdiv = 2; break; case 7: wrpll_params->pdiv = 4; break; default: WARN(1, "Incorrect PDiv\n"); } switch (candidate_p2[min_dco_index]) { case 5: wrpll_params->kdiv = 0; break; case 2: wrpll_params->kdiv = 1; break; case 3: wrpll_params->kdiv = 2; break; case 1: wrpll_params->kdiv = 3; break; default: WARN(1, "Incorrect KDiv\n"); } wrpll_params->qdiv_ratio = candidate_p1[min_dco_index]; wrpll_params->qdiv_mode = (wrpll_params->qdiv_ratio == 1) ? 0 : 1; dco_freq = candidate_p0[min_dco_index] * candidate_p1[min_dco_index] * candidate_p2[min_dco_index] * afe_clock; /* * Intermediate values are in Hz. * Divide by MHz to match bsepc */ wrpll_params->dco_integer = div_u64(dco_freq, (24 * MHz(1))); wrpll_params->dco_fraction = div_u64(((div_u64(dco_freq, 24) - wrpll_params->dco_integer * MHz(1)) * 0x8000), MHz(1)); } /* for this unit test only */ wrpll_params->central_freq_hz = dco_central_freq[min_dco_index]; wrpll_params->p0 = candidate_p0[min_dco_index]; wrpll_params->p1 = candidate_p1[min_dco_index]; wrpll_params->p2 = candidate_p2[min_dco_index]; return true; }
params* staff_segment(const image_t *img, staff_info *staff){ int8_t range_f; float thrsh; uint16_t height; uint16_t width; uint16_t l; uint16_t i; uint16_t staffCounter,staffX,staffY; uint16_t length_staff_lines; uint16_t s_begin,s_end,fudge; int16_t addNum; projection_t* proj_onto_y; flex_array_t* crude_lines; flex_array_t* line_w; flex_array_t* diff_array; flex_array_t* minus_array_var; flex_array_t* compare_array; flex_array_t* kill_array; flex_array_t* less_crude_lines; flex_array_t* diff_lines; flex_array_t* test_lines; flex_array_t* top; flex_array_t* middle; flex_array_t* bottom; params* new_param; range_f=5; /*this code uses a projection to determine cuts for segmenting a music page into individual lines of music*/ height=img->height; width=img->width; /*projection on to vertical axis:*/ proj_onto_y = project(img , 2); /*calculate threshold:*/ thrsh = 0.42 * max_array(proj_onto_y); crude_lines = find(proj_onto_y,thrsh,greater); delete_flex_array(proj_onto_y); /*create array holding y values of all stafflines:*/ l = crude_lines->length; i = 0; length_staff_lines=0; /*find length of staff line array*/ while (i < l){ /*next staffline must be at least two pixels away:*/ while ( ((i+1)<l) && ((crude_lines->data[i]+1)==crude_lines->data[i+1]||((crude_lines->data[i]+2)==crude_lines->data[i+1]))){ i = i + 1; } length_staff_lines++; i = i + 1; } less_crude_lines=make_flex_array(length_staff_lines); line_w=make_flex_array(length_staff_lines); i=0; staffCounter=0; while (i < l){ s_begin = crude_lines->data[i]; /*next staffline must be at least two pixels away:*/ while ( ((i+1)<l) && ((crude_lines->data[i]+1)==crude_lines->data[i+1]||((crude_lines->data[i]+2)==crude_lines->data[i+1]))){ i = i + 1; } s_end = crude_lines->data[i]; /*add staffline to array:*/ less_crude_lines->data[staffCounter]=(s_begin+s_end+1)/2;/*round((s_begin + s_end)/2) works the same*/ line_w->data[staffCounter]=(s_end - s_begin+1);/*think this is necessary the +1 is new*/ i = i + 1; staffCounter++; } delete_flex_array(crude_lines); /*search for any incorrect lines*/ /*(check against others):*/ diff_lines=diff(less_crude_lines); fudge=median(diff_lines); delete_flex_array(diff_lines); l = less_crude_lines->length; kill_array=make_flex_array(l); for (i=0;i<l;i++){ kill_array->data[i]=0; } i = 0; while (i<=(l-5)){ test_lines = sub_array(less_crude_lines,i,i+4);/*staff_lines(i:i+4);*/ diff_array=abs_diff(test_lines); delete_flex_array(test_lines); minus_array_var=minus(diff_array,fudge); delete_flex_array(diff_array); compare_array=find(minus_array_var,fudge/5,greater); delete_flex_array(minus_array_var); if (compare_array){ kill_array->data[i]=1; i = i + 1; delete_flex_array(compare_array); } i = i + 5; } while (i<l){ kill_array->data[i]=1; i++; } /*for (i=0;i<l;i++){ if(kill_array->data[i]==0) printf("%d\n",less_crude_lines->data[i]); } system("PAUSE");*/ /*kill bad stafflines:*/ less_crude_lines=kill_array_indices(less_crude_lines,kill_array); delete_flex_array(kill_array); l=less_crude_lines->length; if(l%5){ fprintf(stderr,"Error, found stafflines not a multiple of 5"); exit(1); } staff->staff_lines=(uint16_t**)multialloc(sizeof(uint16_t),2,5,l/5); staff->staff_bounds=(uint16_t**)multialloc(sizeof(uint16_t),2,l/5,2); staff->number_staffs=l/5; staffX=0; staffY=0; for (i=0;i<l;i++){ staff->staff_lines[staffX][staffY]=less_crude_lines->data[i]; staffX++; if (staffX==5){ staffX=0; staffY++; } } delete_flex_array(less_crude_lines); /*calculate a good place to cut stafflines*/ top=get_line_at_index(staff,0); middle=get_line_at_index(staff,2); bottom=get_line_at_index(staff,4); diff_array=minus_array(bottom,top); delete_flex_array(top); delete_flex_array(bottom); new_param=malloc(sizeof(params)); new_param->staff_h=rounded_mean(diff_array); delete_flex_array(diff_array); range_f=((new_param->staff_h)*range_f+2)/4;/*should be same as round(range_f/2*mean(staff_lines(5, :) - staff_lines(1, :))); where range_f was 2.5*/ for (i=0;i<l/5;i++){ addNum=middle->data[i]-range_f; if(addNum<0) addNum=0; staff->staff_bounds[i][0]=addNum; addNum=middle->data[i]+range_f; if(addNum>=height) addNum=height-1; staff->staff_bounds[i][1]=addNum; } delete_flex_array(middle); /*music parameters */ new_param->thickness=rounded_mean(line_w); delete_flex_array(line_w); addNum=0; for (i=1;i<5;i++){ top=get_line_at_index(staff,i); bottom=get_line_at_index(staff,i-1); diff_array=minus_array(top,bottom); delete_flex_array(top); delete_flex_array(bottom); addNum+=sum(diff_array); delete_flex_array(diff_array); } addNum+=2*(staff->number_staffs); new_param->spacing=addNum/(4*(staff->number_staffs))-new_param->thickness;/*may introduce rounding errors*/ new_param->ks=0; new_param->ks_x=0; return new_param; }
void LC_bores::execComm(Document_Interface *doc, QWidget *parent, QString cmd) { Q_UNUSED(parent); Q_UNUSED(cmd); QList<Plug_Entity *> obj; /* will be filled with selected entities */ bool yes = doc->getSelect(&obj); if (!yes || obj.isEmpty()) return; liste1.clear(); int count_circles = 0; int j; for (j = 0; j < obj.size(); ++j) { QHash<int, QVariant> data2; /* element-daten aus librecad */ EntityData le; /* ziel fuer daten aus data2, gefiltert nach le.typ, unsortiert */ le.x1 = le.y1 = le.x2 = le.y2 = le.len = le.wi = le.r = le.w1 = le.w2 = le.ccx = le.ccy = 0; le.cw = 0; obj.at(j)->getData(&data2); le.typ = data2.value(DPI::ETYPE).toInt(); int found = 0; //specific entity data switch (le.typ) { case DPI::CIRCLE: case DPI::ARC: le.ccx = data2.value(DPI::STARTX).toDouble(); le.ccy = data2.value(DPI::STARTY).toDouble(); le.r = data2.value(DPI::RADIUS).toDouble(); // doubletten ? if(0 < liste1.size()) { for(int k=0; k<liste1.size(); ++k) { //std::cerr << k << " " << liste1[k].ccx << " " << liste1[k].ccy << " " << liste1[k].r << "\n"; // radius may be different if(0 == abs_diff(liste1[k].ccx, le.ccx) && 0 == abs_diff(liste1[k].ccy, le.ccy)) found++; } } if(0 == found) { liste1 << le; count_circles++; } break; default: break; } } /* for (j = 0; j < obj.size(); ++j) */ // write all values into a file (for postprocessor) QString filename(cad_to_cam_exch_file); QFile ofile(filename); if (! ofile.open(QIODevice::WriteOnly)) { std::cerr << "\n err bei file.open \n" ; } else { QTextStream t(&ofile); t << "programmtyp" << "\n" << "bohren" << "\n"; t << "listlength" << "\n" << count_circles << "\n"; for (int i = 0; i < count_circles; ++i) { t << "index" << "\n" << i << "\n"; t << "typ" << "\n" << liste1[i].typ << "\n"; t << "cw" << "\n" << liste1[i].cw << "\n"; t << "x1" << "\n" << liste1[i].x1 << "\n"; t << "y1" << "\n" << liste1[i].y1 << "\n"; t << "x2" << "\n" << liste1[i].x2 << "\n"; t << "y2" << "\n" << liste1[i].y2 << "\n"; t << "len" << "\n" << liste1[i].len << "\n"; t << "wi" << "\n" << liste1[i].wi << "\n"; t << "r" << "\n" << liste1[i].r << "\n"; t << "w1" << "\n" << liste1[i].w1 << "\n"; t << "w2" << "\n" << liste1[i].w2 << "\n"; t << "ccx" << "\n" << liste1[i].ccx << "\n"; t << "ccy" << "\n" << liste1[i].ccy << "\n"; // t << "" << "\n" << liste1[i]. << "\n"; } t << "ende" << "\n" << 0 << "\n"; ofile.close(); } while (!obj.isEmpty()) delete obj.takeFirst(); // ---------------------------- // start the "postprocessor": int ret = system(cam_exec_file); if(0) std::cerr << "\n cam returned: " << ret << "\n"; }