SAF
Loading...
Searching...
No Matches
saf_utility_misc.h
Go to the documentation of this file.
1/*
2 * Copyright 2020 Leo McCormack
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
13 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
15 */
16
27
28#ifndef SAF_MISC_H_INCLUDED
29#define SAF_MISC_H_INCLUDED
30
31#ifdef __cplusplus
32extern "C" {
33#endif /* __cplusplus */
34
35#include "saf_utility_complex.h"
36
37#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && !defined(__STDC_NO_ATOMICS__)
38 #include <stdatomic.h>
39 typedef _Atomic int _Atomic_INT32;
40 typedef _Atomic float _Atomic_FLOAT32;
41#else
42 typedef int _Atomic_INT32;
43 typedef float _Atomic_FLOAT32;
44#endif
45
46/* Cross-platform sleep macro (slightly modified), originally taken from:
47 * https://cboard.cprogramming.com/c-programming/170381-cross-platform-wait-sleep.html */
48#ifdef _WIN32
49 /* For Windows (32- and 64-bit) */
50# include <windows.h>
51# define SAF_SLEEP(msecs) Sleep(msecs)
52#elif defined(__unix) || defined(__APPLE__)
53 /* For linux, OSX, and other unixes */
54# ifndef _POSIX_C_SOURCE
55# define _POSIX_C_SOURCE 199309L /* or greater */
56# endif
57# include <time.h>
58# define SAF_SLEEP(msecs) do { \
59 struct timespec ts; \
60 ts.tv_sec = msecs/1000; \
61 ts.tv_nsec = msecs%1000*1000; \
62 nanosleep(&ts, NULL); \
63} while (0)
64#else
65# error "Unknown system"
66#endif
67
93#define SAF_CASSERT(predicate, file) _impl_CASSERT_LINE(predicate,__LINE__,file)
95#define _impl_PASTE(a,b) a##b
97#define _impl_CASSERT_LINE(predicate, line, file) \
98 typedef char _impl_PASTE(assertion_failed_##file##_,line)[2*!!(predicate)-1];
99
101void convert_0_360To_m180_180(float* dirs_deg,
102 int nDirs);
103
110int nextpow2(int numsamp);
111
120void lagrangeWeights(int N,
121 float* x,
122 int len_x,
123 float* weights);
124
140void findERBpartitions(/* Input Arguments */
141 float* centerFreq,
142 int nBands,
143 float maxFreqLim,
144 /* Output Arguments */
145 int** erb_idx,
146 float** erb_freqs,
147 int* nERBBands);
148
150void randperm(/* Input Arguments */
151 int len,
152 /* Output Arguments */
153 int* randperm_inds);
154
165long double factorial(int n);
166
175float matlab_fmodf(float x, float y);
176
186void cxcorr(float* a,
187 float* b,
188 float* x_ab,
189 size_t la,
190 size_t lb);
191
198void rand_m1_1(float* vector,
199 int length);
200
208void rand_cmplx_m1_1(float_complex* vector,
209 int length);
210
217void rand_0_1(float* vector,
218 int length);
219
229void convd(double* x,
230 double* h,
231 int len_x,
232 int len_h,
233 double* y);
234
244void convz(double_complex* x,
245 double_complex* h,
246 int len_x,
247 int len_h,
248 double_complex* y);
249
257void polyd_v(double* x,
258 double* poly,
259 int len_x);
260
268void polyz_v(double_complex* x,
269 double_complex* poly,
270 int len_x);
271
279void polyd_m(double* X,
280 double_complex* poly,
281 int size_x);
282
284float sumf(float* values,
285 int nValues);
286
291int anyLessThanf(float* values,
292 int nValues,
293 float threshold);
294
310void unique_i(int* input,
311 int nInputs,
312 int** uniqueVals,
313 int** uniqueInds,
314 int* nUnique);
315
328void findCombinations(int* arrValues,
329 int nValues,
330 int nElements,
331 int** comb,
332 int* nComb);
333
363void gexpm(float* D,
364 int sizeD,
365 int m1,
366 float* Y);
367
368
369#ifdef __cplusplus
370}/* extern "C" */
371#endif /* __cplusplus */
372
373#endif /* SAF_MISC_H_INCLUDED */
374 /* doxygen addtogroup Utilities */
void findCombinations(int *arrValues, int nValues, int nElements, int **comb, int *nComb)
Given an array of values, find all the possible combinations (nCr) for subgroups of "nElements"; deri...
void findERBpartitions(float *centerFreq, int nBands, float maxFreqLim, int **erb_idx, float **erb_freqs, int *nERBBands)
This function takes a frequency vector and groups its frequencies into critical bands [Equivalent-Rec...
void convert_0_360To_m180_180(float *dirs_deg, int nDirs)
Wraps around any angles exeeding 180 degrees (e.g., 200-> -160)
float matlab_fmodf(float x, float y)
C fmodf function, except it behaves like 'mod' in Matlab (i.e.
void unique_i(int *input, int nInputs, int **uniqueVals, int **uniqueInds, int *nUnique)
Finds the unique values (and their indices) of the input vector.
void cxcorr(float *a, float *b, float *x_ab, size_t la, size_t lb)
Calculates the cross correlation between two vectors.
void rand_0_1(float *vector, int length)
Generates random numbers between 0 and 1 and stores them in the input vector.
void randperm(int len, int *randperm_inds)
Returns the indices required to randomly permute a vector of length 'len'.
void rand_cmplx_m1_1(float_complex *vector, int length)
Generates random numbers between -1 and 1 and stores them in the input vector for both the real and i...
void gexpm(float *D, int sizeD, int m1, float *Y)
Numerically solves first-order, linear, homogeneous differential equation systems,...
void polyd_m(double *X, double_complex *poly, int size_x)
Convert roots of a matrix to polynomial (real double precision)
long double factorial(int n)
Factorial, accurate up to n<=25.
float sumf(float *values, int nValues)
Returns the sum of all values.
void convd(double *x, double *h, int len_x, int len_h, double *y)
Basic 1-D direct convolution in the time-domain (real double precision)
int nextpow2(int numsamp)
A simple function which returns the next power of 2.
void polyd_v(double *x, double *poly, int len_x)
Convert roots of a vector to polynomial (real double precision)
void polyz_v(double_complex *x, double_complex *poly, int len_x)
Convert roots of a vector to polynomial (complex double precision)
void lagrangeWeights(int N, float *x, int len_x, float *weights)
Computes Lagrange interpolation weights of order 'N' for value 'x'.
int anyLessThanf(float *values, int nValues, float threshold)
Returns 1, if any value in 'values' (nValues x 1) is less than 'threshold', otherwise,...
void convz(double_complex *x, double_complex *h, int len_x, int len_h, double_complex *y)
Basic 1-D direct convolution in the time-domain (complex double precision)
void rand_m1_1(float *vector, int length)
Generates random numbers between -1 and 1 and stores them in the input vector.
Contains wrappers for handling complex numbers across both C99-compliant compilers and Microsoft Visu...