summaryrefslogtreecommitdiff
path: root/cmd/cmd_fir1p.c
blob: b01171908e33513dfa3c51b945fec86f85789e7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "cmd_fir1p.h"

#define FIR_SIZE 12

double fir_coef[FIR_SIZE] = 
{
 -0.0044384,  -0.0041841,   0.0130183,   0.0746628,   
  0.1720210,   0.2489204,   0.2489204,   0.1720210,   
  0.0746628,   0.0130183,  -0.0041841,  -0.0044384
};

#define MAX_INPUT_LEN 80
#define BUFFER_LEN (FIR_SIZE-1+MAX_INPUT_LEN)
double insamp[BUFFER_LEN];

int fir1filter(double *coeffs, double *input, double *output,
	int length, int filter_length)
{
	double acc;
	double *coeffp;
	double *inputp;
	int n;
	int k;

	memcpy(&insamp[filter_length-1], input, length*sizeof(double));

	for (n=0; n<length; n++)
	{
		coeffp = coeffs;
		inputp = &insamp[filter_length - 1 + n];
		acc = 0;

		for (k=0;k<filter_length;k++)
		{
			acc += (*coeffp++)*(*inputp--);
		}

		output[n] = acc;
	}

	memmove(&insamp[0], &insamp[length], (filter_length-1)*sizeof(double));

	return 0;
}

void *cmd_fir1p(void *data)
{
	char *ret = NULL;
	int i=0,j=0;

	if (data == NULL)
	{
		ret = alloc_new_str("FIR filter N=12\n");
		return ret;
	}

	double input[MAX_INPUT_LEN];
	double output[MAX_INPUT_LEN];

	int count;
	sds params = sdsnew(data);
	sds out_result = sdsempty();
	sds *tokens;
	//printf("%s\n",params);
	//tokens = sdssplitlen(params, sdslen(params), " ", 1, &count);
	tokens = sdssplitargs(params, &count);

	for (i=0;i<count;i++)
	{
		//printf("[%s]\n",tokens[i]);
		input[i] = atof(tokens[i]);
	}

	fir1filter(fir_coef, input, output, count, FIR_SIZE);

	for (i=0;i<count;i++)
	{
		//cut to max size 512
		char str_double[16];
		snprintf(str_double, 16, "%.3f ", output[i]);
		out_result = sdscat(out_result, str_double);
	}
	out_result = sdscat(out_result,"\n");



	ret = alloc_new_str(out_result);

	sdsfree(params);
	sdsfree(out_result);
	sdsfreesplitres(tokens, count);

	return ret;
}