Plotting the spectrogram - c++

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.

If you want to stay with C++ and be reasonable cross-platform, you might want to consider Qt as UI and either Qwt or QCustomPlot ar scientific plots widget
Links
http://sourceforge.net/projects/qwt/
http://www.qcustomplot.com/

Related

code is running, but the gpu function won't be executed

I got two functions:
The add_cpu function works fine, but the add_gpu function does not.
I tried to check sum options on my GPU driver Software and read my code over and over again. I tried the exact same code on an other machine and it worked fine.
The checkError result on current machine is 1, what it shouldn't be.
And checkError result on my Laptop is 0, what is correct.
Does anyone have any suggestion of what is the problem with the graphic card or the system?
I have no clue what's the problem here.
Did I miss some sort of option?
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
#include <iostream>
#include <math.h>
#define out std::cout <<
#define end << std::endl
__global__
void add_gpu( int n, float* x, float* y ) {
for ( int i = 0; i < n; i++ ) y[i] = x[i] + y[i];
}
void add_cpu( int n, float* x, float* y ) {
for ( int i = 0; i < n; i++ ) y[i] = x[i] + y[i];
}
void init( int n, float* x, float* y ) {
for ( int i = 0; i < n; i++ ) {
x[i] = 1.0f;
y[i] = 2.0f;
}
}
int checkError( int n, float f, float* y ) {
float c = 0.0f;
for ( int i = 0; i < n; i++ ) c = fmax( c, fabs( y[i] - f ) );
return c;
}
void print( int n, float* obj, char* str = "obj: " ) {
out str << obj[0];
for ( int i = 1; i < n; i++ ) out ", " << obj[i];
out "" end;
}
int main( ) {
int n = 1 << 5;
float* x, * y;
float error = 0.0f;
cudaMallocManaged( &x, n * sizeof( float ) );
cudaMallocManaged( &y, n * sizeof( float ) );
init( n, x, y );
print( n, x, "x" );
print( n, y, "y" );
add_gpu<< <1, 1 >> > ( n, x, y );
//add_cpu(n, x, y);
cudaDeviceSynchronize( );
print( n, y, "y" );
error = checkError( n, 3.0f, y );
out "error: " << error end;
cudaFree( x );
cudaFree( y );
return 0;
}
I don't see exactly where the problem is but in order to debug it you should check the cuda errors.
Most cuda functions return a cuda status. You can maybe use a little wrapper function like this to check the errors
checkCudaError(const cudaError_t error) {
if (error != cudaSuccess) {
std::cout << "Cuda error: " << cudaGetErrorString(error) << std::endl;
// maybe do something else
}
}
and call function like cudaMallocManaged() this way
checkCudaError(cudaMallocManaged(&x, n * sizeof(float));
For all operations which are performed on the device (like custom kernels) you should run the kernel and after that call
cudaGetLastError()
and maybe also use checkCudaError()
checkCudaError(cudaGetLastError())
Note that cudaGetLastError() will always return a error if at some point an error occured and so you have to find the place where the first error occures. That is why you should check cuda error every time the GPU was used in some way.
https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__MEMORY.html#group__CUDART__MEMORY_1gc263dbe6574220cc776b45438fc351e8
Without copying the data to the device your GPU doesnt know the data and without copying them back your host doesnt know the results

Vectors and matrices in C++ for generating a spectrogram

This is my first attempt to generate a spectrogram of a sinusoidal signal with C++.
To generate the spectrogram:
I divided the real sinusoidal signal into B blocks
Applied Hanning window on each block (I assumed there is no overlap). This should give me the inputs for the fft, in[j][k] where k is the block number
Apply fft on in[j][k] for each block and store it.
Here is the script:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <fftw3.h>
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main(){
int i;
int N = 500; // sampled
int Windowsize = 100;
double Fs = 200; // sampling frequency
double T = 1 / Fs; // sample time
double f = 50; // frequency
double *in;
fftw_complex *out;
double t[N]; // time vector
fftw_plan plan_forward;
std::vector<double> signal(N);
int B = N / Windowsize; //number of blocks
in = (double*)fftw_malloc(sizeof(double) * N);
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
//Generating the signal
for(int i = 0; i < = N; i++){
t[i] = i * T;
signal[i] = 0.7 * sin(2 * M_PI * f * t[i]);// generate sine waveform
}
//Applying the Hanning window function on each block B
for(int k = 0; i <= B; k++){
for(int j = 0; j <= Windowsize; j++){
double multiplier = 0.5 * (1 - cos(2 * M_PI * j / (N-1))); // Hanning Window
in[j][k] = multiplier * signal[j];
}
plan_forward = fftw_plan_dft_r2c_1d (Windowsize, in, out, FFTW_ESTIMATE );
fftw_execute(plan_forward);
v[j][k]=(20 * log(sqrt(out[i][0] * out[i][0] + out[i][1] * out[i][1]))) / N;
}
fftw_destroy_plan(plan_forward);
fftw_free(in);
fftw_free(out);
return 0;
}
So, the question is: What is the correct way to declare in[j][k] and v[j][k] variables.
Update:I have declared my v [j] [k] as a matrix : double v [5][249]; according to this site :http://www.cplusplus.com/doc/tutorial/arrays/ so now my script looks like:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <fftw3.h>
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main()
{
int i;
double y;
int N=500;//Number of pints acquired inside the window
double Fs=200;//sampling frequency
int windowsize=100;
double dF=Fs/N;
double T=1/Fs;//sample time
double f=50;//frequency
double *in;
fftw_complex *out;
double t[N];//time vector
double tt[5];
double ff[N];
fftw_plan plan_forward;
double v [5][249];
in = (double*) fftw_malloc(sizeof(double) * N);
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
plan_forward = fftw_plan_dft_r2c_1d ( N, in, out, FFTW_ESTIMATE );
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
}
for (int k=0; k< 5;k++){
for (int i = 0; i<windowsize; i++){
double multiplier = 0.5 * (1 - cos(2*M_PI*i/(windowsize-1)));//Hanning Window
in[i] = multiplier * in[i+k*windowsize];
fftw_execute ( plan_forward );
for (int i = 0; i<= (N/2); i++)
{
v[k][i]=(20*log10(sqrt(out[i][0]*out[i][0]+ out[i][1]*out[i] [1])));//Here I have calculated the y axis of the spectrum in dB
}
}
}
for (int k=0; k< 5;k++)//Center time for each block
{
tt[k]=(2*k+1)*T*(windowsize/2);
}
fstream myfile;
myfile.open("example2.txt",fstream::out);
myfile << "plot '-' using 1:2" << std::endl;
for (int k=0; k< 5;k++){
for (int i = 0; i<= ((N/2)-1); i++)
{
myfile << v[k][i]<< " " << tt[k]<< std::endl;
}
}
myfile.close();
fftw_destroy_plan ( plan_forward );
fftw_free ( in );
fftw_free ( out );
return 0;
}
I do not get errors anymore but the spectrogram plot is not right.
As indicated in FFTW's documentation, the size of the output (out in your case) when using fftw_plan_dft_r2c_1d is not the same as the size of the input. More specifically for an input of N real samples, the output consists of N/2+1 complex values. You may then allocate out with:
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * (N/2 + 1));
For the spectrogram output you will then similarly have (N/2+1) magnitudes for each of the B blocks, resulting in the 2D array:
double** v = new double*[B];
for (int i = 0; i < B; i++){
v[i] = new double[(N/2+1)];
}
Also, note that you may reuse the input buffer in for each iteration (filling it with data for a new block). However since you have chosen to compute an N-point FFT and will be storing smaller blocks of Windowsize samples (in this case N=500 and Windowsize=100), make sure to initialize the remaining samples with zeros:
in = (double*)fftw_malloc(sizeof(double) * N);
for (int i = 0; i < N; i++){
in[i] = 0;
}
Note that in addition to the declaration and allocation of the in and v variables, the code you posted suffers from a few additional issues:
When computing the Hanning window, you should divide by the Windowsize-1 not N-1 (since in your case N correspond to the FFT size).
You are taking the FFT of the same block of signal over and over again since you are always indexing with j in the [0,Windowsize] range. You would most likely want to add an offset each time you process a different block.
Since the FFT size does not change, you only need to create the plan once. At the very least if you are going to create your plan at every iteration, you should similarly destroy it (with fftw_destroy_plan) at every iteration.
And a few additional points which may require some thoughts:
Scaling the log-scaled magnitudes by dividing by N might not do what you think. You are much more likely to want to scale the linear-scale magnitudes (ie. divide the magnitude before taking the logarithm). Note that this will result in a constant offset of the spectrum curve, which for many application is not that significant. If the scaling is important for your application, you may have a look at another answer of mine for more details.
The common formula 20*log10(x) typically used to convert linear scale to decibels uses a base-10 logarithm instead of the natural log (base e~2.7182) function which you've used. This would result in a multiplicative scaling (stretching), which may or may not be significant depending on your application.
To summarize, the following code might be more in line with what you are trying to do:
// Allocate & initialize buffers
in = (double*)fftw_malloc(sizeof(double) * N);
for (int i = 0; i < N; i++){
in[i] = 0;
}
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * (N/2 + 1));
v = new (double*)[B];
for (int i = 0; i < B; i++){
v[i] = new double[(N/2+1)];
}
// Generate the signal
...
// Create the plan once
plan_forward = fftw_plan_dft_r2c_1d (Windowsize, in, out, FFTW_ESTIMATE);
// Applying the Hanning window function on each block B
for(int k = 0; k < B; k++){
for(int j = 0; j < Windowsize; j++){
// Hanning Window
double multiplier = 0.5 * (1 - cos(2 * M_PI * j / (Windowsize-1)));
in[j] = multiplier * signal[j+k*Windowsize];
}
fftw_execute(plan_forward);
for (int j = 0; j <= N/2; j++){
// Factor of 2 is to account for the fact that we are only getting half
// the spectrum (the other half is not return by a R2C plan due to symmetry)
v[k][j] = 2*(out[j][0] * out[j][0] + out[j][1] * out[j][1])/(N*N);
}
// DC component and at Nyquist frequency do not have a corresponding symmetric
// value, so should not have been doubled up above. Correct those special cases.
v[k][0] *= 0.5;
v[k][N/2] *= 0.5;
// Convert to decibels
for (int j = 0; j <= N/2; j++){
// 20*log10(sqrt(x)) is equivalent to 10*log10(x)
// also use some small epsilon (e.g. 1e-5) to avoid taking the log of 0
v[k][j] = 10 * log10(v[k][j] + epsilon);
}
}
// Clean up
fftw_destroy_plan(plan_forward);
fftw_free(in);
fftw_free(out);
// Delete this last one after you've done something useful with the spectrogram
for (int i = 0; i < B; i++){
delete[] v[i];
}
delete[] v;
Looks like you're missing the initial declaration for 'v' altogether, and 'in' is not declared properly.
See this page for a related question about creating 2D arrays in C++. As I understand, fftw_malloc() is basically new() or malloc() but aligns the variable properly for the FFTW algorithm.
Since you're not supplying 'v' to the anything related to FFTW, you could use standard malloc() for that.

Plotting frequency spectrum with c++

Please see the Edits in the answer below this question.
I have written a script to plot the frequency spectrum of a sinusoidal signal with c++. Here are the steps
Applying Hanning window
Apply FFT using fftw3 library
I have three graphs: Signal, Signal when is multiplied to Hanning function, and the frequency spectrum. The frequency spectrum looks wrong. It should have a peak at 50 Hz. Any suggestion would be appreciated. Here is the code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <fftw3.h>
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main()
{
int i;
double y;
int N=50;
double Fs=1000;//sampling frequency
double T=1/Fs;//sample time
double f=50;//frequency
double *in;
fftw_complex *out;
double t[N];//time vector
double ff[N];
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;
ff[i]=1/t[i];
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 );
fftw_execute ( plan_forward );
double v[N];
for (int i = 0; i < N; i++)
{
v[i]=20*log(sqrt(out[i][0]*out[i][0]+ out[i][1]*out[i][1])/N/2);//Here I have calculated the y axis of the spectrum in dB
}
fstream myfile;
myfile.open("example2.txt",fstream::out);
myfile << "plot '-' using 1:2" << std::endl;
for(i = 0; i < N; ++i)
{
myfile << ff[i]<< " " << v[i]<< std::endl;
}
myfile.close();
fftw_destroy_plan ( plan_forward );
fftw_free ( in );
fftw_free ( out );
return 0;
}
I have to add that I have plotted the graphs using gnuplot after inserting the results into example2.txt. So ff[i] vs v[i] should give me the frequency spectrum.
Here are the plots: Frequency Spectrum and Sinusoidal time Window respectively:
My Frequency intervals were completely wrong. According to http://www.ni.com/white-paper/3995/en/#toc1; the frequency range and resolution on the x-axis depend on sampling rate and N. The last point on the frequency axis should be Fs/2-Fs/N and the resolution dF=FS/N.So I have changed my script to: (since frequency resolution is Fs/N as you increase the number of smaples N (or decrease sampling frequency Fs) you get smaller frequency resolution and better results.)
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <fftw3.h>
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main()
{
int i;
double y;
int N=550;//Number of points acquired inside the window
double Fs=200;//sampling frequency
double dF=Fs/N;
double T=1/Fs;//sample time
double f=50;//frequency
double *in;
fftw_complex *out;
double t[N];//time vector
double ff[N];
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];
}
for (int i=0; i<= ((N/2)-1);i++)
{ff[i]=Fs*i/N;
}
plan_forward = fftw_plan_dft_r2c_1d ( N, in, out, FFTW_ESTIMATE );
fftw_execute ( plan_forward );
double v[N];
for (int i = 0; i<= ((N/2)-1); i++)
{
v[i]=(20*log(sqrt(out[i][0]*out[i][0]+ out[i][1]*out[i][1])))/N; //Here I have calculated the y axis of the spectrum in dB
}
fstream myfile;
myfile.open("example2.txt",fstream::out);
myfile << "plot '-' using 1:2" << std::endl;
for(i = 0;i< ((N/2)-1); i++)
{
myfile << ff[i]<< " " << v[i]<< std::endl;
}
myfile.close();
fftw_destroy_plan ( plan_forward );
fftw_free ( in );
fftw_free ( out );
return 0;
}
I think you may not have enough samples, particularly, reference this Electronics.StackExhcange post: https://electronics.stackexchange.com/q/12407/84272.
You're sampling for 50 samples, so 25 FFT bins. You're sampling at 1000 Hz, so 1000 / 2 / 25 == 250 Hz per FFT bins. Your bin resolution is too low.
I think you need to lower the sampling frequency or increase the number of samples.
Since your question in on SO, your code could use some indentation and style improvement to make it easier to read.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <fftw3.h>
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main(){
// use meaningful names for all the variables
int i;
double y;
int N = 550; // number of points acquired inside the window
double Fs = 200; // sampling frequency
double dF = Fs / N;
double T = 1 / Fs; // sample time
double f = 50; // frequency
double *in;
fftw_complex *out;
double t[N]; // time vector
double ff[N];
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];
}
for(int i = 0; i <= ((N/2)-1); i++){
ff[i] = (Fs * i) / N;
}
plan_forward = fftw_plan_dft_r2c_1d(N, in, out, FFTW_ESTIMATE);
fftw_execute(plan_forward);
double v[N];
// Here I have calculated the y axis of the spectrum in dB
for(int i = 0; i <= ((N/2)-1); i++){
v[i] = (20 * log(sqrt(out[i][0] * out[i][0] + out[i][1] * out[i][1]))) / N;
}
fstream myfile;
myfile.open("example2.txt", fstream::out);
myfile << "plot '-' using 1:2" << std::endl;
for(i = 0; i < ((N/2)-1); i++){
myfile << ff[i] << " " << v[i] << std::endl;
}
myfile.close();
fftw_destroy_plan(plan_forward);
fftw_free(in);
fftw_free(out);
return 0;
}
Your code can use more comments, especially before loops or function calls to specify their input value (purpose) and/or returning value (result).

