Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem with convertion from char to float

Status
Not open for further replies.

julian403

Full Member level 5
Joined
Feb 28, 2014
Messages
254
Helped
1
Reputation
2
Reaction score
1
Trophy points
18
Location
Argentina
Activity points
2,105
I take a secuence of 8bit from an ADC, so I used a char array.

char array[10][10];

I want to take the average, so
float average=0;
float aux;
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
aux=average;
average= aux + array[j]

}}


Then I want to see the value of average on a faile:

fprintf(file, "%f", average);

But there is nothing on the file. If average is int, it works. Why?
 

You need to look up your compiler documentation for the structure for float. Float is normally 32 bits but it is hardware dependent. int is 8 bits by default and can be set to 16 bits for long int.
 

Try this:
Code:
float average = 0;
for(int i=0;i<10;i++) 
  for(int j=0;j<10;j++) 
    average += (float)array[i][j]
average /= 100;
 

Hi,

Usually between two samples the CPU has to wait....unused processing power.

Therefore maybe you can add up the each ADCvalue a 16 bit integer just after each conversion.
Then you have the sum of all samples.

After the 100 samples...
To get the average you have to divide it by 100. Int16 --> division by 100 --> float

Klaus
 

So, Why I do not get something in the file?

Code:
float entorno( snd_pcm_t *handle, snd_pcm_uframes_t frames, unsigned int val, int dir)
{
	char *periodo;
	float promedio=0;

	periodo = ( char *) malloc(frames);    //periodo es la lectura que hace readi() y cuyo tamaño es frames

	float loops = 1000000 / val;    //acá loops setea la cantidad de periodos que se leeran que son 5 segundos

	char **buffer;  //buffer es la cantidad total de datos leidos del adc y tiene

	buffer= (char **)malloc (loops);

	int rc;
	int size=frames;
	int i=0;


			  while (loops > 0) {
			    loops--;
			    rc = snd_pcm_readi(handle, periodo, frames);
			    buffer [i] = (char *) malloc (size);
			    buffer [i]= periodo;
			    i++;

			    if (rc == -EPIPE) {
			      /* EPIPE means overrun */
			      fprintf(stderr, "overrun occurred\n");
			      snd_pcm_prepare(handle);
			    } else if (rc < 0) {
			      fprintf(stderr,
			              "error from read: %s\n",
			              snd_strerror(rc));
			    } else if (rc != (int)frames) {
			      fprintf(stderr, "short read, read %d frames\n", rc);
			    }
			    rc = write(1, buffer, size);
			    if (rc != size)
			      fprintf(stderr,
			              "short write: wrote %d bytes\n", rc);
			  }

			  for(int i=0; i<20;i++)
			  {
				  for(int j=0;j<32;j++)
				  {

					  promedio+=(float) buffer[i][j];
				  }
			  }

			  promedio/=(187);
			  free(buffer);


			  return promedio;
}

int main()
{
	FILE *archivo;
	archivo=fopen("entorno.txt","w+");

		  snd_pcm_t *handle;
		  snd_pcm_hw_params_t *params;
		  unsigned int val;
		  int dir;
		  snd_pcm_uframes_t frames;
		  char ** buffer;
		  float ruido;

		  //Se inicia la placa de Sonido
		  if(configurarSoundCar(&handle, &params, &frames, &val, &dir)==0)
		  {
			  return 0;
		  }
		  //termina el seteo de parámetros de la placa de sonido

		  //En las filas de buffer están los frames, cada uno de 32 unidades.

		  ruido=entorno(handle, frames, val, dir);

		  fprintf(archivo, "\n");
		 fprintf(archivo, "%f ", ruido);
		 snd_pcm_drain(handle);
		 snd_pcm_close(handle);



		  fclose(archivo);
		return mraa::SUCCESS;
	}

but this works

Code:
char ** lecturaADC (snd_pcm_t *handle, snd_pcm_uframes_t frames, unsigned int val, int dir)
{
	char *periodo;
	periodo = ( char *) malloc(frames);    //periodo es la lectura que hace readi() y cuyo tamaño es frames

	float loops = 5000000 / val;    //acá loops setea la cantidad de periodos que se leeran que son 5 segundos

	char **buffer;  //buffer es la cantidad total de datos leidos del adc y tiene

	buffer= (char **)malloc (loops);

	int rc;
	int size=frames;
	int i=0;


			  while (loops > 0) {
			    loops--;
			    rc = snd_pcm_readi(handle, periodo, frames);
			    buffer [i] = (char *) malloc (size);
			    buffer [i]= periodo;
			    i++;

			    if (rc == -EPIPE) {
			      /* EPIPE means overrun */
			      fprintf(stderr, "overrun occurred\n");
			      snd_pcm_prepare(handle);
			    } else if (rc < 0) {
			      fprintf(stderr,
			              "error from read: %s\n",
			              snd_strerror(rc));
			    } else if (rc != (int)frames) {
			      fprintf(stderr, "short read, read %d frames\n", rc);
			    }
			    rc = write(1, buffer, size);
			    if (rc != size)
			      fprintf(stderr,
			              "short write: wrote %d bytes\n", rc);
			  }



			  return buffer;
}

int main()
{
	FILE *archivo;
	archivo=fopen("entorno.txt","w+");

		  snd_pcm_t *handle;
		  snd_pcm_hw_params_t *params;
		  unsigned int val;
		  int dir;
		  snd_pcm_uframes_t frames;
		  char ** buffer;
		  float promedio=0;

		  //Se inicia la placa de Sonido
		  if(configurarSoundCar(&handle, &params, &frames, &val, &dir)==0)
		  {
			  return 0;
		  }
		  //termina el seteo de parámetros de la placa de sonido

		  //En las filas de buffer están los frames, cada uno de 32 unidades.

		  buffer=lecturaADC(handle, frames, val, dir);

		  for(int i=0; i<10;i++)
		  {
			  for(int j=0;j<32;j++)
			  {
				  promedio+=buffer[i][j];
			  }
		  }
		  promedio/=(42);
		  fprintf(archivo, "\n");
		 fprintf(archivo, "%f ", promedio);
		 snd_pcm_drain(handle);
		 snd_pcm_close(handle);



		  fclose(archivo);
		return mraa::SUCCESS;
	}

I think there something wrong in the function which return ruido. By the way promedio is equal to average and ruido is equal to noise.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top