Skip to content

Instantly share code, notes, and snippets.

@dmazzer
Last active September 5, 2018 23:46
Show Gist options
  • Save dmazzer/c674185befb215eb09ef4d8557ceeb32 to your computer and use it in GitHub Desktop.
Save dmazzer/c674185befb215eb09ef4d8557ceeb32 to your computer and use it in GitHub Desktop.
FFTW3 example.
#include <complex.h>
#include <fftw3.h>
#include <stdio.h>
int main(void)
{
int N = 8;
double *in;
fftw_complex *d_out;
double *i_out;
fftw_plan fft_plan_direct;
fftw_plan fft_plan_inverse;
in = fftw_alloc_real(N); // (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*N);
d_out = fftw_alloc_complex(N); // (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*N);
i_out = fftw_alloc_real(N); // (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*N);
fft_plan_direct = fftw_plan_dft_r2c_1d(N, in, d_out, FFTW_PATIENT);
fft_plan_inverse = fftw_plan_dft_c2r_1d(N, d_out, i_out, FFTW_PATIENT);
// printf("plan1 \n");
// fftw_print_plan(fft_plan_direct);
// printf("\n");
// printf("plan2 \n");
// fftw_print_plan(fft_plan_inverse);
// printf("\n");
// printf("\n");
printf("input array: ");
for(int i=0; i<N ; i++)
{
in[i] = i;
d_out[i] = 9+9*_Complex_I;
printf("%.1f, ", in[i]);
}
printf("\n");
fftw_execute(fft_plan_direct);
printf("input array: ");
for(int i=0; i<N ; i++)
printf("%.1f, ", in[i]);
printf("\n");
printf("direct output array: ");
for(int i=0; i<N ; i++)
printf("%.1f + i%.1f, ", creal(d_out[i]), cimag(d_out[i]));
printf("\n");
fftw_execute(fft_plan_inverse);
printf("direct output array: ");
for(int i=0; i<N ; i++)
printf("%.1f + i%.1f, ", creal(d_out[i]), cimag(d_out[i]));
printf("\n");
printf("inverse output array: ");
for(int i=0; i<N ; i++)
{
i_out[i] *= 1./N;
printf("%.1f, ", i_out[i]);
}
printf("\n");
fftw_destroy_plan(fft_plan_direct);
fftw_destroy_plan(fft_plan_inverse);
fftw_free(in);
fftw_free(d_out);
fftw_free(i_out);
return 0;
}
TARGET = ex1
LIBS = -lfftw3 -lm
CC = gcc
CFLAGS = -g -Wall -fPIC #-Ofast -ffast-math
.PHONY: clean all default
default: $(TARGET)
all: default
OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c))
HEADERS = $(wildcard *.h)
PROTO_OBJECTS = $(wildcard *.proto)
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
.PRECIOUS: $(TARGET) $(OBJECTS)
$(TARGET): $(OBJECTS)
$(CC) $(OBJECTS) -Wall $(LIBS) -o $@
clean:
-rm -f *.o
-rm -f $(TARGET)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment