Ejemplo n.º 1
0
void gen_geo_graph(char graph[MAX_NR_VERTICES][MAX_NR_VERTICESdiv8],
		char block[MAX_NR_VERTICES][MAX_NR_VERTICESdiv8],
		long order,
		double dist,
		int dim,
		long part,
		int wrap,
		int dflag,
		long *size,
		FILE *file
		)
{

	long i,j,k;
	double d;
	double *vert;

	printf("Generating a geometric graph on %ld vertices.\n\
Distance is %f.\n\
Dimension of %d\n\
There is a %ld colouring\n",order,sqrt(dist),dim,part);
	vert = (double *) malloc(sizeof(double)*dim*order);
	*size = 0;

	if(part > order) part = order;

	/* to randomize assign edges in random order */
	/* Still not completely random order - all edges to a vertex are
	   assigned at once - how to do this without too much memory?? */
	for (i=0;i<dim;i++) 
	  vert[i] = dblrand();

	for(i=1;i<order;i++) {
	  for (j=0;j<dim;j++) 
	    vert[i*dim+j] = dblrand();
	  for (j=0;j<i;j++) {
	    d = distance(vert,dim,i,j,dim-1,wrap);
	    if ( (dflag && (d <= dist) && (!get_edge(i,j,block)))
			|| (!dflag && (d > dist) && (!get_edge(i,j,block)))) {
	      set_edge(i,j,1,graph);
	      (*size)++;
	      if (file != NULL) {
		for(k=0;k<dim;k++)
		  fprintf(file,"%14.12f ",vert[i*dim+k]);
		fprintf(file,"\n");
		for(k=0;k<dim;k++)
		  fprintf(file,"%14.12f ",vert[j*dim+k]);
		fprintf(file,"\n\n");
	      }
	    }
	  }
	}
}
Ejemplo n.º 2
0
double *synt_noise(double **d, int32_t Xsize, int32_t bands, int32_t *samplecount, int32_t samplerate, double basefreq, double pixpersec, double bpo)
{
	int32_t	i;			// general purpose iterator
	int32_t	ib;			// bands iterator
	int32_t	il;			// loop iterator
	double	*s;			// final signal
	double	coef;
	double	*noise;			// filtered looped noise
	double	loop_size_sec=LOOP_SIZE_SEC;	// size of the filter bank loop, in seconds. Later to be taken from user input
	int32_t	loop_size;		// size of the filter bank loop, in samples. Deduced from loop_size_sec
	int32_t	loop_size_min;		// minimum required size for the filter bank loop, in samples. Calculated from the longest windowed sinc's length
	double	*pink_noise;		// original pink noise (in the frequency domain)
	double	mag, phase;		// parameters for the creation of pink_noise's samples
	double	*envelope;		// interpolated envelope
	double	*lut;			// Blackman Sqaure look-up table

	double	*freq;		// frequency look-up table
	double	maxfreq;	// central frequency of the last band
	int32_t	Fa;		// Fa is the index of the band's start in the frequency domain
	int32_t	Fd;		// Fd is the index of the band's end in the frequency domain
	double	La;		// La is the log2 of the frequency of Fa
	double	Ld;		// Ld is the log2 of the frequency of Fd
	double	Li;		// Li is the iterative frequency between La and Ld defined logarithmically

	freq = freqarray(basefreq, bands, bpo);

	if (LOGBASE==1.0)
		maxfreq = bpo;	// in linear mode we use bpo to store the maxfreq since we couldn't deduce maxfreq otherwise
	else
		maxfreq = basefreq * pow(LOGBASE, ((double) (bands-1)/ bpo));

	clocka=gettime();

	*samplecount = roundoff(Xsize/pixpersec);		// calculation of the length of the final signal
	printf("Sound duration : %.3f s\n", (double) *samplecount/samplerate);

	s = calloc (*samplecount, sizeof(double));		// allocation of the final signal
	envelope = calloc (*samplecount, sizeof(double));	// allocation of the interpolated envelope

	//********Loop size calculation********

	loop_size = loop_size_sec * samplerate;

	if (LOGBASE==1.0)
		loop_size_min = (int32_t) roundoff(4.0*5.0/ freq[1]-freq[0]);	// linear mode
	else
		loop_size_min = (int32_t) roundoff(2.0*5.0/((freq[0] * pow(2.0, -1.0/(bpo))) * (1.0 - pow(2.0, -1.0 / bpo))));	// this is the estimate of how many samples the longest FIR will take up in the time domain

	if (loop_size_min > loop_size)
		loop_size = loop_size_min;

	loop_size = nextsprime(loop_size);	// enlarge the loop_size to the next multiple of short primes in order to make IFFTs faster
	//--------Loop size calculation--------

	//********Pink noise generation********

	pink_noise = calloc (loop_size, sizeof(double));

	for (i=1; i<(loop_size+1)>>1; i++)
	{
		mag = pow((double) i, 0.5 - 0.5*LOGBASE);	// FIXME something's not necessarily right with that formula
		phase = dblrand() * pi;				// random phase between -pi and +pi

		pink_noise[i]= mag * cos(phase);		// real part
		pink_noise[loop_size-i]= mag * sin(phase);	// imaginary part
	}
	//--------Pink noise generation--------

	noise = malloc(loop_size * sizeof(double));		// allocate noise
	lut = bmsq_lut(BMSQ_LUT_SIZE);				// Blackman Square look-up table initalisation

	for (ib=0; ib<bands; ib++)
	{
		printf("%4d/%d\r", ib+1, bands);

		memset(noise, 0, loop_size * sizeof(double));	// reset filtered noise

		//********Filtering********

		Fa = roundoff(log_pos((double) (ib-1)/(double) (bands-1), basefreq, maxfreq) * loop_size);
		Fd = roundoff(log_pos((double) (ib+1)/(double) (bands-1), basefreq, maxfreq) * loop_size);
		La = log_pos_inv((double) Fa / (double) loop_size, basefreq, maxfreq);
		Ld = log_pos_inv((double) Fd / (double) loop_size, basefreq, maxfreq);

		if (Fd > loop_size/2)
			Fd = loop_size/2;	// stop reading if reaching the Nyquist frequency

		if (Fa<1)
			Fa=1;

		printf("%4d/%d   %.2f Hz - %.2f Hz\r", ib+1, bands, (double) Fa*samplerate/loop_size, (double) Fd*samplerate/loop_size);

		for (i=Fa; i<Fd; i++)
		{
			Li = log_pos_inv((double) i / (double) loop_size, basefreq, maxfreq);	// calculation of the logarithmic position
			Li = (Li-La)/(Ld-La);
			coef = 0.5 - 0.5*cos(2.0*pi*Li);		// Hann function
			noise[i+1] = pink_noise[i+1] * coef;
			noise[loop_size-1-i] = pink_noise[loop_size-1-i] * coef;
		}
		//--------Filtering--------

		fft(noise, noise, loop_size, 1);	// IFFT of the filtered noise

		memset(envelope, 0, *samplecount * sizeof(double));							// blank the envelope
		blackman_square_interpolation(d[bands-ib-1], envelope, Xsize, *samplecount, lut, BMSQ_LUT_SIZE);	// interpolation of the envelope

		il = 0;
		for (i=0; i<*samplecount; i++)
		{
			s[i] += envelope[i] * noise[il];	// modulation
			il++;					// increment loop iterator
			if (il==loop_size)			// if the array iterator has reached the end of the array, it's reset
				il=0;
		}
	}
	
	printf("\n");

	normi(&s, *samplecount, 1, 1.0);

	return s;
}
Ejemplo n.º 3
0
double *synt_sine(double **d, int32_t Xsize, int32_t bands, int32_t *samplecount, int32_t samplerate, double basefreq, double pixpersec, double bpo)
{
	double *s, *freq, *filter, *sband, sine[4], rphase;
	int32_t i, ib;
	int32_t Fc, Bc, Mh, Mn, sbsize;

	/*
	 d is the original image (spectrogram)
	 s is the output sound
	 sband is the band's envelope upsampled and shifted up in frequency
	 sbsize is the length of sband
	 sine is the random sine look-up table
	 *samplecount is the output sound's length
	 ib is the band iterator
	 i is a general purpose iterator
	 bands is the total count of bands
	 Fc is the index of the band's centre in the frequency domain on the new signal
	 Bc is the index of the band's centre in the frequency domain on sband (its imaginary match being sbsize-Bc)
	 Mh is the length of the real or imaginary part of the envelope's FFT, DC element included and Nyquist element excluded
	 Mn is the length of the real or imaginary part of the sound's FFT, DC element included and Nyquist element excluded
	 freq is the band's central frequency
	 rphase is the band's sine's random phase
	*/

	freq = freqarray(basefreq, bands, bpo);

	clocka=gettime();

	sbsize = nextsprime(Xsize * 2);				// In Circular mode keep it to sbsize = Xsize * 2;
	
	*samplecount = roundoff(Xsize/pixpersec);
	printf("Sound duration : %.3f s\n", (double) *samplecount/samplerate);
	*samplecount = roundoff(0.5*sbsize/pixpersec);		// Do not change this value as it would stretch envelopes
	
	s = calloc(*samplecount, sizeof(double));		// allocation of the sound signal
	sband = malloc (sbsize * sizeof(double));		// allocation of the shifted band

	Bc = roundoff(0.25 * (double) sbsize);

	Mh = (sbsize + 1) >> 1;
	Mn = (*samplecount + 1) >> 1;

	filter = wsinc_max(Mh, 1.0/TRANSITION_BW_SYNT);		// generation of the frequency-domain filter

	for (ib=0; ib<bands; ib++)
	{
		memset(sband, 0, sbsize * sizeof(double));	// reset sband

		//********Frequency shifting********

		rphase = dblrand() * pi;			// random phase between -pi and +pi

		for (i=0; i<4; i++)				// generating the random sine LUT
			sine[i]=cos(i*2.0*pi*0.25 + rphase);

		for (i=0; i<Xsize; i++)				// envelope sampling rate * 2 and frequency shifting by 0.25
		{
			if ((i & 1) == 0)
			{
				sband[i<<1] = d[bands-ib-1][i] * sine[0];
				sband[(i<<1) + 1] = d[bands-ib-1][i] * sine[1];
			}
			else
			{
				sband[i<<1] = d[bands-ib-1][i] * sine[2];
				sband[(i<<1) + 1] = d[bands-ib-1][i] * sine[3];
			}			
		}
		//--------Frequency shifting--------

		fft(sband, sband, sbsize, 0);			// FFT of the envelope
		Fc = roundoff(freq[ib] * *samplecount);	// band's centre index (envelope's DC element)

		printf("%4d/%d   %.2f Hz\r", ib+1, bands, (double) Fc*samplerate / *samplecount);

		//********Write FFT********

		for (i=1; i<Mh; i++)
		{
			if (Fc-Bc+i > 0 && Fc-Bc+i < Mn)	// if we're between frequencies 0 and 0.5 of the new signal and that we're not at Fc
			{
				s[i+Fc-Bc] += sband[i] * filter[i];				// Real part
				s[*samplecount-(i+Fc-Bc)] += sband[sbsize-i] * filter[i]; 	// Imaginary part
			}
		}
		//--------Write FFT--------
	}

	printf("\n");

	fft(s, s, *samplecount, 1);			// IFFT of the final sound
	*samplecount = roundoff(Xsize/pixpersec);	// chopping tails by ignoring them

	normi(&s, *samplecount, 1, 1.0);

	return s;
}
float OMNeTEnergyAwareStochasticForwardingPolicy::getRandomNumber(){
    return dblrand();
}