Google

main
dspblog
cyclic_signals
FFT_interpolation
FFT_interpolation_how_does_it_work
FFT_smoothness_cyclic
discrete_time_reconstruction
discrete_time_reconstruction2
Nyquist_on_the_edge
FFT_delay_special_case
Fourier_reconstruction
pulse_and_Nyquist
FFT_complexError
matlabOctave
matlab_upsampling
matlab_downsampling
FFT_delay
FFT_filter_example
FFT_interpolation_example
FFT_bin_frequencies
fit_signal
FFT_peaksearch_audio_example
matlab_binary_readwrite
Octavesvg
C
FFTW_example
looprecord
SNR
SNR3
SNR_example_96kAudio
SNR_FFT_correlation_example
lua
luagpib
luasplit
luadump
mnoofltk
wxLuaDll
wxLua_loadAsDll
wxLua_HelloWorld
wxLua_simpleButton
wxLua_resourceManagement
wxLua_XMLparser
DSP
IQ_LO
IQ_LO_2
optimum_receiver
DSP_basics
sampleRateChange_terms
dirac_pulse
freqresp_s
zfilter_example
freqresp_z
freqresp_z_sign
misc
zero_forcing_equalizer_example
nonminphase_inverse
periodic_spectrum
lagrange_multipliers
Entropy
RC_chopper
TRex450_setup
EP100
EP100SE
EP100Gremlins
essential_spares
tail_rotor
motoradjustment
blade_balancing
blade_repair
GAUI_SAE12A
Walkera43


valid html (click to verify)



prevupnextdisable ads

FFTW example

Summary

Example code for using the FFTW library.

Code

/* ****************************************************
 * FFTW example
 * Markus Nentwig, 2007
 * This program is in the public domain
 * ****************************************************/
#include <stdio.h>
#include <math.h>
#include <complex.h> 
#include <fftw3.h>
#include <stdlib.h>

int main(void){
  /* ****************************************************
   * Allocate memory
   * ****************************************************/
  int n=128;
  fftw_complex* b1=fftw_malloc(sizeof(fftw_complex)*n);
  fftw_complex* b2=fftw_malloc(sizeof(fftw_complex)*n);

  /* ****************************************************
   * Create forward FFT plan from b1 into b2
   * ****************************************************/
  fftw_plan p1=fftw_plan_dft_1d(n, b1, b2, FFTW_FORWARD, FFTW_ESTIMATE);

  /* ****************************************************
   * Create reverse FFT plan from b2 into b1
   * ****************************************************/
  fftw_plan p2=fftw_plan_dft_1d(n, b2, b1, FFTW_BACKWARD, FFTW_ESTIMATE);

  /* ****************************************************
   * Generate a single sine wave cycle
   * ****************************************************/
  int i;
  for (i=0; i < n; ++i){
    b1[i]=sin((double)i/(double)n*2.0*M_PI);
  }
  
  /* ****************************************************
   * Execute FFT from b1 into b2
   * ****************************************************/
  fftw_execute(p1);

  /* ****************************************************
   * Dump the "spectrum".
   * Result: 
   * First bin ("DC  term") is zero
   * * i*n/2 in 2nd bin 
   * * -i*n/2 in last bin 
   * * all other bins are zero
   * ****************************************************/
  for (i=0; i < n; ++i){
    printf("%1.7f\t%1.7f\n", creal(b2[i]), cimag(b2[i]));
  };

  /* ****************************************************
   * IFFT back into b1
   * ****************************************************/
  fftw_execute(p2);
  printf("\n");

  /* ****************************************************
   * Result: Original waveform scaled by n
   * ****************************************************/
  for (i=0; i < n; ++i){
    printf("%1.7f\t%1.7f\n", creal(b1[i]), cimag(b1[i]));
  };

  /* ****************************************************
   * Clean up
   * ****************************************************/
  fftw_destroy_plan(p1);
  fftw_destroy_plan(p2);

  fftw_free(b1);
  fftw_free(b2);
  return 0;
}
Download

Compiling the example

gcc fftdemo.c -lfftw3 -lm

If FFTW is not installed in the regular place, add
-I/home/my_username/my_include_folder -L/home/my_username/my_lib_folder

where the /home/my_username/my_include_folder folder should contain the file fftw3.h, and the /home/my_username_my_lib_folder folder should contain the file libfftw3.a, respectively.


prevupnextdisable ads

© Markus Nentwig 2007-2008
The content of this page is provided without any warranty and may not be reproduced without permission.

Comments? Questions?

Please send me a mail! mnentwig@elisanet.fi