summaryrefslogtreecommitdiff
path: root/src/temporal/filter.c
blob: 776d2e6f2cb51c2c4f7401c908c5a2c4db09259c (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>

  This file is part of aubio.

  aubio is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  aubio is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with aubio.  If not, see <http://www.gnu.org/licenses/>.

*/


/* Requires lsmp_t to be long or double. float will NOT give reliable 
 * results */

#include "aubio_priv.h"
#include "fvec.h"
#include "lvec.h"
#include "mathutils.h"
#include "temporal/filter.h"

struct _aubio_filter_t
{
  uint_t order;
  uint_t samplerate;
  lvec_t *a;
  lvec_t *b;
  lvec_t *y;
  lvec_t *x;
};

void
aubio_filter_do_outplace (aubio_filter_t * f, const fvec_t * in, fvec_t * out)
{
  fvec_copy (in, out);
  aubio_filter_do (f, out);
}

void
aubio_filter_do (aubio_filter_t * f, fvec_t * in)
{
  uint_t j, l, order = f->order;
  lsmp_t *x = f->x->data;
  lsmp_t *y = f->y->data;
  lsmp_t *a = f->a->data;
  lsmp_t *b = f->b->data;

  for (j = 0; j < in->length; j++) {
    /* new input */
    x[0] = KILL_DENORMAL (in->data[j]);
    y[0] = b[0] * x[0];
    for (l = 1; l < order; l++) {
      y[0] += b[l] * x[l];
      y[0] -= a[l] * y[l];
    }
    /* new output */
    in->data[j] = y[0];
    /* store for next sample */
    for (l = order - 1; l > 0; l--) {
      x[l] = x[l - 1];
      y[l] = y[l - 1];
    }
  }
}

/* The rough way: reset memory of filter between each run to avoid end effects. */
void
aubio_filter_do_filtfilt (aubio_filter_t * f, fvec_t * in, fvec_t * tmp)
{
  uint_t j;
  uint_t length = in->length;
  /* apply filtering */
  aubio_filter_do (f, in);
  aubio_filter_do_reset (f);
  /* mirror */
  for (j = 0; j < length; j++)
    tmp->data[length - j - 1] = in->data[j];
  /* apply filtering on mirrored */
  aubio_filter_do (f, tmp);
  aubio_filter_do_reset (f);
  /* invert back */
  for (j = 0; j < length; j++)
    in->data[j] = tmp->data[length - j - 1];
}

lvec_t *
aubio_filter_get_feedback (const aubio_filter_t * f)
{
  return f->a;
}

lvec_t *
aubio_filter_get_feedforward (const aubio_filter_t * f)
{
  return f->b;
}

uint_t
aubio_filter_get_order (const aubio_filter_t * f)
{
  return f->order;
}

uint_t
aubio_filter_get_samplerate (const aubio_filter_t * f)
{
  return f->samplerate;
}

uint_t
aubio_filter_set_samplerate (aubio_filter_t * f, uint_t samplerate)
{
  f->samplerate = samplerate;
  return AUBIO_OK;
}

void
aubio_filter_do_reset (aubio_filter_t * f)
{
  lvec_zeros (f->x);
  lvec_zeros (f->y);
}

aubio_filter_t *
new_aubio_filter (uint_t order)
{
  aubio_filter_t *f = AUBIO_NEW (aubio_filter_t);
  if ((sint_t)order < 1) {
    AUBIO_FREE(f);
    return NULL;
  }
  f->x = new_lvec (order);
  f->y = new_lvec (order);
  f->a = new_lvec (order);
  f->b = new_lvec (order);
  /* by default, samplerate is not set */
  f->samplerate = 0;
  f->order = order;
  /* set default to identity */
  f->a->data[0] = 1.;
  f->b->data[0] = 1.;
  return f;
}

void
del_aubio_filter (aubio_filter_t * f)
{
  del_lvec (f->a);
  del_lvec (f->b);
  del_lvec (f->x);
  del_lvec (f->y);
  AUBIO_FREE (f);
  return;
}