Logo SoCRocket

Transaction-Level Modeling Framework for Space Applications

Classes | Macros | Typedefs | Functions
tolstoy.c File Reference
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
Include dependency graph for tolstoy.c:

Classes

struct  complex
 
struct  filter
 

Macros

#define MYMATH_H
 
#define RTOLS_H
 
#define RTOLS_ERROR_GENERAL   -1
 
#define RTOLS_ERROR_RESTART_FAILED   -2
 
#define RTOLS_ERROR_SHARED_INPUT   -4
 
#define RTOLS_ERROR_UNSHARED_INPUT   -8
 
#define RTOLS_SHARED_NONE   0
 
#define RTOLS_SHARED_FFT   1
 
#define RTOLS_SHARED_INPUT   2
 
#define RTOLS_SHARED_FILTER   4
 
#define RTOLS_SHARED_OUTPUT_ADD   8
 
#define TOLS_H
 
#define TWOPI   6.28318530717959
 
#define SWAP(a, b)   temp = (a); (a) = (b); (b) = temp
 
#define SWAP(a, b)   temp = (a); (a) = (b); (b) = temp
 

Typedefs

typedef double real
 

Functions

complex cadd (complex x, complex y)
 
complex csub (complex x, complex y)
 
complex cmult (complex x, complex y)
 
complex cdiv (complex x, complex y)
 
real cmag (complex x)
 
real mycarg (complex x)
 
complex mycexp (real mag, real arg)
 
int r2c (real *in, complex *out, int len)
 
int c2r (complex *in, real *out, int len, int mode)
 
int bitrev (int i, int m)
 
void ffw (complex *w, int idir, int n)
 
void perm (complex *x, int m)
 
void permrev (complex *x, int m)
 
void permcpy (complex *x, complex *y, int m)
 
void permrevcpy (complex *x, complex *y, int m)
 
void fft1d (complex *x, complex *w, int idir, int n)
 
void fft (complex *x, complex *w, int m)
 
void fftrev (complex *x, complex *w, int m)
 
void ifft (complex *x, complex *w, int m)
 
void fftcpy (complex *x, complex *y, complex *w, int m)
 
void ifftcpy (complex *x, complex *y, complex *w, int m)
 
void fconv (complex *x, complex *h, complex *y, complex *wf, complex *wi, int m)
 
void fwconv (complex *x, complex *h, complex *y, int m)
 
void tconv (complex *x, int xlen, complex *h, int ylen, complex *y)
 
void printcvec (complex *x, int n)
 
void printrvec (real *x, int n)
 
void printbits (int n)
 
int rtols_prepare (filter *filt, complex *h, int hlen, int m)
 
int rtols_prepare_sigshare (filter *filt, complex *h, int hlen, complex *wf, complex *wi, complex **XX, int R, int m, int addout)
 
int rtols_prepare_filtshare (filter *filt, complex *wf, complex *wi, complex **HH, int R, int hlen, int m)
 
int rtols_filter (filter *filt, real *inbuf, real *outbuf)
 
int rtols_filter_sigshare (filter *filters, int nfilt, real *inbuf, real **outbuf, int outoffset)
 
int rtols_filter_update (filter *filt, complex *h, int hlen, int transition)
 
int rtols_finish (filter *filt, int len)
 
int rtols_restart (filter *filt)
 
int rtols_cleanup (filter *filt)
 
void ols (complex *x, int xlen, complex *h, int hlen, complex *y, int ylen, int m)
 
int tols (complex *x, int xlen, complex *h, int hlen, complex *y, int ylen, int m)
 

Macro Definition Documentation

