35 V
mod(
const V& a,
const V& b)
37 return (a % b + b) % b;
54 time_trace_ffmt(
bool num_samples_on_,
bool header_on_, std::string header_, std::string col_sep_)
58 time_trace_ffmt(
bool num_samples_on_,
bool header_on_, std::string header_, std::string col_sep_,
int fmt_id_)
75 std::streampos start_of_data = inpf.beg;
79 std::getline(inpf,line);
82 std::size_t found_hash = line.find_first_of(
"#");
83 if(found_hash != std::string::npos)
86 start_of_data = inpf.tellg();
92 size_t items = sscanf(line.c_str(),
"%lf %lf", &t, &f);
98 size_t item = sscanf(line.c_str(),
"%d", &num_samples);
115 inpf.seekg(start_of_data,inpf.beg);
153 time_trace(_duration, _dt, _rtOff, _label,
"",
"")
165 int N = ceil(_duration/
dt) + 1;
171 for(
size_t i=0; i<
t.
size(); i++) {
224 void set_labels(std::string _label, std::string _t_unit, std::string _f_unit)
230 void setUnits(std::string _t_unit, std::string _f_unit)
255 for(std::size_t i=0; i<
len(); i++)
266 for(std::size_t i=0; i<
len(); i++)
283 for(std::size_t i=0; i<
len()-1; i++)
284 f[i] =
f[i+1] -
f[i];
307 dFdt[0] = (
f[1] -
f[0]) / (
t[1] -
t[0]);
308 dFdt[end] = (
f[end] -
f[end-1]) / (
t[end] -
t[end-1]);
311 for(
int i=1; i<end-1; i++)
313 sReal fdF = (
f[i ] -
f[i-1]) / (
t[i ] -
t[i-1]);
314 sReal bdF = (
f[i+1] -
f[i ]) / (
t[i+1] -
t[i ]);
316 dFdt[i] = (fdF + bdF) * 0.5;
354 for (std::size_t i=1;i<
len();i++) {
368 sReal unsc = fabs(_mx)>=fabs(_mn) ? 1./fabs(_mx):1./fabs(_mn);
370 for (std::size_t i=0;i<
len();i++)
389 sReal r_err = 0.001*dt_o;
391 for (std::size_t i=1; i<
len()-1; i++) {
393 if ( (dt_n - dt_o) > r_err)
405 return fade ? 0. :
f[0];
411 while(
t[i] <= _t) i++;
413 sReal K = (
f[i] -
f[i-1]) / (
t[i] -
t[i-1]);
414 sReal fval =
f[i-1] + K * (_t -
t[i-1]);
421 for(std::size_t i=0; i<trc.
len(); i++)
445 for (std::size_t i=0; i<trc.
len(); i++) {
451 if ( (_t <
t[0]) || (_t >
t.
back()) )
468 trc.
f[i] =
f[i1] + (
f[i2]-
f[i1])/(
t[i2]-
t[i1])*(_t-
t[i1]);
471 if ((trc.
t[i]-
t[i1]) >= (
t[i2]-trc.
t[i]))
486 assert(
len() == a.
len() );
488 for(
size_t i=0; i<
len(); i++)
495 for(
auto & v :
f) v *= s;
498 inline void time_trace::time_trace::operator += (
const sReal a)
500 for(
auto & v : f) v += a;
506 assert(
len() == a.
len() );
508 for(
size_t i=0; i<
len(); i++)
514 bool ring = delta < 0;
522 for(
int i=0; i<N; i++)
523 f[
mod((i-delta),N)] = tmp[i];
527 for(std::size_t i=
len()-delta;i<
len();i++)
533 bool ring = delta < 0;
541 for(
int i=0; i<N; i++)
542 f[
mod((i+delta),N)] = tmp[i];
546 for(
int i=0;i<delta;i++)
554 std::ostringstream sheader_;
559 sheader_ <<
"# " << ffmt_spec.
header << std::endl;
562 sheader_ <<
"# version = " << ffmt_spec.
fmt_id << std::endl;
565 sheader_ <<
"#" << std::endl;
566 sheader_ <<
"# TABLE_HEAD" << std::endl;
567 sheader_ <<
"# A = time" << std::endl;
568 sheader_ <<
"# B = " <<
label << std::endl;
569 sheader_ <<
"# [" << std::setw(ffmt_spec.
colw[0]) <<
"A" <<
"]";
570 sheader_ <<
"[" << std::setw(ffmt_spec.
colw[1]) <<
"B" <<
"]" << std::endl;
571 sheader_ <<
"# [" << std::setw(ffmt_spec.
colw[0]) <<
t_unit <<
"]";
572 sheader_ <<
"[" << std::setw(ffmt_spec.
colw[1]) <<
f_unit <<
"]" << std::endl;
573 sheader_ <<
"#" << std::endl;
574 sheader_ <<
"# DATA" << std::endl;
579 sheader_ <<
len() << std::endl;
581 std::string sheader = sheader_.str();
612 std::ifstream inpf(fname);
618 std::cerr <<
"Trace file " << fname <<
" could not be opened for reading!" << std::endl;
629 if(tt_ffmt.num_samples_on)
639 while( inpf >>
t[i] >>
f[i] )
642 if(tt_ffmt.num_samples_on)
644 if(i<numSamples || i<2)
722 std::ofstream outf(fname);
727 std::cerr <<
"Trace file " << fname <<
" could not be opened for writing!" << std::endl;
739 outf <<
len() << std::endl;
744 for(std::size_t i=0; i<
len(); i++)
746 outf << std::setw(ffmt_spec.
colw[0]+3) << std::setprecision(3) <<
t[i] << ffmt_spec.
col_sep;
747 outf << std::setw(ffmt_spec.
colw[1]+1) << std::setprecision(6) <<
f[i] << std::endl;
758 assert( a.
len() == b.
len() );
762 for(std::size_t i=0; i<a.
len(); i++)
763 _mult.
f[i] = a.
f[i] * b.
f[i];
770 assert( a.
len() == b.
len() );
774 for(std::size_t i=0; i<a.
len(); i++)
775 _div.
f[i] = a.
f[i] / b.
f[i];
782 assert( a.
len() == b.
len() );
785 for(std::size_t i=0; i<a.
len(); i++)
786 _add.
f[i] = a.
f[i] + b.
f[i];
793 assert( a.
len() == b.
len() );
796 for(std::size_t i=0; i<a.
len(); i++)
797 _sub.
f[i] = a.
f[i] - b.
f[i];
904 for(std::size_t i=0; i<trc.
len(); i++)
929 for(std::size_t i=0; i<trc.
t.
size(); i++)
949 for(std::size_t i=0; i<trc.
len(); i++)
968 for(std::size_t i=0; i<trc.
len(); i++)
996 for(std::size_t i=0; i<trc.
t.
size(); i++)
1011 stepPars.
rise =
false;
1023 int i1 = int(t1/trc.
dt);
1024 sReal f0 = phase_0.
f[i1];
1025 for(std::size_t i=i1; i<trc.
len(); i++)
1029 time_trace pulse = phase_0 * eps_0 + phase_1 * eps_1;
The vector class and related algorithms.
size_t size() const
The current size of the vector.
void resize(size_t n)
Resize a vector.
const T * end() const
Pointer to the vector's end.
void assign(InputIterator s, InputIterator e)
Assign a memory range.
const T * begin() const
Pointer to the vector's start.
float s2r
strength of subpulse relative to leading pulse (biphasic pulse)
sReal duration
pulse duration, default is 1 ms
float tau_plat
time constant governing plateau of pulse
float tau_edge
time constant for leading/trailing edges
float bias
constant term to add to stimulus waveform
bool constant
constant value for all time
float strength
pulse amplitude, default is unit strength
double d1
duration of first sub-pulse in [ms] (zero with monophasic pulse)
biphasic truncated exponentials (capacitive discharge)
virtual SF::vector< sReal > & sample(time_trace &trc)
biphasicTruncExpFunc(Pulse _pars)
virtual SF::vector< sReal > & sample(time_trace &trc)
monophasic truncated exponentials (capacitive discharge)
virtual SF::vector< sReal > & sample(time_trace &trc)
monophasicTruncExpFunc(Pulse _pars)
virtual SF::vector< sReal > & sample(time_trace &trc)=0
virtual SF::vector< sReal > & sample(time_trace &trc)
sineWave(sReal _frq, sReal _phase)
float phase
phase in degree
virtual SF::vector< sReal > & sample(time_trace &trc)
bool header_on
turn on header output
time_trace_ffmt(bool num_samples_on_, bool header_on_, std::string header_, std::string col_sep_, int fmt_id_)
bool num_samples_on
turn on # of samples output
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_)
std::string col_sep
column separator, either " " or ","
SF::vector< int > colw
width of columns
int fmt_id
format identifier
void set_labels(std::string _label)
time_trace(sReal _dt)
prescribe dt only, default duration is 1000. ms
void resize(int numSamples)
time_trace(sReal _duration, sReal _dt)
prescribe duration and sampling, use default real time offset
bool fade
signal fades to zero after end or remains at final amplitude
time_trace(sReal _duration, sReal _dt, sReal _rtOff, std::string _label, std::string _t_unit, std::string _f_unit)
full constructor, prescribe all members
bool IsEquDistSampling()
check whether time trace is sampled with constant sampling interval
void resample(time_trace &trc, IpMeth_t meth)
sReal fval_t(sReal _t)
retrieve value of a function at a given time t
void interp1(time_trace &trc, IpMeth_t meth)
interpolate a time trace
std::string label
descriptive label of signal
void unitize()
scale signal to unity strength
void set_labels(std::string _label, std::string _t_unit, std::string _f_unit)
time_trace(sReal _duration, sReal _dt, sReal _rtOff, std::string _label)
SF::vector< sReal > t
time axis
SF::vector< sReal > & gradient()
void resample(time_trace &trc)
std::string format_file_header(const time_trace_ffmt ffmt_spec)
IO format file header from format specifciation and given time trace.
size_t timeShift(sReal t_shift)
void operator<<=(int delta)
SF::vector< sReal > & diff()
std::string t_unit
unit of time axis
sReal dt
temporal discretization of trace, if regularly sampled
time_trace(sReal _duration, sReal _dt, sReal _rtOff)
SF::vector< sReal > f
store function values of trace
time_trace(const time_trace &a, sReal val)
copy constructor with setting of constant signal value
sReal rtOff
real time offset of time axis
std::string f_unit
unit of signal
void setUnits(std::string _t_unit, std::string _f_unit)
int write_trace()
write traces to file
time_trace & operator=(const time_trace &in)
void operator>>=(int delta)
int read_trace(const std::string fname)
determine duration of a signal stored in file
void operator*=(const time_trace &a)
overloaded operators
size_t len() const
< length of trace in samples
time_trace()
use defaults only, 1000 ms duration, 10 us sampling interval, no real time offset
time_trace(const time_trace &a)
copy constructor with setting of constant signal value
void operator+=(const sReal a)
V mod(const V &a, const V &b)
time_trace operator*(const time_trace &a, const time_trace &b)
time_trace operator+(const time_trace &a, const time_trace &b)
time_trace operator-(const time_trace &a, const time_trace &b)
time_trace operator/(const time_trace &a, const time_trace &b)
enum opencarp::sig::ip_method IpMeth_t