50 V
mod(
const V& a,
const V& b)
52 return (a % b + b) % b;
69 time_trace_ffmt(
bool num_samples_on_,
bool header_on_, std::string header_, std::string col_sep_)
73 time_trace_ffmt(
bool num_samples_on_,
bool header_on_, std::string header_, std::string col_sep_,
int fmt_id_)
74 : num_samples_on(num_samples_on_), header_on(header_on_), header(header_), col_sep(col_sep_), fmt_id(fmt_id_)
90 std::streampos start_of_data = inpf.beg;
94 std::getline(inpf,line);
97 std::size_t found_hash = line.find_first_of(
"#");
98 if(found_hash != std::string::npos)
101 start_of_data = inpf.tellg();
107 size_t items = sscanf(line.c_str(),
"%lf %lf", &t, &f);
113 size_t item = sscanf(line.c_str(),
"%d", &num_samples);
130 inpf.seekg(start_of_data,inpf.beg);
167 time_trace(sReal _duration, sReal _dt, sReal _rtOff, std::string _label) :
168 time_trace(_duration, _dt, _rtOff, _label,
"",
"")
176 time_trace(sReal _duration, sReal _dt, sReal _rtOff, std::string _label, std::string _t_unit, std::string _f_unit)
180 int N = ceil(_duration/dt) + 1;
186 for(
size_t i=0; i<t.
size(); i++) {
191 set_labels(_label, _t_unit, _f_unit);
227 void operator *= (
const sReal s);
228 void operator += (
const sReal a);
230 void operator <<= (
int delta);
231 void operator >>= (
int delta);
239 void set_labels(std::string _label, std::string _t_unit, std::string _f_unit)
242 setUnits(_t_unit, _f_unit);
245 void setUnits(std::string _t_unit, std::string _f_unit)
260 return t[t.
size()-1];
270 for(std::size_t i=0; i<len(); i++)
281 for(std::size_t i=0; i<len(); i++)
298 for(std::size_t i=0; i<len()-1; i++)
299 f[i] = f[i+1] - f[i];
322 dFdt[0] = (f[1] - f[0]) / (t[1] - t[0]);
323 dFdt[end] = (f[end] - f[end-1]) / (t[end] - t[end-1]);
326 for(
int i=1; i<end-1; i++)
328 sReal fdF = (f[i ] - f[i-1]) / (t[i ] - t[i-1]);
329 sReal bdF = (f[i+1] - f[i ]) / (t[i+1] - t[i ]);
331 dFdt[i] = (fdF + bdF) * 0.5;
342 int read_trace(
const std::string fname,
bool unitize);
345 int write_trace(
const std::string fname);
369 for (std::size_t i=1;i<len();i++) {
383 sReal unsc = fabs(_mx)>=fabs(_mn) ? 1./fabs(_mx):1./fabs(_mn);
385 for (std::size_t i=0;i<len();i++)
401 sReal dt_o = t[1] - t[0];
404 sReal r_err = 0.001*dt_o;
406 for (std::size_t i=1; i<len()-1; i++) {
407 sReal dt_n = t[i+1] - t[i];
408 if ( (dt_n - dt_o) > r_err)
420 return fade ? 0. : f[0];
423 return fade ? 0. : f.
back();
426 while(t[i] <= _t) i++;
428 sReal K = (f[i] - f[i-1]) / (t[i] - t[i-1]);
429 sReal fval = f[i-1] + K * (_t - t[i-1]);
436 for(std::size_t i=0; i<trc.
len(); i++)
437 trc.
f[i] = fval_t(trc.
t[i]);
460 for (std::size_t i=0; i<trc.
len(); i++) {
466 if ( (_t < t[0]) || (_t > t.
back()) )
483 trc.
f[i] = f[i1] + (f[i2]-f[i1])/(t[i2]-t[i1])*(_t-t[i1]);
486 if ((trc.
t[i]-t[i1]) >= (t[i2]-trc.
t[i]))
501 assert( len() == a.
len() );
503 for(
size_t i=0; i<len(); i++)
510 for(
auto & v : f) v *= s;
513 inline void time_trace::time_trace::operator += (
const sReal a)
515 for(
auto & v : f) v += a;
521 assert( len() == a.
len() );
523 for(
size_t i=0; i<len(); i++)
529 bool ring = delta < 0;
537 for(
int i=0; i<N; i++)
538 f[
mod((i-delta),N)] = tmp[i];
542 for(std::size_t i=len()-delta;i<len();i++)
548 bool ring = delta < 0;
556 for(
int i=0; i<N; i++)
557 f[
mod((i+delta),N)] = tmp[i];
561 for(
int i=0;i<delta;i++)
569 std::ostringstream sheader_;
574 sheader_ <<
"# " << ffmt_spec.
header << std::endl;
577 sheader_ <<
"# version = " << ffmt_spec.
fmt_id << std::endl;
580 sheader_ <<
"#" << std::endl;
581 sheader_ <<
"# TABLE_HEAD" << std::endl;
582 sheader_ <<
"# A = time" << std::endl;
583 sheader_ <<
"# B = " << label << std::endl;
584 sheader_ <<
"# [" << std::setw(ffmt_spec.
colw[0]) <<
"A" <<
"]";
585 sheader_ <<
"[" << std::setw(ffmt_spec.
colw[1]) <<
"B" <<
"]" << std::endl;
586 sheader_ <<
"# [" << std::setw(ffmt_spec.
colw[0]) << t_unit <<
"]";
587 sheader_ <<
"[" << std::setw(ffmt_spec.
colw[1]) << f_unit <<
"]" << std::endl;
588 sheader_ <<
"#" << std::endl;
589 sheader_ <<
"# DATA" << std::endl;
594 sheader_ << len() << std::endl;
596 std::string sheader = sheader_.str();
627 std::ifstream inpf(fname);
633 std::cerr <<
"Trace file " << fname <<
" could not be opened for reading!" << std::endl;
644 if(tt_ffmt.num_samples_on)
654 while( inpf >> t[i] >> f[i] )
657 if(tt_ffmt.num_samples_on)
659 if(i<numSamples || i<2)
703 return write_trace(label +
".trc");
719 int err = write_trace(fname, ffmt_spec);
737 std::ofstream outf(fname);
742 std::cerr <<
"Trace file " << fname <<
" could not be opened for writing!" << std::endl;
749 outf << format_file_header(ffmt_spec);
754 outf << len() << std::endl;
759 for(std::size_t i=0; i<len(); i++)
761 outf << std::setw(ffmt_spec.
colw[0]+3) << std::setprecision(3) << t[i] << ffmt_spec.
col_sep;
762 outf << std::setw(ffmt_spec.
colw[1]+1) << std::setprecision(6) << f[i] << std::endl;
773 assert( a.
len() == b.
len() );
777 for(std::size_t i=0; i<a.
len(); i++)
778 _mult.
f[i] = a.
f[i] * b.
f[i];
785 assert( a.
len() == b.
len() );
789 for(std::size_t i=0; i<a.
len(); i++)
790 _div.
f[i] = a.
f[i] / b.
f[i];
797 assert( a.
len() == b.
len() );
800 for(std::size_t i=0; i<a.
len(); i++)
801 _add.
f[i] = a.
f[i] + b.
f[i];
808 assert( a.
len() == b.
len() );
811 for(std::size_t i=0; i<a.
len(); i++)
812 _sub.
f[i] = a.
f[i] - b.
f[i];
921 for(std::size_t i=0; i<trc.
len(); i++)
944 sReal off = pars.
rise ? 1. : 0.;
945 sReal on = pars.
rise ? 0. : 1.;
946 for(std::size_t i=0; i<trc.
t.
size(); i++)
947 trc.
f[i] = trc.
t[i] < pars.
trig ? on : off;
966 for(std::size_t i=0; i<trc.
len(); i++)
967 trc.
f[i] = sin(2*
M_PI*pars.
frq*trc.
t[i] + phase_shift);
985 for(std::size_t i=0; i<trc.
len(); i++)
986 trc.
f[i] = pars.
A*exp(trc.
t[i]/pars.
tau_f) + pars.
B;
1013 for(std::size_t i=0; i<trc.
t.
size(); i++)
1016 sReal fp = exp(-(trc.
t[i]-t0)/pars.
tau_plat);
1028 stepPars.
rise =
false;
1040 int i1 = int(t1/trc.
dt);
1041 sReal f0 = phase_0.
f[i1];
1042 for(std::size_t i=i1; i<trc.
len(); i++)
1043 phase_1.
f[i] = f0*exp(-(trc.
t[i]-t1)/pars.
tau_edge);
1046 time_trace pulse = phase_0 * eps_0 + phase_1 * eps_1;
sReal duration
pulse duration, default is 1 ms
void operator<<=(int delta)
The vector class and related algorithms.
std::string col_sep
column separator, either " " or ","
virtual SF::vector< sReal > & sample(time_trace &trc)
float s2r
strength of subpulse relative to leading pulse (biphasic pulse)
float bias
constant term to add to stimulus waveform
bool header_on
turn on header output
int fmt_id
format identifier
time_trace(sReal _duration, sReal _dt, sReal _rtOff, std::string _label)
sReal dt
temporal discretization of trace, if regularly sampled
int read_trace(const std::string fname)
determine duration of a signal stored in file
int read_trace(trace *tr, const char *name)
float tau_plat
time constant governing plateau of pulse
int parse_header(std::ifstream &inpf)
parse trace header from file
time_trace_ffmt(bool num_samples_on_, bool header_on_, std::string header_, std::string col_sep_)
void operator*=(const time_trace &a)
overloaded operators
virtual SF::vector< sReal > & sample(time_trace &trc)
std::string label
descriptive label of signal
size_t len() const
< length of trace in samples
SF::vector< sReal > t
time axis
time_trace(const time_trace &a)
copy constructor with setting of constant signal value
time_trace operator/(const time_trace &a, const time_trace &b)
float phase
phase in degree
void set_labels(std::string _label, std::string _t_unit, std::string _f_unit)
std::string format_file_header(const time_trace_ffmt ffmt_spec)
IO format file header from format specifciation and given time trace.
bool IsEquDistSampling()
check whether time trace is sampled with constant sampling interval
V mod(const V &a, const V &b)
time_trace(sReal _duration, sReal _dt, sReal _rtOff)
bool fade
signal fades to zero after end or remains at final amplitude
monophasic truncated exponentials (capacitive discharge)
const T * end() const
Pointer to the vector's end.
void interp1(time_trace &trc, IpMeth_t meth)
interpolate a time trace
time_trace operator*(const time_trace &a, const time_trace &b)
time_trace(sReal _dt)
prescribe dt only, default duration is 1000. ms
std::string f_unit
unit of signal
time_trace & operator=(const time_trace &in)
bool num_samples_on
turn on # of samples output
float tau_edge
time constant for leading/trailing edges
time_trace(sReal _duration, sReal _dt)
prescribe duration and sampling, use default real time offset
void operator>>=(int delta)
size_t timeShift(sReal t_shift)
double d1
duration of first sub-pulse in [ms] (zero with monophasic pulse)
void setUnits(std::string _t_unit, std::string _f_unit)
std::string t_unit
unit of time axis
time_trace()
use defaults only, 1000 ms duration, 10 us sampling interval, no real time offset ...
biphasic truncated exponentials (capacitive discharge)
virtual SF::vector< sReal > & sample(time_trace &trc)
void resample(time_trace &trc, IpMeth_t meth)
time_trace operator+(const time_trace &a, const time_trace &b)
int decay
edge decay time (multiples of tau_edge)
biphasicTruncExpFunc(Pulse _pars)
virtual SF::vector< sReal > & sample(time_trace &trc)
sReal rtOff
real time offset of time axis
void operator+=(const sReal a)
void set_labels(std::string _label)
SF::vector< sReal > & gradient()
int write_trace()
write traces to file
time_trace(const time_trace &a, sReal val)
copy constructor with setting of constant signal value
sReal fval_t(sReal _t)
retrieve value of a function at a given time t
void resample(time_trace &trc)
sineWave(sReal _frq, sReal _phase)
SF::vector< int > colw
width of columns
size_t size() const
The current size of the vector.
bool constant
constant value for all time
time_trace(sReal _duration, sReal _dt, sReal _rtOff, std::string _label, std::string _t_unit, std::string _f_unit)
full constructor, prescribe all members
SF::vector< sReal > & diff()
time_trace operator-(const time_trace &a, const time_trace &b)
void unitize()
scale signal to unity strength
const T * begin() const
Pointer to the vector's start.
float strength
pulse amplitude, default is unit strength
void assign(InputIterator s, InputIterator e)
Assign a memory range.
void resize(size_t n)
Resize a vector.
virtual SF::vector< sReal > & sample(time_trace &trc)
void resize(int numSamples)
monophasicTruncExpFunc(Pulse _pars)
SF::vector< sReal > f
store function values of trace
void interp1(const double *x, const float *y, int N, double *xi, float *yi, int NI, double dxi, IpMeth_t meth)
time_trace_ffmt(bool num_samples_on_, bool header_on_, std::string header_, std::string col_sep_, int fmt_id_)
enum opencarp::sig::ip_method IpMeth_t