Using the answer to this link:Spectrogram C++ library I have written a code to calculate the spectrogram of a sinusoidal signal: 1-Created a sinusoidal signal. 2- I applied the Hann Window. 3- used FFTW . 4- Calculated log magnitude of frequency coefficients. Here is the script:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <fftw3.h>
#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{
int i;
double y;
int N=256;
double Fs=30000;//sampling frequency
double T=1/Fs;//sample time
double f=5000;//frequency
double *in;
fftw_complex *out;
double t[N-1];//time vector
fftw_plan plan_forward;
in = (double*) fftw_malloc(sizeof(double) * N);
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
for (int i=0; i< N;i++)
{
t[i]=i*T;
in[i] =0.7 *sin(2*M_PI*f*t[i]);// generate sine waveform
double multiplier = 0.5 * (1 - cos(2*M_PI*i/(N-1)));//Hanning Window
in[i] = multiplier * in[i];
}
plan_forward = fftw_plan_dft_r2c_1d ( N, in, out, FFTW_ESTIMATE );
printf ( "\n" );
printf ( " Input Data:\n" );
printf ( "\n" );
for ( i = 0; i < N; i++ )
{
printf ( " %4d %12f\n", i, in[i] );
}
fftw_execute ( plan_forward );
printf ( "\n" );
printf ( " log magnitude of frequency domain components :\n" );
printf ( "\n" );
for ( i = 0; i < N; i++ )
{
cout << log(sqrt(out[i][0]*out[i][0]+ out[i][1]*out[i][1])) ;
}
fftw_destroy_plan ( plan_forward );
fftw_free ( in );
fftw_free ( out );
return 0;
}
The question is how should I proceed from here? Which library should I use to plot the spectrogram? Any suggestions? Thanks.