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

Working with NaN's in TAC struct. More...

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

Go to the source code of this file.

Functions

int tacXNaNs (TAC *tac)
int tacYNaNs (TAC *tac, const int i)
int tacNaNs (TAC *tac)
int tacNotNaNs (TAC *tac, const int i)
int tacFixNaNs (TAC *tac)
int tacNthSample (TAC *tac, const int sn, const int i)

Detailed Description

Working with NaN's in TAC struct.

Definition in file tacnan.c.

Function Documentation

◆ tacFixNaNs()

int tacFixNaNs ( TAC * tac)

Fix missing y values in TAC structure by interpolation. X values cannot be fixed. Extrapolation of the last missing values is not possible, but beginning can be estimated assuming first point (0,0).

See also
tacXNaNs, tacYNaNs, tacNotNaNs, tacDeleteMissingSamples
Returns
Returns 0 if missing values could be filled with reasonable values.
Parameters
tacPointer to TAC structure.

Definition at line 121 of file tacnan.c.

124 {
125 if(tac==NULL || tac->sampleNr<1 || tac->tacNr<1) return(1);
126 if(tacXNaNs(tac)>0) return(2);
127
128 int ri, fi, fj;
129 double x1, x2, y1, y2, x, y;
130 /* If NaNs with negative sample times, then set value to zero */
131 for(fi=0; fi<tac->sampleNr; fi++) {
132 if(tac->isframe) x=0.5*(tac->x1[fi]+tac->x2[fi]); else x=tac->x[fi];
133 if(x>0.0) continue;
134 for(ri=0; ri<tac->tacNr; ri++)
135 if(!isfinite(tac->c[ri].y[fi])) tac->c[ri].y[fi]=0.0;
136 }
137 /* Go through the data, finding previous and next good value */
138 for(ri=0; ri<tac->tacNr; ri++) if(tacYNaNs(tac, ri)>0) {
139 x1=x2=y1=y2=nan("");
140 for(fi=0; fi<tac->sampleNr; fi++) {
141 if(tac->isframe) x=0.5*(tac->x1[fi]+tac->x2[fi]); else x=tac->x[fi];
142 /* Save previous good point */
143 if(isfinite(tac->c[ri].y[fi])) {y1=tac->c[ri].y[fi]; x1=x; continue;}
144 /* We have missing value if we are here */
145 /* Search for the next good point after this NaN */
146 x2=y2=nan("");
147 for(fj=fi+1; fj<tac->sampleNr; fj++) {
148 if(!isfinite(tac->c[ri].y[fj])) continue;
149 if(tac->isframe) x2=0.5*(tac->x1[fj]+tac->x2[fj]); else x2=tac->x[fj];
150 y2=tac->c[ri].y[fj];
151 break;
152 }
153 /* If no good points after current point, then that is bad */
154 if(!isfinite(x2)) return(3);
155 /* If the point before is not available, then use (0,0) */
156 if(!isfinite(x1)) x1=y1=0.0;
157 /* Now, interpolate value for current point */
158 if(x2==x1) y=0.5*(y1+y2); else y=y2-(x2-x)*(y2-y1)/(x2-x1);
159 tac->c[ri].y[fi]=y;
160 } // next sample
161 } // next TAC
162
163 return(0);
164}
double * y
Definition tpctac.h:75
double * x
Definition tpctac.h:97
int sampleNr
Definition tpctac.h:89
int isframe
Definition tpctac.h:95
TACC * c
Definition tpctac.h:117
double * x2
Definition tpctac.h:101
double * x1
Definition tpctac.h:99
int tacNr
Definition tpctac.h:91
int tacYNaNs(TAC *tac, const int i)
Definition tacnan.c:47
int tacXNaNs(TAC *tac)
Definition tacnan.c:23

◆ tacNaNs()

int tacNaNs ( TAC * tac)

Check TAC structure for missing sample times and values.

See also
tacXNaNs, tacYNaNs, tacNotNaNs, tacFixNaNs
Returns
Returns the number of missing values that were found.
Parameters
tacPointer to TAC structure.

Definition at line 71 of file tacnan.c.

74 {
75 return(tacXNaNs(tac)+tacYNaNs(tac, -1));
76}

Referenced by tacFramesToSteps(), tacInterpolate(), tacInterpolateInto(), tacReadModelingData(), and tacReadModelingInput().

◆ tacNotNaNs()

int tacNotNaNs ( TAC * tac,
const int i )

Check TAC structure for missing sample times and values.

See also
tacNaNs, tacXNaNs, tacYNaNs, tacFixNaNs
Returns
Returns the number of samples which do not contain missing x or y values.
Parameters
tacPointer to TAC structure.
iIndex of TAC to test; <0 to test all TACs.

Definition at line 84 of file tacnan.c.

