Skip to content

Commit

Permalink
Make possible to use double and own fft lib.
Browse files Browse the repository at this point in the history
Projects which use the library may have own fft librray.
Projects may use double precision floating point numbers.
  • Loading branch information
dcherednik committed Jul 6, 2024
1 parent 311a633 commit 41705ed
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 48 deletions.
81 changes: 49 additions & 32 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,50 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -fsanitize=address -fno-omit-frame-pointer")

project(gha)
add_library(gha src/gha.c src/sle.c)

set(SOURCE_FFT_LIB
src/3rd/kissfft/kiss_fft.c
src/3rd/kissfft/tools/kiss_fftr.c
)

set_source_files_properties(
src/3rd/kissfft/kiss_fft.c
src/3rd/kissfft/tools/kiss_fftr.c
PROPERTIES COMPILE_FLAGS -Dkiss_fft_scalar=float
)

add_library(fft_impl STATIC ${SOURCE_FFT_LIB})
target_include_directories(
fft_impl
PRIVATE
src/3rd/kissfft
)
if (NOT GHA_FFT_LIB)

set(SOURCE_FFT_LIB
src/3rd/kissfft/kiss_fft.c
src/3rd/kissfft/tools/kiss_fftr.c
)
add_library(gha_fft_impl STATIC ${SOURCE_FFT_LIB})
set(GHA_FFT_LIB gha_fft_impl)

target_include_directories(
gha_fft_impl
PRIVATE
src/3rd/kissfft
)

target_include_directories(
gha
PRIVATE
src/3rd/kissfft
)

endif()

if (GHA_USE_DOUBLE_API)
set_source_files_properties(
src/gha.c
src/sle.c
src/3rd/kissfft/kiss_fft.c
src/3rd/kissfft/tools/kiss_fftr.c
test/main.c
test/dtmf.c
test/ut.c
PROPERTIES COMPILE_FLAGS -DGHA_USE_DOUBLE_API
)
endif()

add_library(gha src/gha.c src/sle.c)
target_include_directories(
gha
PRIVATE
src/3rd
src/3rd/kissfft
.
)
target_link_libraries(gha fft_impl)
target_link_libraries(gha ${GHA_FFT_LIB})

add_definitions("-Wall -O2 -g")

