/*
* noise.h
* calculation tool for the sensor noise.
*
* -when- -who- -what-
* 2012.10.21 mars v1.0
*/
#ifndef __NOISE_H__
#define __NOISE_H__
int cal_acc_sensor_noise(float *xyz, int cnt, float *xyz_o);
int parse_sensor_data(char *file_name, int dump_row_cnt);
endif /* __NOISE_H__ */
/*
* noise.c
* calculation tool for the sensor noise.
*
* -when- -who- -what-
* 2012.10.21 mars v1.0
*/
#include "noise.h"
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <math.h>
#define noise_debug
#ifdef noise_debug
#define debug(x,argc...) printf(x,##argc)
#else
#define debug(x,argc...) NULL
#endif /* noise_debug */
#define COLUMN_CNT 3
int
cal_acc_sensor_noise(float *xyz, int cnt, float *xyz_o)
{
int i, k;
float avg;
float sum;
float tmp;
debug ("## cal variance ... \r\n");
if (xyz == NULL || xyz_o == NULL) return 0;
if (cnt < COLUMN_CNT || cnt%COLUMN_CNT) return 0;
cnt = cnt/COLUMN_CNT;
for (k = 0; k < COLUMN_CNT; ++k) {
for (sum = 0.0, i = 0; i < cnt; ++i) {
sum += xyz[COLUMN_CNT*i+k];
}
avg = sum / (float)cnt;
for (sum = 0.0, i = 0; i < cnt; ++i) {
tmp = xyz[COLUMN_CNT*i+k] - avg;
sum += tmp * tmp;
}
xyz_o[k] = sqrt (sum / (float)cnt);
}
debug ("variance %f, %f, %f \r\n", xyz_o[0], xyz_o[1], xyz_o[2]);
debug ("## cal variance, done. \r\n\r\n");
return 1;
}
static int
get_file_size(FILE *f_stream)
{
long cur;
long size;
cur = ftell(f_stream);
fseek(f_stream, 0L, SEEK_END);
size = ftell(f_stream);
fseek(f_stream, cur, SEEK_SET);
return size;
}
int
parse_sensor_data(char *file_name, int dump_row_cnt)
{
int i;
int rt;
int sz;
char ch;
FILE *f_stream;
float *f_vals;
float variance[3];
const char *out_file = {"variance.txt"};
debug ("##open file %s ... \r\n", file_name);
if ((f_stream = fopen(file_name, "rb")) == NULL) {
debug ("can not open %s \r\n", file_name);
goto END;
}
debug ("##file opened, done. \r\n\r\n");
sz = get_file_size(f_stream);
debug ("file size %d byes\r\n", sz);
f_vals = (float*)malloc (sz * sizeof(float));
if (f_vals == NULL) {
debug ("malloc failed. \r\n");
goto END;
}
debug ("##dump the first %d rows ... \r\n", dump_row_cnt);
while (dump_row_cnt > 0) {
rt = fread(&ch, 1, 1, f_stream);
if (rt <= 0) goto END;
if (ch == '\n') dump_row_cnt--;
}
debug ("##dump the rows, done. %d \r\n\r\n");
debug ("## read xyz data with format:\"%s\" ... \r\n", "%f,%f,%f");
memset((void*)f_vals, 0, sz * sizeof(float));
for (i = 0; i < sz; i += 3) {
rt = fscanf(f_stream, "%f,%f,%f", &f_vals[i], &f_vals[i+1], &f_vals[i+2]);
if (rt != 3) break;
#if 0
debug ("%f, %f, %f\r\n", f_vals[i], f_vals[i+1], f_vals[i+2]);
#endif
}
if (i <= 0) goto END;
debug ("xyz cnt %d \r\n", i/3);
debug ("## read xyz data, done. \r\n\r\n");
rt = cal_acc_sensor_noise (f_vals, i, variance);
if (!rt) goto END;
if (f_stream != NULL) {
fclose(f_stream);
f_stream = NULL;
}
debug ("##open file %s ... \r\n", out_file);
if ((f_stream = fopen(out_file, "wb+")) == NULL) {
debug ("can not open %s \r\n", out_file);
goto END;
}
debug ("##file opened, done. \r\n\r\n");
debug ("## write variances to %s ... \r\n", out_file);
sz = sprintf((char*)f_vals, "%f, %f, %f \r\n",
variance[0], variance[1], variance[2]);
rt = fwrite ((char*)f_vals, 1, sz, f_stream);
if (rt < sz) goto END;
debug ("## write variances to %s, done. \r\n", out_file);
if (f_stream != NULL) {
fclose(f_stream);
f_stream = NULL;
}
if (f_vals != NULL) {
free(f_vals);
f_vals = NULL;
}
return 1;
END:
if (f_stream != NULL) {
fclose(f_stream);
f_stream = NULL;
}
if (f_vals != NULL) {
free(f_vals);
f_vals = NULL;
}
return 0;
}
#ifdef noise_debug
int
main(int argc, char **argv)
{
int rt;
int dump_row_cnt;
dump_row_cnt = 1;
if (argc <= 1) goto END;
if (argc == 3) dump_row_cnt = atoi(argv[2]);
rt = parse_sensor_data (argv[1], dump_row_cnt);
if (!rt) goto END;
END:
while (1);
}
#endif /* noise_debug */
//log.txt
[BEGIN] 2012-10-17 16:06:45
106,94,-54
109,96,-51
107,93,-52
105,94,-52
108,91,-53
106,90,-53
105,93,-53
106,91,-52
108,92,-52
107,90,-48
109,93,-54
107,91,-49
107,93,-51
108,93,-52
106,93,-51
107,92,-55
105,91,-48
107,90,-52
104,94,-49
107,90,-52
105,94,-51
104,91,-52
107,91,-49
106,93,-53
107,90,-53
103,92,-54
104,92,-53
...
// gcc
gcc noise.c -o cal_noise
//cal_noise.bat
start "debug" /D "." "cal_noise.exe" log.txt 1
enjoy~
mars
october 21,2012
[email protected]