TPCCLIB
Loading...
Searching...
No Matches
convolut.c File Reference

Linear convolution for discrete data. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "tpccm.h"

Go to the source code of this file.

Functions

int convolve1D (double *data, const int n, double *kernel, const int m, double *out)
 Calculates the convolution sum of a discrete real data set data[0..n-1] and a discretized response function kernel[0..m].
int simIsSteadyInterval (double *x, const int n, double *f)

Detailed Description

Linear convolution for discrete data.

Definition in file convolut.c.

Function Documentation

◆ convolve1D()

int convolve1D ( double * data,
const int n,
double * kernel,
const int m,
double * out )

Calculates the convolution sum of a discrete real data set data[0..n-1] and a discretized response function kernel[0..m].

Convolution is not aware of the step size (default is 1); if step size is not 1 (it usually isn't), the step size must be taken into account either when computing the kernel or by scaling the convolution sum.

Remarks
This function does not use FFT, which would be faster with very large dataset, but slightly less precise.
See also
tacInterpolateToEqualLengthFrames, simDispersion, simC1
Returns
Function returns 0 when successful, or 1 if input data is not valid.
Parameters
dataData array of length n-1 to be convolved, including any user-defined zero-padding.
nNr of data values.
kernelResponse function values in an array of length m.
mLength of kernel array.
outThe convolved sum data is returned in out[0..n-1]; this must not overlap the input data.

Definition at line 27 of file convolut.c.

38 {
39 if(n<1 || m<1 || data==NULL || kernel==NULL || out==NULL || out==data) return(1);
40
41 for(int di=0; di<n; di++) out[di]=0.0;
42
43 for(int di=m-1; di<n; di++) {
44 int dj=di;
45 for(int k=0; k<m; k++)
46 out[di] += data[dj--] * kernel[k];
47 }
48
49 for(int di=0; di<n && di<m-1; di++) {
50 int k=0;
51 for(int dj=di; dj>=0; dj--)
52 out[di] += data[dj] * kernel[k++];
53 }
54
55 return(0);
56}

◆ simIsSteadyInterval()

int simIsSteadyInterval ( double * x,
const int n,
double * f )

Check whether given values have steady intervals, with the first interval starting at zero. Optionally return the interval, or in case of uneven intervals, the shortest interval.

Returns
Returns 1 if values are equidistant, and 0 if intervals are uneven, or cannot be determined.
See also
convolve1D, tacInterpolateToEqualLengthFrames
Parameters
xArray of values to check; must be sorted in ascending order.
nSize of the data array; at least two.
fPointer for the interval, either the common or the shortest interval; enter NULL, if not needed.

Definition at line 66 of file convolut.c.

74 {
75 if(f!=NULL) *f=nan("");
76 if(x==NULL || n<2) return(0);
77 //if(!(fabs(x[0])>1.0E-12)) return(0); // on purpose, to catch NaN
78
79 double d, freq=nan("");
80 int isdif=0;
81 for(int i=1; i<n; i++) {
82 d=x[i]-x[i-1];
83 if(!(d>1.0E-12)) return(0); // interval must be > 0
84 if(isnan(freq)) {freq=d; continue;} // first interval
85 if(fabs(freq-d)<1.0E-12) continue; // same interval as before
86 isdif++; // different
87 if(d<freq) freq=d; // save smaller interval
88 }
89 if(f!=NULL) *f=freq;
90 if(isdif>0) return(0); // variable intervals
91 /* Check that first sample time is at the midpoint of interval starting from 0 */
92 d=0.5*freq; //printf("d=%g x[0]=%g\n", d, x[0]);
93 if(fabs(x[0]-d)>1.0E-06) return(0);
94 return(1);
95}