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
|
/*
* bfft.c - Code for some fourrier transform variants and utility
* functions. Data organization is in (real, imag) pairs the first 2
* components are (DC, NY)
*
* Copyright (c) 2000-2003 by Tom Schouten
*
* Restructured code to separate objects and library blob mode
* Fred Jan Kraan, 2020-01-12
*
* 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>
#include <stdio.h>
#include <stdlib.h>
#define MAXORDER 64
typedef struct bfftctl
{
int c_levels;
char c_name[16];
int *c_clutter;
int *c_unclutter;
int c_kill_DC;
int c_kill_NY;
} t_bfftctl;
typedef struct bfft
{
t_object x_obj;
t_float x_f;
t_bfftctl x_ctl;
} t_bfft;
t_class *bfft_class, *ibfft_class, *fht_class;
static inline void bfft_perform_permutation(t_float *S, int n, int *f)
{
int k,l;
t_float swap;
for(k=0; k<n; k++)
{
l = f[k];
while (l<k) l = f[l];
swap = S[k];
S[k] = S[l];
S[l] = swap;
}
}
static void bfft_permutation(t_bfft *x, int n){
t_bfftctl *ctl = &x->x_ctl;
int i;
if (ctl->c_clutter) free(ctl->c_clutter);
if (ctl->c_unclutter) free(ctl->c_unclutter);
ctl->c_clutter = (int *)malloc(n*sizeof(int));
ctl->c_unclutter = (int *)malloc(n*sizeof(int));
ctl->c_unclutter[0] = 0;
ctl->c_unclutter[1] = n/2;
for (i=1; i<n/2; i++){
ctl->c_unclutter[2*i] = i;
ctl->c_unclutter[2*i+1] = n-i;
}
for(i=0; i<n; i++)
ctl->c_clutter[ctl->c_unclutter[i]] = i;
return;
/* debug */
/* for(k=0; k<n; k++)
** printf("clutter[%d] = %d\n", k, ctl->c_clutter[k]);
** for(k=0; k<n; k++)
** printf("unclutter[%d] = %d\n", k, ctl->c_unclutter[k]);
**
** exit(1);
*/
}
static t_int *bfft_perform(t_int *w)
{
// t_float *in = (t_float *)(w[3]);
t_float *out = (t_float *)(w[4]);
t_bfftctl *ctl = (t_bfftctl *)(w[1]);
int n = (int)(w[2]);
t_float scale = sqrt(1.0f / (t_float)(n));
mayer_fht(out, n);
bfft_perform_permutation(out, n, ctl->c_unclutter);
while (n--) *out++ *= scale;
return (w+5);
}
static void bfft_dsp(t_bfft *x, t_signal **sp)
{
int n = sp[0]->s_n;
t_float *in = sp[0]->s_vec;
t_float *out = sp[1]->s_vec;
bfft_permutation(x, n);
if (in != out)
{
dsp_add_copy(in, out, n);
in = out;
}
dsp_add(bfft_perform, 4, &x->x_ctl, n, in, out);
}
static void bfft_free(t_bfft *x)
{
if (x->x_ctl.c_clutter) free(x->x_ctl.c_clutter);
if (x->x_ctl.c_unclutter) free(x->x_ctl.c_unclutter);
}
static void *bfft_new(void)
{
t_bfft *x = (t_bfft *)pd_new(bfft_class);
outlet_new(&x->x_obj, gensym("signal"));
sprintf(x->x_ctl.c_name,"bfft");
x->x_ctl.c_clutter = NULL;
x->x_ctl.c_unclutter = NULL;
return (void *)x;
}
void bfft_tilde_setup(void)
{
//post("bfft~ v0.1");
bfft_class = class_new(gensym("bfft~"), (t_newmethod)bfft_new,
(t_method)bfft_free, sizeof(t_bfft), 0, 0);
CLASS_MAINSIGNALIN(bfft_class, t_bfft, x_f);
class_addmethod(bfft_class, (t_method)bfft_dsp, gensym("dsp"), 0);
}
|