aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/vasp/source/obj_peaks.cpp
blob: be83348bde3b94906efbba24249bc310c7d95887 (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
/* 

VASP modular - vector assembling signal processor / objects for Max/MSP and PD

Copyright (c) 2002 Thomas Grill (xovo@gmx.net)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.  

*/

#include "main.h"
#include "classes.h"
#include "util.h"
#include <math.h>


/*! \class vasp_qpeaks
	\remark \b vasp.peaks?
	\brief Get most pronounced peaks of a single vasp vector.
	\since 0.0.6
	\param inlet vasp - is stored and output triggered
	\param inlet bang - triggers output
	\param inlet set - vasp to be stored 
	\retval outlet.0 list - peak positions
	\retval outlet.1 list - peak values

	\note Outputs nothing if vasp is undefined or invalid
	\note Only works for a vasp with one vector. No output otherwise.
	\todo Units for peak position list
*/
class vasp_qpeaks:
	public vasp_op
{
	FLEXT_HEADER_S(vasp_qpeaks,vasp_op,Setup)

public:
	vasp_qpeaks(I argc,const t_atom *argv):
		peaks(1)
	{
		if(argc >= 1 && CanbeInt(argv[0]))
			m_peaks(GetAInt(argv[0]));
		else if(argc)
			post("%s - Number argument invalid -> ignored",thisName());

		AddInAnything();
		AddInInt();
		AddOutAnything(2);
	}

	static V Setup(t_classid c)
	{
		FLEXT_CADDMETHOD(c,1,m_peaks);
		FLEXT_CADDATTR_VAR(c,"peaks",peaks,m_peaks);
	}

	V m_peaks(I n) { peaks = n;	}

	virtual V m_bang() 
	{ 
		if(!ref.Ok())
			post("%s - Invalid vasp!",thisName());
		else if(ref.Vectors() > 1) 
			post("%s - More than one vector in vasp!",thisName());
		else {
			VBuffer *buf = ref.Buffer(0);
			I i,cnt = buf->Length(),pkfnd = 0;
			S *p = buf->Pointer();

			I mxpk = min(cnt,peaks);
			t_atom *pos = new t_atom[mxpk],*lst = new t_atom[mxpk];
			for(i = 0; i < mxpk; ++i) SetFloat(lst[i],0);

			for(i = 0; i < cnt; ++i) {
				const F v = fabs(p[i]);

				if(v && v > GetFloat(lst[mxpk-1])) {
					I ix;

					for(ix = min(pkfnd-1,mxpk-1); ix >= 0; --ix) {
						if(v > GetFloat(lst[ix])) {
							if(ix < mxpk-1) {
								pos[ix+1] = pos[ix];
								lst[ix+1] = lst[ix];
							}
						}
						else break;
					}
					++ix;

					SetFloat(pos[ix],i);
					SetFloat(lst[ix],v);

					if(++pkfnd > mxpk) pkfnd = mxpk;
				}
			}

			ToOutAnything(0,sym_list,pkfnd,pos);
			ToOutAnything(1,sym_list,pkfnd,lst);
			delete[] pos;
			delete[] lst;

            delete buf;
		}
	}

	virtual V m_help() { post("%s - Get list of most pronounced peaks of a vasp vector",thisName()); }

protected:
	I peaks;

private:
	FLEXT_CALLBACK_I(m_peaks);
	FLEXT_CALLSET_I(m_peaks);
	FLEXT_ATTRGET_I(peaks);
};

FLEXT_LIB_V("vasp.peaks?, vasp",vasp_qpeaks)