~ubuntu-branches/ubuntu/oneiric/pd-smlib/oneiric

« back to all changes in this revision

Viewing changes to lhist.c

  • Committer: Bazaar Package Importer
  • Author(s): Hans-Christoph Steiner
  • Date: 2010-11-10 15:17:58 UTC
  • Revision ID: james.westby@ubuntu.com-20101110151758-3acjf69kiudo3gh4
Tags: upstream-0.12.1
ImportĀ upstreamĀ versionĀ 0.12.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "defines.h"
 
2
 
 
3
/*--------------- lhist ---------------*/
 
4
 
 
5
static t_class *lhist_class;
 
6
 
 
7
typedef struct _lhist
 
8
{
 
9
    t_object x_obj;
 
10
        float m_lo;
 
11
        float m_hi;
 
12
        float m_scale;
 
13
        float m_c_leak;
 
14
        float m_leak;
 
15
        int m_nbins;
 
16
//      int m_n_observations;
 
17
        float *m_lhist;
 
18
} t_lhist;
 
19
 
 
20
static void lhist_setHalfDecay(t_lhist *x, t_float halfDecayTime)
 
21
{
 
22
        x->m_c_leak=(float)powf(.5,(1.0f/halfDecayTime));
 
23
        x->m_leak=1.0f-x->m_c_leak;
 
24
}
 
25
 
 
26
static void lhist_perform_float(t_lhist *x, t_float f)
 
27
{
 
28
        int j;
 
29
        j=(int)(.5+(f-x->m_lo)*x->m_scale);
 
30
        j=(j>0)?(j<x->m_nbins?j:x->m_nbins-1):0; // limit without IF
 
31
        x->m_lhist[j]++;
 
32
//      x->m_n_observations++;
 
33
}
 
34
 
 
35
static void lhist_perform_list(t_lhist *x, t_symbol *s, int argc, t_atom *argv)
 
36
{
 
37
        int i,j;
 
38
        for (i = 0; i < argc; i++)
 
39
        {
 
40
                j=(int)(.5f+(atom_getfloat(&argv[i])-x->m_lo)*x->m_scale);
 
41
                j=(j>0)?(j<x->m_nbins?j:x->m_nbins-1):0; // limit without IF
 
42
                x->m_lhist[j]++;
 
43
        }
 
44
//      x->m_n_observations+=argc;
 
45
}
 
46
 
 
47
static void lhist_leak(t_lhist *x)
 
48
{
 
49
        int i;
 
50
        float *f;
 
51
        float sc;
 
52
        f=x->m_lhist;
 
53
    sc=x->m_c_leak;
 
54
        i=x->m_nbins;
 
55
        while(i--)
 
56
      *f++*=sc;
 
57
}
 
58
 
 
59
static void lhist_bang(t_lhist *x)
 
60
{
 
61
        int i,n;
 
62
        float *f;
 
63
        t_atom *ap,*app;
 
64
        n=x->m_nbins;
 
65
    ap = (t_atom *)getbytes(sizeof(t_atom)*n);
 
66
        app=ap;
 
67
        i=n;
 
68
        f=x->m_lhist;
 
69
 
 
70
    while(i--){
 
71
      SETFLOAT(app, *f);
 
72
          f++;
 
73
      app++;
 
74
    }
 
75
        outlet_list(x->x_obj.ob_outlet,gensym("list"),n,ap);
 
76
        freebytes(ap,n);
 
77
}
 
78
 
 
79
static void lhist_relative(t_lhist *x)
 
80
{
 
81
        int i,n;
 
82
        float *f;
 
83
        float invn,sum;
 
84
        t_atom *ap,*app;
 
85
 
 
86
        n=x->m_nbins;
 
87
    ap = (t_atom *)getbytes(sizeof(t_atom)*n);
 
88
        app=ap;
 
89
        i=x->m_nbins;
 
90
        f=x->m_lhist;
 
91
        sum=0.0f;
 
92
    while(i--) sum+=*f++;
 
93
        invn=1.0f/(1e-10f+sum);
 
94
 
 
95
        i=x->m_nbins;
 
96
        f=x->m_lhist;
 
97
 
 
98
    while(i--){
 
99
      SETFLOAT(app, (*f*invn));
 
100
          f++;
 
101
      app++;
 
102
    }
 
103
        outlet_list(x->x_obj.ob_outlet,gensym("list"),n,ap);
 
104
        freebytes(ap,n);
 
105
}
 
106
 
 
107
static void lhist_clear(t_lhist *x)
 
108
{
 
109
        int i;
 
110
        float *f;
 
111
        f=x->m_lhist;
 
112
        for (i=0;i<x->m_nbins;i++)
 
113
                *f++=0.0f;
 
114
//      x->m_n_observations=0;
 
115
}
 
116
 
 
117
static void lhist_set(t_lhist *x, t_float lo, t_float hi, t_float nbins)
 
118
{
 
119
        if (nbins<1)
 
120
        {
 
121
                nbins=1;
 
122
                post("lhist: number of bins is minimum 1...");
 
123
        }
 
124
        if (hi<=lo)
 
125
        {
 
126
                post("lhist: higher bound must be higher than lower bound..."); 
 
127
                hi=lo+1.0f;
 
128
        }
 
129
        freebytes(x->m_lhist, x->m_nbins);
 
130
        x->m_hi=hi;
 
131
        x->m_lo=lo;
 
132
        x->m_nbins=(int)nbins;
 
133
        x->m_scale=(float)x->m_nbins/(hi-lo);
 
134
    x->m_lhist = (float*)getbytes(sizeof(float)*x->m_nbins);
 
135
 
 
136
        lhist_clear(x);
 
137
}
 
138
 
 
139
static void *lhist_new(t_float lo, t_float hi, t_float nbins, t_float decay)
 
140
{
 
141
        t_lhist *x=(t_lhist *)pd_new(lhist_class);
 
142
        outlet_new(&x->x_obj, gensym("list"));
 
143
        lhist_setHalfDecay(x,decay);
 
144
        x->m_nbins=0;
 
145
        x->m_lhist=0;
 
146
        lhist_set(x, lo, hi, nbins);
 
147
        return (void *)x;
 
148
}
 
149
 
 
150
static void lhist_free(t_lhist *x)
 
151
{
 
152
        freebytes(x->m_lhist, x->m_nbins);
 
153
}
 
154
 
 
155
void lhist_setup(void)
 
156
{
 
157
    lhist_class = class_new(gensym("lhist"),
 
158
        (t_newmethod)lhist_new, (t_method)lhist_free,
 
159
                sizeof(t_lhist), 
 
160
                CLASS_DEFAULT,
 
161
            A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT,A_DEFFLOAT,0);
 
162
 
 
163
        class_addmethod(lhist_class, (t_method)lhist_clear, gensym("clear"),0);
 
164
        class_addmethod(lhist_class, (t_method)lhist_bang, gensym("absolute"),0);
 
165
        class_addmethod(lhist_class, (t_method)lhist_relative, gensym("relative"),0);
 
166
        class_addmethod(lhist_class, (t_method)lhist_leak, gensym("leak"),0);
 
167
        class_addmethod(lhist_class, (t_method)lhist_setHalfDecay,
 
168
        gensym("decay"), A_DEFFLOAT, NULL);
 
169
 
 
170
    class_addlist(lhist_class, (t_method)lhist_perform_list);
 
171
    class_addfloat(lhist_class, (t_method)lhist_perform_float);
 
172
    class_addbang(lhist_class, (t_method)lhist_bang);
 
173
}
 
174