summaryrefslogtreecommitdiffhomepage
path: root/src/lingot-gauge.c
blob: d092ed16e3444862d0cbb50aeb499ef81f65f07e (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
//-*- C++ -*-
/*
 * lingot, a musical instrument tuner.
 *
 * Copyright (C) 2004-2007  Ibán Cereijo Graña, Jairo Chapela Martínez.
 *
 * This file is part of lingot.
 *
 * lingot 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 2 of the License, or
 * (at your option) any later version.
 *
 * lingot 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 lingot; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "lingot-gauge.h"

LingotGauge* lingot_gauge_new(FLT initial_position)
  {

    //
    // ----- ERROR GAUGE FILTER CONFIGURATION -----
    //
    // dynamic model of the gauge:
    //
    //               2
    //              d                              d      
    //              --- s(t) = k (e(t) - s(t)) - q -- s(t)
    //                2                            dt     
    //              dt
    //
    // acceleration of gauge position 's(t)' linealy depends on the difference
    // respect to the input stimulus 'e(t)' (destination position). Inserting
    // a friction coefficient 'q', acceleration proportionaly diminish with
    // velocity (typical friction in mechanics). 'k' is the adaptation constant,
    // and depends on the gauge mass.
    //
    // with the following derivatives approximation (valid for high sample rate):
    //
    //                 d
    //                 -- s(t) ~= (s[n] - s[n - 1])*fs
    //                 dt
    //
    //            2
    //           d                                            2
    //           --- s(t) ~= (s[n] - 2*s[n - 1] + s[n - 2])*fs
    //             2
    //           dt
    //
    // we can obtain a difference equation, and implement it with an IIR digital
    // filter.
    //

    FLT k = 60;
    // adaptation constant.
    FLT q = 6;
    // friction coefficient.

    FLT a[] =
      {
        k + GAUGE_RATE*(q + GAUGE_RATE),
        -GAUGE_RATE*(q + 2.0*GAUGE_RATE),
        GAUGE_RATE*GAUGE_RATE};
    FLT b[] =
      { k};

    LingotGauge* gauge = malloc(sizeof(LingotGauge));
    
    gauge->filter = lingot_filter_new( 2, 0, a, b );
    lingot_gauge_compute(gauge, initial_position);
    return gauge;
  }

void lingot_gauge_destroy(LingotGauge* gauge)
  {
    lingot_filter_destroy(gauge->filter);
    free(gauge);
  }

void lingot_gauge_compute(LingotGauge* gauge, FLT sample)
  {
    gauge->position = lingot_filter_filter_sample(gauge->filter, sample);
  }