aboutsummaryrefslogtreecommitdiffstats
path: root/include/osmocom/dsp/cxvec_math.h
blob: a35f0a59e1d14450a03e4b136d69cf59c30ba80b (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * cxvec_math.h
 *
 * Complex vectors math and signal processing
 *
 * Copyright (C) 2011  Sylvain Munaut <tnt@246tNt.com>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef __OSMO_DSP_CXVEC_MATH_H__
#define __OSMO_DSP_CXVEC_MATH_H__

/*! \defgroup cxvec_math Complex vectors math and signal processing
 *  \ingroup cxvec
 *  @{
 */

/*! \file cxvec_math.h
 *  \brief Osmocom Complex vectors math header
 */

#include <complex.h>
#include <math.h>

#include <osmocom/dsp/cxvec.h>


	/* Generic math stuff */

#define M_PIf (3.14159265358979323846264338327f) /*!< \brief PI value float */

/*! \brief Unnormalized sinc function
 *  \param[in] x Value for which to compute the sinc function.
 *  \returns The sinc(x) value
 *
 *  The function is defined as \f$\frac{\sin(x)}{x}\f$
 */
static inline float
osmo_sinc(float x)
{
	if ((x >= 0.01f) || (x <= -0.01f)) return (sinf(x)/x);
	return 1.0f;
}

/*! \brief Squared norm of a given complex
 *  \param[in] c Complex number for which to compute the squared norm
 *  \returns \f$|c|^2\f$
 */
static inline float
osmo_normsqf(float complex c)
{
	return crealf(c) * crealf(c) + cimagf(c) * cimagf(c);
}


	/* Complex vector math */

struct osmo_cxvec *
osmo_cxvec_scale(const struct osmo_cxvec *in, float complex scale,
                 struct osmo_cxvec *out);

struct osmo_cxvec *
osmo_cxvec_rotate(const struct osmo_cxvec *in, float freq_shift,
                  struct osmo_cxvec *out);

/*! \brief Various possible types of convolution span */
enum osmo_cxvec_conv_type {
	/*! \brief Full span (every possible overlap of f onto g) */
	CONV_FULL_SPAN,	
	/*! \brief Every possible full overlap of f onto g */
	CONV_OVERLAP_ONLY,
	/*! \brief Center f sequence on every g sample */
	CONV_NO_DELAY,
};

struct osmo_cxvec *
osmo_cxvec_convolve(const struct osmo_cxvec *f, const struct osmo_cxvec *g,
                    enum osmo_cxvec_conv_type type, struct osmo_cxvec *out);

struct osmo_cxvec *
osmo_cxvec_correlate(const struct osmo_cxvec *f, const struct osmo_cxvec *g,
                     int g_corr_step, struct osmo_cxvec *out);

float complex
osmo_cxvec_interpolate_point(const struct osmo_cxvec *cv, float pos);

/*! \brief Various possible peak finding algorithms */
enum osmo_cxvec_peak_alg {
	/*! \brief Weigthed position for the max pwr window */
	PEAK_WEIGH_WIN,	
	/*! \brief Weighted position of the peak centered window */
	PEAK_WEIGH_WIN_CENTER,
	/*! \brief Early-Late balancing around peak */
	PEAK_EARLY_LATE,
};

float
osmo_cxvec_peak_energy_find(const struct osmo_cxvec *cv, int win_size,
                            enum osmo_cxvec_peak_alg alg,
                            float complex *peak_val_p);

struct osmo_cxvec *
osmo_cxvec_sig_normalize(const struct osmo_cxvec *sig,
                         int decim, float freq_shift,
                         struct osmo_cxvec *out);

/*! @} */

#endif /* __OSMO_DSP_CXVEC_MATH_H__ */