#define MYMATH_H
#define RTOLS_ERROR_GENERAL   -1
#define RTOLS_ERROR_RESTART_FAILED   -2
#define RTOLS_ERROR_SHARED_INPUT   -4
#define RTOLS_ERROR_UNSHARED_INPUT   -8
#define RTOLS_H
#define RTOLS_SHARED_FFT   1
#define RTOLS_SHARED_FILTER   4
#define RTOLS_SHARED_INPUT   2
#define RTOLS_SHARED_NONE   0
#define RTOLS_SHARED_OUTPUT_ADD   8
#define SWAP (   a,
 
)    temp = (a); (a) = (b); (b) = temp
#define SWAP (   a,
 
)    temp = (a); (a) = (b); (b) = temp
#define TOLS_H
#define TWOPI   6.28318530717959

Typedef Documentation

typedef double real

real data type (float or double)

Function Documentation

int bitrev ( int  i,
int  m 
)

bit reversal

Parameters
inumber with bits
mnumber of total bits (=ld of size of fft n=2^m).
int c2r ( complex in,
real out,
int  len,
int  mode 
)

conversion from complex to real

Parameters
incomplex input
outreal output
lenbuffer lengths
modeuses Re(z) if 0, Im(z) if 1, |z| if 2, arg(z) if 3
Returns
error code <0 or 0.
complex cadd ( complex  x,
complex  y 
)

complex multiplication

Parameters
x
y
Returns
x * y
complex cdiv ( complex  x,
complex  y 
)

complex division

Parameters
x
y
Returns
x * y
real cmag ( complex  x)

magnitude of a complex number

Parameters
x
Returns
|x|
complex cmult ( complex  x,
complex  y 
)

complex multiplication

Parameters
x
y
Returns
x * y
complex csub ( complex  x,
complex  y 
)

complex subtraction

Parameters
x
y
Returns
x * y
void fconv ( complex x,
complex h,
complex y,
complex wf,
complex wi,
int  m 
)

frequency-domain convolution

Parameters
x[in] time-domain signal x, length 2^(m-1)
h[in] impulse response h, length 2^(m-1)
y[out] convolved time-domain signal y = x ** h, length 2^m
wfphase factors for fft
wfphase factors for inverse fft
mld of fft size
void fft ( complex x,
complex w,
int  m 
)

complete in-place fft

Parameters
x[in/out] time/freq
wphase factors for transformation
void fft1d ( complex x,
complex w,
int  idir,
int  n 
)

fft core routine. (must be prepended by a permutation)

Parameters
[in/out]time/freq domain vector
direction(forward = 1, backward = -1)
sizeof fft (should be 2^N)
void fftcpy ( complex x,
complex y,
complex w,
int  m 
)

complete fft preserving input

Parameters
x[in] time
y[out] freq
wphase factors for transformation
void fftrev ( complex x,
complex w,
int  m 
)

complete in-place fft with reversed half-frame order

Parameters
x[in/out] time (with reversed half-frame order)/freq
wphase factors for transformation
void ffw ( complex w,
int  idir,
int  n 
)

calculate phase factors

Parameters
w[out] array of phase factors (fft size / 2)
idirdirection (forward = 1, backward = -1)
sizeof fft
void fwconv ( complex x,
complex h,
complex y,
int  m 
)

frequency-domain convolution with internal weights calculation

Parameters
x[in] time-domain signal x, length 2^(m-1) plus zero padding to length 2^m
h[in] impulse response h, length 2^(m-1) plus zero padding to length 2^m
y[out] convolved time-domain signal y = x ** h, length 2^m
mld of fft size
void ifft ( complex x,
complex w,
int  m 
)

complete in-place ifft

Parameters
x[in/out] freq/time
wphase factors for inverse transformation
void ifftcpy ( complex x,
complex y,
complex w,
int  m 
)

complete ifft preserving input

Parameters
x[in] freq
y[out] time
wphase factors for transformation
real mycarg ( complex  x)

phase of a complex number

Parameters
x
Returns
arg(x)
complex mycexp ( real  mag,
real  arg 
)

complex number from magnitude and phase

Parameters
magmagnitude
Returns
mag * exp(j arg)
void ols ( complex x,
int  xlen,
complex h,
int  hlen,
complex y,
int  ylen,
int  m 
)
void perm ( complex x,
int  m 
)

