import 0.1.7.1
[aubio.git] / src / tss.c
1 /*
2    Copyright (C) 2003 Paul Brossier
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /* default values : alfa=4, beta=3, threshold=0.25 */
21
22 #include "aubio_priv.h"
23 #include "sample.h"
24 #include "mathutils.h"
25 #include "tss.h"
26
27 struct _aubio_tss_t 
28 {
29   smpl_t thrs;
30   smpl_t alfa;
31   smpl_t beta;
32   smpl_t parm;
33   fvec_t *theta1;
34   fvec_t *theta2;
35   fvec_t *oft1;
36   fvec_t *oft2;
37   fvec_t *dev;
38 };
39
40 void aubio_tss_do(aubio_tss_t *o, cvec_t * input, 
41     cvec_t * trans, cvec_t * stead)
42 {
43   uint_t i,j;
44   uint_t test;  
45   uint_t nbins     = input->length;
46   uint_t channels  = input->channels;
47   smpl_t alfa      = o->alfa;
48   smpl_t beta      = o->beta;
49   smpl_t parm      = o->parm;
50   smpl_t ** dev    = (smpl_t **)o->dev->data;
51   smpl_t ** oft1   = (smpl_t **)o->oft1->data;
52   smpl_t ** oft2   = (smpl_t **)o->oft2->data;
53   smpl_t ** theta1 = (smpl_t **)o->theta1->data;
54   smpl_t ** theta2 = (smpl_t **)o->theta2->data;
55   /* second phase derivative */
56   for (i=0;i<channels; i++){
57     for (j=0;j<nbins; j++){
58       dev[i][j] = unwrap2pi(input->phas[i][j]
59           -2.0*theta1[i][j]+theta2[i][j]);
60       theta2[i][j] = theta1[i][j];
61       theta1[i][j] = input->phas[i][j];
62     }
63
64     for (j=0;j<nbins; j++){
65       /* transient analysis */
66       test = (ABS(dev[i][j]) > parm*oft1[i][j]);
67       trans->norm[i][j] = input->norm[i][j] * test;
68       trans->phas[i][j] = input->phas[i][j] * test;
69     }
70
71     for (j=0;j<nbins; j++){
72       /* steady state analysis */
73       test = (ABS(dev[i][j]) < parm*oft2[i][j]);
74       stead->norm[i][j] = input->norm[i][j] * test;
75       stead->phas[i][j] = input->phas[i][j] * test;
76
77       /*increase sstate probability for sines */
78       test = (trans->norm[i][j]==0.);
79       oft1[i][j]  = test;
80       test = (stead->norm[i][j]==0.);
81       oft2[i][j]  = test;
82       test = (trans->norm[i][j]>0.);
83       oft1[i][j] += alfa*test;
84       test = (stead->norm[i][j]>0.);
85       oft2[i][j] += alfa*test;
86       test = (oft1[i][j]>1. && trans->norm[i][j]>0.);
87       oft1[i][j] += beta*test;
88       test = (oft2[i][j]>1. && stead->norm[i][j]>0.);
89       oft2[i][j] += beta*test;
90     }
91   }
92 }
93
94 aubio_tss_t * new_aubio_tss(smpl_t thrs, smpl_t alfa, smpl_t beta, 
95     uint_t size, uint_t overlap,uint_t channels)
96 {
97   aubio_tss_t * o = AUBIO_NEW(aubio_tss_t);
98   uint_t rsize = size/2+1;
99   o->thrs = thrs;
100   o->alfa = alfa;       
101   o->beta = beta;       
102   o->parm = thrs*TWO_PI*overlap/rsize;
103   o->theta1 = new_fvec(rsize,channels);
104   o->theta2 = new_fvec(rsize,channels);
105   o->oft1 = new_fvec(rsize,channels);
106   o->oft2 = new_fvec(rsize,channels);
107   o->dev = new_fvec(rsize,channels);
108   return o;
109 }
110
111 void del_aubio_tss(aubio_tss_t *s)
112 {
113   free(s->theta1);
114   free(s->theta2);
115   free(s->oft1);
116   free(s->oft2);
117   free(s->dev);
118   free(s);
119 }
120