source: src/temporal/filter.c @ 29808b1

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 29808b1 was 3f99693, checked in by Paul Brossier <piem@piem.org>, 15 years ago

src/temporal/filter.c: reset filter memory in filtfilt

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/*
2  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
3
4  This file is part of aubio.
5
6  aubio is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10
11  aubio is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21
22/* Requires lsmp_t to be long or double. float will NOT give reliable
23 * results */
24
25#include "aubio_priv.h"
26#include "fvec.h"
27#include "lvec.h"
28#include "mathutils.h"
29#include "temporal/filter.h"
30
31struct _aubio_filter_t
32{
33  uint_t order;
34  uint_t samplerate;
35  lvec_t *a;
36  lvec_t *b;
37  lvec_t *y;
38  lvec_t *x;
39};
40
41void
42aubio_filter_do_outplace (aubio_filter_t * f, fvec_t * in, fvec_t * out)
43{
44  fvec_copy (in, out);
45  aubio_filter_do (f, out);
46}
47
48void
49aubio_filter_do (aubio_filter_t * f, fvec_t * in)
50{
51  uint_t i, j, l, order = f->order;
52  lsmp_t *x;
53  lsmp_t *y;
54  lsmp_t *a = f->a->data[0];
55  lsmp_t *b = f->b->data[0];
56
57  for (i = 0; i < in->channels; i++) {
58    x = f->x->data[i];
59    y = f->y->data[i];
60    for (j = 0; j < in->length; j++) {
61      /* new input */
62      x[0] = KILL_DENORMAL (in->data[i][j]);
63      y[0] = b[0] * x[0];
64      for (l = 1; l < order; l++) {
65        y[0] += b[l] * x[l];
66        y[0] -= a[l] * y[l];
67      }
68      /* new output */
69      in->data[i][j] = y[0];
70      /* store for next sample */
71      for (l = order - 1; l > 0; l--) {
72        x[l] = x[l - 1];
73        y[l] = y[l - 1];
74      }
75    }
76    /* store for next run */
77    f->x->data[i] = x;
78    f->y->data[i] = y;
79  }
80}
81
82/* The rough way: reset memory of filter between each run to avoid end effects. */
83void aubio_filter_do_filtfilt(aubio_filter_t * f, fvec_t * in, fvec_t * tmp) {
84  uint_t j,i=0;
85  uint_t length = in->length;
86  /* apply filtering */
87  aubio_filter_do(f,in);
88  aubio_filter_do_reset(f);
89  /* mirror */
90  for (j = 0; j < length; j++)
91    tmp->data[i][length-j-1] = in->data[i][j];
92  /* apply filtering on mirrored */
93  aubio_filter_do(f,tmp);
94  aubio_filter_do_reset(f);
95  /* invert back */
96  for (j = 0; j < length; j++)
97    in->data[i][j] = tmp->data[i][length-j-1];
98}
99
100lvec_t *
101aubio_filter_get_feedback (aubio_filter_t * f)
102{
103  return f->a;
104}
105
106lvec_t *
107aubio_filter_get_feedforward (aubio_filter_t * f)
108{
109  return f->b;
110}
111
112uint_t
113aubio_filter_get_order (aubio_filter_t * f)
114{
115  return f->order;
116}
117
118uint_t
119aubio_filter_get_samplerate (aubio_filter_t * f)
120{
121  return f->samplerate;
122}
123
124uint_t
125aubio_filter_set_samplerate (aubio_filter_t * f, uint_t samplerate)
126{
127  f->samplerate = samplerate;
128  return AUBIO_OK;
129}
130
131void
132aubio_filter_do_reset (aubio_filter_t * f)
133{
134  lvec_zeros(f->x);
135  lvec_zeros(f->y);
136}
137
138aubio_filter_t *
139new_aubio_filter (uint_t order, uint_t channels)
140{
141  aubio_filter_t *f = AUBIO_NEW (aubio_filter_t);
142  f->x = new_lvec (order, channels);
143  f->y = new_lvec (order, channels);
144  f->a = new_lvec (order, 1);
145  f->b = new_lvec (order, 1);
146  /* by default, samplerate is not set */
147  f->samplerate = 0;
148  f->order = order;
149  /* set default to identity */
150  f->a->data[0][1] = 1.;
151  return f;
152}
153
154void
155del_aubio_filter (aubio_filter_t * f)
156{
157  del_lvec (f->a);
158  del_lvec (f->b);
159  del_lvec (f->x);
160  del_lvec (f->y);
161  AUBIO_FREE (f);
162  return;
163}
Note: See TracBrowser for help on using the repository browser.