19 typedef struct _stats{
22 double sumOfSquaredValues;
23 unsigned long maxWindowSize;
24 unsigned long windowSize;
26 unsigned long valueCount;
41 void *stats_new(t_symbol *s,
long argc, t_atom *argv);
42 void stats_free(t_stats *x);
43 void stats_setwindowed(t_stats *x,
void *attr,
long argc, t_atom *argv);
44 void stats_bang(t_stats *x);
45 void stats_int(t_stats *x,
long n);
46 void stats_float(t_stats *x,
double f);
47 void stats_clear(t_stats *x);
48 void stats_window(t_stats *x,
long n);
49 void stats_set(t_stats *x,
double f);
50 void stats_assist(t_stats *x,
void *b,
long msg,
long arg,
char *dst);
60 int JAMOMA_EXPORT_MAXOBJ
main(
void)
66 common_symbols_init();
69 c = class_new(
"j.stats",(method)stats_new, (method)stats_free,
sizeof(t_stats), (method)0L, A_GIMME, 0);
73 class_addmethod(c, (method)stats_bang,
"bang", 0);
74 class_addmethod(c, (method)stats_int,
"int", A_LONG, 0);
75 class_addmethod(c, (method)stats_float,
"float", A_FLOAT, 0);
76 class_addmethod(c, (method)stats_assist,
"assist", A_CANT, 0);
77 class_addmethod(c, (method)stats_set,
"set", A_FLOAT, 0);
78 class_addmethod(c, (method)stats_window,
"window_size", A_LONG, 0);
79 class_addmethod(c, (method)stats_clear,
"clear", 0);
80 class_addmethod(c, (method)object_obex_dumpout,
"dumpout", A_CANT, 0);
83 attr = attr_offset_new(
"windowed", _sym_long, attrflags,
84 (method)0, (method)stats_setwindowed, calcoffset(t_stats, attr_windowed));
85 class_addattr(c, attr);
88 class_register(CLASS_BOX, c);
97 void *stats_new(t_symbol *s,
long argc, t_atom *argv)
104 long attrstart = attr_args_offset(argc, argv);
107 object_obex_store((
void *)x, _sym_dumpout, (
object *)outlet_new(x,NULL));
108 x->outlet5 = floatout(x);
109 x->outlet4 = floatout(x);
110 x->outlet3 = floatout(x);
111 x->outlet2 = floatout(x);
112 x->outlet = intout(x);
115 x->maxWindowSize = 0;
117 if (attrstart && argv)
118 x->maxWindowSize = atom_getlong(argv);
120 if ((attrstart==2) && argv)
121 x->windowSize = atom_getlong(argv+1);
122 if (x->maxWindowSize <= 0)
123 x->maxWindowSize = 500;
124 if (x->windowSize <= 0)
125 x->windowSize = x->maxWindowSize;
126 if (x->windowSize > x->maxWindowSize)
127 x->windowSize = x->maxWindowSize;
130 x->values = (
double *)sysmem_newptr(
sizeof(
double) * x->maxWindowSize);
131 if (x->values == NULL) {
132 error(
"j.stats: memory allocation error");
136 x->attr_windowed = 1;
137 attr_args_process(x, argc, argv);
147 void stats_free(t_stats *x)
149 sysmem_freeptr(x->values);
158 void stats_setwindowed(t_stats *x,
void *attr,
long argc, t_atom *argv)
160 x->attr_windowed = atom_getlong(argv);
165 void stats_bang(t_stats *x)
167 double mean, standardDeviation;
180 if (x->valueCount >= 0)
181 mean = x->sumOfValues / x->valueCount;
184 if (x->valueCount > 1)
185 standardDeviation = sqrt((x->sumOfSquaredValues - (x->sumOfValues*mean)) / (x->valueCount - 1));
187 standardDeviation = 0;
189 outlet_float(x->outlet5, standardDeviation);
190 outlet_float(x->outlet4, mean);
191 outlet_float(x->outlet3, x->max);
192 outlet_float(x->outlet2, x->min);
193 outlet_int(x->outlet, x->valueCount);
198 void stats_int(t_stats *x,
long n)
200 stats_set(x,(
double)n);
206 void stats_float(t_stats *x,
double f)
215 void stats_set(t_stats *x,
double f)
221 if (x->attr_windowed) {
222 if (x->valueCount < x->windowSize)
226 old = x->values[x->index];
227 x->sumOfValues -= old;
228 x->sumOfSquaredValues -= old*old;
231 x->values[x->index] = f;
233 x->sumOfSquaredValues += f*f;
237 if (x->valueCount == 1) {
250 for (i=1; i< x->valueCount ; i++)
251 if (x->values[i] < temp)
259 for (i=1; i<x->valueCount ; i++)
260 if (x->values[i] > temp)
267 if (x->index >= x->windowSize)
275 x->sumOfSquaredValues += f*f;
276 if (x->valueCount == 1) {
290 void stats_window(t_stats *x,
long n)
294 if (x->windowSize <= 0)
295 x->windowSize = x->maxWindowSize;
296 if (x->windowSize > x->maxWindowSize)
297 x->windowSize = x->maxWindowSize;
303 void stats_clear(t_stats *x)
308 x->sumOfSquaredValues = 0;
311 for (i=0; i<x->windowSize; i++)
319 void stats_assist(t_stats *x,
void *b,
long msg,
long arg,
char *dst)
322 strcpy(dst,
"(int/float) function value");
326 strcpy(dst,
"(int) counter");
328 strcpy(dst,
"(float) minimum");
330 strcpy(dst,
"(float) maximum");
332 strcpy(dst,
"(float) mean");
334 strcpy(dst,
"(float) standard deviation");
336 strcpy(dst,
"dumpout");
int JAMOMA_EXPORT_MAXOBJ main(void)
Set up this class as a Max external the first time an object of this kind is instantiated.
t_class * this_class
Required. Global pointing to this class.