permutation from data order to butterfly order,

Parameters
x[in/out] data
mld of fft size
void permcpy ( complex x,
complex y,
int  m 
)

permutation from data order to butterfly order into provided array.

Parameters
x[in] data
y[out] data
mld of fft size
void permrev ( complex x,
int  m 
)

permutation from data order to butterfly order using reversed half- frames

Parameters
x[in/out] data whose actual order is x[2^(m-1):2^m-1]x[0:2^(m-1)]
mld of fft size
void permrevcpy ( complex x,
complex y,
int  m 
)

permutation from data order to butterfly order using reversed half- frames, copies into the provided array.

Parameters
x[in/out] data whose actual order is x[2^(m-1):2^m-1]x[0:2^(m-1)]
mld of fft size
void printbits ( int  n)

print 8 lowest significant bits

void printcvec ( complex x,
int  n 
)

print complex vector of length n

void printrvec ( real x,
int  n 
)

print real vector of length n

int r2c ( real in,
complex out,
int  len 
)

conversion from real to complex

Parameters
inreal input
outcomplex output
lenbuffer lengths
Returns
error code <0 or 0.
int rtols_cleanup ( filter filt)

deallocates the internal filter memory (inbuf and outbuf must be freed).

Parameters
filterto be destoyed
Returns
error code <0 or 0
int rtols_filter ( filter filt,
real inbuf,
real outbuf 
)

time-partitioned OLS convolution, streaming mode.

Triggers the algorithm that a new frame has been written into inbuf and last result will be read from outbuf. Important: Do not use for sharing inputs between filters, otherwise this is ignored and -4 returned.

Parameters
filtersthe filters to be triggered.
inbufreal input buffer where the frame is read from (size 2^(m-1) = fftsize/2 expected). After calling rtols_finish and the returned number of frames, inbuf is ignored (as there is no signal left).
outbufreal output buffer where the result is written to (size 2^(m-1) expected). If not the full buffer is filled with signal, it is zero-padded.
Returns
number of samples in the output buffer that are the signal or -1 if there is no signal left or error code <-1. This is especially useful after calls to rtols_finish().
int rtols_filter_sigshare ( filter filters,
int  nfilt,
real inbuf,
real **  outbuf,
int  outoffset 
)

time-partitioned OLS convolution, streaming mode.

Triggers the algorithm that a new frame has been written into inbuf and last result will be read from outbuf.

Parameters
filtersthe filters to be triggered.
nfiltnumber of filters (and output buffers)
inbufreal input buffer where the frame is read from (size 2^(m-1) = fftsize/2 expected). After calling rtols_finish and the returned number of frames, inbuf is ignored (as there is no signal left).
outbufreal output buffers where the result is written to (size 2^(m-1) expected).
outoffsetoffset to each output buffer (internal: pointer addition on each single buffer)
Returns
number of samples in the output buffer that are the signal or -1 if there is no signal left or error code <-1. This is especially useful after calls to rtols_finish().
int rtols_filter_update ( filter filt,
complex h,
int  hlen,
int  transition 
)

time-partitioned OLS convolution, streaming mode.

Updates the filter coefficients now

Parameters
hnew impulse response
hlenlength of new IR
transitionnumber of frames that the IR will be crossfaded from the old one (frequency-domain interpolation)
Returns
error code <0 or 0
int rtols_finish ( filter filt,
int  len 
)

time-partitioned OLS convolution, streaming mode.

finishes the filter operation. Afterwards, the filter can be reused by calling rtols_restart. If the filter is shared by its input, finishes all filters that belong to the signal.

Parameters
filterfilter to be finished
lenthe length of the input signal that remains to be filtered
Returns
number of times that rtols_filter must be called before the signal ends (and can be restarted).
int rtols_prepare ( filter filt,
complex h,
int  hlen,
int  m 
)

