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
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/* Cross-platform sleep macro (slightly modified), originally taken from:
38 * https://cboard.cprogramming.com/c-programming/170381-cross-platform-wait-sleep.html */
39#ifdef _WIN32
40 /* For Windows (32- and 64-bit) */
41# include <windows.h>
42# define SAF_SLEEP(msecs) Sleep(msecs)
43#elif defined(__unix) || defined(__APPLE__)
44 /* For linux, OSX, and other unixes */
45# ifndef _POSIX_C_SOURCE
46# define _POSIX_C_SOURCE 199309L /* or greater */
47# endif
48# include <time.h>
49# define SAF_SLEEP(msecs) do { \
50 struct timespec ts; \
51 ts.tv_sec = msecs/1000; \
52 ts.tv_nsec = msecs%1000*1000; \
53 nanosleep(&ts, NULL); \
54} while (0)
55#else
56# error "Unknown system"
57#endif
58
84#define SAF_CASSERT(predicate, file) _impl_CASSERT_LINE(predicate,__LINE__,file)
86#define _impl_PASTE(a,b) a##b
88#define _impl_CASSERT_LINE(predicate, line, file) \
89 typedef char _impl_PASTE(assertion_failed_##file##_,line)[2*!!(predicate)-1];
90
92void convert_0_360To_m180_180(float* dirs_deg,
93 int nDirs);
94
101int nextpow2(int numsamp);
102
111void lagrangeWeights(int N,
112 float* x,
113 int len_x,
114 float* weights);
115
131void findERBpartitions(/* Input Arguments */
132 float* centerFreq,
133 int nBands,
134 float maxFreqLim,
135 /* Output Arguments */
136 int** erb_idx,
137 float** erb_freqs,
138 int* nERBBands);
139
141void randperm(/* Input Arguments */
142 int len,
143 /* Output Arguments */
144 int* randperm_inds);
145
156long double factorial(int n);
157
166float matlab_fmodf(float x, float y);
167
177void cxcorr(float* a,
178 float* b,
179 float* x_ab,
180 size_t la,
181 size_t lb);
182
189void rand_m1_1(float* vector,
190 int length);
191
199void rand_cmplx_m1_1(float_complex* vector,
200 int length);
201
208void rand_0_1(float* vector,
209 int length);
210
220void convd(double* x,
221 double* h,
222 int len_x,
223 int len_h,
224 double* y);
225
235void convz(double_complex* x,
236 double_complex* h,
237 int len_x,
238 int len_h,
239 double_complex* y);
240
248void polyd_v(double* x,
249 double* poly,
250 int len_x);
251
259void polyz_v(double_complex* x,
260 double_complex* poly,
261 int len_x);
262
270void polyd_m(double* X,
271 double_complex* poly,
272 int size_x);
273
275float sumf(float* values,
276 int nValues);
277
282int anyLessThanf(float* values,
283 int nValues,
284 float threshold);
285
301void unique_i(int* input,
302 int nInputs,
303 int** uniqueVals,
304 int** uniqueInds,
305 int* nUnique);
306
319void findCombinations(int* arrValues,
320 int nValues,
321 int nElements,
322 int** comb,
323 int* nComb);
324
354void gexpm(float* D,
355 int sizeD,
356 int m1,
357 float* Y);
358
359
360#ifdef __cplusplus
361}/* extern "C" */
362#endif /* __cplusplus */
363
364#endif /* SAF_MISC_H_INCLUDED */
365
366 /* 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...