void AdjustSync::AutosyncOffset() { const float mean = calc_mean( s_fAutosyncOffset, s_fAutosyncOffset+OFFSET_SAMPLE_COUNT ); const float stddev = calc_stddev( s_fAutosyncOffset, s_fAutosyncOffset+OFFSET_SAMPLE_COUNT ); RString sAutosyncType; switch( GAMESTATE->m_SongOptions.GetCurrent().m_AutosyncType ) { case SongOptions::AUTOSYNC_SONG: sAutosyncType = AUTOSYNC_SONG; break; case SongOptions::AUTOSYNC_MACHINE: sAutosyncType = AUTOSYNC_MACHINE; break; default: ASSERT(0); } if( stddev < .03f ) // If they stepped with less than .03 error { switch( GAMESTATE->m_SongOptions.GetCurrent().m_AutosyncType ) { case SongOptions::AUTOSYNC_SONG: GAMESTATE->m_pCurSong->m_Timing.m_fBeat0OffsetInSeconds += mean; break; case SongOptions::AUTOSYNC_MACHINE: PREFSMAN->m_fGlobalOffsetSeconds.Set( PREFSMAN->m_fGlobalOffsetSeconds + mean ); break; default: ASSERT(0); } SCREENMAN->SystemMessage( AUTOSYNC_CORRECTION_APPLIED.GetValue() ); } else { SCREENMAN->SystemMessage( AUTOSYNC_CORRECTION_NOT_APPLIED.GetValue() ); } s_iAutosyncOffsetSample = 0; s_fStandardDeviation = stddev; }
void frameblob::calc_searchgrid() { #if FRAMEBLOB_DEBUG cout << "[frameblob debug]: in the calc_searchgrid method..." << endl; #endif // First calculate dimensions of the searchgrid array unsigned int xfull, yfull, xextra, yextra, xdim, ydim; xfull = xpix / grid; // # of full-sized cells yfull = ypix / grid; xextra = xpix % grid; // # extra pixels in small edge cells yextra = ypix % grid; xgrid = (xfull + (xextra>0)); // total # grid cells ygrid = (yfull + (yextra>0)); ngrid = (xgrid+xextra) * ygrid; // unallocate searchgrid and meangrid before we start in case we have // run this function several times delete[] searchgrid; delete[] meangrid; searchgrid = new double[ngrid]; // S/N of brightest pixel/grid cell meangrid = new double[ngrid]; // array of grid cell means // Allocate an array to contain all the GRID*GRID elements within this // grid cell of our input map MAPTYPE *cell; cell = new MAPTYPE[grid*grid]; MAPTYPE pix; // current pixel values MAPTYPE max; // the maximum value pixel in the cell int total; // running total elements in cell (big number) double meancell ; // cell mean double level; // max adjusted by centre value double sn; // signal to noise level of brightest pixel double x = 0; // x and y pixel positions for brightest pixels double y = 0; // for the large input map unsigned int i, j, k, l, mapindex, cellindex; int startx, endx, starty, endy; double thisflux; double M[D][D]; // small map centred over blob candidate double xcen, ycen, nap; int clip_startx, clip_endx, clip_starty, clip_endy; // int count=0; clearblobs(); // Make sure we don't have old blobs sitting around for( i=0; i<xgrid; i++ ) for( j=0; j<ygrid; j++ ) { // Sum all the elements within this cell of the input map. // Also make a list of the elements in the cell cellindex = 0; total = 0; max = 0; // Pixel dimensions of grid cell. grix*grid unless edge if( i==xfull ) xdim = xextra; else xdim = grid; if( j==yfull ) ydim = yextra; else ydim = grid; for( k=0; k<xdim; k++ ) for( l=0; l<ydim; l++ ) { mapindex = (j*grid+l)*xpix + (i*grid+k); pix = map[mapindex]; cell[cellindex] = pix; total += pix; // Check for greatest pixel brightness if( pix > max ) { x = (double) (i*grid + k); // store pixel coordinates y = (double) (j*grid + l); // of brightest pixel max = pix; } cellindex ++; // end of loop = # pixels in cell } // Get the mean for this cell meancell = (double) total / (cellindex); // Level is the brightness of a pixel - the mean level for the cell level = (double) max - meancell; // Calculate the sample variance about the central value if( mapmean == 0 ) calc_mapstat(); // Calculate the map mean if (stddev == 0) calc_stddev(); sigma = stddev; /* Don't use noise model any more, finding map standard deviation is fast enough and much better if( mapmean < readout_offset ) sigma = readout_noise; else sigma = (double)sqrt(gain*(mapmean-readout_offset) + readout_noise*readout_noise); // Poisson if( sigma < 1 ) sigma = 1; // prevent 0 sigmas */ sn = level/sigma; // Store information about this grid cell searchgrid[j*xgrid+i] = sn; // s/n brightest pixel in cell meangrid[j*xgrid+i] = meancell; // mean of this cell // If we got a pixel > threshold sigma, possibly a source if( sn >= threshold ) { //printf("%i ",count); //count ++; // -------------------------------------------------------------- // Decide if this is a single-point spike or a real source startx = (int) x - 1; // Aperture boundaries starty = (int) y - 1; endx = (int) x + 1; endy = (int) y + 1; if( startx < 0 ) startx = 0; // clipping map boundaries if( endx >= (int) xpix ) endx = xpix-1; if( starty < 0 ) starty = 0; if( endy >= (int) ypix ) endy = ypix-1; thisflux = 0.; nap = (double) (endx-startx+1)*(endy-starty+1); // pixels in ap. // add up flux centred over bright pixel for( k=(unsigned int)startx; k<=(unsigned int)endx; k++ ) for( l=(unsigned int)starty; l<=(unsigned int)endy; l++ ) thisflux += (double) map[l*xpix + k]; // remove flux centre pixel and check remaining flux // exceeds the theoretical flux from pure noise // (check extendedness) thisflux -= max; // remove the baseline for the remaining pixels thisflux -= ((nap-1)*meancell); // Extended case if( (thisflux/(sqrt(nap-1)*sigma)) >= threshold ) { // ------------------------------------------------------------ // Re-calculate the baseline over perimeter of a larger box startx = (int) x - D/2; // box boundary starty = (int) y - D/2; endx = startx + D-1; endy = starty + D-1; if( startx < 0 ) startx = 0; // clipping for boundaries if( endx >= (int) xpix ) endx = xpix-1; if( starty < 0 ) starty = 0; if( endy >= (int) ypix ) endy = ypix-1; meancell = 0; for( k=(unsigned int)startx; k<=(unsigned int)endx; k++ ) meancell += (double) map[starty*xpix + k] + (double) map[endy*xpix + k]; for( l=(unsigned int)starty+1; l<=(unsigned int)endy-1; l++ ) meancell += (double) map[l*xpix + startx] + (double) map[l*xpix + endx]; meancell /= ( 2*(endx-startx) + 2*(endy-starty) ); // ------------------------------------------------------------ // Centroid the blob startx = (int) x - D/2; starty = (int) y - D/2; endx = startx + D-1; endy = starty + D-1; if( startx < 0 ) clip_startx = 0; // clipping for boundaries else clip_startx = startx; if( endx >= (int) xpix ) clip_endx = xpix-1; else clip_endx = endx; if( starty < 0 ) clip_starty = 0; else clip_starty = starty; if( endy >= (int) ypix ) clip_endy = ypix-1; else clip_endy = endy; // boundary case if( (startx != clip_startx) || (starty != clip_starty) || (endx != clip_endx) || (endy != clip_endy) ) { // fill the sub-map with the mean value for( k=0; k<D; k++ ) for( l=0; l<D; l++ ) M[k][l] = (double) meancell; // paste in the useful part of the map for( k=(unsigned int)clip_startx-startx; k<(unsigned int)clip_endx-startx; k++ ) for( l=(unsigned int)clip_starty-starty; l<(unsigned int)clip_endy-starty; l++ ) M[k][l] = (double) map[(l+clip_starty)*xpix + k+clip_startx]; } // normal case else { for( k=0; k<D; k++ ) for( l=0; l<D; l++ ) M[k][l] = (double) map[(l+starty)*xpix + k+startx]; } // Find the centroid in this small map findCentre( &xcen, &ycen, &thisflux, M ); // Correct for the baseline and final check realness thisflux = thisflux - meancell; //printf("Flux: %lf Sigma: %lf\n", //thisflux,sqrt(convweight)*sigma); // Add to the list if significant if( thisflux/(sqrt(convweight)*sigma) >= threshold ) { addblob( (int)thisflux, x+xcen+0.5, y+ycen+0.5 ); thisblob->setmean(meancell); thisblob->setsnr( (double)thisflux / (sqrt(convweight)*sigma) ); } } // -------------------------------------------------------------- } } // clean up delete[] cell; }
void histogram_show(Histogram *hi) { int i; int *bins; double lines_per_count; char tmp[32]; size_t n_left, n_mid, n_right; calc_bins(hi); calc_stddev(hi); bins = malloc(hi->n_bins*sizeof(int)); if ( bins == NULL ) { fprintf(stderr, "Couldn't scale bins\n"); return; } lines_per_count = (double)hi->plot_height / hi->peak; for ( i=0; i<hi->n_bins; i++ ) { bins[i] = hi->bins[i] * lines_per_count; } for ( i=hi->plot_height-1; i>=0; i-- ) { int j; for ( j=0; j<hi->n_bins; j++ ) { if ( bins[j] > i ) { printf("*"); } else { printf(" "); } } printf("\n"); } printf("|"); for ( i=1; i<hi->n_bins-1; i++ ) { double bin_low, bin_high; bin_low = hi->min + i*hi->bin_width; bin_high = hi->min + (i+1)*hi->bin_width; if ( (bin_low < 0.0) && (bin_high > 0.0) ) { printf("+"); } else { printf("-"); } } printf("|\n"); snprintf(tmp, 31, "%.2f", hi->min); n_left = strlen(tmp); snprintf(tmp, 31, "%.2f", hi->max); n_right = strlen(tmp); n_mid = hi->n_bins - (n_left + n_right); printf("%.2f", hi->min); for ( i=0; i<n_mid; i++ ) printf(" "); printf("%.2f\n", hi->max); printf("Mean = %.2f, Std dev = %.2f\n", hi->mean, hi->stddev); free(bins); }