aboutsummaryrefslogtreecommitdiffstats
path: root/include/libgha.h
blob: 5ff09c880fdccc218994aaca8b593232a5936bbc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#ifndef LIBGHA_H
#define LIBGHA_H

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */

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

#include <stddef.h>

typedef struct gha_ctx *gha_ctx_t;

struct gha_info {
	FLOAT frequency;
	FLOAT phase;
	FLOAT magnitude;
};

/*
 * Create context to perform GHA, size is number of samples provided to analyze.
 * Size must be even
 *
 * Returns null in case of fail.
 *
 */
gha_ctx_t gha_create_ctx(size_t size);

/*
 * Free GHA context
 */
void gha_free_ctx(gha_ctx_t ctx);

/*
 * Performs one GHA step for given PCM signal,
 * the result will be writen in to given gha_info structure
 *
 * Complexity: O(n * log(n)),
 * where n is number of samples to anayze
 *
 */
void gha_analyze_one(const FLOAT* pcm, struct gha_info* info, gha_ctx_t ctx);

/*
 * Performs one GHA step and extracts analysed harmonic from given PCM signal
 * the result will be writen in to given gha_info structure
 *
 * Complexity: O(n * log(n)),
 * where n is number of samples to anayze
 *
 */
void gha_extract_one(FLOAT* pcm, struct gha_info* info, gha_ctx_t ctx);

/*
 * Performs k GHA steps and extracts analysed harmonics from given PCM signal.
 *
 * The result will be writen in to given gha_info structures
 * corresponding ammount of memory should be allocated (k * sizeof(struct gha_info))
 *
 * Effectively this function is equivalent of calling gha_extract_one k times
 *
 * Complexity: O(n * log(n) * k),
 * where n is number of samples to anayze, k is number of harmonics to extract
 *
 */
void gha_extract_many_simple(FLOAT* pcm, struct gha_info* info, size_t k, gha_ctx_t ctx);

/*
 * Performs multidimensional optimization of extracted harmonics.
 *
 * Given gha_info will be adjusted to minimize resuidal level.
 * Newton multidimensional optimization method is used.
 *
 * Complexity: O(k^2 * n + k^3)
 * where n is number of samples to anayze, k is number of harmonics to extract
 *
 */
int gha_adjust_info(const FLOAT* pcm, struct gha_info* info, size_t k, gha_ctx_t ctx);

/*
 * Set callback to perform action on resuidal pcm signal.
 *
 * user_ctx is used to pass user context in to callback
 *
 */
void gha_set_user_resuidal_cb(void (*cb)(FLOAT* resuidal, size_t size, void* user_ctx), void* user_ctx, gha_ctx_t ctx);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif