source: src/temporal/filter.c @ c85da04

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since c85da04 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
RevLine 
[96fb8ad]1/*
[a54502c]2  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
[96fb8ad]3
[a54502c]4  This file is part of aubio.
[96fb8ad]5
[a54502c]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.
[96fb8ad]10
[a54502c]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/>.
[96fb8ad]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"
[6c7d49b]26#include "fvec.h"
[a7667ce]27#include "lvec.h"
[96fb8ad]28#include "mathutils.h"
[32d6958]29#include "temporal/filter.h"
[a4364b8]30
[a54502c]31struct _aubio_filter_t
32{
[a4364b8]33  uint_t order;
34  uint_t samplerate;
[a54502c]35  lvec_t *a;
36  lvec_t *b;
37  lvec_t *y;
38  lvec_t *x;
[a4364b8]39};
[96fb8ad]40
[a54502c]41void
42aubio_filter_do_outplace (aubio_filter_t * f, fvec_t * in, fvec_t * out)
43{
44  fvec_copy (in, out);
[6481c0c]45  aubio_filter_do (f, out);
[96fb8ad]46}
47
[a54502c]48void
49aubio_filter_do (aubio_filter_t * f, fvec_t * in)
50{
51  uint_t i, j, l, order = f->order;
[a7667ce]52  lsmp_t *x;
53  lsmp_t *y;
54  lsmp_t *a = f->a->data[0];
55  lsmp_t *b = f->b->data[0];
[96fb8ad]56
[a7667ce]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 */
[a54502c]62      x[0] = KILL_DENORMAL (in->data[i][j]);
[6481c0c]63      y[0] = b[0] * x[0];
[a54502c]64      for (l = 1; l < order; l++) {
[6481c0c]65        y[0] += b[l] * x[l];
66        y[0] -= a[l] * y[l];
[a7667ce]67      }
68      /* new output */
[6481c0c]69      in->data[i][j] = y[0];
[a7667ce]70      /* store for next sample */
[a54502c]71      for (l = order - 1; l > 0; l--) {
72        x[l] = x[l - 1];
73        y[l] = y[l - 1];
[a7667ce]74      }
[96fb8ad]75    }
[a7667ce]76    /* store for next run */
77    f->x->data[i] = x;
78    f->y->data[i] = y;
[96fb8ad]79  }
80}
81
[3f99693]82/* The rough way: reset memory of filter between each run to avoid end effects. */
[96fb8ad]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);
[3f99693]88  aubio_filter_do_reset(f);
89  /* mirror */
[96fb8ad]90  for (j = 0; j < length; j++)
91    tmp->data[i][length-j-1] = in->data[i][j];
[3f99693]92  /* apply filtering on mirrored */
[96fb8ad]93  aubio_filter_do(f,tmp);
[3f99693]94  aubio_filter_do_reset(f);
[96fb8ad]95  /* invert back */
96  for (j = 0; j < length; j++)
97    in->data[i][j] = tmp->data[i][length-j-1];
98}
99
[a54502c]100lvec_t *
101aubio_filter_get_feedback (aubio_filter_t * f)
102{
[a4364b8]103  return f->a;
104}
105
[a54502c]106lvec_t *
107aubio_filter_get_feedforward (aubio_filter_t * f)
108{
[a4364b8]109  return f->b;
110}
111
[a54502c]112uint_t
113aubio_filter_get_order (aubio_filter_t * f)
114{
[a4364b8]115  return f->order;
116}
117
[a54502c]118uint_t
119aubio_filter_get_samplerate (aubio_filter_t * f)
120{
[a4364b8]121  return f->samplerate;
122}
123
[59c046d]124uint_t
125aubio_filter_set_samplerate (aubio_filter_t * f, uint_t samplerate)
126{
127  f->samplerate = samplerate;
128  return AUBIO_OK;
129}
130
[3f99693]131void
132aubio_filter_do_reset (aubio_filter_t * f)
133{
134  lvec_zeros(f->x);
135  lvec_zeros(f->y);
136}
137
[a54502c]138aubio_filter_t *
[59c046d]139new_aubio_filter (uint_t order, uint_t channels)
[a54502c]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);
[59c046d]146  /* by default, samplerate is not set */
147  f->samplerate = 0;
[96fb8ad]148  f->order = order;
[a4364b8]149  /* set default to identity */
150  f->a->data[0][1] = 1.;
[96fb8ad]151  return f;
152}
153
[a54502c]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);
[bb20f87]162  return;
163}
Note: See TracBrowser for help on using the repository browser.