fftw how to use the fftw and opencv to decomposed image into its magnitude and phase

Currently I have an image loaded in using opencv and trying to decompose it into its magnitude and phase components with fftw_plan_dft_2d. here is my code, but i could not obtain the correct result.
Edit(added from comment): i had finished the code in matlab,and i just try to convert the matlab code to c/c++, but i am not sure it is corrctely decomposed into magnitude and phase in this way
int main(){
cv::Mat img1=cv::imread("Lena.bmp",0);
cv::Mat img2;
fftw_complex *in;
fftw_complex *fft;
fftw_plan p;
int nx=img1.rows;
int ny=img1.cols;
int i,j;
img2=img1.clone();
in = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * nx * ny );
fft = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * nx * ny );
int **x=new int*[nx];
for(i=0;i<nx;i++)
x[i]=new int[ny];
int **y=new int*[nx];
for(i=0;i<nx;i++)
y[i]=new int[ny];
for(i=0;i<nx;i++)
for(j=0;j<ny;j++)
{
x[i][j]=j+1;
y[i][j]=i+1;
}
for( i = 0; i < nx ; i++ ) {
for( j = 0 ; j < ny ; j++ ) {
in[i*ny+j][0] =( ( double )img1.data[i * ny + j]*pow(-1,x[i][j]+y[i][j]));
in[i*ny+j][1] = 0.0;
}
}
p = fftw_plan_dft_2d( nx , ny, in, fft, FFTW_FORWARD, FFTW_ESTIMATE );
// perform FFT
fftw_execute( p );
cv::Mat mag(img1.size(),img1.type());
cv::Mat pha(img1.size(),img1.type());
double **magf=new double*[nx];
for(i=0;i<nx;i++)
magf[i]=new double[ny];
double **phaf=new double*[nx];
for(i=0;i<nx;i++)
phaf[i]=new double[ny];
fftw_complex *e = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*nx*ny);
float pi=3.14156;
for(i=0;i<nx;i++)
for(j=0;j<ny;j++)
{
/*fft[i*ny+j][0]/=(double)nx*ny;
fft[i*ny+j][1]/=(double)nx*ny;*/
magf[i][j]=(double)sqrt(pow(fft[i*ny+j][0],2.0)+pow(fft[i*ny+j][1],2.0));
phaf[i][j]=atan2(fft[i*ny+j][0],fft[i*ny+j][1]);
e[i*ny+j][0]=cos(phaf[i][j]);
e[i*ny+j][1]=sin(phaf[i][j]);
}
for(i=0;i<nx;i++)
for(j=0;j<ny;j++)
{
mag.data[i*ny+j]=magf[i][j];
}
cv::namedWindow("magnitude",1);
cv::imshow("magnitude",mag);
fftw_complex* G= (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*nx*ny);
fftw_complex* H= (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*nx*ny);
for(i=0;i<nx;i++)
for(j=0;j<ny;j++)
{
G[i*ny+j][0]=magf[i][j]*e[i*ny+j][0];
G[i*ny+j][1]=magf[i][j]*e[i*ny+j][1];
}
p = fftw_plan_dft_2d(nx,ny, G, H, FFTW_BACKWARD, FFTW_ESTIMATE);
//// normalize IFFT result
for( i = 0 ; i < ( nx * ny ) ; i++ ) {
H[i][0] /= ( double )( nx * ny );
}
// // copy IFFT result to img2's data
for( i = 0 ; i < nx ; i++ ) {
for( j = 0 ; j < ny ; j++ ) {
img2.data[i * ny + j] = ( uchar )(H[i * ny + j][0]*pow(-1,x[i][j]+y[i][j]));
}
}
cv::namedWindow("recover",1);
cv::imshow("recover",img2);
/*cv::namedWindow( "original_image", CV_WINDOW_AUTOSIZE );
cv::namedWindow( "IFFT", CV_WINDOW_AUTOSIZE );
cv::imshow( "original_image", img1 );
cv::imshow( "IFFT", img2 );
cv::waitKey(0);*/
cv::waitKey(0);
fftw_destroy_plan( p );
//fftw_destroy_plan( plan_b );
fftw_free( in );
fftw_free( fft );
//fftw_free( ifft );
return 0;
}

