From 6db1849b7465b1c250c8b9c9ed4d401657e3322d Mon Sep 17 00:00:00 2001 From: Dominic Spill Date: Thu, 28 Jul 2016 17:08:26 +0100 Subject: [PATCH] Add some fftw magic to proceedings --- host/cmake/modules/FindFFTW.cmake | 22 +++++++++++++++++ host/hackrf-tools/src/CMakeLists.txt | 1 + host/hackrf-tools/src/hackrf_sweep.c | 37 ++++++++++++++++++++++++++-- 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 host/cmake/modules/FindFFTW.cmake diff --git a/host/cmake/modules/FindFFTW.cmake b/host/cmake/modules/FindFFTW.cmake new file mode 100644 index 00000000..00c3401c --- /dev/null +++ b/host/cmake/modules/FindFFTW.cmake @@ -0,0 +1,22 @@ +# - Find FFTW +# Find the native FFTW includes and library +# +# FFTW_INCLUDES - where to find fftw3.h +# FFTW_LIBRARIES - List of libraries when using FFTW. +# FFTW_FOUND - True if FFTW found. + +if (FFTW_INCLUDES) + # Already in cache, be silent + set (FFTW_FIND_QUIETLY TRUE) +endif (FFTW_INCLUDES) + +find_path (FFTW_INCLUDES fftw3.h) + +find_library (FFTW_LIBRARIES NAMES fftw3) + +# handle the QUIETLY and REQUIRED arguments and set FFTW_FOUND to TRUE if +# all listed variables are TRUE +include (FindPackageHandleStandardArgs) +find_package_handle_standard_args (FFTW DEFAULT_MSG FFTW_LIBRARIES FFTW_INCLUDES) + +mark_as_advanced (FFTW_LIBRARIES FFTW_INCLUDES) diff --git a/host/hackrf-tools/src/CMakeLists.txt b/host/hackrf-tools/src/CMakeLists.txt index 6f925cde..1873165c 100644 --- a/host/hackrf-tools/src/CMakeLists.txt +++ b/host/hackrf-tools/src/CMakeLists.txt @@ -64,6 +64,7 @@ if(MSVC) LIST(APPEND TOOLS_LINK_LIBS libgetopt_static) endif() +LIST(APPEND TOOLS_LINK_LIBS m fftw3f) target_link_libraries(hackrf_max2837 ${TOOLS_LINK_LIBS}) target_link_libraries(hackrf_si5351c ${TOOLS_LINK_LIBS}) diff --git a/host/hackrf-tools/src/hackrf_sweep.c b/host/hackrf-tools/src/hackrf_sweep.c index e2cab8d6..a71c2a9c 100644 --- a/host/hackrf-tools/src/hackrf_sweep.c +++ b/host/hackrf-tools/src/hackrf_sweep.c @@ -31,6 +31,8 @@ #include #include #include +#include +#include #ifndef bool typedef int bool; @@ -137,8 +139,11 @@ uint32_t amp_enable; bool antenna = false; uint32_t antenna_enable; -bool sample_rate = false; -uint32_t sample_rate_hz; +int fftSize; +fftwf_complex *fftwIn = NULL; +fftwf_complex *fftwOut = NULL; +fftwf_plan fftwPlan = NULL; +double* pwr; int rx_callback(hackrf_transfer* transfer) { /* This is where we need to do interesting things with the samples @@ -148,11 +153,33 @@ int rx_callback(hackrf_transfer* transfer) { */ ssize_t bytes_to_write; ssize_t bytes_written; + uint16_t* buf_short, frequency; + int i, j; if( fd != NULL ) { byte_count += transfer->valid_length; bytes_to_write = transfer->valid_length; + buf_short = (uint16_t*) transfer->buffer; + for(j=0; j<16; j++) { + if(buf_short[0] == 0x7F7F) { + frequency = buf_short[1]; + fprintf(stderr, "Received sweep buffer(%dMHz)\n", frequency); + } + /* copy to fftwIn as floats */ + buf_short = buf_short + 2; + for(i=0; i<2046; i+=2) { + fftwIn[i][0] = (float) buf_short[i]; + fftwIn[i][1] = (float) buf_short[i+1]; + } + buf_short = buf_short + 8190; + fftwf_execute(fftwPlan); + for (i=0; i < fftSize; i++) { + pwr[i] += pow(fftwOut[i][0], 2) + pow(fftwOut[i][1], 2); + fprintf(stderr, "%f\n", pwr[i]); + } + fprintf(stderr, "\n"); + } bytes_written = fwrite(transfer->buffer, 1, bytes_to_write, fd); if (bytes_written != bytes_to_write) { @@ -269,6 +296,12 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } } + + fftSize = 2048; + fftwIn = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * fftSize); + fftwOut = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * fftSize); + fftwPlan = fftwf_plan_dft_1d(fftSize, fftwIn, fftwOut, FFTW_FORWARD, FFTW_MEASURE); + pwr = (double*)fftwf_malloc(sizeof(double) * fftSize); result = hackrf_init(); if( result != HACKRF_SUCCESS ) {