source: roaraudio/libroardsp/filter_goertzel.c @ 5548:73e43db30a1c

Last change on this file since 5548:73e43db30a1c was 5548:73e43db30a1c, checked in by phi, 12 years ago

Added filter based on Goertzel algorithm

File size: 4.0 KB
Line 
1//filter_goertzel.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012
5 *
6 *  This file is part of libroardsp a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroardsp is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "libroardsp.h"
27
28#ifdef ROAR_HAVE_LIBM
29
30#define DEFAULT_FREQ 1000.0
31
32struct roardsp_goertzel {
33 float   freq;
34 int32_t coeff;
35 int32_t old[ROAR_MAX_CHANNELS][2];
36};
37
38int roardsp_goertzel_init  (struct roardsp_filter * filter, struct roar_stream * stream, int id) {
39 struct roardsp_goertzel * self = roar_mm_malloc(sizeof(struct roardsp_goertzel));
40
41 (void)stream, (void)id;
42
43 if ( self == NULL )
44  return -1;
45
46 memset(self, 0, sizeof(struct roardsp_goertzel));
47
48 filter->inst = (void*) self;
49
50 roardsp_goertzel_reset(filter, ROARDSP_RESET_FULL);
51
52 return 0;
53}
54
55int roardsp_goertzel_uninit(struct roardsp_filter * filter) {
56
57 roar_mm_free(filter->inst);
58 return 0;
59}
60
61#define _calcX(bits,twobits) \
62int roardsp_goertzel_calc##bits  (struct roardsp_filter * filter, void * data, size_t samples) { \
63 struct roardsp_goertzel * self = (struct roardsp_goertzel *) filter->inst; \
64 int##bits##_t * samp = (int##bits##_t *) data; \
65 /*register int##twobits##_t s, g, h;*/ \
66 register float s, g, h; \
67 /*register int64_t s, g, h;*/ \
68 int channel; \
69 size_t i; \
70\
71 roardsp_goertzel_reset(filter, ROARDSP_RESET_STATE); \
72\
73 for (i = 0, channel = 0; i < samples; i++, channel = channel == (filter->channels - 1) ? 0 : channel + 1) { \
74  g        = self->old[channel][0]; \
75  g       *= self->coeff; \
76  g       /= 32767; \
77  s        = samp[i]; \
78  ROAR_DBG("roardsp_goertzel_calc*(*): channel=%i, g=%f, s=%f, old[0]=%f, old[1]=%f", channel, g, s, self->old[channel][0], self->old[channel][1]); \
79  s       += g; \
80  s       -= self->old[channel][1]; \
81  self->old[channel][1] = self->old[channel][0]; \
82  self->old[channel][0] = s; \
83  /* samp[i]  = s; */ \
84  g  = s; \
85  g *= self->old[channel][1]; \
86  g *= self->coeff; \
87  g /= 32767; \
88  h  = self->old[channel][1]; \
89  h *= self->old[channel][1]; \
90  s  *= s; \
91  s  += h; \
92  s  -= g; \
93  s  *= 0.0001;
94  s  /= (float)((i+1)*(i+1));
95  samp[i]  = s; \
96 }; \
97\
98 return 0; \
99}
100
101_calcX(8,16)
102_calcX(16,32)
103_calcX(32,64)
104
105int roardsp_goertzel_ctl   (struct roardsp_filter * filter, int cmd, void * data) {
106 struct roardsp_goertzel * self = (struct roardsp_goertzel *) filter->inst;
107 float old;
108
109 switch (cmd) {
110  case ROARDSP_FCTL_FREQ:
111   old = self->freq;
112   self->freq = *(float*)data;
113   *(float*)data = old;
114   old = 2.*cos(2.*M_PI*self->freq/(float)filter->rate);
115   self->coeff = old*32767.;
116   ROAR_DBG("roardsp_goertzel_ctl(filter=%p, cmd=%i, data=%p): self->coeff=%li (%f)", filter, cmd, data, (long int)self->coeff, old);
117   break;
118  default:
119    roar_err_set(ROAR_ERROR_BADRQC);
120    return -1;
121   break;
122 }
123
124 return 0;
125}
126
127int roardsp_goertzel_reset (struct roardsp_filter * filter, int what) {
128 struct roardsp_goertzel * self;
129 float freq;
130
131 if ( filter == NULL )
132  return -1;
133
134 if ( filter->inst == NULL )
135  return -1;
136
137 self = filter->inst;
138
139 switch (what) {
140  case ROARDSP_RESET_NONE:
141    return 0;
142   break;
143  case ROARDSP_RESET_FULL:
144    freq = self->freq = DEFAULT_FREQ;
145    roardsp_goertzel_ctl(filter, ROARDSP_FCTL_FREQ, &freq);
146  case ROARDSP_RESET_STATE:
147    memset(self->old, 0, sizeof(self->old));
148    return 0;
149   break;
150  default:
151    roar_err_set(ROAR_ERROR_BADRQC);
152    return -1;
153   break;
154 }
155
156 return -1;
157}
158
159#endif
160
161//ll
Note: See TracBrowser for help on using the repository browser.