C/C++ Goertzel algorithm with complex output or magnitude+phase?

Does someone know where i can get code or a library to perform the Goertzel algorithm with a complex output?
(or any other 1-bin-DFT algorithm?)
The following is code that I wrote do this, some several years ago. Feel free to use it as you wish, with appropriate attribution.
goertzelfilter.h
/* goertzelfilter.h
*/
#ifndef GOERTZELFILTER_H_
#define GOERTZELFILTER_H_
#include <complex.h>
typedef struct goertzelfilterstruct {
double coeff ;
double sine ;
double cosine ;
} GoertzelFilter;
GoertzelFilter goertzelSetup( double normalizedfreq );
double complex goertzelFilterC( double *sample, int nsamples, GoertzelFilter *g );
#endif
goerzelfilter.c
/* goertzelfilter.c
*/
#include <math.h>
#include <stdlib.h>
#include <complex.h>
#include "goertzelfilter.h"
GoertzelFilter goertzelSetup( double normalizedfreq )
{
double w = 2*M_PI*normalizedfreq;
double wr, wi;
GoertzelFilter g;
wr = cos(w);
wi = sin(w);
g.coeff = 2 * wr;
g.cosine = wr;
g.sine = wi;
return g;
}
double complex goertzelFilterC( double *samples, int nsamples, GoertzelFilter *g )
{
double sprev = 0.0;
double sprev2 = 0.0;
double s, imag, real;
int n;
for (n=0; n<nsamples; n++ ) {
s = samples[n] + g->coeff * sprev - sprev2;
sprev2 = sprev;
sprev = s;
}
real = sprev*g->cosine - sprev2;
imag = -sprev*g->sine;
return real + I*imag;
}
And for a test, you can try this,
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <complex.h>
#include "goertzelfilter.h"
#define LEN(a) (sizeof(a)/sizeof(a[0]) )
int main() {
// This will hold our input and output data
double data[1024] = { 0. };
double complex filtered[1024] = { 0. };
// This will hold the filter constants
GoertzelFilter g = { 0. };
int n;
int nwindow = 16;
double f = 4./LEN(data) ;
// Generate data with noise
for ( n = 0 ; n < LEN(data) ; n++ ) {
data[n] = sin( n * (2.*M_PI) * f ) + 0.5*((float)rand()/RAND_MAX - 0.5);
}
// Set up the filter constants, note that we choose a frequency
g = goertzelSetup( f );
// Filter the data using a sliding window
for( n = 0 ; n < LEN(data)-nwindow ; n++ ) {
filtered[n+nwindow/2] = goertzelFilterC( &data[n], nwindow, &g )/nwindow;
}
// Print the real Valued Data (1st column) and complex valued Goertzel output
for( n = 0 ; n < LEN(data); n++ ) {
printf( "%g %g %g\n", data[n], creal(filtered[n]), cimag(filtered[n]) );
}
}
And here is a graph showing the input and output from the test code: