long __Call_arctg( CDSR_VMEval& /*vm*/, MMD_Address& addr, UniWord *arg ) { #if _DEBUG if( addr.param2 < 0 ) throw _T("__Call_(fun) : internal error, out of range"); #endif if( addr.param2 == 0 ) // DSRDATA_TYPE_REAL *(arg - 1) = CDSRReal( arctg( (arg - 1)->getReal() ) ); else // DSRDATA_TYPE_COMPLEX *(arg - 1) = CDSRComplex( arctg( (arg - 1)->getComplex() ) ); return 1 - addr.param3; }
gboolean rcm_gray_motion_notify_event (GtkWidget *widget, GdkEventMotion *event, RcmGray *circle) { gint x, y; GdkGCValues values; values.function = GDK_INVERT; xor_gc = gdk_gc_new_with_values (Current.From->preview->window, &values, GDK_GC_FUNCTION); if (circle->action_flag == DRAG_START) { GtkStyle *style = gtk_widget_get_style (circle->preview); gtk_widget_queue_draw (circle->preview); color_rotate_draw_large_circle (circle->preview->window, style->black_gc, circle->gray_sat); circle->action_flag = DRAGING; } else { color_rotate_draw_little_circle (widget->window, xor_gc, circle->hue, circle->satur); /* erase */ } x = event->x - GRAY_CENTER - LITTLE_RADIUS; y = GRAY_CENTER - event->y + LITTLE_RADIUS; circle->hue = angle_mod_2PI (arctg (y, x)); circle->satur = sqrt (SQR (x) + SQR (y)) / GRAY_RADIUS; if (circle->satur > 1.0) circle->satur = 1; color_rotate_draw_little_circle (widget->window, xor_gc, circle->hue, circle->satur); gtk_spin_button_set_value (GTK_SPIN_BUTTON (circle->hue_entry), circle->hue * rcm_units_factor(Current.Units)); gtk_spin_button_set_value (GTK_SPIN_BUTTON (circle->satur_entry), circle->satur); if (Current.RealTime) rcm_render_preview (Current.Bna->after); gdk_event_request_motions (event); return TRUE; }
gboolean rcm_gray_motion_notify_event (GtkWidget *widget, GdkEventMotion *event, RcmGray *circle) { gint x, y; gtk_widget_queue_draw (circle->preview); x = event->x - GRAY_CENTER - LITTLE_RADIUS; y = GRAY_CENTER - event->y + LITTLE_RADIUS; circle->hue = angle_mod_2PI (arctg (y, x)); circle->satur = sqrt (SQR (x) + SQR (y)) / GRAY_RADIUS; if (circle->satur > 1.0) circle->satur = 1; gtk_spin_button_set_value (GTK_SPIN_BUTTON (circle->hue_entry), circle->hue * rcm_units_factor(Current.Units)); gtk_spin_button_set_value (GTK_SPIN_BUTTON (circle->satur_entry), circle->satur); if (Current.RealTime) rcm_render_preview (Current.Bna->after); gdk_event_request_motions (event); return TRUE; }
gboolean rcm_button_press_event (GtkWidget *widget, GdkEventButton *event, RcmCircle *circle) { float clicked_angle; float *alpha; float *beta; alpha = &circle->angle->alpha; beta = &circle->angle->beta; circle->action_flag = DRAG_START; clicked_angle = angle_mod_2PI (arctg (CENTER - event->y, event->x - CENTER)); circle->prev_clicked = clicked_angle; if ((sqrt (SQR (event->y - CENTER) + SQR (event->x - CENTER)) > RADIUS * EACH_OR_BOTH) && (min_prox (*alpha, *beta, clicked_angle) < G_PI / 12)) { circle->mode = EACH; circle->target = closest (alpha, beta, clicked_angle); if (*(circle->target) != clicked_angle) { GtkStyle *style = gtk_widget_get_style (widget); *(circle->target) = clicked_angle; gtk_widget_queue_draw (circle->preview); color_rotate_draw_arrows (widget->window, style->black_gc, circle->angle); gtk_spin_button_set_value (GTK_SPIN_BUTTON (circle->alpha_entry), circle->angle->alpha * rcm_units_factor(Current.Units)); gtk_spin_button_set_value (GTK_SPIN_BUTTON (circle->beta_entry), circle->angle->beta * rcm_units_factor(Current.Units)); if (Current.RealTime) rcm_render_preview (Current.Bna->after); } } else circle->mode = BOTH; return TRUE; }
gboolean rcm_motion_notify_event (GtkWidget *widget, GdkEventMotion *event, RcmCircle *circle) { gfloat clicked_angle, delta; clicked_angle = angle_mod_2PI (arctg (CENTER - event->y, event->x - CENTER)); delta = clicked_angle - circle->prev_clicked; circle->prev_clicked = clicked_angle; if (delta) { if (circle->mode == EACH) { *(circle->target) = clicked_angle; } else { circle->angle->alpha = angle_mod_2PI (circle->angle->alpha + delta); circle->angle->beta = angle_mod_2PI (circle->angle->beta + delta); } gtk_widget_queue_draw (widget); gdk_window_process_updates (gtk_widget_get_window (widget), FALSE); gtk_spin_button_set_value (GTK_SPIN_BUTTON (circle->alpha_entry), circle->angle->alpha * rcm_units_factor(Current.Units)); gtk_spin_button_set_value (GTK_SPIN_BUTTON (circle->beta_entry), circle->angle->beta * rcm_units_factor(Current.Units)); if (Current.RealTime) rcm_render_preview (Current.Bna->after); } gdk_event_request_motions (event); return TRUE; }
int main() { int action = 0; char exitFlag = 0; while(exitFlag == 0){ printf("1) Add\n"); printf("2) Sub\n"); printf("3) Mul\n"); printf("4) Div\n"); printf("5) Mod\n"); printf("6) AddMod2\n"); printf("7) Sin\n"); printf("8) Cos\n"); printf("9) Tan\n"); printf("10) ArcTan\n"); printf("11) Factorial\n"); printf("12) Exit\n" ); printf("Action: "); scanf("%d", &action); getchar(); fflush(stdin); sync(); switch(action) { case(1): add(); break; case(2): sub_func(); break; case(3): multiplication(); break; case(4): Div(); break; case(5): mod(); break; case(6): modul2(); break; case(7): sinus(); break; case(8): my_cos(); break; case(9): tang(); break; case(10): arctg(); break; case(11): factorial(); break; case(12): exitFlag = 1; break; default: printf("Wrong index\n"); } } return 0; }
void rcm_render_circle (GtkWidget *preview, int sum, int margin) { gint i, j; gdouble h, s, v; guchar *a; if (preview == NULL) return; a = g_new (guchar, 3*sum*sum); for (j = 0; j < sum; j++) { for (i = 0; i < sum; i++) { s = sqrt ((SQR (i - sum / 2.0) + SQR (j - sum / 2.0)) / (float) SQR (sum / 2.0 - margin)); if (s > 1) { a[(j*sum+i)*3+0] = 255; a[(j*sum+i)*3+1] = 255; a[(j*sum+i)*3+2] = 255; } else { h = arctg (sum / 2.0 - j, i - sum / 2.0) / (2 * G_PI); v = 1 - sqrt (s) / 4; gimp_hsv_to_rgb4 (&a[(j*sum+i)*3], h, s, v); } } } gimp_preview_area_draw (GIMP_PREVIEW_AREA (preview), 0, 0, sum, sum, GIMP_RGB_IMAGE, a, sum * 3); g_free (a); }
gboolean rcm_gray_button_press_event (GtkWidget *widget, GdkEventButton *event, RcmGray *circle) { GtkStyle *style = gtk_widget_get_style (widget); int x, y; x = event->x - GRAY_CENTER - LITTLE_RADIUS; y = GRAY_CENTER - event->y + LITTLE_RADIUS; circle->action_flag = DRAG_START; circle->hue = angle_mod_2PI(arctg(y, x)); circle->satur = sqrt (SQR (x) + SQR (y)) / GRAY_RADIUS; if (circle->satur > 1.0) circle->satur = 1; gtk_widget_queue_draw (circle->preview); color_rotate_draw_little_circle (widget->window, style->black_gc, circle->hue, circle->satur); color_rotate_draw_large_circle (circle->preview->window, gtk_widget_get_style (circle->preview)->black_gc, circle->gray_sat); gtk_spin_button_set_value (GTK_SPIN_BUTTON (circle->hue_entry), circle->hue * rcm_units_factor (Current.Units)); gtk_spin_button_set_value (GTK_SPIN_BUTTON (circle->satur_entry), circle->satur); if (Current.RealTime) rcm_render_preview (Current.Bna->after); return TRUE; }
gboolean rcm_motion_notify_event (GtkWidget *widget, GdkEventMotion *event, RcmCircle *circle) { gfloat clicked_angle, delta; gfloat *alpha, *beta; gint cw_ccw; GdkGCValues values; alpha = &(circle->angle->alpha); beta = &(circle->angle->beta); cw_ccw = circle->angle->cw_ccw; delta = angle_mod_2PI (cw_ccw * (*beta - *alpha)); values.function = GDK_INVERT; xor_gc = gdk_gc_new_with_values (Current.From->preview->window, &values, GDK_GC_FUNCTION); clicked_angle = angle_mod_2PI (arctg (CENTER - event->y, event->x - CENTER)); delta = clicked_angle - circle->prev_clicked; circle->prev_clicked = clicked_angle; if (delta) { if (circle->action_flag == DRAG_START) { gtk_widget_queue_draw (circle->preview); circle->action_flag = DRAGING; } else { /* this should be erasing entire angle */ color_rotate_draw_arrows (widget->window, xor_gc, circle->angle); } if (circle->mode == EACH) { *(circle->target)=clicked_angle; } else { circle->angle->alpha=angle_mod_2PI(circle->angle->alpha + delta); circle->angle->beta =angle_mod_2PI(circle->angle->beta + delta); } gdk_window_process_updates (widget->window, FALSE); color_rotate_draw_arrows (widget->window, xor_gc, circle->angle); gtk_spin_button_set_value (GTK_SPIN_BUTTON (circle->alpha_entry), circle->angle->alpha * rcm_units_factor(Current.Units)); gtk_spin_button_set_value (GTK_SPIN_BUTTON (circle->beta_entry), circle->angle->beta * rcm_units_factor(Current.Units)); if (Current.RealTime) rcm_render_preview (Current.Bna->after); } gdk_event_request_motions (event); return TRUE; }
Pitch Sound_to_Pitch_shs (Sound me, double timeStep, double minimumPitch, double maximumFrequency, double ceiling, long maxnSubharmonics, long maxnCandidates, double compressionFactor, long nPointsPerOctave) { try { double firstTime, newSamplingFrequency = 2 * maximumFrequency; double windowDuration = 2 / minimumPitch, halfWindow = windowDuration / 2; double atans = nPointsPerOctave * NUMlog2 (65.0 / 50.0) - 1; // Number of speech samples in the downsampled signal in each frame: // 100 for windowDuration == 0.04 and newSamplingFrequency == 2500 long nx = lround (windowDuration * newSamplingFrequency); // The minimum number of points for the fft is 256. long nfft = 1; while ( (nfft *= 2) < nx || nfft <= 128) { ; } long nfft2 = nfft / 2 + 1; double frameDuration = nfft / newSamplingFrequency; double df = newSamplingFrequency / nfft; // The number of points on the octave scale double fminl2 = NUMlog2 (minimumPitch), fmaxl2 = NUMlog2 (maximumFrequency); long nFrequencyPoints = (long) floor ((fmaxl2 - fminl2) * nPointsPerOctave); double dfl2 = (fmaxl2 - fminl2) / (nFrequencyPoints - 1); autoSound sound = Sound_resample (me, newSamplingFrequency, 50); long numberOfFrames; Sampled_shortTermAnalysis (sound.peek(), windowDuration, timeStep, &numberOfFrames, &firstTime); autoSound frame = Sound_createSimple (1, frameDuration, newSamplingFrequency); autoSound hamming = Sound_createHamming (nx / newSamplingFrequency, newSamplingFrequency); autoPitch thee = Pitch_create (my xmin, my xmax, numberOfFrames, timeStep, firstTime, ceiling, maxnCandidates); autoNUMvector<double> cc (1, numberOfFrames); autoNUMvector<double> specAmp (1, nfft2); autoNUMvector<double> fl2 (1, nfft2); autoNUMvector<double> yv2 (1, nfft2); autoNUMvector<double> arctg (1, nFrequencyPoints); autoNUMvector<double> al2 (1, nFrequencyPoints); Melder_assert (frame->nx >= nx); Melder_assert (hamming->nx == nx); // Compute the absolute value of the globally largest amplitude w.r.t. the global mean. double globalMean, globalPeak; Sound_localMean (sound.peek(), sound -> xmin, sound -> xmax, &globalMean); Sound_localPeak (sound.peek(), sound -> xmin, sound -> xmax, globalMean, &globalPeak); /* For the cubic spline interpolation we need the frequencies on an octave scale, i.e., a log2 scale. All frequencies must be DIFFERENT, otherwise the cubic spline interpolation will give corrupt results. Because log2(f==0) is not defined, we use the heuristic: f[2]-f[1] == f[3]-f[2]. */ for (long i = 2; i <= nfft2; i++) { fl2[i] = NUMlog2 ( (i - 1) * df); } fl2[1] = 2 * fl2[2] - fl2[3]; // Calculate frequencies regularly spaced on a log2-scale and // the frequency weighting function. for (long i = 1; i <= nFrequencyPoints; i++) { arctg[i] = 0.5 + atan (3 * (i - atans) / nPointsPerOctave) / NUMpi; } // Perform the analysis on all frames. for (long i = 1; i <= numberOfFrames; i++) { Pitch_Frame pitchFrame = &thy frame[i]; double hm = 1, f0, pitch_strength, localMean, localPeak; double tmid = Sampled_indexToX (thee.peek(), i); /* The center of this frame */ long nx_tmp = frame -> nx; // Copy a frame from the sound, apply a hamming window. Get local 'intensity' frame -> nx = nx; /*begin vies */ Sound_into_Sound (sound.peek(), frame.peek(), tmid - halfWindow); Sounds_multiply (frame.peek(), hamming.peek()); Sound_localMean (sound.peek(), tmid - 3 * halfWindow, tmid + 3 * halfWindow, &localMean); Sound_localPeak (sound.peek(), tmid - halfWindow, tmid + halfWindow, localMean, &localPeak); pitchFrame -> intensity = localPeak > globalPeak ? 1 : localPeak / globalPeak; frame -> nx = nx_tmp; /* einde vies */ // Get the Fourier spectrum. autoSpectrum spec = Sound_to_Spectrum (frame.peek(), 1); Melder_assert (spec->nx == nfft2); // From complex spectrum to amplitude spectrum. for (long j = 1; j <= nfft2; j++) { double rs = spec -> z[1][j], is = spec -> z[2][j]; specAmp[j] = sqrt (rs * rs + is * is); } // Enhance the peaks in the spectrum. spec_enhance_SHS (specAmp.peek(), nfft2); // Smooth the enhanced spectrum. spec_smoooth_SHS (specAmp.peek(), nfft2); // Go to a logarithmic scale and perform cubic spline interpolation to get // spectral values for the increased number of frequency points. NUMspline (fl2.peek(), specAmp.peek(), nfft2, 1e30, 1e30, yv2.peek()); for (long j = 1; j <= nFrequencyPoints; j++) { double f = fminl2 + (j - 1) * dfl2; NUMsplint (fl2.peek(), specAmp.peek(), yv2.peek(), nfft2, f, &al2[j]); } // Multiply by frequency selectivity of the auditory system. for (long j = 1; j <= nFrequencyPoints; j++) al2[j] = al2[j] > 0 ? al2[j] * arctg[j] : 0; // The subharmonic summation. Shift spectra in octaves and sum. Pitch_Frame_init (pitchFrame, maxnCandidates); autoNUMvector<double> sumspec (1, nFrequencyPoints); pitchFrame -> nCandidates = 0; /* !!!!! */ for (long m = 1; m <= maxnSubharmonics + 1; m++) { long kb = 1 + (long) floor (nPointsPerOctave * NUMlog2 (m)); for (long k = kb; k <= nFrequencyPoints; k++) { sumspec[k - kb + 1] += al2[k] * hm; } hm *= compressionFactor; } // First register the voiceless candidate (always present). Pitch_Frame_addPitch (pitchFrame, 0, 0, maxnCandidates); /* Get the best local estimates for the pitch as the maxima of the subharmonic sum spectrum by parabolic interpolation on three points: The formula for a parabole with a maximum is: y(x) = a - b (x - c)^2 with a, b, c >= 0 The three points are (-x, y1), (0, y2) and (x, y3). The solution for a (the maximum) and c (the position) is: a = (2 y1 (4 y2 + y3) - y1^2 - (y3 - 4 y2)^2)/( 8 (y1 - 2 y2 + y3) c = dx (y1 - y3) / (2 (y1 - 2 y2 + y3)) (b = (2 y2 - y1 - y3) / (2 dx^2) ) */ for (long k = 2; k <= nFrequencyPoints - 1; k++) { double y1 = sumspec[k - 1], y2 = sumspec[k], y3 = sumspec[k + 1]; if (y2 > y1 && y2 >= y3) { double denum = y1 - 2 * y2 + y3, tmp = y3 - 4 * y2; double x = dfl2 * (y1 - y3) / (2 * denum); double f = pow (2, fminl2 + (k - 1) * dfl2 + x); double strength = (2 * y1 * (4 * y2 + y3) - y1 * y1 - tmp * tmp) / (8 * denum); Pitch_Frame_addPitch (pitchFrame, f, strength, maxnCandidates); } } /* Check whether f0 corresponds to an actual periodicity T = 1 / f0: correlate two signal periods of duration T, one starting at the middle of the interval and one starting T seconds before. If there is periodicity the correlation coefficient should be high. However, some sounds do not show any regularity, or very low frequency and regularity, and nevertheless have a definite pitch, e.g. Shepard sounds. */ Pitch_Frame_getPitch (pitchFrame, &f0, &pitch_strength); if (f0 > 0) { cc[i] = Sound_correlateParts (sound.peek(), tmid - 1.0 / f0, tmid, 1.0 / f0); } } // Base V/UV decision on correlation coefficients. // Resize the pitch strengths w.r.t. the cc. double vuvCriterium = 0.52; for (long i = 1; i <= numberOfFrames; i++) { Pitch_Frame_resizeStrengths (& thy frame[i], cc[i], vuvCriterium); } return thee.transfer(); } catch (MelderError) { Melder_throw (me, U": no Pitch (shs) created."); } }