aboutsummaryrefslogtreecommitdiff
path: root/adaptive/src/nlms3~.c
blob: a1857a29db9d32f089276fce66a9b6dc0bf0df69 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/******************************************************
 *
 * Adaptive Systems for PD
 *
 * copyleft (c) Gerda Strobl, Georg Holzmann
 * 2005
 *
 * for complaints, suggestions: grh@mur.at
 *
 ******************************************************
 *
 * license: GNU General Public License v.2
 *
 ******************************************************/

#include "adaptive.h"


/* ------------------------ nlms3~ ------------------------- */

static t_class *nlms3_tilde_class;

typedef struct _nlms3
{
  t_object x_obj;
  t_float f;
  t_atom *coef;
  t_sample *buf;
  t_sample *xbuf;
  t_sample *in_tmp;
  t_sample *y_tmp;
  t_sample *e_tmp;
  t_int bufsize;
  t_outlet *c_out;
  int adapt; // enable/disable adaptation
  
  t_int N; //number of coefficients of the adaptive system
  t_float *c; // coefficients of the system
  t_float mu; // step-size parameter (learning rate)
  t_float alpha; // small constant to avoid division by zero

  t_canvas *x_canvas;
} t_nlms3_tilde;

static void nlms3_tilde_a(t_nlms3_tilde *x, t_floatarg f)
{
  x->adapt = (f==0) ? 0 : 1;
  
  if(!x->adapt)
  {
    int i;
    
    // clear temp buffers
    for(i=0; i<x->N-1; i++)
      x->buf[i] = 0;
    for(i=0; i<x->N-1; i++)
      x->xbuf[i] = 0;
  }
}

static void nlms3_tilde_geta(t_nlms3_tilde *x)
{
  if(x->adapt==0)
    post("nlms3~: adaptation is currently OFF");
  else
    post("nlms3~: adaptation is currently ON");
}

static void nlms3_tilde_mu(t_nlms3_tilde *x, t_floatarg f)
{
  x->mu = f;
}

static void nlms3_tilde_getmu(t_nlms3_tilde *x)
{
  post("mu (step-size parameter): %f", x->mu);
}

static void nlms3_tilde_alpha(t_nlms3_tilde *x, t_floatarg f)
{
  x->alpha = f;
}

static void nlms3_tilde_getalpha(t_nlms3_tilde *x)
{
  post("alpha: %f", x->alpha);
}

static void nlms3_tilde_getN(t_nlms3_tilde *x)
{
  post("N (number of coefficients): %d", x->N);
}

static void nlms3_tilde_clear(t_nlms3_tilde *x)
{
  int i;
  
  // clear coefficients
  for(i=0; i<x->N; i++)
    x->c[i] = 0;
  
  // clear temp buffers
  for(i=0; i<x->N-1; i++)
    x->buf[i] = 0;
  for(i=0; i<x->N-1; i++)
    x->xbuf[i] = 0;
}

static void nlms3_tilde_init(t_nlms3_tilde *x)
{
  int i;
  
  // set the first coefficient to 1, all others to 0
  // so this is a delay free transmission
  x->c[0] = 1;
  for(i=1; i<x->N; i++)
    x->c[i] = 0;
  
  // clear temp buffers
  for(i=0; i<x->N-1; i++)
    x->buf[i] = 0;
  for(i=0; i<x->N-1; i++)
    x->xbuf[i] = 0;
}

static void nlms3_tilde_print(t_nlms3_tilde *x)
{
  int i;
  
  // print coefficients
  post("\nNr. of coefficients: %d",x->N);
  post("coefficients:");
  for(i=0; i<x->N; i++)
    post("\t%d: %f",i,x->c[i]);
}

static void nlms3_tilde_write(t_nlms3_tilde *x, t_symbol *s)
{
  // make correct path
  char filnam[MAXPDSTRING];
  char filename[MAXPDSTRING];
  canvas_makefilename(x->x_canvas, s->s_name, filnam, MAXPDSTRING);
  sys_bashfilename(filnam, filename);

  // save to file
  adaptation_write(filename, x->N, x->mu, x->c);
}

