aboutsummaryrefslogtreecommitdiff
path: root/modules/cmath~.c
blob: 5526b9f9e379a521b1d2b5e0b3b893593c8b0ff2 (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
/*
 *   cmath.c  - some complex math dsp objects
 *   Copyright (c) 2000-2003 by Tom Schouten
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


#include "m_pd.h"
#include <math.h>

#define MINNORM 0.0000000001

typedef struct cmath
{
    t_object x_obj;
    t_float x_f;
    t_perfroutine x_perf;
} t_cmath;


static t_int *cmath_perform_clog(t_int *w)
{
    t_float *inx    = (float *)(w[2]);
    t_float *iny    = (float *)(w[3]);
    t_float *outx    = (float *)(w[5]); // clockwize addressing
    t_float *outy    = (float *)(w[4]);
    t_int i;
    t_int n = (t_int)(w[1]);
    t_float x;

    while (n--){
	float x = *inx++;
	float y = *iny++;
 	float norm = sqrt(x*x + y*y);
	float arg = atan2(y, x);
	if (norm < MINNORM){
	    norm = MINNORM;
	}
	*outx++ = log(norm);
	*outy++ = arg;
    }
    
    return (w+6);
}


static t_int *cmath_perform_cexp(t_int *w)
{
    t_float *inx    = (float *)(w[2]);
    t_float *iny    = (float *)(w[3]);
    t_float *outx    = (float *)(w[5]); // clockwize addressing
    t_float *outy    = (float *)(w[4]);
    t_int i;
    t_int n = (t_int)(w[1]);
    t_float x;

    while (n--){
	float x = *inx++;
	float y = *iny++;
	float norm = exp(x);
	*outx++ = norm * cos(y);
	*outy++ = norm * sin(y);
    }
    
    return (w+6);
}

static t_int *cmath_perform_nfft(t_int *w)
{
    t_float *inx    = (float *)(w[2]);
    t_float *iny    = (float *)(w[3]);
    t_float *outx    = (float *)(w[5]); // clockwize addressing
    t_float *outy    = (float *)(w[4]);
    t_int i;
    t_int n = (t_int)(w[1]);
    t_float x;
    t_float scale = 1.0f / (sqrt((float)n));

    mayer_fft(n, inx, outx);

    while (n--){
	float x = *inx++;
	float y = *iny++;
	*outx++ = scale * x;
	*outy++ = scale * y;
    }
    
    return (w+6);
}

static t_int *cmath_perform_nifft(t_int *w)
{
    t_float *inx    = (float *)(w[2]);
    t_float *iny    = (float *)(w[3]);
    t_float *outx    = (float *)(w[5]); // clockwize addressing
    t_float *outy    = (float *)(w[4]);
    t_int i;
    t_int n = (t_int)(w[1]);
    t_float x;
    t_float scale = 1.0f / (sqrt((float)n));

    mayer_ifft(n, inx, outx);

    while (n--){
	float x = *inx++;
	float y = *iny++;
	*outx++ = scale * x;
	*outy++ = scale * y;
    }
    
    return (w+6);
}

static void cmath_dsp(t_cmath *x, t_signal **sp)
{
    dsp_add(x->x_perf, 5, sp[0]->s_n, sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[3]->s_vec);

}                                  
void cmath_free(void)
{

}

t_class *cmath_class;

t_cmath *cmath_new_common(void)
{
    t_cmath *x = (t_cmath *)pd_new(cmath_class);
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("signal"), gensym("signal"));  
    outlet_new(&x->x_obj, gensym("signal")); 
    outlet_new(&x->x_obj, gensym("signal")); 
    return x;
}

#define DEFNEWCMATH(name, perfmethod)		\
void * name (void)				\
{						\
    t_cmath *x = cmath_new_common();		\
    x->x_perf = perfmethod ;			\
    return (void*)x;				\
}

DEFNEWCMATH(cmath_new_clog, cmath_perform_clog)
DEFNEWCMATH(cmath_new_cexp, cmath_perform_cexp)
DEFNEWCMATH(cmath_new_nfft, cmath_perform_nfft)
DEFNEWCMATH(cmath_new_nifft, cmath_perform_nifft)


void cmath_tilde_setup(void)
{
  //post("cmath~ v0.1");
    cmath_class = class_new(gensym("clog~"), (t_newmethod)cmath_new_clog,
    	(t_method)cmath_free, sizeof(t_cmath), 0, 0);

    class_addcreator((t_newmethod)cmath_new_cexp, gensym("cexp~"), A_NULL);
    class_addcreator((t_newmethod)cmath_new_nfft, gensym("nfft~"), A_NULL);
    class_addcreator((t_newmethod)cmath_new_nifft, gensym("nifft~"), A_NULL);

    CLASS_MAINSIGNALIN(cmath_class, t_cmath, x_f);

    class_addmethod(cmath_class, (t_method)cmath_dsp, gensym("dsp"), 0); 
}