PSD utilizando la transformación FFTW Halfcomplex
He pedido unsimilar pregunta, que fue respondida pero cuando trato de hacerlo a mi manera obtengo valores "extraños". Quiero obtener el PSD de una onda sinusoidal usando la transformación de medio complejo como:
#include <stdio.h>
#include <fftw3.h>
#include <complex.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.141592653589793
int main (){
double* inputValues;
double* outputValues;
double realVal;
double imagVal;
double powVal=0.0;
double absVal;
double timer;
fftw_plan plan;
double timeIntervall= 1.0; // 1sec
int numberOfSamples =512;
double timeSteps = timeIntervall/numberOfSamples;
float frequency=10.0;
float dcValue = 0.2;
float value=0.0;
int index=0;
// allocating the memory for the fftw arrays
inputValues = (double*) fftw_malloc(sizeof(double)* numberOfSamples);
outputValues = (double *) fftw_malloc(sizeof(double)*(numberOfSamples/*2*/));
plan = fftw_plan_r2r_1d(numberOfSamples,inputValues,outputValues,FFTW_R2HC,FFTW_ESTIMATE);
for (timer = 0; timer<=timeIntervall; timer += timeSteps){
value = sin(2*M_PI*frequency*timer) +dcValue;
inputValues[index++] = value;
}
fftw_execute(plan);
for (index=0;index<=numberOfSamples/*2*/;index++){
powVal = outputValues[index]*outputValues[index]+outputValues[numberOfSamples-index]*outputValues[numberOfSamples- index];
if(index==0)
powVal/=2;
powVal/=numberOfSamples;
fprintf(stdout," index %d \t PSD value %lf \n",index,powVal);
}
return 0;
}
los valores que obtengo son:
index 0 PSD value 12.24 // expecting 0.2
................
.....................
index 10 PSD value 129.99999 // expecting 0.5
........
.......
index 502 PSD value 127.9999 // expecting 0.5
......................
......................
index 512 PSD value 12.24 // expecting 0.2
de lo contrario, el valor de PSD es cero, la posición del pico es correcta pero su valor no tiene idea de por qué.
gracias por adelantado !
ACTUALIZAR
Lo resuelvo pero no entiendo por qué funciona, así que no lo pondré como respuesta: esto es lo que he cambiado en el código:
.......................................
fftw_execute_r2r(plan_r2hc, in, out);
powVal = outputValues[0]*outputValues[0];
powVal /= (numberOfSamples*numberOfSamples)/2; ///WHY ??????
index = 0;
fprintf(stdout," index %d \t PSD value %lf \t \t %lf \n",index,powVal,outputValues[index]);
for (index =1; index<numberOfSamples/2;index++){
powVal = outputValues[index]*outputValues[index]+outputValues[numberOfSamples-index]*outputValues[numberOfSamples- index];
powVal/=(numberOfSamples*numberOfSamples)/2; //WHY?????
fprintf(stdout," index %d \t PSD value %lf \t \t %lf \n",index,powVal,outputValues[index]);
}
el resultado es exacto, espero obtener alguna explicación sobre por qué debería dividir en el cuadrado de windowsSize y el on 2? De nuevo, gracias por tu ayuda !