static void nlms3_tilde_read(t_nlms3_tilde *x, t_symbol *s)
{
  // make correct path
  char filnam[MAXPDSTRING];
  char filename[MAXPDSTRING];
  int n = x->N;
  canvas_makefilename(x->x_canvas, s->s_name, filnam, MAXPDSTRING);
  sys_bashfilename(filnam, filename);
  
  // read file
  adaptation_read(filename, &x->N, &x->mu, x->c, x->buf);
  
  // if length changes:
  if(x->N != n)
  {
    if(x->coef) freebytes(x->coef, sizeof(t_atom) * x->N);
    x->coef = (t_atom *)getbytes(sizeof(t_atom) * x->N);
  }
}

static t_int *nlms3_tilde_perform(t_int *w)
{
  t_sample *in_ = (t_sample *)(w[1]);
  t_sample *x_  = (t_sample *)(w[2]);
  t_sample *d_  = (t_sample *)(w[3]);
  t_sample *out_= (t_sample *)(w[4]);
  t_sample *y_  = (t_sample *)(w[5]);
  t_sample *e_  = (t_sample *)(w[6]);
  int n = (int)(w[7]);
  t_nlms3_tilde *x = (t_nlms3_tilde *)(w[8]);
  int i, j, tmp;
  t_sample x_2;
  
  
  // calculate inlet2 (filter+adaptation)
  if(x->adapt)
  {
    for(i=0; i<n; i++)
    {
      x->y_tmp[i]=0;
      x_2=0;
    
      // y_[i] += x->c[j] * x_[i-j];
      // so lets split in two halfs, so that
      // negative indezes get samples from the
      // last audioblock (x->buf) ...
      tmp = (i+1 - x->N)*(-1);
      tmp = tmp<0 ? 0 : tmp;
    
      for(j=0; j<x->N-tmp; j++)
        x->y_tmp[i] += x->c[j] * x_[i-j];
    
      for(j=x->N-tmp; j<x->N; j++)
        x->y_tmp[i] += x->c[j] * x->xbuf[(i-j)*(-1)-1];
    
      // error computation
      x->e_tmp[i] = d_[i] - x->y_tmp[i];
        
      // Normalized LMS Adaptmsation Algorithm
      // (split in the same way as above)
      //
      // c[n] = c[n-1] + mu/(alpha + x'[n]*x[n])*e[n]*x[n]

      // calc x'[n]*x[n]

      for(j=0; j<x->N-tmp; j++)
        x_2 += x_[i-j] * x_[i-j];
      for(j=x->N-tmp; j<x->N; j++)
        x_2 += x->xbuf[(i-j)*(-1)-1] * x->xbuf[(i-j)*(-1)-1];
      
      for(j=0; j<x->N-tmp; j++)
        x->c[j] = x->c[j] + x->mu/(x->alpha+x_2) * x_[i-j] * x->e_tmp[i];
      for(j=x->N-tmp; j<x->N; j++)
        x->c[j] = x->c[j] + x->mu/(x->alpha+x_2) * x->xbuf[(i-j)*(-1)-1] * x->e_tmp[i];
    }
    
    // outlet coefficients
    for(i=0; i<x->N; i++)
      SETFLOAT(&x->coef[i],x->c[i]);
  
    outlet_list(x->c_out, &s_list, x->N, x->coef);
  
    // store last samples for next audiobuffer
    for(i=0; i<x->N-1; i++)
      x->xbuf[i] = x_[n-1-i];
  }
  
  
  // calculate filter output (inlet 1)
  for(i=0; i<n; i++)
  {
    x->in_tmp[i]=0;
    
    // y_[i] += x->c[j] * x_[i-j];
    // so lets split in two halfs, so that
    // negative indezes get samples from the
    // last audioblock (x->buf) ...
    tmp = (i+1 - x->N)*(-1);
    tmp = tmp<0 ? 0 : tmp;
    
    for(j=0; j<x->N-tmp; j++)
      x->in_tmp[i] += x->c[j] * in_[i-j];
    
    for(j=x->N-tmp; j<x->N; j++)
      x->in_tmp[i] += x->c[j] * x->buf[(i-j)*(-1)-1];
  }
  // store last samples for next audiobuffer
  for(i=0; i<x->N-1; i++)
    x->buf[i] = in_[n-1-i];
  
  
  // write to the outlets
  if(x->adapt)
  {
    while(n--)
    {
      out_[n] = x->in_tmp[n];
      y_[n] = x->y_tmp[n];
      e_[n] = x->e_tmp[n];
    }
  }
  else
  {
    while(n--)
    {
      out_[n] = x->in_tmp[n];
      y_[n] = 0;
      e_[n] = 0;
    }
  }

    
  return (w+9);
}