time-partitioned OLS convolution, streaming mode.

Prepares the filtering by allocating internal memory of a filter and setting up the transformations. The input and output buffers are complex arrays inbuf and outbuf. Therefore, the streaming framework that calls trigger must call the appropriate conversion methods rtols_*2*(in, out, len). TODO: fix this to avoid this inner-loop conversion.

In the system, two internal i/o buffers of type complex are generated, one of size 2^m for the input data, and one of size 2^(m-1) for the output data.

Parameters
filterthe structure that contains all information for filtering
himpulse response
hlenlength of h
mld of fft size used to partition the signals, note that input and output buffers are fftsize/2
Returns
error code <0 or 0
int rtols_prepare_filtshare ( filter filt,
complex wf,
complex wi,
complex **  HH,
int  R,
int  hlen,
int  m 
)

time-partitioned OLS convolution, streaming mode with shared resources for fft and filter transfer function.

Prepares the filtering by allocating internal memory of a filter and setting up the transformations. The input and output buffers are complex arrays inbuf and outbuf. Therefore, the streaming framework that calls trigger must call the appropriate conversion methods rtols_*2*(in, out, len). TODO: fix this to avoid this inner-loop conversion.

In the system, two internal i/o buffers of type complex are generated, one of size 2^m for the input data, and one of size 2^(m-1) for the output data.

Parameters
filterthe structure that contains all information for filtering
wfforward fft structure
wiinverse fft structure
HHfilter transfer functions (frame spectra), size R x 2^m
Rnumber of filter frames
hlenoriginal length of filter impulse response
mld of fft size used to partition the signals, note that input and output buffers are fftsize/2
Returns
error code <0 or 0
int rtols_prepare_sigshare ( filter filt,
complex h,
int  hlen,
complex wf,
complex wi,
complex **  XX,
int  R,
int  m,
int  addout 
)

time-partitioned OLS convolution, streaming mode with shared resources for fft and input buffer. Sharing the input buffer requires that the rtols_filter_sigshare method be used instead of rtols_filter, which will return -1 and ignore the call.

Prepares the filtering by allocating internal memory of a filter and setting up the transformations. The input and output buffers are complex arrays inbuf and outbuf. Therefore, the streaming framework that calls trigger must call the appropriate conversion methods rtols_*2*(in, out, len). TODO: fix this to avoid this inner-loop conversion.

In the system, two internal i/o buffers of type complex are generated, one of size 2^m for the input data, and one of size 2^(m-1) for the output data.

Parameters
filterthe structure that contains all information for filtering
himpulse response
hlenlength of h
wfforward fft structure
wiinverse fft structure
XXcircular frame spectrum buffer of size R x 2^m, R is checked if it is compatible with hlen: R = ceil(hlen * 2^(-m+1)), if not, -2 as the error code is returned.
mld of fft size used to partition the signals, note that input and output buffers are fftsize/2
addout0 if outgoing signals overwrite the output buffer, 1 if they are added.
Returns
error code <0 or 0
int rtols_restart ( filter filt)

after a call to rtols_finish, the filter is restarted by a new signal at its input buffer.

Parameters
filterthe filter to be reinitialised
Returns
error code <0 or 0, -1 is returned if the signal is still finishing and the restart is ignored
void tconv ( complex x,
int  xlen,
complex h,
int  ylen,
complex y 
)

time-domain convolution

Parameters
x[in] time-domain signal x
xlenlength of x
h[in] impulse response h
hlenlength of h
y[out] convolved time-domain signal y = x ** h, length ylen = xlen + hlen - 1
int tols ( complex x,
int  xlen,
complex h,
int  hlen,
complex y,
int  ylen,
int  m 
)

time-partitioned OLS convolution

Parameters
xinput signal (time domain)
xlenlength of x
himpulse response
hlenlength of h
youtput (length >= xlen + hlen)
mld of fft size used to partition the signals.
Returns
error code <0 or 0.