Calculating the Power spectral density I've already asked this question on SO, but didn't get an answer, I tqake a shot here.
I am trying to get the PSD of a real data set by making use of  fftw3 library
To test I wrote a small program as shown below ,that generates the  a signal which follows sinusoidal function
#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#define PI 3.14

int main (){
    double  value= 0.0;
    float frequency = 5;
    float  length = 2;
    int index = 4;
    double i = 0 ; 
    double time = 0.0;
    FILE* outputFile = NULL;
    outputFile = fopen(&quot;sinvalues&quot;,&quot;wb&quot;);
    if(outputFile==NULL){
        printf(&quot; couldn't open the file \n&quot;);
        return -1;
    }

    for (i = 0; i&lt;=5000;i++){
        
        value =  sin(2*PI*frequency*zeit);
        fwrite(&amp;value,sizeof(double),1,outputFile);
        zeit += (1.0/frequency);
    }
    fclose(outputFile);
    return 0;

}

Now I'm reading the output file of above program and trying to calculate its PSD like as shown below
#include &lt;stdio.h&gt;
#include &lt;fftw3.h&gt;
#include &lt;complex.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;math.h&gt;
#define PI 3.14
int main (){
    FILE* inp = NULL;
    FILE* oup = NULL;
    double* value;// = 0.0;
    double* result;
    double spectr = 0.0 ;
    int windowsSize =512;
    double  power_spectrum = 0.0;
    fftw_plan plan;

    int index=0,i ,k;
    double multiplier =0.0;
    inp = fopen(&quot;1&quot;,&quot;rb&quot;);
    oup = fopen(&quot;psd&quot;,&quot;wb+&quot;);

    value=(double*)malloc(sizeof(double)*windowsSize);
    result = (double*)malloc(sizeof(double)*(windowsSize)); // what is the length that I have to choose here ? 
        plan =fftw_plan_r2r_1d(windowsSize,value,result,FFTW_R2HC,FFTW_ESTIMATE);

    while(!feof(inp)){
        
        index =fread(value,sizeof(double),windowsSize,inp);
            // zero padding 
        if( index != windowsSize){
            for(i=index;i&lt;windowsSize;i++){
                    value[i] = 0.0;
                        }

        }
        

        // windowing  Hann 
           
        for (i=0; i&lt;windowsSize; i++){
            multiplier = 0.5*(1-cos(2*PI*i/(windowsSize-1)));
            value[i] *= multiplier;
        }

        
        fftw_execute(plan);

        
        for(i = 0;i&lt;(windowsSize/2 +1) ;i++){ //why only tell the half size of the window
            power_spectrum = result[i]*result[i] +result[windowsSize/2 +1 -i]*result[windowsSize/2 +1 -i];
            printf(&quot;%lf \t\t\t %d \n&quot;,power_spectrum,i);
            fprintf(oup,&quot; %lf \n &quot;,power_spectrum);
        }
        
    }
    fclose(oup);
    fclose(inp);
    return 0;

}

Iam not sure about the correctness of the way I am doing this, but below are the results i have obtained:

Can any one help me in tracing the errors of the above approach
and the input data look like :

Thanks in advance
Well, first of all you should note that you are taking batches of 512 samples. It basically means that the first FFT will perform a transform of the first 512 samples, slightly more than a quarter of a period, assuming that signal is periodic. (The windowing tries to compensate for this to some extent.) However, the information from that signal is not really what brings you any information about the spectrum.

While I haven't read the code, I suggest that you try one of these things (maybe both):


Increase the signal frequency
Increase the window length


Ideally, there should be at least one full period of the sinusoidal in the window if you want to interpret the result in a straightforward way.