static void nlms3_tilde_dsp(t_nlms3_tilde *x, t_signal **sp)
{
  // allocate new temp buffer if buffersize changes
  if(x->bufsize != sp[0]->s_n)
  {
    if(sp[0]->s_n < x->N)
      post("nlms3~ WARNING: buffersize must be bigger than N, you will get wrong results !!!"); 
    
    if(x->in_tmp) freebytes(x->in_tmp, sizeof(t_sample) * x->bufsize);
    x->in_tmp = (t_sample *)getbytes(sizeof(t_sample) * sp[0]->s_n);
    
    if(x->y_tmp) freebytes(x->y_tmp, sizeof(t_sample) * x->bufsize);
    x->y_tmp = (t_sample *)getbytes(sizeof(t_sample) * sp[0]->s_n);
    
    if(x->e_tmp) freebytes(x->e_tmp, sizeof(t_sample) * x->bufsize);
    x->e_tmp = (t_sample *)getbytes(sizeof(t_sample) * sp[0]->s_n);
    
    x->bufsize =  sp[0]->s_n;
  }

  dsp_add(nlms3_tilde_perform, 8, sp[0]->s_vec, sp[1]->s_vec, 
          sp[2]->s_vec, sp[3]->s_vec, sp[4]->s_vec, 
          sp[5]->s_vec, sp[0]->s_n, x);
}

static void nlms3_tilde_helper(void)
{
  post("\nnlms3~: Adaptive transversal filter using normalized LMS");
  post("INPUT:");
  post("\tinlet1: input signal without adaptation, only filter");
  post("\tinlet2: input signal for adaptation x[n]");
  post("\tinlet3: desired output signal d[n]");
  post("\tinit_arg1: number of coefficients of the adaptive system");
  post("\tinit_arg2, mu: step-size parameter (learning rate)");
  post("OUTPUT:");
  post("\toutlet1: output signal from inlet1");
  post("\toutlet2: output signal from inlet2");
  post("\toutlet3: error signal e[n]");
  post("\toutlet4: coefficients c[n] (only per block)\n");
}

static void *nlms3_tilde_new(t_symbol *s, int argc, t_atom *argv)
{
  t_nlms3_tilde *x = (t_nlms3_tilde *)pd_new(nlms3_tilde_class);
  int i;
  
  // default values:
  x->N = 8;
  x->mu = 0.05;
  x->alpha = 0.000001;
  x->adapt = 0;
  x->in_tmp = NULL;
  x->y_tmp = NULL;
  x->e_tmp = NULL;
  x->bufsize = 0;
  
  switch(argc)
  {
    case 2:
      x->mu = atom_getfloat(argv+1);
    case 1:
      x->N = atom_getint(argv);
      x->N = (x->N<=0) ? 1 : x->N;
  }
  
  // allocate mem and init coefficients
  x->c = (t_float *)getbytes(sizeof(t_float) * x->N);
  for(i=0; i<x->N; i++)
    x->c[i] = 0;
  
  // allocate mem for temp buffers
  x->buf = (t_sample *)getbytes(sizeof(t_sample) * x->N-1);
  for(i=0; i<x->N-1; i++)
    x->buf[i] = 0;
  x->xbuf = (t_sample *)getbytes(sizeof(t_sample) * x->N-1);
  for(i=0; i<x->N-1; i++)
    x->xbuf[i] = 0;

  // for output atoms (coefficients):
  x->coef = (t_atom *)getbytes(sizeof(t_atom) * x->N);
  
  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_signal, &s_signal);
  outlet_new(&x->x_obj, &s_signal);
  outlet_new(&x->x_obj, &s_signal);
  outlet_new(&x->x_obj, &s_signal);
  x->c_out = outlet_new(&x->x_obj, 0);

  x->x_canvas = canvas_getcurrent();

  return (x);
}

static void nlms3_tilde_free(t_nlms3_tilde *x)
{
  if(x->c) freebytes(x->c, sizeof(t_float) * x->N);
  if(x->buf) freebytes(x->buf, sizeof(t_sample) * x->N-1);
  if(x->xbuf) freebytes(x->xbuf, sizeof(t_sample) * x->N-1);
  if(x->in_tmp) freebytes(x->y_tmp, sizeof(t_sample) * x->bufsize);
  if(x->y_tmp) freebytes(x->y_tmp, sizeof(t_sample) * x->bufsize);
  if(x->e_tmp) freebytes(x->e_tmp, sizeof(t_sample) * x->bufsize);
  if(x->coef) freebytes(x->coef, sizeof(t_atom) * x->N);
}

void nlms3_tilde_setup(void)
{
  nlms3_tilde_class = class_new(gensym("nlms3~"), (t_newmethod)nlms3_tilde_new, 
                    (t_method)nlms3_tilde_free, sizeof(t_nlms3_tilde), 
                    CLASS_DEFAULT, A_GIMME, 0);

  class_addmethod(nlms3_tilde_class, (t_method)nlms3_tilde_a,
                  gensym("adaptation"), A_DEFFLOAT, 0);
  class_addmethod(nlms3_tilde_class, (t_method)nlms3_tilde_geta,
                  gensym("getadaptation"), 0);  
  class_addmethod(nlms3_tilde_class, (t_method)nlms3_tilde_mu, 
                  gensym("mu"), A_DEFFLOAT, 0);
  class_addmethod(nlms3_tilde_class, (t_method)nlms3_tilde_getmu,
                  gensym("getmu"), 0);
  class_addmethod(nlms3_tilde_class, (t_method)nlms3_tilde_alpha, 
                  gensym("alpha"), A_DEFFLOAT, 0);
  class_addmethod(nlms3_tilde_class, (t_method)nlms3_tilde_getalpha,
                  gensym("getalpha"), 0);
  class_addmethod(nlms3_tilde_class, (t_method)nlms3_tilde_getN,
                  gensym("getN"), 0);
  class_addmethod(nlms3_tilde_class, (t_method)nlms3_tilde_init,
                  gensym("init_unity"), 0);
  class_addmethod(nlms3_tilde_class, (t_method)nlms3_tilde_clear,
                  gensym("clear"), 0);
  class_addmethod(nlms3_tilde_class, (t_method)nlms3_tilde_print,
                  gensym("print"), 0);
  class_addmethod(nlms3_tilde_class, (t_method)nlms3_tilde_write, 
                  gensym("write"), A_DEFSYMBOL, 0);
  class_addmethod(nlms3_tilde_class, (t_method)nlms3_tilde_read, 
                  gensym("read"), A_DEFSYMBOL, 0);

  class_addmethod(nlms3_tilde_class, (t_method)nlms3_tilde_dsp, gensym("dsp"), 0);
  CLASS_MAINSIGNALIN(nlms3_tilde_class, t_nlms3_tilde, f);
  
  class_addmethod(nlms3_tilde_class, (t_method)nlms3_tilde_helper, gensym("help"), 0);
}