Reputation: 31
How can we calculate the first derivative of a function with FFTW_RODFT00 (sine transform)?
I have some luck calculating the 2nd derivative because sqrt(-1)^2 = -1 and whilst working with imaginary values, this returns an imaginary value that we can hand down the Inverse Sine Transform to get back df(x). With the first derivative, on the other hand, I fear we might have a real value (multiplication with i=sqrt(-1) ) to inverse transform with FFTW_REDFT00?
Here's my code:
#include "math.h"
#include "fftw3w.h"
#include "common.h"
//#include <complex>
#define M_PI 3.14159265358979323846
#define FFN_R2R 3
float b[FFN_R2R]={1,2,3};
static void normalize_r2r(void){
const float const_k=1.f/(2*(FFN_R2R+1));
for(unsigned int i=0; i<FFN_R2R; ++i) b[i]*=const_k;
}
static void delta_r2r(void) {
const float fM_PI= (float) M_PI;
fftwf_plan gplan[2];
gplan[0]= fftwf_plan_r2r_1d(FFN_R2R, b, b, FFTW_RODFT00, FFTW_ESTIMATE);
gplan[1]= fftwf_plan_r2r_1d(FFN_R2R, b, b, FFTW_RODFT00, FFTW_ESTIMATE);
fftwf_execute(gplan[0]);
/**/
const float rcpL=1.f/(1*(FFN_R2R+1));
unsigned int i;
float k;
for(i=0, k=0.f; i<FFN_R2R; ++i, ++k){
b[i]*= -( fM_PI*rcpL*(k+1.f) * fM_PI*rcpL*(k+1.f) );
}
fftwf_execute(gplan[1]);
normalize_r2r();
for(uint i=0; i<FFN_R2R; ++i) printf("%f \n", b[i]);
}
Upvotes: 0
Views: 225