aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniil Cherednik <dan.cherednik@gmail.com>2024-07-06 10:56:15 +0000
committerDaniil Cherednik <dan.cherednik@gmail.com>2024-07-06 10:56:15 +0000
commit41705edc6d36ad013cbcb171d53891f3c33764d8 (patch)
treec88a75ab8089720d0e354037d0585eb1d51af20a
parent311a633f24410ce3302526941b32639aa8869654 (diff)
downloadlibgha-41705edc6d36ad013cbcb171d53891f3c33764d8.tar.gz
Make possible to use double and own fft lib.HEADmaster
Projects which use the library may have own fft librray. Projects may use double precision floating point numbers.
-rw-r--r--CMakeLists.txt81
-rw-r--r--include/libgha.h7
-rw-r--r--src/gha.c2
-rw-r--r--test/common.h5
-rw-r--r--test/dtmf.c16
-rw-r--r--test/main.c4
6 files changed, 67 insertions, 48 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f7cbd88..53c9055 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -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")
@@ -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)
diff --git a/include/libgha.h b/include/libgha.h
index f3221f8..5ff09c8 100644
--- a/include/libgha.h
+++ b/include/libgha.h
@@ -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>
diff --git a/src/gha.c b/src/gha.c
index cd61312..d09fd39 100644
--- a/src/gha.c
+++ b/src/gha.c
@@ -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
diff --git a/test/common.h b/test/common.h
index 6757422..f29461d 100644
--- a/test/common.h
+++ b/test/common.h
@@ -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);
@@ -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];
diff --git a/test/dtmf.c b/test/dtmf.c
index ae73a6b..1a9db3f 100644
--- a/test/dtmf.c
+++ b/test/dtmf.c
@@ -1,5 +1,3 @@
-#include <include/libgha.h>
-
#include "common.h"
void usage(const char* selfname) {
@@ -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];
}
@@ -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();
@@ -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");
@@ -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;
diff --git a/test/main.c b/test/main.c
index c9725f0..6565ce1 100644
--- a/test/main.c
+++ b/test/main.c
@@ -1,5 +1,3 @@
-#include <include/libgha.h>
-
#include "common.h"
void usage(const char* selfname) {
@@ -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();