[PD-dev] rfft~, rifft~ bug fix
musil at iem.at
musil at iem.at
Thu Feb 3 00:38:30 CET 2005
hi all,
i warm up an old thread (jan. 2002, from marius schebella)
the objects rfft~ and rifft~ are dropping the Nyquist bin, and the imaginary
part of rfft~ is inverted, but it corresponds to rifft~.
for music it is ok, for scientific use we should fix this bug.
i put the Nyquist-bin at the N/2+1. element of the real array.
(1. element is DC, 2. to N/2 are the frequencies between of the real part;
1. element is 0, 2. to N/2 are the other freq.,
N/2+1 is 0 of the imaginary part, N is the blocksize).
so we have now N/2+1 relevant complex frequency bins.
the other solution would be: put the Nyquist bin to the place of the unused
1. element of the imaginary array. i think this is not the best idea
because if you compute the power spectrum of noise~, you would have both
energies on the 1. bin, DC plus Nyquist.
the file d_fft.c is attached, it was written on a windows-computer,
the changes are marked with /* tm 01.2005 */
cheers,
thomas musil
-------------- next part --------------
/* Copyright (c) 1997-1999 Miller Puckette and others.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
#include "m_pd.h"
static t_int *sigfft_swap(t_int *w) /* tm 01.2005 */
{
t_float *in1 = (t_float *)(w[1]);
t_float *in2 = (t_float *)(w[2]);
int n = (int)(w[3]);
int residue = n & 0x00000007;
n &= 0xfffffff8;
for (; n; n -= 8, in1 += 8, in2 += 8)
{
float f0 = in1[0];
float f1 = in1[1];
float f2 = in1[2];
float f3 = in1[3];
float f4 = in1[4];
float f5 = in1[5];
float f6 = in1[6];
float f7 = in1[7];
in1[0] = in2[0];
in1[1] = in2[1];
in1[2] = in2[2];
in1[3] = in2[3];
in1[4] = in2[4];
in1[5] = in2[5];
in1[6] = in2[6];
in1[7] = in2[7];
in2[0] = f0;
in2[1] = f1;
in2[2] = f2;
in2[3] = f3;
in2[4] = f4;
in2[5] = f5;
in2[6] = f6;
in2[7] = f7;
}
while (residue--) {
float f = *in1;
*in1++ = *in2;
*in2++ = f;
}
return (w+4);
}
static t_int *sigfft_flip(t_int *w) /* tm 01.2005 */
{
t_float *in1 = (t_float *)(w[1]);
t_float *out = (t_float *)(w[2]);
int n = (int)(w[3]);
int residue = n & 0x00000007;
n &= 0xfffffff8;
for (; n; n -= 8, in1 += 8, out -= 8)
{
float f0 = in1[0];
float f1 = in1[1];
float f2 = in1[2];
float f3 = in1[3];
float f4 = in1[4];
float f5 = in1[5];
float f6 = in1[6];
float f7 = in1[7];
out[-1] = -f0;
out[-2] = -f1;
out[-3] = -f2;
out[-4] = -f3;
out[-5] = -f4;
out[-6] = -f5;
out[-7] = -f6;
out[-8] = -f7;
}
while (residue--)
*(--out) = -(*in1++);
return (w+4);
}
static t_int *sigfft_zero(t_int *w) /* tm 01.2005 */
{
t_float *out = (t_float *)(w[1]);
int n = (int)(w[2]);
int residue = n & 0x00000007;
n &= 0xfffffff8;
for (; n; n -= 8, out += 8)
{
out[0] = 0;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 0;
out[5] = 0;
out[6] = 0;
out[7] = 0;
}
while (residue--)
*out++ = 0;
return (w+3);
}
static t_int *sigfft_copy(t_int *w) /* tm 01.2005 */
{
t_float *in1 = (t_float *)(w[1]);
t_float *out = (t_float *)(w[2]);
int n = (int)(w[3]);
int residue = n & 0x00000007;
n &= 0xfffffff8;
for (; n; n -= 8, in1 += 8, out += 8)
{
float f0 = in1[0];
float f1 = in1[1];
float f2 = in1[2];
float f3 = in1[3];
float f4 = in1[4];
float f5 = in1[5];
float f6 = in1[6];
float f7 = in1[7];
out[0] = f0;
out[1] = f1;
out[2] = f2;
out[3] = f3;
out[4] = f4;
out[5] = f5;
out[6] = f6;
out[7] = f7;
}
while (residue--)
*out++ = *in1++;
return (w+4);
}
/* ------------------------ fft~ and ifft~ -------------------------------- */
static t_class *sigfft_class, *sigifft_class;
typedef struct fft
{
t_object x_obj;
float x_f;
} t_sigfft;
static void *sigfft_new(void)
{
t_sigfft *x = (t_sigfft *)pd_new(sigfft_class);
outlet_new(&x->x_obj, gensym("signal"));
outlet_new(&x->x_obj, gensym("signal"));
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
x->x_f = 0;
return (x);
}
static void *sigifft_new(void)
{
t_sigfft *x = (t_sigfft *)pd_new(sigifft_class);
outlet_new(&x->x_obj, gensym("signal"));
outlet_new(&x->x_obj, gensym("signal"));
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
x->x_f = 0;
return (x);
}
static t_int *sigfft_perform(t_int *w)
{
float *in1 = (t_float *)(w[1]);
float *in2 = (t_float *)(w[2]);
int n = w[3];
mayer_fft(n, in1, in2);
return (w+4);
}
static t_int *sigifft_perform(t_int *w)
{
float *in1 = (t_float *)(w[1]);
float *in2 = (t_float *)(w[2]);
int n = w[3];
mayer_ifft(n, in1, in2);
return (w+4);
}
static void sigfft_dspx(t_sigfft *x, t_signal **sp, t_int *(*f)(t_int *w))
{
int n = sp[0]->s_n;
float *in1 = sp[0]->s_vec;
float *in2 = sp[1]->s_vec;
float *out1 = sp[2]->s_vec;
float *out2 = sp[3]->s_vec;
if (out1 == in2 && out2 == in1)
dsp_add(sigfft_swap, 3, out1, out2, n);
else if (out1 == in2)
{
dsp_add(sigfft_copy, 3, in2, out2, n); /* tm 01.2005 */
dsp_add(sigfft_copy, 3, in1, out1, n); /* tm 01.2005 */
}
else
{
if (out1 != in1) dsp_add(sigfft_copy, 3, in1, out1, n); /* tm 01.2005 */
if (out2 != in2) dsp_add(sigfft_copy, 3, in2, out2, n); /* tm 01.2005 */
}
dsp_add(f, 3, sp[2]->s_vec, sp[3]->s_vec, n);
}
static void sigfft_dsp(t_sigfft *x, t_signal **sp)
{
sigfft_dspx(x, sp, sigfft_perform);
}
static void sigifft_dsp(t_sigfft *x, t_signal **sp)
{
sigfft_dspx(x, sp, sigifft_perform);
}
static void sigfft_setup(void)
{
sigfft_class = class_new(gensym("fft~"), sigfft_new, 0,
sizeof(t_sigfft), 0, 0);
CLASS_MAINSIGNALIN(sigfft_class, t_sigfft, x_f);
class_addmethod(sigfft_class, (t_method)sigfft_dsp, gensym("dsp"), 0);
sigifft_class = class_new(gensym("ifft~"), sigifft_new, 0,
sizeof(t_sigfft), 0, 0);
CLASS_MAINSIGNALIN(sigifft_class, t_sigfft, x_f);
class_addmethod(sigifft_class, (t_method)sigifft_dsp, gensym("dsp"), 0);
class_sethelpsymbol(sigifft_class, gensym("fft~"));
}
/* ----------------------- rfft~ -------------------------------- */
static t_class *sigrfft_class;
typedef struct rfft
{
t_object x_obj;
float x_f;
} t_sigrfft;
static void *sigrfft_new(void)
{
t_sigrfft *x = (t_sigrfft *)pd_new(sigrfft_class);
outlet_new(&x->x_obj, gensym("signal"));
outlet_new(&x->x_obj, gensym("signal"));
x->x_f = 0;
return (x);
}
static t_int *sigrfft_perform(t_int *w)
{
float *in = (t_float *)(w[1]);
int n = w[2];
mayer_realfft(n, in);
return (w+3);
}
static void sigrfft_dsp(t_sigrfft *x, t_signal **sp)
{
int n = sp[0]->s_n, n2 = (n>>1);
float *in1 = sp[0]->s_vec;
float *out1 = sp[1]->s_vec;
float *out2 = sp[2]->s_vec;
if (n < 4)
{
error("fft: minimum 4 points");
return;
}
if (in1 == out2) /* this probably never happens */
{
dsp_add(sigrfft_perform, 2, out2, n);
dsp_add(sigfft_copy, 3, out2, out1, n2); /* tm 01.2005 */
dsp_add(sigfft_flip, 3, out2 + (n2+1), out2 + n2, n2-1); /* tm 01.2005 */
}
else
{
if (in1 != out1)
dsp_add(sigfft_copy, 3, in1, out1, n); /* tm 01.2005 */
dsp_add(sigrfft_perform, 2, out1, n);
dsp_add(sigfft_flip, 3, out1 + (n2+1), out2 + n2, n2-1); /* tm 01.2005 */
}
dsp_add(sigfft_zero, 2, out1 + n2+1, n2-1); /* tm 01.2005 */
dsp_add(sigfft_zero, 2, out2 + n2, n2); /* tm 01.2005 */
}
static void sigrfft_setup(void)
{
sigrfft_class = class_new(gensym("rfft~"), sigrfft_new, 0,
sizeof(t_sigrfft), 0, 0);
CLASS_MAINSIGNALIN(sigrfft_class, t_sigrfft, x_f);
class_addmethod(sigrfft_class, (t_method)sigrfft_dsp, gensym("dsp"), 0);
class_sethelpsymbol(sigrfft_class, gensym("fft~"));
}
/* ----------------------- rifft~ -------------------------------- */
static t_class *sigrifft_class;
typedef struct rifft
{
t_object x_obj;
float x_f;
} t_sigrifft;
static void *sigrifft_new(void)
{
t_sigrifft *x = (t_sigrifft *)pd_new(sigrifft_class);
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
outlet_new(&x->x_obj, gensym("signal"));
x->x_f = 0;
return (x);
}
static t_int *sigrifft_perform(t_int *w)
{
float *in = (t_float *)(w[1]);
int n = w[2];
mayer_realifft(n, in);
return (w+3);
}
static void sigrifft_dsp(t_sigrifft *x, t_signal **sp)
{
int n = sp[0]->s_n, n2 = (n>>1);
float *in1 = sp[0]->s_vec;
float *in2 = sp[1]->s_vec;
float *out1 = sp[2]->s_vec;
if (n < 4)
{
error("fft: minimum 4 points");
return;
}
if (in2 == out1)
{
dsp_add(sigfft_flip, 3, out1+1, out1 + n, n2-1); /* tm 01.2005 */
dsp_add(sigfft_copy, 3, in1, out1, n2+1); /* tm 01.2005 */
}
else
{
if (in1 != out1)
dsp_add(sigfft_copy, 3, in1, out1, n2+1); /* tm 01.2005 */
dsp_add(sigfft_flip, 3, in2+1, out1 + n, n2-1); /* tm 01.2005 */
}
dsp_add(sigrifft_perform, 2, out1, n);
}
static void sigrifft_setup(void)
{
sigrifft_class = class_new(gensym("rifft~"), sigrifft_new, 0,
sizeof(t_sigrifft), 0, 0);
CLASS_MAINSIGNALIN(sigrifft_class, t_sigrifft, x_f);
class_addmethod(sigrifft_class, (t_method)sigrifft_dsp, gensym("dsp"), 0);
class_sethelpsymbol(sigrifft_class, gensym("fft~"));
}
/* ----------------------- framp~ -------------------------------- */
static t_class *sigframp_class;
typedef struct framp
{
t_object x_obj;
float x_f;
} t_sigframp;
static void *sigframp_new(void)
{
t_sigframp *x = (t_sigframp *)pd_new(sigframp_class);
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
outlet_new(&x->x_obj, gensym("signal"));
outlet_new(&x->x_obj, gensym("signal"));
x->x_f = 0;
return (x);
}
static t_int *sigframp_perform(t_int *w)
{
float *inreal = (t_float *)(w[1]);
float *inimag = (t_float *)(w[2]);
float *outfreq = (t_float *)(w[3]);
float *outamp = (t_float *)(w[4]);
float lastreal = 0, currentreal = inreal[0], nextreal = inreal[1];
float lastimag = 0, currentimag = inimag[0], nextimag = inimag[1];
int n = w[5];
int m = n + 1;
float fbin = 1, oneovern2 = 1.f/((float)n * (float)n);
inreal += 2;
inimag += 2;
*outamp++ = *outfreq++ = 0;
n -= 2;
while (n--)
{
float re, im, pow, freq;
lastreal = currentreal;
currentreal = nextreal;
nextreal = *inreal++;
lastimag = currentimag;
currentimag = nextimag;
nextimag = *inimag++;
re = currentreal - 0.5f * (lastreal + nextreal);
im = currentimag - 0.5f * (lastimag + nextimag);
pow = re * re + im * im;
if (pow > 1e-19)
{
float detune = ((lastreal - nextreal) * re +
(lastimag - nextimag) * im) / (2.0f * pow);
if (detune > 2 || detune < -2) freq = pow = 0;
else freq = fbin + detune;
}
else freq = pow = 0;
*outfreq++ = freq;
*outamp++ = oneovern2 * pow;
fbin += 1.0f;
}
while (m--) *outamp++ = *outfreq++ = 0;
return (w+6);
}
t_int *sigsqrt_perform(t_int *w);
static void sigframp_dsp(t_sigframp *x, t_signal **sp)
{
int n = sp[0]->s_n, n2 = (n>>1);
if (n < 4)
{
error("framp: minimum 4 points");
return;
}
dsp_add(sigframp_perform, 5, sp[0]->s_vec, sp[1]->s_vec,
sp[2]->s_vec, sp[3]->s_vec, n2);
dsp_add(sigsqrt_perform, 3, sp[3]->s_vec, sp[3]->s_vec, n2);
}
static void sigframp_setup(void)
{
sigframp_class = class_new(gensym("framp~"), sigframp_new, 0,
sizeof(t_sigframp), 0, 0);
CLASS_MAINSIGNALIN(sigframp_class, t_sigframp, x_f);
class_addmethod(sigframp_class, (t_method)sigframp_dsp, gensym("dsp"), 0);
}
/* ------------------------ global setup routine ------------------------- */
void d_fft_setup(void)
{
sigfft_setup();
sigrfft_setup();
sigrifft_setup();
sigframp_setup();
}
More information about the Pd-dev
mailing list