aboutsummaryrefslogtreecommitdiff
path: root/iemlib1/src/mov_avrg_kern~.c
blob: ef7fe3e3d388608e5aed20e429c8df74118b7028 (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
/* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.

iemlib1 written by Thomas Musil, Copyright (c) IEM KUG Graz Austria 2000 - 2006 */


#include "m_pd.h"
#include "iemlib.h"


/* -- mov_avrg_kern~ - kernel for a moving-average-filter with IIR - */

typedef struct _mov_avrg_kern_tilde
{
  t_object x_obj;
  double   x_wn1;
  double   x_a0;
  double   x_sr;
  double   x_mstime;
  int      x_nsamps;
  int      x_counter;
  t_float  x_msi;
} t_mov_avrg_kern_tilde;

t_class *mov_avrg_kern_tilde_class;

static t_int *mov_avrg_kern_tilde_perform(t_int *w)
{
  t_float *in_direct = (t_float *)(w[1]);
  t_float *in_delayed = (t_float *)(w[2]);
  t_float *out = (t_float *)(w[3]);
  t_mov_avrg_kern_tilde *x = (t_mov_avrg_kern_tilde *)(w[4]);
  int i, n = (int)(w[5]);
  double wn0, wn1=x->x_wn1, a0=x->x_a0;
  
  if(x->x_counter)
  {
    int counter = x->x_counter;
    
    if(counter >= n)
    {
      x->x_counter = counter - n;
      for(i=0; i<n; i++)
      {
        wn0 = wn1 + a0*(double)(*in_direct++);
        *out++ = (t_float)wn0;
        wn1 = wn0;
      }
    }
    else
    {
      x->x_counter = 0;
      for(i=0; i<counter; i++)
      {
        wn0 = wn1 + a0*(double)(*in_direct++);
        *out++ = (t_float)wn0;
        wn1 = wn0;
      }
      for(i=counter; i<n; i++)
      {
        wn0 = wn1 + a0*(double)(*in_direct++ - *in_delayed++);
        *out++ = (t_float)wn0;
        wn1 = wn0;
      }
    }
  }
  else
  {
    for(i=0; i<n; i++)
    {
      wn0 = wn1 + a0*(double)(*in_direct++ - *in_delayed++);
      *out++ = (t_float)wn0;
      wn1 = wn0;
    }
  }
  x->x_wn1 = wn1;
  return(w+6);
}

static void mov_avrg_kern_tilde_ft1(t_mov_avrg_kern_tilde *x, t_floatarg mstime)
{
  if(mstime < 0.04)
    mstime = 0.04;
  x->x_mstime = (double)mstime;
  x->x_nsamps = (int)(x->x_sr * x->x_mstime);
  x->x_counter = x->x_nsamps;
  x->x_wn1 = 0.0;
  x->x_a0 = 1.0/(double)(x->x_nsamps);
}

static void mov_avrg_kern_tilde_reset(t_mov_avrg_kern_tilde *x)
{
  x->x_counter = x->x_nsamps;
  x->x_wn1 = 0.0;
}

static void mov_avrg_kern_tilde_dsp(t_mov_avrg_kern_tilde *x, t_signal **sp)
{
  x->x_sr = 0.001*(double)(sp[0]->s_sr);
  x->x_nsamps = (int)(x->x_sr * x->x_mstime);
  x->x_counter = x->x_nsamps;
  x->x_wn1 = 0.0;
  x->x_a0 = 1.0/(double)(x->x_nsamps);
  dsp_add(mov_avrg_kern_tilde_perform, 5, sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, x, sp[0]->s_n);
}

static void *mov_avrg_kern_tilde_new(t_floatarg mstime)
{
  t_mov_avrg_kern_tilde *x = (t_mov_avrg_kern_tilde *)pd_new(mov_avrg_kern_tilde_class);
  
  if(mstime < 0.04)
    mstime = 0.04;
  x->x_mstime = (double)mstime;
  x->x_sr = 44.1;
  x->x_nsamps = (int)(x->x_sr * x->x_mstime);
  x->x_counter = x->x_nsamps;
  x->x_wn1 = 0.0;
  x->x_a0 = 1.0/(double)(x->x_nsamps);
  
  inlet_new(&x->x_obj,  &x->x_obj.ob_pd, &s_signal, &s_signal);
  inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("ft1"));
  outlet_new(&x->x_obj, &s_signal);
  x->x_msi = 0;
  return(x);
}

void mov_avrg_kern_tilde_setup(void)
{
  mov_avrg_kern_tilde_class = class_new(gensym("mov_avrg_kern~"), (t_newmethod)mov_avrg_kern_tilde_new,
        0, sizeof(t_mov_avrg_kern_tilde), 0, A_FLOAT, 0);
  CLASS_MAINSIGNALIN(mov_avrg_kern_tilde_class, t_mov_avrg_kern_tilde, x_msi);
  class_addmethod(mov_avrg_kern_tilde_class, (t_method)mov_avrg_kern_tilde_dsp, gensym("dsp"), 0);
  class_addmethod(mov_avrg_kern_tilde_class, (t_method)mov_avrg_kern_tilde_ft1, gensym("ft1"), A_FLOAT, 0);
  class_addmethod(mov_avrg_kern_tilde_class, (t_method)mov_avrg_kern_tilde_reset, gensym("reset"), 0);
}