예제 #1
0
    double PureProjection::courseBetweenLatLng(PointLatLng const& p1,PointLatLng const& p2)
    {

        double lon1_R = p1.Lng() * DEG2RAD;
        double lat1_R = p1.Lat() * DEG2RAD;
        double lon2_R = p2.Lng() * DEG2RAD;
        double lat2_R = p2.Lat() * DEG2RAD;

        return TWO_PI - myfmod(atan2(sin(lon1_R - lon2_R) * cos(lat2_R),
                       cos(lat1_R) * sin(lat2_R) - sin(lat1_R) * cos(lat2_R) * cos(lon1_R - lon2_R)), TWO_PI);
    }
예제 #2
0
void blackman_square_interpolation(double *in, double *out, int32_t Mi, int32_t Mo, double *lut, int32_t lut_size)	// Interpolation based on an estimate of the Blackman Square function, which is a Blackman function convolved with a square. It's like smoothing the result of a nearest neighbour interpolation with a Blackman FIR
{
	int32_t i, j;		// general purpose iterators
	double pos_in;		// position in the original signal
	double x;		// position of the iterator in the blackman_square(x) formula
	double ratio;		// scaling ratio (> 1.0)
	double ratio_i;		// ratio^-1
	double coef;		// Blackman square final coefficient
	double pos_lut;		// Index on the look-up table
	int32_t pos_luti;	// Integer index on the look-up table
	double mod_pos;		// modulo of the position on the look-up table
	double y0, y1;		// values of the two closest values on the LUT
	double foo = (double) lut_size / 3.0;
	int32_t j_start, j_stop;	// boundary values for the j loop

	/*
	 * Mi is the original signal's length
	 * Mo is the output signal's length
	 */

	ratio = (double) Mi/Mo;
	ratio_i = 1.0/ratio;

	for (i=0; i<Mo; i++)
	{
		pos_in = (double) i * ratio;

		j_stop = pos_in + 1.5;

		j_start = j_stop - 2;
		if (j_start<0)
			j_start=0;

		if (j_stop >= Mi)		// The boundary check is done after j_start is calculated to avoid miscalculating it
			j_stop = Mi - 1;

		for (j=j_start; j<=j_stop; j++)
		{
			x = j - pos_in + 1.5;			// calculate position within the Blackman square function in the [0.0 ; 3.0] range
			pos_lut = x * foo;
			pos_luti = (int32_t) pos_lut;

			mod_pos = myfmod(pos_lut, 1.0);		// modulo of the index

			y0 = lut[pos_luti];		// interpolate linearly between the two closest values
			y1 = lut[pos_luti + 1];
			coef = y0 + mod_pos * (y1 - y0);	// linear interpolation

			out[i] += in[j] * coef;	// convolve
		}
	}
}
예제 #3
0
double R_pow(double x, double y) /* = x ^ y */
{
    if(x == 1. || y == 0.)
        return(1.);
    if(x == 0.) {
        if(y > 0.) return(0.);
        /* y < 0 */return(ML_POSINF);
    }
    if (R_FINITE(x) && R_FINITE(y))
        return(pow(x,y));
    if (ISNAN(x) || ISNAN(y)) {
#ifdef IEEE_754
        return(x + y);
#else
        return(ML_NAN);
#endif
    }
    if(!R_FINITE(x)) {
        if(x > 0)		/* Inf ^ y */
            return((y < 0.)? 0. : ML_POSINF);
        else {			/* (-Inf) ^ y */
            if(R_FINITE(y) && y == floor(y)) /* (-Inf) ^ n */
                return((y < 0.) ? 0. : (myfmod(y,2.) ? x  : -x));
        }
    }
    if(!R_FINITE(y)) {
        if(x >= 0) {
            if(y > 0)		/* y == +Inf */
                return((x >= 1)? ML_POSINF : 0.);
            else		/* y == -Inf */
                return((x < 1) ? ML_POSINF : 0.);
        }
    }
    return(ML_NAN);		/* all other cases: (-Inf)^{+-Inf,
				   non-int}; (neg)^{+-Inf} */
}
예제 #4
0
void settingsinput(int32_t *bands, int32_t samplecount, int32_t *samplerate, double *basefreq, double maxfreq, double *pixpersec, double *bandsperoctave, int32_t Xsize, int32_t mode)
{
	/* mode :
	 * 0 = Analysis mode
	 * 1 = Synthesis mode
	 */

	int32_t i;
	double gf, f, trash;
	double ma;			// maximum allowed frequency
	FILE *freqcfg;
	char byte;
	int32_t unset=0, set_min=0, set_max=0, set_bpo=0, set_y=0;			// count of unset interdependant settings
	int32_t set_pps=0, set_x=0;
	size_t filesize;		// boolean indicating if the configuration file's last expected byte is there (to verify the file's integrity)
	char conf_path[FILENAME_MAX];	// Path to the configuration file (only used on non-Windows platforms)

	#ifdef DEBUG
	printf("settingsinput...\n");
	#endif

	#ifdef WIN32
	freqcfg=fopen("arss.conf", "rb");					// open saved settings file
	#else
	sprintf(conf_path, "%s/%s", getenv("HOME"), ".arss.conf");
	freqcfg=fopen(conf_path, "rb");
	#endif

	if (*samplerate==0)					// if we're in synthesis mode and that no samplerate has been defined yet
	{
		if (quiet==1)
		{
			fprintf(stderr, "Please provide a sample rate for your output sound.\nUse --sample-rate (-r).\nExiting with error.\n");
			exit(EXIT_FAILURE);
		}
		//********Output settings querying********
	
		printf("Sample rate [44100] : ");			// Query for a samplerate
		*samplerate=getfloat();
		if (*samplerate==0 || *samplerate<-2147483647)		// The -2147483647 check is used for the sake of compatibility with C90
			*samplerate = 44100;				// Default value
		//--------Output settings querying--------
	}

	if (*basefreq!=0)	set_min=1;	// count unset interdependant frequency-domain settings
	if (maxfreq!=0)		set_max=1;
	if (*bandsperoctave!=0)	set_bpo=1;
	if (*bands!=0)		set_y=1;
	unset = set_min + set_max + set_bpo + set_y;

	if (unset==4)				// if too many settings are set
	{
		if (mode==0)
			fprintf(stderr, "You have set one parameter too many.\nUnset either --min-freq (-min), --max-freq (-max), --bpo (-b)\nExiting with error.\n");
		if (mode==1)
			fprintf(stderr, "You have set one parameter too many.\nUnset either --min-freq (-min), --max-freq (-max), --bpo (-b) or --height (-y)\nExiting with error.\n");
		exit(EXIT_FAILURE);
	}

	if (*pixpersec!=0)	set_pps=1;
	if (Xsize!=0)		set_x=1;

	if (set_x+set_pps==2 && mode==0)
	{
		fprintf(stderr, "You cannot define both the image width and the horizontal resolution.\nUnset either --pps (-p) or --width (-x)\nExiting with error.\n");
		exit(EXIT_FAILURE);
	}

	if (freqcfg)								// load settings from file if it exists
	{
		for (i=0; i<(int32_t) (4*sizeof(double)); i++)
			filesize=fread(&byte, sizeof(char), 1, freqcfg);	// verify the file's length
		rewind(freqcfg);
	}
	if (filesize==1)							// if the file is at least as long as expected
	{
		if (*basefreq==0)	fread(basefreq, sizeof(double), 1, freqcfg);		// load values from it if they haven't been set yet
		else			fread(&trash, sizeof(double), 1, freqcfg);
		if (maxfreq==0)		fread(&maxfreq, sizeof(double), 1, freqcfg);		// unless we have enough of them (unset==3)
		else			fread(&trash, sizeof(double), 1, freqcfg);
		if (*bandsperoctave==0)	fread(bandsperoctave, sizeof(double), 1, freqcfg);
		else			fread(&trash, sizeof(double), 1, freqcfg);
		if (*pixpersec==0)	fread(pixpersec, sizeof(double), 1, freqcfg);
		else			fread(&trash, sizeof(double), 1, freqcfg);
	}
	else
	{
		if (*basefreq==0)	*basefreq=27.5;				// otherwise load default values
		if (maxfreq==0)		maxfreq=20000;
		if (*bandsperoctave==0)	*bandsperoctave=12;
		if (*pixpersec==0)	*pixpersec=150;
	}
	if (freqcfg)
		fclose(freqcfg);

	if (unset<3 && set_min==0)
	{
		if (quiet==1)
		{
			fprintf(stderr, "Please define a minimum frequency.\nUse --min-freq (-min).\nExiting with error.\n");
			exit(EXIT_FAILURE);
		}
		printf("Min. frequency (Hz) [%.3f]: ", *basefreq);
		gf=getfloat();
		if (gf != 0)
			*basefreq=gf;
		unset++;
		set_min=1;
	}
	*basefreq /= *samplerate;	// turn basefreq from Hz to fractions of samplerate

	if (unset<3 && set_bpo==0)
	{
		if (quiet==1)
		{
			fprintf(stderr, "Please define a bands per octave setting.\nUse --bpo (-b).\nExiting with error.\n");
			exit(EXIT_FAILURE);
		}
		printf("Bands per octave [%.3f]: ", *bandsperoctave);
		gf=getfloat();
		if (gf != 0)
			*bandsperoctave=gf;
		unset++;
		set_bpo=1;
	}

	if (unset<3 && set_max==0)
	{
		i=0;
		do
		{
			i++;
			f=*basefreq * pow(LOGBASE, (i / *bandsperoctave));
		}
		while (f<0.5);
		
		ma=*basefreq * pow(LOGBASE, ((i-2) / *bandsperoctave)) * *samplerate;	// max allowed frequency
		
	
		if (maxfreq > ma)
			if (myfmod(ma, 1.0) == 0.0)
				maxfreq = ma;			// replaces the "Upper frequency limit above Nyquist frequency" warning
			else
				maxfreq = ma - myfmod(ma, 1.0);
	
		if (mode==0)					// if we're in Analysis mode
		{
			if (quiet==1)
			{
				fprintf(stderr, "Please define a maximum frequency.\nUse --max-freq (-max).\nExiting with error.\n");
				exit(EXIT_FAILURE);
			}
			printf("Max. frequency (Hz) (up to %.3f) [%.3f]: ", ma, maxfreq);
			gf=getfloat();
			if (gf != 0)
				maxfreq=gf;
		
			if (maxfreq > ma)
				if (myfmod(ma, 1.0) == 0.0)
					maxfreq = ma;		// replaces the "Upper frequency limit above Nyquist frequency" warning
				else
					maxfreq = ma - myfmod(ma, 1.0);
		}
		
		unset++;
		set_max=1;
	}

	if (set_min==0)
	{
		*basefreq = pow(LOGBASE, (*bands-1) / *bandsperoctave) * maxfreq;		// calculate the lower frequency in Hz
		printf("Min. frequency : %.3f Hz\n", *basefreq);
		*basefreq /= *samplerate;
	}

	if (set_max==0)
	{
		maxfreq = pow(LOGBASE, (*bands-1) / *bandsperoctave) * (*basefreq * *samplerate);	// calculate the upper frequency in Hz
		printf("Max. frequency : %.3f Hz\n", maxfreq);
	}

	if (set_y==0)
	{
		*bands = 1 + roundoff(*bandsperoctave * (log_b(maxfreq) - log_b(*basefreq * *samplerate)));
		printf("Bands : %d\n", *bands);
	}

	if (set_bpo==0)
	{
		if (LOGBASE==1.0)
			*bandsperoctave = maxfreq / *samplerate;
		else
			*bandsperoctave = (*bands-1) / (log_b(maxfreq) - log_b(*basefreq * *samplerate));
		printf("Bands per octave : %.3f\n", *bandsperoctave);
	}

	if (set_x==1 && mode==0)	// If we're in Analysis mode and that X is set (by the user)
	{
		*pixpersec = (double) Xsize * (double) *samplerate / (double) samplecount;	// calculate pixpersec
		printf("Pixels per second : %.3f\n", *pixpersec);
	}

	if ((mode==0 && set_x==0 && set_pps==0) || (mode==1 && set_pps==0))	// If in Analysis mode none are set or pixpersec isn't set in Synthesis mode
	{
		if (quiet==1)
		{
			fprintf(stderr, "Please define a pixels per second setting.\nUse --pps (-p).\nExiting with error.\n");
			exit(EXIT_FAILURE);
		}
		printf("Pixels per second [%.3f]: ", *pixpersec);
		gf=getfloat();
		if (gf != 0)
			*pixpersec=gf;
	}

	*basefreq *= *samplerate;		// turn back to Hz just for the sake of writing to the file

	#ifdef WIN32
	freqcfg=fopen("arss.conf", "wb");	// saving settings to a file
	#else
	freqcfg=fopen(conf_path, "wb");		// saving settings to a file
	#endif
	if (freqcfg==NULL)
	{
		fprintf(stderr, "Cannot write to configuration file");
		exit(EXIT_FAILURE);
	}
	fwrite(basefreq, sizeof(double), 1, freqcfg);
	fwrite(&maxfreq, sizeof(double), 1, freqcfg);
	fwrite(bandsperoctave, sizeof(double), 1, freqcfg);
	fwrite(pixpersec, sizeof(double), 1, freqcfg);
	fclose(freqcfg);

	*basefreq /= *samplerate;	// basefreq is now in fraction of the sampling rate instead of Hz
	*pixpersec /= *samplerate;	// pixpersec is now in fraction of the sampling rate instead of Hz
}