Expand Down Expand Up @@ -60,20 +76,21 @@ target_include_directories(
target_link_libraries(ut gha m)

enable_testing()
add_test(gha_test_simple_1000_0_a main ../test/data/1000hz_0.85.pcm 0 1024 0.142476 0.0000 0.850000)
add_test(gha_test_simple_1000_0_b main ../test/data/1000hz_0.85.pcm 0 1000 0.142476 0.0000 0.850000)
add_test(gha_test_simple_1000_0_c main ../test/data/1000hz_0.85.pcm 0 800 0.142476 0.0000 0.850000)
add_test(gha_test_simple_1000_90_a main ../test/data/1000hz_0.85.pcm 11 1024 0.142476 1.5670 0.850000)
add_test(gha_test_simple_1000_90_b main ../test/data/1000hz_0.85.pcm 11 1000 0.142476 1.5670 0.850000)
add_test(gha_test_simple_1000_90_c main ../test/data/1000hz_0.85.pcm 11 800 0.142476 1.5670 0.850000)
add_test(gha_test_simple_1000_0_a main ${CMAKE_CURRENT_SOURCE_DIR}/test/data/1000hz_0.85.pcm 0 1024 0.142476 0.0000 0.850000)
add_test(gha_test_simple_1000_0_b main ${CMAKE_CURRENT_SOURCE_DIR}/test/data/1000hz_0.85.pcm 0 1000 0.142476 0.0000 0.850000)
add_test(gha_test_simple_1000_0_c main ${CMAKE_CURRENT_SOURCE_DIR}/test/data/1000hz_0.85.pcm 0 800 0.142476 0.0000 0.850000)
add_test(gha_test_simple_1000_90_a main ${CMAKE_CURRENT_SOURCE_DIR}/test/data/1000hz_0.85.pcm 11 1024 0.142476 1.5670 0.850000)
add_test(gha_test_simple_1000_90_b main ${CMAKE_CURRENT_SOURCE_DIR}/test/data/1000hz_0.85.pcm 11 1000 0.142476 1.5670 0.850000)
add_test(gha_test_simple_1000_90_c main ${CMAKE_CURRENT_SOURCE_DIR}/test/data/1000hz_0.85.pcm 11 800 0.142476 1.5670 0.850000)


add_test(gha_test_simple_20000_0_a main ${CMAKE_CURRENT_SOURCE_DIR}/test/data/20000hz_0.85.pcm 0 1024 2.8495171 0.0000 0.850000)
add_test(gha_test_simple_20000_0_b main ${CMAKE_CURRENT_SOURCE_DIR}/test/data/20000hz_0.85.pcm 0 500 2.8495171 0.0000 0.850000)
add_test(gha_test_simple_20000_0_c main ${CMAKE_CURRENT_SOURCE_DIR}/test/data/20000hz_0.85.pcm 0 128 2.8495171 0.0000 0.850000)
add_test(gha_test_simple_20000_0_d main ${CMAKE_CURRENT_SOURCE_DIR}/test/data/20000hz_0.85.pcm 0 96 2.8495171 0.0000 0.850000)


add_test(gha_test_simple_20000_0_a main ../test/data/20000hz_0.85.pcm 0 1024 2.8495171 0.0000 0.850000)
add_test(gha_test_simple_20000_0_b main ../test/data/20000hz_0.85.pcm 0 500 2.8495171 0.0000 0.850000)
add_test(gha_test_simple_20000_0_c main ../test/data/20000hz_0.85.pcm 0 128 2.8495171 0.0000 0.850000)
add_test(gha_test_simple_20000_0_d main ../test/data/20000hz_0.85.pcm 0 96 2.8495171 0.0000 0.850000)
add_test(gha_test_dtmf_1 dtmf ${CMAKE_CURRENT_SOURCE_DIR}/test/data/dtmf.pcm 32 256 0.547416 0.201057 0.949511 0.200154)

add_test(gha_test_dtmf_1 dtmf ../test/data/dtmf.pcm 32 256 0.547416 0.201057 0.949511 0.200154)

add_test(ut ut)
7 changes: 6 additions & 1 deletion include/libgha.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ extern "C"
{
#endif /* __cplusplus */

#define FLOAT float
#ifdef GHA_USE_DOUBLE_API
# define kiss_fft_scalar double
# define FLOAT double
#else
# define FLOAT float
#endif

#include <stddef.h>

Expand Down
2 changes: 1 addition & 1 deletion src/gha.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <include/libgha.h>

#include <kissfft/tools/kiss_fftr.h>
#include <tools/kiss_fftr.h>

/*
* Ref: http://www.apsipa.org/proceedings_2009/pdf/WA-L3-3.pdf
Expand Down
5 changes: 3 additions & 2 deletions test/common.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include <include/libgha.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>

static inline int compare_phase(float a, float b, float delta) {
static inline int compare_phase(FLOAT a, FLOAT b, FLOAT delta) {
if (fabs(a - b) < delta)
return 0;
a = fmod(a + M_PI, 2 * M_PI);
Expand All @@ -15,7 +16,7 @@ static inline int compare_phase(float a, float b, float delta) {
return -1;
}

static inline int load_file(const char* name, size_t len, size_t offset, size_t bits, float* buf)
static inline int load_file(const char* name, size_t len, size_t offset, size_t bits, FLOAT* buf)
{
union {
char b[4];
Expand Down
16 changes: 7 additions & 9 deletions test/dtmf.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <include/libgha.h>

#include "common.h"

void usage(const char* selfname) {
Expand All @@ -12,11 +10,11 @@ void usage(const char* selfname) {
fprintf(stderr, "EXPECTED_MAGNITUDE_(LOW|HIGH) - expected magnitude (0 - 1)\n");
}

static void calc_resuidal(float* resuidal, size_t size, void* user_ctx)
static void calc_resuidal(FLOAT* resuidal, size_t size, void* user_ctx)
{
int i;
double s = 0.0;
float* result = (float*)user_ctx;
FLOAT* result = (FLOAT*)user_ctx;
for (i = 0; i < size; i++) {
s += resuidal[i] * resuidal[i];
}
Expand All @@ -33,8 +31,8 @@ int main(int argc, char** argv) {

gha_ctx_t ctx;

float* buf = malloc(len * sizeof(float));
float* buf2 = malloc(len * sizeof(float));
FLOAT* buf = malloc(len * sizeof(FLOAT));
FLOAT* buf2 = malloc(len * sizeof(FLOAT));
if (!buf || !buf2)
abort();

Expand All @@ -45,10 +43,10 @@ int main(int argc, char** argv) {
}

//Make copy of data to adjust extracted params
memcpy(buf2, buf, sizeof(float) * len);
memcpy(buf2, buf, sizeof(FLOAT) * len);
ctx = gha_create_ctx(len);

float resuidal;
FLOAT resuidal;
gha_set_user_resuidal_cb(&calc_resuidal, &resuidal, ctx);
if (!ctx) {
fprintf(stderr, "Unable to create gha ctx\n");
Expand All @@ -59,7 +57,7 @@ int main(int argc, char** argv) {
struct gha_info res[2];
gha_extract_many_simple(buf, &res[0], 2, ctx);

float resuidal_1 = resuidal;
FLOAT resuidal_1 = resuidal;

if (res[0].frequency > res[1].frequency) {
struct gha_info tmp;
Expand Down
4 changes: 1 addition & 3 deletions test/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <include/libgha.h>

#include "common.h"

void usage(const char* selfname) {
Expand All @@ -24,7 +22,7 @@ int main(int argc, char** argv) {

gha_ctx_t ctx;

float* buf = malloc(len * sizeof(float));
FLOAT* buf = malloc(len * sizeof(FLOAT));
if (!buf)
abort();

Expand Down

0 comments on commit 41705ed

Please sign in to comment.