~ubuntu-branches/ubuntu/wily/pd-ekext/wily

« back to all changes in this revision

Viewing changes to hssc~.c

  • Committer: Bazaar Package Importer
  • Author(s): Hans-Christoph Steiner
  • Date: 2010-08-18 17:14:25 UTC
  • Revision ID: james.westby@ubuntu.com-20100818171425-7wehl0fqtaacqulx
Tags: upstream-0.1.1
ImportĀ upstreamĀ versionĀ 0.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  hssc~ : Highest Significant Spectral Component, according to amplitude ratio to
 
3
 *  Strongest Significant Spectral Component. 
 
4
 *  Copyright (C) 2005 Edward Kelly <morph_2016@yahoo.co.uk>
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#include "m_pd.h"
 
21
#include <math.h>
 
22
 
 
23
static t_class *hssc_tilde_class;
 
24
 
 
25
typedef struct _hssc_tilde 
 
26
{
 
27
  t_object x_obj;
 
28
  t_float f;
 
29
  t_float f_maxbin, f_minbin, f_ratio;
 
30
  t_outlet *f_hssc, *f_sssc;
 
31
} t_hssc_tilde;
 
32
 
 
33
t_int *hssc_tilde_perform(t_int *w)
 
34
{
 
35
  t_hssc_tilde *x = (t_hssc_tilde *)(w[1]);
 
36
  t_sample  *real =     (t_sample *)(w[2]);
 
37
  t_sample  *imag =     (t_sample *)(w[3]);
 
38
  int           n =            (int)(w[4]);
 
39
  int incr = 0;
 
40
  double vectorr, vectori;
 
41
  double max = 0;
 
42
  double alpha;
 
43
  x->f_maxbin = x->f_minbin = 0;
 
44
  x->f_ratio = x->f_ratio > 0 ? x->f_ratio : 100;
 
45
 
 
46
  while (n--)
 
47
    {
 
48
      vectorr = (*real++);
 
49
          vectori = (*imag++);
 
50
          alpha = sqrt((vectorr * vectorr) + (vectori * vectori));
 
51
      x->f_maxbin = alpha > max ? incr : x->f_maxbin;
 
52
      max = alpha > max ? alpha : max;
 
53
      x->f_minbin = alpha > (max / x->f_ratio) ? incr : x->f_minbin;
 
54
      incr++;
 
55
    }
 
56
  outlet_float(x->f_sssc, x->f_maxbin); 
 
57
  outlet_float(x->f_hssc, x->f_minbin);
 
58
  
 
59
  return(w+5);
 
60
}
 
61
 
 
62
void hssc_tilde_dsp(t_hssc_tilde *x, t_signal **sp)
 
63
{
 
64
  dsp_add(hssc_tilde_perform, 4, x,
 
65
          sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
 
66
}
 
67
 
 
68
void *hssc_tilde_new(t_floatarg f)
 
69
{
 
70
  t_hssc_tilde *x = (t_hssc_tilde *)pd_new(hssc_tilde_class);
 
71
 
 
72
  x->f_ratio = f;
 
73
 
 
74
  inlet_new (&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
 
75
  floatinlet_new (&x->x_obj, &x->f_ratio);
 
76
  x->f_hssc = outlet_new(&x->x_obj, gensym("float"));
 
77
  x->f_sssc = outlet_new(&x->x_obj, gensym("float"));
 
78
 
 
79
  return (void *)x;
 
80
}
 
81
 
 
82
 
 
83
void hssc_tilde_setup(void)
 
84
{
 
85
  hssc_tilde_class = class_new(gensym("hssc~"),
 
86
                                     (t_newmethod)hssc_tilde_new,
 
87
                                     0, sizeof(t_hssc_tilde),
 
88
                                     CLASS_DEFAULT, A_DEFFLOAT, 0);
 
89
 
 
90
  post("|=================hssc~==================|");
 
91
  post("|=highest significant spectral component=|");
 
92
  post("|======edward=======kelly=======2005=====|");
 
93
 
 
94
  class_addmethod(hssc_tilde_class, (t_method)hssc_tilde_dsp,
 
95
                  gensym("dsp"), 0);
 
96
 
 
97
  CLASS_MAINSIGNALIN(hssc_tilde_class, t_hssc_tilde, f);
 
98
}