static void render_tile(unsigned char *img, int comps, float *fimg, int w, int h, int nsubsamples, int tilex, int tiley, int tilew, int tileh) { int endx = tilex + min_i(tilew, w - tilex); int endy = tiley + min_i(tileh, h - tiley); int x, y; int u, v; for (y = tiley; y < endy; y++) { for (x = tilex; x < endx; x++) { for (v = 0; v < nsubsamples; v++) { for (u = 0; u < nsubsamples; u++) { float px = (x + (u / (float)nsubsamples) - (w / 2.0)) / (w / 2.0); float py = -(y + (v / (float)nsubsamples) - (h / 2.0)) / (h / 2.0); Ray ray; ray.org.x = 0.0; ray.org.y = 0.0; ray.org.z = 0.0; ray.dir.x = px; ray.dir.y = py; ray.dir.z = -1.0; vnormalize(&(ray.dir)); Isect isect; isect.t = 1.0e+17; isect.hit = 0; ray_sphere_intersect(&isect, &ray, &spheres[0]); ray_sphere_intersect(&isect, &ray, &spheres[1]); ray_sphere_intersect(&isect, &ray, &spheres[2]); ray_plane_intersect (&isect, &ray, &plane); if (isect.hit) { vec col; ambient_occlusion(&col, &isect); fimg[3 * (y * w + x) + 0] += col.x; fimg[3 * (y * w + x) + 1] += col.y; fimg[3 * (y * w + x) + 2] += col.z; } } } fimg[3 * (y * w + x) + 0] /= (float)(nsubsamples * nsubsamples); fimg[3 * (y * w + x) + 1] /= (float)(nsubsamples * nsubsamples); fimg[3 * (y * w + x) + 2] /= (float)(nsubsamples * nsubsamples); img[comps * (y * w + x) + 0] = clamp(fimg[3 *(y * w + x) + 0]); img[comps * (y * w + x) + 1] = clamp(fimg[3 *(y * w + x) + 1]); img[comps * (y * w + x) + 2] = clamp(fimg[3 *(y * w + x) + 2]); } } }
/* * Intersection area of two axis-aligned bounding boxes. Negative if they do not * intersect. */ int bb_overlap_area(const BB *a, const BB *b) { int wi, hi; assert(a != NULL && bb_valid(*a)); assert(b != NULL && bb_valid(*b)); wi = min_i(4, a->r - b->l, b->r - a->l, a->r - a->l, b->r - b->l); hi = min_i(4, a->t - b->b, b->t - a->b, a->t - a->b, b->t - b->b); return (wi < 0.0 || hi < 0.0) ? -abs(wi * hi) : wi * hi; }
void reassignment_frequency_correction(sample_t *samples, index_t n_samples, index_t frame_length, index_t fft_length, double sampling_rate, double correct_frequency) { double binwidth = sampling_rate*(1.0/(fft_length/2.0))*(1/2.0); frame_length = min_i(frame_length, min_i(fft_length, n_samples)); int n_bins = (fft_length+1)/2; double window[fft_length]; double frequency_window[fft_length]; double window_correction = 0,frequency_window_correction = 0; for (int k = 0; k < fft_length; k++) { window[k] = 0.5*(1-cos(2*M_PI*(k+0.5)/fft_length)); window_correction += window[k]*window[k]; dp(31, "window[%d]=%g\n", k, window[k]); } for (int k = 1; k < fft_length-1; k++) frequency_window[k] = 0.5*(window[k+1]-window[k-1]); frequency_window[0] = 0.5*window[1]; frequency_window[fft_length-1] = -0.5*window[fft_length-2]; for (int k = 0; k < fft_length; k++) frequency_window_correction += frequency_window[k]*frequency_window[k]; double frequency_window1[fft_length]; for (int k = 0; k < fft_length; k++) frequency_window1[k] = sin(2*M_PI*(k+0.5)/fft_length); // gnuplotf("width=1200 height=1000 rows=2 title='hann' length=%d %lf;title='frequency' %lf title='frequency1' %lf", fft_length, window, frequency_window,frequency_window1);exit(0); double in[fft_length+2]; for (int k = 0; k < fft_length; k++) in[k] = window[k]*samples[k]; fftw_complex out[fft_length+2]; fftw_execute(fftw_plan_dft_r2c_1d(fft_length, in, out, FFTW_ESTIMATE)); for (int k = 0; k < fft_length; k++) in[k] = frequency_window[k]*samples[k]; fftw_complex frequency_out[fft_length+2]; fftw_execute(fftw_plan_dft_r2c_1d(fft_length, in, frequency_out, FFTW_ESTIMATE)); // gnuplotf("width=1200 height=1000 rows=2 title='hann' length=%d %lf;title='frequency' %lf", fft_length, out, frequency_out); double power[n_bins] ; for (int k = 0; k < n_bins; k++) { double real = out[k][0]; double imaginary = out[k][1]; power[k] = (real*real + imaginary*imaginary); } for (int bin = 1; bin < n_bins-1; bin++) { if (power[bin] < 0.0001) continue; if ((bin > 0 && power[bin-1] >= power[bin]) || (bin < n_bins - 1 && power[bin+1] >= power[bin])) continue; //calculate complex conjugate for complex division double deviation = -(frequency_out[bin][1] *out[bin][0] - frequency_out[bin][0] * out[bin][1]); deviation *= 2*sqrt(frequency_window_correction)/sqrt(window_correction); printf("reassignment bin=%d amplitude=%g frequency=%g deviation=%g\n", bin, sqrt(power[bin]), ((double)bin + deviation) * binwidth, deviation); printf("error=%g\n", deviation*binwidth/(correct_frequency-bin*binwidth)); } }
static void loggerwindow_do_draw(LoggerWindow *lw) { int i, ndisplines, startline; int sb_rect[2][2], sb_thumb[2][2]; GHOST_ActivateWindowDrawingContext(lw->win); glClearColor(1, 1, 1, 1); glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.8, 0.8, 0.8); rect_bevel_smooth(lw->textarea, 4); scrollbar_get_rect(lw->scroll, sb_rect); scrollbar_get_thumb(lw->scroll, sb_thumb); glColor3f(0.6, 0.6, 0.6); rect_bevel_smooth(sb_rect, 1); if (scrollbar_is_scrolling(lw->scroll)) { glColor3f(0.6, 0.7, 0.5); } else { glColor3f(0.9, 0.9, 0.92); } rect_bevel_smooth(sb_thumb, 1); startline= scrollbar_get_thumbpos(lw->scroll)*(lw->nloglines-1); ndisplines= min_i(lw->ndisplines, lw->nloglines-startline); if (lw->fonttexid!=-1) { glBindTexture(GL_TEXTURE_2D, lw->fonttexid); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); glEnable(GL_TEXTURE_2D); } glColor3f(0, 0, 0); for (i=0; i<ndisplines; i++) { /* stored in reverse order */ char *line= lw->loglines[(lw->nloglines-1)-(i+startline)]; int x_pos= lw->textarea[0][0] + 4; int y_pos= lw->textarea[0][1] + 4 + i*lw->fontheight; if (lw->fonttexid==-1) { glRasterPos2i(x_pos, y_pos); BMF_DrawString(lw->font, line); } else { BMF_DrawStringTexture(lw->font, line, x_pos, y_pos, 0.0); } } if (lw->fonttexid!=-1) { glDisable(GL_TEXTURE_2D); glDisable(GL_BLEND); } GHOST_SwapWindowBuffers(lw->win); }
int clamp_i(int val, int min, int max) { return min_i(max_i(val, min), max); }
/* * returns sinusoids in decreasing order of amplitude */ int estimate_sinusoid_parameters(int n_bins, double power[n_bins], double phase[n_bins], double next_phase[n_bins], int overlap, double threshold, int frequency_correction_method, int max_sinusoids, sinusoid_t sinusoids[max_sinusoids]) { double local_maxima[n_bins], peak_energy[n_bins], peak_frequency[n_bins], **sorted_energies; int n_sinsuoids, i, n_peaks; double power_dominant, amplitude_dominant; double hop = 1.0/overlap; dp(13, "estimate_sinusoid_parameters(n_bins =%d max_sinusoids=%d threshold=%g)\n", n_bins, max_sinusoids, threshold); memset(local_maxima, 0, sizeof local_maxima); power_dominant = 0; amplitude_dominant = 0; for (i = 1; i < n_bins - 1; i++) { if (power[i] > 0 && power[i] >= power[i - 1] && power[i] >= power[i + 1]) { local_maxima[i] = 1; if (power[i] >= power_dominant) power_dominant = power[i]; } } // for (i = 0; i < n_bins; i++) // dp(31, "power[%d]=%g\n", i, power[i]); dp(25, "uncorrected power_dominant=%g (uncorrected)\n", power_dominant); n_peaks = 0; for (i = 2; i < n_bins - 1; i++) { int start = i; if (!local_maxima[i]) continue; for (; local_maxima[i]; i++) local_maxima[i] = 0; int bin = (start + i)/2; dp(29, "power[%d]=%g\n", bin,power[bin]); if (power[bin] < power_dominant * (threshold*threshold)*0.5) continue; int use_parabolic_interpolation = frequency_correction_method == 1; double frequency_delta = 0; if (frequency_correction_method == 0) { double raw_phase_diff = next_phase[bin] - phase[bin]; double phase_diff = raw_phase_diff - bin * 2 * M_PI * hop; dp(31, "phase_diff=%g\n", phase_diff); double phase_diff_div_pi = phase_diff/M_PI; double x = fabs(phase_diff-((int)phase_diff_div_pi)*M_PI); dp(31, "x=%g\n", x); // phase difference corrections seems unstable where x ~= 1 if (x > 0.99 || x < 0.01) use_parabolic_interpolation = 1; else { int piSigner = phase_diff_div_pi; if (piSigner >= 0) piSigner += piSigner & 1; else piSigner -= piSigner & 1; phase_diff -= M_PI * (double)piSigner; frequency_delta = (overlap * phase_diff)/(2*M_PI); dp(31, "raw_phase_diff=%g expect_phased_diff=%g raw_phase_diff/pi=%g corrected phase diff=%g frequency_delta=%g\n", raw_phase_diff, bin * 2 * M_PI * hop, raw_phase_diff/M_PI, phase_diff, frequency_delta); } } if (use_parabolic_interpolation) frequency_delta = parabolic_frequency_interpolation(power, bin); peak_frequency[n_peaks] = bin + frequency_delta; /* * amplitude correction from http://www.unibw-hamburg.de/EWEB/ANT/dafx2002/papers/DAFX02_Keiler_Marchand_sine_extract_compare.pdf */ double uncorrected_amplitude = 2*sqrt(power[bin]/(2*n_bins)); double amplitude_correction = w(2*n_bins,M_PI*frequency_delta/n_bins)/n_bins; peak_energy[n_peaks] = amplitude_correction*2*sqrt(power[bin]/(2*n_bins)); double corrected_power = parabolic_amplitude_interpolation(power, bin); dp(11, "parabolic=%g\n", 2*sqrt(corrected_power/(2*n_bins))); peak_energy[n_peaks] = 2*sqrt(corrected_power/(2*n_bins)); dp(11, "bin=%d frequency_delta=%g uncorrected amplitude=%g amplitude_correction=%g corrected amplitude=%g\n", bin, frequency_delta, uncorrected_amplitude, amplitude_correction, peak_energy[n_peaks]); if (peak_energy[n_peaks] > amplitude_dominant) amplitude_dominant = peak_energy[n_peaks]; /* power_dominant is uncorrected */ n_peaks++; } sorted_energies = quicksort_double_indices(peak_energy, n_peaks); if (verbosity > 21) { for (i = 0; i < min_i(10, n_peaks); i++) fprintf(debug_stream, "i=%d j=%d energy=%g\n", i, sorted_energies[n_peaks - (i+1)] - &peak_energy[0], peak_energy[sorted_energies[n_peaks - (i+1)] - &peak_energy[0]]); } n_sinsuoids = min_i(max_sinusoids, n_peaks); for (i = 0; i < n_sinsuoids; i++) { int j = sorted_energies[n_peaks - (i+1)] - &peak_energy[0]; dp(23, "i=%d j=%d frequency=%g peak_energy[%d]=%g amplitude_dominant=%g\n", i, j, peak_frequency[j], j, peak_energy[j], amplitude_dominant); if (peak_energy[j] < amplitude_dominant*threshold) { dp(23, "ignoring this and further peaks\n"); break; } if (sinusoids) { sinusoids[i].frequency = peak_frequency[j]; sinusoids[i].amplitude = peak_energy[j]; sinusoids[i].phase = phase[(int)peak_frequency[j]]; } } g_free(sorted_energies); return i; }