89 {
90 if(tac==NULL || tac->sampleNr<1 || tac->tacNr<1 || i>=tac->tacNr) return(0);
91
92 int ri, fi, nan_nr=0;
93 for(fi=0; fi<tac->sampleNr; fi++) {
94 /* First, check x value(s) */
95 if(tac->isframe) {
96 if(!isfinite(tac->x1[fi])) {nan_nr++; continue;}
97 if(!isfinite(tac->x2[fi])) {nan_nr++; continue;}
98 } else {
99 if(!isfinite(tac->x[fi])) {nan_nr++; continue;}
100 }
101 /* If ok, then check y value(s) */
102 if(i>=0) {
103 if(!isfinite(tac->c[i].y[fi])) nan_nr++;
104 continue;
105 }
106 for(ri=0; ri<tac->tacNr; ri++)
107 if(!isfinite(tac->c[ri].y[fi])) {nan_nr++; break;}
108 } // next sample
109 return(tac->sampleNr-nan_nr);
110}

◆ tacNthSample()

int tacNthSample ( TAC * tac,
const int sn,
const int i )

Search the Nth sample (1..n) in TAC structure which does not contain missing x or y values. Use tacNotNaNs to get the number of existing samples in TAC.

See also
tacNotNaNs, tacFixNaNs
Returns
Returns the sample index, or <1 if not available.
Parameters
tacPointer to TAC structure.
snNumber (1..n) of sample to search for; 1 to search for the first one, tacNotNaNs(&tac, -1) to search for the last one.
iIndex of TAC to test; <0 to test all TACs

Definition at line 173 of file tacnan.c.

181 {
182 if(tac==NULL || tac->sampleNr<1 || tac->tacNr<1 || i>=tac->tacNr) return(-1);
183 if(sn<1 || sn>tac->sampleNr) return(-1);
184
185 int ri, fi, n=0;
186 for(fi=0; fi<tac->sampleNr; fi++) {
187 /* First, check x value(s) */
188 if(tac->isframe) {
189 if(!isfinite(tac->x1[fi]) || !isfinite(tac->x2[fi])) continue;
190 } else {
191 if(!isfinite(tac->x[fi])) continue;
192 }
193 /* If ok, then check y value(s) */
194 if(i>=0) {
195 if(!isfinite(tac->c[i].y[fi])) continue;
196 } else {
197 for(ri=0; ri<tac->tacNr; ri++) if(!isfinite(tac->c[ri].y[fi])) break;
198 if(ri!=tac->tacNr) continue;
199 }
200 n++;
201 if(n==sn) return(fi);
202 } // next sample, if necessary
203 return(-1);
204}

◆ tacXNaNs()

int tacXNaNs ( TAC * tac)

Check TAC structure for missing sample times (x, or x1 and x2).

See also
tacDeleteMissingSamples, tacIsX, tacSetX
Returns
Returns the number of missing values that were found.
Parameters
tacPointer to TAC structure.

Definition at line 23 of file tacnan.c.

26 {
27 if(tac==NULL || tac->sampleNr<1) return(0);
28
29 int fi, nan_nr=0;
30 if(tac->isframe) {
31 for(fi=0; fi<tac->sampleNr; fi++) {
32 if(!isfinite(tac->x1[fi])) nan_nr++;
33 if(!isfinite(tac->x2[fi])) nan_nr++;
34 }
35 } else {
36 for(fi=0; fi<tac->sampleNr; fi++) {
37 if(!isfinite(tac->x[fi])) nan_nr++;
38 }
39 }
40 return(nan_nr);
41}

Referenced by tacDelay(), tacFixNaNs(), tacInterpolate(), tacInterpolateInto(), tacIsXContiguous(), tacNaNs(), tacSetXContiguous(), tacSortByTime(), and tacVerifyTimeOrder().

◆ tacYNaNs()

int tacYNaNs ( TAC * tac,
const int i )

Check TAC structure for missing sample values (y).

See also
tacNaNs, tacFixNaNs
Returns
Returns the number of missing values that were found.
Parameters
tacPointer to TAC structure.
iIndex of TAC to test; <0 to test all TACs.

Definition at line 47 of file tacnan.c.

52 {
53 if(tac==NULL || tac->sampleNr<1 || tac->tacNr<1 || i>=tac->tacNr) return(0);
54
55 int ri, fi, nan_nr=0;
56 if(i>=0) {
57 for(fi=0; fi<tac->sampleNr; fi++)
58 if(!isfinite(tac->c[i].y[fi])) nan_nr++;
59 return(nan_nr);
60 }
61 for(ri=0; ri<tac->tacNr; ri++)
62 for(fi=0; fi<tac->sampleNr; fi++)
63 if(!isfinite(tac->c[ri].y[fi])) nan_nr++;
64 return(nan_nr);
65}

Referenced by tacDelay(), tacFixNaNs(), and tacNaNs().